diff --git a/index.d.ts b/index.d.ts index 6f529756..eb1a51ca 100644 --- a/index.d.ts +++ b/index.d.ts @@ -76,6 +76,10 @@ interface GenerateApiParams { * disabled SSL check */ disableStrictSSL?: boolean; + /** + * disabled Proxy + */ + disableProxy?: boolean; /** * generate separated files for http client, data contracts, and routes (default: false) */ @@ -275,6 +279,7 @@ export interface GenerateApiConfiguration { moduleNameIndex: number; moduleNameFirstTag: boolean; disableStrictSSL: boolean; + disableProxy: boolean; extractRequestParams: boolean; fileNames: { dataContracts: string; diff --git a/index.js b/index.js index 1d1cb7e0..da10bee2 100755 --- a/index.js +++ b/index.js @@ -63,6 +63,7 @@ program ) .option("--module-name-first-tag", "splits routes based on the first tag", false) .option("--disableStrictSSL", "disabled strict SSL", false) + .option("--disableProxy", "disabled proxy", false) .option("--axios", "generate axios http client", false) .option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false) .option("--silent", "Output only errors to console", false) @@ -94,6 +95,7 @@ const { extractRequestParams, enumNamesAsValues, disableStrictSSL, + disableProxy, cleanOutput, defaultResponse, singleHttpClient, @@ -123,6 +125,7 @@ generateApi({ moduleNameIndex: +(moduleNameIndex || 0), moduleNameFirstTag: moduleNameFirstTag, disableStrictSSL: !!disableStrictSSL, + disableProxy: !!disableProxy, singleHttpClient: !!singleHttpClient, cleanOutput: !!cleanOutput, silent: !!silent, diff --git a/src/config.js b/src/config.js index 82409bb7..30be3f37 100644 --- a/src/config.js +++ b/src/config.js @@ -32,6 +32,7 @@ const config = { /** use the first tag for the module name */ moduleNameFirstTag: false, disableStrictSSL: false, + disableProxy: false, extractRequestParams: false, fileNames: { dataContracts: "data-contracts", diff --git a/src/index.js b/src/index.js index 7bb79ad3..2fe2185c 100644 --- a/src/index.js +++ b/src/index.js @@ -48,6 +48,7 @@ module.exports = { extraTemplates, enumNamesAsValues, disableStrictSSL = config.disableStrictSSL, + disableProxy = config.disableProxy, cleanOutput, silent = config.silent, typePrefix = config.typePrefix, @@ -70,6 +71,7 @@ module.exports = { hooks: _.merge(config.hooks, rawHooks || {}), enumNamesAsValues, disableStrictSSL, + disableProxy, cleanOutput, defaultResponseType, singleHttpClient, @@ -79,7 +81,10 @@ module.exports = { typePrefix, typeSuffix, }); - (spec ? convertSwaggerObject(spec) : getSwaggerObject(input, url, disableStrictSSL)) + (spec + ? convertSwaggerObject(spec) + : getSwaggerObject(input, url, disableStrictSSL, disableProxy) + ) .then(({ usageSchema, originalSchema }) => { const templatePaths = getTemplatePaths(config); diff --git a/src/swagger.js b/src/swagger.js index 145ba4aa..231aa20d 100644 --- a/src/swagger.js +++ b/src/swagger.js @@ -17,25 +17,35 @@ const parseSwaggerFile = (file) => { } }; -const getSwaggerFile = (pathToSwagger, urlToSwagger, disableStrictSSL) => +const getSwaggerFile = (pathToSwagger, urlToSwagger, disableStrictSSL, disableProxy) => new Promise((resolve) => { if (pathIsExist(pathToSwagger)) { log(`try to get swagger by path "${pathToSwagger}"`); resolve(getFileContent(pathToSwagger)); } else { log(`try to get swagger by url "${urlToSwagger}"`); - let agent = undefined; + // setup options for Axios + const axiosOptions = {}; + // if (disableStrictSSL) { - agent = new https.Agent({ + axiosOptions.httpsAgent = new https.Agent({ rejectUnauthorized: false, }); } - axios.get(urlToSwagger, { httpsAgent: agent }).then((res) => resolve(res.data)); + // + if (disableProxy) axiosOptions.proxy = false; + // + axios + .get(urlToSwagger, axiosOptions) + .then((res) => resolve(res.data)) + .catch((err) => + log(`error while getting swagger by URL ${urlToSwagger}: ${JSON.stringify(err)}`), + ); } }); -const getSwaggerObject = (pathToSwagger, urlToSwagger, disableStrictSSL) => - getSwaggerFile(pathToSwagger, urlToSwagger, disableStrictSSL).then((file) => +const getSwaggerObject = (pathToSwagger, urlToSwagger, disableStrictSSL, disableProxy) => + getSwaggerFile(pathToSwagger, urlToSwagger, disableStrictSSL, disableProxy).then((file) => convertSwaggerObject(parseSwaggerFile(file)), );