Skip to content
This repository has been archived by the owner on May 5, 2018. It is now read-only.

Commit

Permalink
Merge e9f8c6b into be52ce1
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrech committed Jan 28, 2017
2 parents be52ce1 + e9f8c6b commit f2c71d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -18,6 +18,8 @@ fetch-openapi --api-doc-url http://petstore.swagger.io/v2/swagger.json \

See `fetch-openapi -h` for more info.

Using a local api doc schema? Use the parameter `--api-doc-file-path` instead of `--api-doc-url`.

## Programatic Usage

```javascript
Expand Down
49 changes: 37 additions & 12 deletions bin/fetch-openapi.js
Expand Up @@ -12,40 +12,65 @@ var request = require('request');
cli
.version(require('../package.json').version)
.description('This script requests an OpenAPI document and processes it with fetch-openapi.')
.option('--api-doc-file-path [file path]', 'The local path to your OpenAPI document E.G. ./schemas/v2/swagger.json')
.option('--api-doc-url [url]', 'The URL to your OpenAPI document E.G. http://petstore.swagger.io/v2/swagger.json')
.option('--output-file-path [file path]', 'The path to the file generated by fetch-openapi. If it currently exists it will be overwritten.')
.option('--preset [preset]', 'May be one of the following values: es6, node (default)')
.option('--verbose', 'Outputs more information than is normally needed.')
.parse(process.argv);

var apiDocUrl = cli.apiDocUrl;
if (cli.apiDocFilePath) {
var apiDocFilePath = path.resolve(process.cwd(), cli.apiDocFilePath);
debug('apiDocFilePath is ' + apiDocFilePath);
} else {
var apiDocUrl = cli.apiDocUrl;
debug('apiDocUrl is ' + apiDocUrl);
requireArg('apiDocUrl', '--api-doc-url');
}

var outputFilePath = path.resolve(process.cwd(), cli.outputFilePath);
var preset = cli.preset || 'node';

debug('apiDocUrl is ' + apiDocUrl);
debug('outputFilePath is ' + outputFilePath);
debug('preset is ' + preset);

requireArg('apiDocUrl', '--api-doc-url');
requireArg('outputFilePath', '--output-file-path');

if (cli.preset && 'es6,node'.split(',').indexOf(cli.preset) === -1) {
error('preset must be one of: es6, node');
}

request({ url: apiDocUrl, json: true }, errHandler(error, function(err, res, body) {
if (res.statusCode !== 200) {
debug('statusCode is ' + res.statusCode);
error('Bad satus code for "' + apiDocUrl + '". Does it exist?');
var options = {
preset: preset
};
var sdk;


if (apiDocFilePath) {
var apiDoc;
try {
apiDoc = require(apiDocFilePath);
} catch (e) {
debug(e);
error('Could not import api doc at path "' + apiDocFilePath + '". Does it exist?');
return;
}

var options = {
preset: preset
};
generateSdk(apiDoc);
} else {
request({url: apiDocUrl, json: true}, errHandler(error, function (err, res, body) {
if (res.statusCode !== 200) {
debug('statusCode is ' + res.statusCode);
error('Bad satus code for "' + apiDocUrl + '". Does it exist?');
}
generateSdk(body);
}));
}

function generateSdk(apiDoc) {
var sdk;
try {
sdk = fetchOpenApi(body, options);
sdk = fetchOpenApi(apiDoc, options);
debug('sdk is\n' + sdk);
} catch (e) {
error('Received the following error when generating the client: ' + e.message);
Expand All @@ -55,7 +80,7 @@ request({ url: apiDocUrl, json: true }, errHandler(error, function(err, res, bod
msg('SDK written to ' + outputFilePath);
exit(0);
}));
}));
}

function error(msg) {
console.error(chalk.red(LOG_PREFIX) + msg);
Expand Down

0 comments on commit f2c71d1

Please sign in to comment.