Skip to content

Commit 757873c

Browse files
author
Dom Harrington
committed
Add better error checking to swagger command
Make it so that you can either provide: --token={key}-{id} OR --key={key} --id={id} With a deprecation notice for the former
1 parent 50294e9 commit 757873c

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ npm install rdme
1616
### Uploading a new Swagger file to ReadMe
1717

1818
```sh
19-
rdme swagger {path-to-swagger.json} --token={api-key}
19+
rdme swagger {path-to-swagger.json} --key={api-key}
2020
```
2121

2222
### Editing an existing Swagger file
2323
```sh
24-
rdme swagger {path-to-swagger.json} --token={api-key}-{existing-id}
24+
rdme swagger {path-to-swagger.json} --key={api-key} --id={existing-id}
2525
```
2626

2727
## Future

lib/swagger.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,20 @@ function defaultCallback(err) {
1616
}
1717

1818
exports.run = function({ args, opts }, cb = defaultCallback) {
19-
const [apiKey, id] = opts.token.split('-');
19+
let {key, id} = opts;
20+
21+
if (!key && opts.token) {
22+
console.warn('Using `rdme` with --token has been deprecated. Please use --key and --id instead');
23+
[key, id] = opts.token.split('-')
24+
}
25+
26+
if (!key) {
27+
return cb(new Error('No api key provided. Please use --key'));
28+
}
29+
30+
if (!args[0]) {
31+
return cb(new Error('No swagger file provided. Usage `rdme swagger <swagger-file>`'));
32+
}
2033

2134
function callback(err, response, data) {
2235
if (err) return cb(err);
@@ -36,12 +49,12 @@ exports.run = function({ args, opts }, cb = defaultCallback) {
3649
formData: {
3750
swagger: fs.createReadStream(path.resolve(process.cwd(), args[0])),
3851
},
39-
auth: { user: apiKey },
52+
auth: { user: key },
4053
};
4154

4255
if (!id) {
43-
request.post(`${config.host}/api/v1/swagger`, options, callback);
56+
return request.post(`${config.host}/api/v1/swagger`, options, callback);
4457
} else {
45-
request.put(`${config.host}/api/v1/swagger/${id}`, options, callback);
58+
return request.put(`${config.host}/api/v1/swagger/${id}`, options, callback);
4659
}
4760
};

test/swagger.test.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
const nock = require('nock');
22
const config = require('config');
3+
const assert = require('assert');
34

45
const swagger = require('../lib/swagger');
56

6-
const apiKey = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs';
7+
const key = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs';
78

89
describe('swagger command', () => {
910
afterAll(() => nock.cleanAll());
1011

12+
it('should error if no api key provided', (done) => {
13+
swagger.run({
14+
args: ['./test/fixtures/swagger.json'],
15+
opts: {},
16+
}, (err) => {
17+
assert.equal(err.message, 'No api key provided. Please use --key');
18+
return done();
19+
});
20+
});
21+
22+
it('should error if no file provided', (done) => {
23+
swagger.run({
24+
args: [],
25+
opts: { key },
26+
}, (err) => {
27+
assert.equal(err.message, 'No swagger file provided. Usage `rdme swagger <swagger-file>`');
28+
return done();
29+
})
30+
});
31+
1132
it('should POST to the swagger api if no id provided', done => {
1233
const mock = nock(config.host)
1334
.post('/api/v1/swagger', body => body.match('form-data; name="swagger"'))
14-
.basicAuth({ user: apiKey })
35+
.basicAuth({ user: key })
1536
.reply(201);
1637

1738
swagger.run(
18-
{ args: ['./test/fixtures/swagger.json'], opts: { token: apiKey } },
39+
{ args: ['./test/fixtures/swagger.json'], opts: { key } },
1940
err => {
2041
if (err) return done(err);
2142
mock.done();
@@ -29,13 +50,34 @@ describe('swagger command', () => {
2950
const id = '5aa0409b7cf527a93bfb44df';
3051
const mock = nock(config.host)
3152
.put(`/api/v1/swagger/${id}`, body => body.match('form-data; name="swagger"'))
32-
.basicAuth({ user: apiKey })
53+
.basicAuth({ user: key })
54+
.reply(201);
55+
56+
swagger.run(
57+
{
58+
args: ['./test/fixtures/swagger.json'],
59+
opts: { key, id },
60+
},
61+
err => {
62+
if (err) return done(err);
63+
mock.done();
64+
65+
return done();
66+
},
67+
);
68+
});
69+
70+
it('should still work with `token`', done => {
71+
const id = '5aa0409b7cf527a93bfb44df';
72+
const mock = nock(config.host)
73+
.put(`/api/v1/swagger/${id}`, body => body.match('form-data; name="swagger"'))
74+
.basicAuth({ user: key })
3375
.reply(201);
3476

3577
swagger.run(
3678
{
3779
args: ['./test/fixtures/swagger.json'],
38-
opts: { token: `${apiKey}-${id}` },
80+
opts: { token: `${key}-${id}` },
3981
},
4082
err => {
4183
if (err) return done(err);

0 commit comments

Comments
 (0)