Skip to content

Commit

Permalink
If an OAS is not supplied, attempt to discover one in the root dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Jun 22, 2019
1 parent 5031754 commit 40354c2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 29 deletions.
79 changes: 53 additions & 26 deletions lib/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,64 @@ exports.run = function({ args, opts }) {
return Promise.reject(new Error('No api key provided. Please use --key'));
}

if (!args[0]) {
return Promise.reject(
new Error('No swagger file provided. Usage `rdme swagger <swagger-file>`'),
);
}
function callApi(specPath) {
function success(data) {
console.log(data);
console.log('Success! '.green);
}

function success(data) {
console.log(data);
console.log('Success! '.green);
}
function error(err) {
try {
if (err.statusCode === 401) {
return Promise.reject(
new Error('There was a problem with your api key. Please try again.'),
);
}

function error(err) {
try {
return Promise.reject(new Error(JSON.parse(err.error).description));
} catch (e) {
return Promise.reject(new Error('There was an error uploading!'));
return Promise.reject(new Error(JSON.parse(err.error).description));
} catch (e) {
return Promise.reject(new Error('There was an error uploading!'));
}
}
}

const options = {
formData: {
swagger: fs.createReadStream(path.resolve(process.cwd(), args[0])),
},
auth: { user: key },
};
const options = {
formData: {
swagger: fs.createReadStream(path.resolve(process.cwd(), specPath)),
},
auth: { user: key },
};

// Create
if (!id) {
return request.post(`${config.host}/api/v1/swagger`, options).then(success, error);
}

// Create
if (!id) {
return request.post(`${config.host}/api/v1/swagger`, options).then(success, error);
// Update
return request.put(`${config.host}/api/v1/swagger/${id}`, options).then(success, error);
}

// Update
return request.put(`${config.host}/api/v1/swagger/${id}`, options).then(success, error);
if (args[0]) {
return callApi(args[0]);
}

// If the user didn't supply a specification, let's try to locate what they've got, and upload
// that. If they don't have any, let's let the user know how they can get one going.
return new Promise((resolve, reject) => {
['swagger.json', 'swagger.yaml', 'openapi.json', 'openapi.yaml'].forEach(function(file) {

This comment has been minimized.

Copy link
@gkoberger

gkoberger Jun 28, 2019

Contributor

There's code in the oas tool that opens all .yaml/.json files in the directory and tries to find it. If this doesn't always work, we can try doing it that way!

if (!fs.existsSync(file)) {
return;
}

console.log(`We found ${file} and are attempting to upload it.`.yellow);

resolve(callApi(file));
});

reject(
new Error(
'We were unable to locate a Swagger or OpenAPI file to upload.\n' +
"Don't worry, it's easy to get started! Run `rdme oas init` to get started.",
),
);
});
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"scripts": {
"lint": "eslint -f unix .",
"prettier": "prettier --list-different \"./**/**.js\"",
"prettier:write": "prettier --write \"./**/**.js\"",
"pretest": "npm run lint && npm run prettier",
"test": "jest --coverage"
},
Expand Down
40 changes: 37 additions & 3 deletions test/swagger.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const nock = require('nock');
const config = require('config');
const fs = require('fs');

const swagger = require('../cli').bind(null, 'swagger');

Expand All @@ -14,10 +15,43 @@ describe('swagger command', () => {
'No api key provided. Please use --key',
));

it('should error if no file provided', () =>
it('should error if a bad api key is provided', async () => {
const badKey = 'thisIsABadApiKey';
const mock = nock(config.host)
.post('/api/v1/swagger', body => body.match('form-data; name="swagger"'))
.basicAuth({ user: badKey })
.reply(401);

await expect(swagger(['./test/fixtures/swagger.json'], { key: badKey })).rejects.toThrow(
'There was a problem with your api key. Please try again.',
);

mock.done();
});

it('should error if no file was provided or able to be discovered', () => {
expect(swagger([], { key })).rejects.toThrow(
'No swagger file provided. Usage `rdme swagger <swagger-file>`',
));
'We were unable to locate a Swagger or OpenAPI file to upload.\n' +
"Don't worry, it's easy to get started! Run `rdme oas init` to get started.",
);
});

it('should POST a discovered file if none provided', () => {
const mock = nock(config.host)
.post('/api/v1/swagger', body => body.match('form-data; name="swagger"'))
.basicAuth({ user: key })
.reply(201);

// Surface our test fixture to the root directory so rdme can autodiscover it. It's easier to do
// this than mocking out the fs module because mocking the fs module here causes Jest sourcemaps
// to break.
fs.copyFileSync('./test/fixtures/swagger.json', './swagger.json');

return swagger([], { key }).then(() => {
fs.unlinkSync('./swagger.json');
mock.done();
});
});

it('should error if API errors', async () => {
const mock = nock(config.host)
Expand Down

0 comments on commit 40354c2

Please sign in to comment.