From dfa6ffbd33aac50cdbf7ac59634257641d715ba2 Mon Sep 17 00:00:00 2001 From: Nicolas Froidure Date: Fri, 2 Oct 2020 11:17:57 +0200 Subject: [PATCH] refactor(options): add new options allow to ignore some parameters BREAKING CHANGE: The second argument of the generator is now an option object concerns #1 --- API.md | 10 +++++--- README.md | 10 +++++--- src/__snapshots__/index.test.ts.snap | 6 +++++ src/index.test.ts | 34 ++++++++++++++++++++++++--- src/index.ts | 35 +++++++++++++++++----------- 5 files changed, 72 insertions(+), 23 deletions(-) diff --git a/API.md b/API.md index 2e99c31..f00efd1 100644 --- a/API.md +++ b/API.md @@ -4,7 +4,7 @@ ## openapi-ts-sdk-builder -### openapi-ts-sdk-builder~generateSDKFromOpenAPI(openAPIContent, sdkVersion) ⇒ Promise.<string> +### openapi-ts-sdk-builder~generateSDKFromOpenAPI(openAPIContent, options) ⇒ Promise.<string> Build a JS SDK from an OpenAPI file **Kind**: inner method of [openapi-ts-sdk-builder](#module_openapi-ts-sdk-builder) @@ -12,6 +12,10 @@ Build a JS SDK from an OpenAPI file | Param | Type | | --- | --- | -| openAPIContent | String | -| sdkVersion | String | +| openAPIContent | string | +| options | Object | +| options.sdkVersion | string | +| [options.sdkName] | string | +| [options.ignoredParametersNames] | string | +| [options.undocumentedParametersNames] | string | diff --git a/README.md b/README.md index cd4f99e..c7ff6c3 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ export default function useAPISWR>( ## openapi-ts-sdk-builder -### openapi-ts-sdk-builder~generateSDKFromOpenAPI(openAPIContent, sdkVersion) ⇒ Promise.<string> +### openapi-ts-sdk-builder~generateSDKFromOpenAPI(openAPIContent, options) ⇒ Promise.<string> Build a JS SDK from an OpenAPI file **Kind**: inner method of [openapi-ts-sdk-builder](#module_openapi-ts-sdk-builder) @@ -168,8 +168,12 @@ Build a JS SDK from an OpenAPI file | Param | Type | | --- | --- | -| openAPIContent | String | -| sdkVersion | String | +| openAPIContent | string | +| options | Object | +| options.sdkVersion | string | +| [options.sdkName] | string | +| [options.ignoredParametersNames] | string | +| [options.undocumentedParametersNames] | string | # Authors diff --git a/src/__snapshots__/index.test.ts.snap b/src/__snapshots__/index.test.ts.snap index cf7085f..8e1b009 100644 --- a/src/__snapshots__/index.test.ts.snap +++ b/src/__snapshots__/index.test.ts.snap @@ -45,10 +45,14 @@ declare namespace APITypes { export namespace GetDelay { export type Input = { readonly duration: Parameters.Duration; + readonly cookie?: Parameters.Cookie; + readonly xApplicationVersion?: Parameters.XApplicationVersion; }; export type Output = any; export namespace Parameters { export type Duration = NonNullable; + export type Cookie = NonNullable; + export type XApplicationVersion = NonNullable; } } export namespace GetDiagnostic { @@ -266,6 +270,7 @@ async function getPing( async function getDelay( { duration, + xApplicationVersion, } : APITypes.GetDelay.Input, options: AxiosRequestConfig = {} ) : Promise> { @@ -283,6 +288,7 @@ async function getDelay( const headers = Object.assign(((options || {}).headers || {}), { 'X-API-Version': '3.1.3', 'X-SDK-Version': '1.0.0', + 'X-Application-Version': xApplicationVersion, }); const qs = cleanQuery({ duration: duration, diff --git a/src/index.test.ts b/src/index.test.ts index 295894d..01ec3db 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -80,6 +80,26 @@ describe('generateSDKFromOpenAPI', () => { description: 'Duration in milliseconds', schema: { type: 'number' }, }, + { + name: 'Cookie', + in: 'header', + required: false, + example: 'a_cookie=yop', + schema: { + type: 'string', + }, + }, + { + name: 'X-Application-Version', + in: 'header', + required: false, + example: '1.1.2-beta.1', + schema: { + type: 'string', + pattern: + '^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$', + }, + }, ], responses: { '204': { description: 'Delay expired' } }, }, @@ -172,7 +192,11 @@ describe('generateSDKFromOpenAPI', () => { tags: [], }; expect( - await generateSDKFromOpenAPI(JSON.stringify(schema), '1.0.0'), + await generateSDKFromOpenAPI(JSON.stringify(schema), { + sdkVersion: '1.0.0', + ignoredParametersNames: ['Cookie'], + undocumentedParametersNames: ['X-Application-Version'], + }), ).toMatchSnapshot(); }); @@ -244,7 +268,9 @@ describe('generateSDKFromOpenAPI', () => { tags: [], }; expect( - await generateSDKFromOpenAPI(JSON.stringify(schema), '1.0.0'), + await generateSDKFromOpenAPI(JSON.stringify(schema), { + sdkVersion: '1.0.0', + }), ).toMatchSnapshot(); }); test('should work with Pet Store', async () => { @@ -1309,7 +1335,9 @@ describe('generateSDKFromOpenAPI', () => { }, }; expect( - await generateSDKFromOpenAPI(JSON.stringify(schema), '1.0.0'), + await generateSDKFromOpenAPI(JSON.stringify(schema), { + sdkVersion: '1.0.0', + }), ).toMatchSnapshot(); }); }); diff --git a/src/index.ts b/src/index.ts index 90a21fb..7f11081 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,14 +26,27 @@ import type { OpenAPIV3 } from 'openapi-types'; * * @async * @method generateSDKFromOpenAPI - * @param {String} openAPIContent - * @param {String} sdkVersion + * @param {string} openAPIContent + * @param {Object} options + * @param {string} options.sdkVersion + * @param {string} [options.sdkName] + * @param {string} [options.ignoredParametersNames] + * @param {string} [options.undocumentedParametersNames] * @return {Promise} The SDK JS code */ export async function generateSDKFromOpenAPI( openAPIContent: string, - sdkVersion: string, - sdkName = 'API', + { + sdkVersion, + sdkName = 'API', + ignoredParametersNames = [], + undocumentedParametersNames = [], + }: { + sdkVersion: string; + sdkName?: string; + ignoredParametersNames?: string[]; + undocumentedParametersNames?: string[]; + }, ) { const API = JSON.parse(openAPIContent); const $refs = await SwaggerParser.resolve(API as any); @@ -80,9 +93,9 @@ const API = { ${operations .map((operation) => { const { path, method, operationId, parameters, requestBody } = operation; - const dereferencedParameters = (parameters || []).map((parameter) => - dereference(parameter), - ); + const dereferencedParameters = (parameters || []) + .map((parameter) => dereference(parameter)) + .filter((p) => !ignoredParametersNames.includes(p.name)); return ` /** @@ -95,12 +108,7 @@ ${operations ` : '' }${dereferencedParameters - .filter( - (p) => - !['X-API-Version', 'X-SDK-Version', 'X-Application-Version'].includes( - p.name, - ), - ) + .filter((p) => !undocumentedParametersNames.includes(p.name)) .map( (parameter) => ` * @param {${ @@ -153,7 +161,6 @@ async function ${operationId}(${ body,` : '' }${dereferencedParameters - .filter((p) => !['X-API-Version', 'X-SDK-Version'].includes(p.name)) .map((parameter) => { const variableName = camelCase(parameter.name);