From e9f8c6bbfe54da9d2bf67b850a2a99ddc364fb4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFck=20Piera?= Date: Sat, 28 Jan 2017 16:26:12 +0100 Subject: [PATCH] Generate sdk for local openapi doc --- README.md | 2 ++ bin/fetch-openapi.js | 49 +++++++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 31c71a9..87df3e5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/fetch-openapi.js b/bin/fetch-openapi.js index 1211696..666ddcd 100755 --- a/bin/fetch-openapi.js +++ b/bin/fetch-openapi.js @@ -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); @@ -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);