Skip to content

Commit fc56e8f

Browse files
author
Dom Harrington
committed
Add support for updating swagger file via the api
1 parent 866d0d1 commit fc56e8f

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/push.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ exports.category = "services";
1111
exports.run = function(config, info, cb) {
1212
if (!cb) cb = () => {};
1313

14-
var formData = {
15-
swagger: fs.createReadStream(path.join(process.cwd(), info.args[0])),
16-
};
17-
request.post(`${config.host.url}/v1/api/swagger`, { formData, auth: { user: info.opts.token } }, function(err, response, data) {
14+
const [apiKey, id] = info.opts.token.split('-');
15+
16+
function callback(err, response, data) {
1817
if (err) return cb(err);
1918
if (response.statusCode === 201) {
2019
console.log("");
@@ -26,8 +25,18 @@ exports.run = function(config, info, cb) {
2625

2726
if (cb) return cb();
2827
process.exit();
29-
});
28+
}
3029

31-
//utils.open(info.swaggerUrl, info);
30+
const options = {
31+
formData: {
32+
swagger: fs.createReadStream(path.join(process.cwd(), info.args[0])),
33+
},
34+
auth: { user: apiKey },
35+
};
3236

37+
if (!id) {
38+
request.post(`${config.host.url}/v1/api/swagger`, options, callback);
39+
} else {
40+
request.put(`${config.host.url}/v1/api/swagger/${id}`, options, callback);
41+
}
3342
};

test/push.test.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ describe('push action', () => {
1111

1212
it('should POST to the swagger api if no id provided', (done) => {
1313
const mock = nock(config.host.url).post('/v1/api/swagger', (body) => {
14-
return true;
1514
return body.match('form-data; name=\"swagger\"');
16-
}).basicAuth({ user: apiKey, pass: '' }).reply(201);
15+
}).basicAuth({ user: apiKey }).reply(201);
1716

1817
push.run(config, { args: ['./test/fixtures/json/swagger.json'], opts: { token: apiKey } }, (err) => {
1918
if (err) return done(err);
@@ -22,4 +21,18 @@ describe('push action', () => {
2221
return done();
2322
});
2423
});
24+
25+
it('should PUT to the swagger api if id is provided', (done) => {
26+
const id = '5aa0409b7cf527a93bfb44df';
27+
const mock = nock(config.host.url).put(`/v1/api/swagger/${id}`, (body) => {
28+
return body.match('form-data; name=\"swagger\"');
29+
}).basicAuth({ user: apiKey }).reply(201);
30+
31+
push.run(config, { args: ['./test/fixtures/json/swagger.json'], opts: { token: `${apiKey}-${id}` } }, (err) => {
32+
if (err) return done(err);
33+
mock.done();
34+
35+
return done();
36+
});
37+
});
2538
});

0 commit comments

Comments
 (0)