Skip to content

Commit

Permalink
Add support for updating swagger file via the api
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Mar 7, 2018
1 parent 866d0d1 commit fc56e8f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
21 changes: 15 additions & 6 deletions lib/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ exports.category = "services";
exports.run = function(config, info, cb) {
if (!cb) cb = () => {};

var formData = {
swagger: fs.createReadStream(path.join(process.cwd(), info.args[0])),
};
request.post(`${config.host.url}/v1/api/swagger`, { formData, auth: { user: info.opts.token } }, function(err, response, data) {
const [apiKey, id] = info.opts.token.split('-');

function callback(err, response, data) {
if (err) return cb(err);
if (response.statusCode === 201) {
console.log("");
Expand All @@ -26,8 +25,18 @@ exports.run = function(config, info, cb) {

if (cb) return cb();
process.exit();
});
}

//utils.open(info.swaggerUrl, info);
const options = {
formData: {
swagger: fs.createReadStream(path.join(process.cwd(), info.args[0])),
},
auth: { user: apiKey },
};

if (!id) {
request.post(`${config.host.url}/v1/api/swagger`, options, callback);
} else {
request.put(`${config.host.url}/v1/api/swagger/${id}`, options, callback);
}
};
17 changes: 15 additions & 2 deletions test/push.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ describe('push action', () => {

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

push.run(config, { args: ['./test/fixtures/json/swagger.json'], opts: { token: apiKey } }, (err) => {
if (err) return done(err);
Expand All @@ -22,4 +21,18 @@ describe('push action', () => {
return done();
});
});

it('should PUT to the swagger api if id is provided', (done) => {
const id = '5aa0409b7cf527a93bfb44df';
const mock = nock(config.host.url).put(`/v1/api/swagger/${id}`, (body) => {
return body.match('form-data; name=\"swagger\"');
}).basicAuth({ user: apiKey }).reply(201);

push.run(config, { args: ['./test/fixtures/json/swagger.json'], opts: { token: `${apiKey}-${id}` } }, (err) => {
if (err) return done(err);
mock.done();

return done();
});
});
});

0 comments on commit fc56e8f

Please sign in to comment.