Skip to content

Commit

Permalink
refactor(options): add new options
Browse files Browse the repository at this point in the history
allow to ignore some parameters

BREAKING CHANGE: The second argument of the generator is now an option object

concerns #1
  • Loading branch information
nfroidure committed Oct 2, 2020
1 parent 361e2e3 commit dfa6ffb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 23 deletions.
10 changes: 7 additions & 3 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
## openapi-ts-sdk-builder
<a name="module_openapi-ts-sdk-builder..generateSDKFromOpenAPI"></a>

### openapi-ts-sdk-builder~generateSDKFromOpenAPI(openAPIContent, sdkVersion) ⇒ <code>Promise.&lt;string&gt;</code>
### openapi-ts-sdk-builder~generateSDKFromOpenAPI(openAPIContent, options) ⇒ <code>Promise.&lt;string&gt;</code>
Build a JS SDK from an OpenAPI file

**Kind**: inner method of [<code>openapi-ts-sdk-builder</code>](#module_openapi-ts-sdk-builder)
**Returns**: <code>Promise.&lt;string&gt;</code> - The SDK JS code

| Param | Type |
| --- | --- |
| openAPIContent | <code>String</code> |
| sdkVersion | <code>String</code> |
| openAPIContent | <code>string</code> |
| options | <code>Object</code> |
| options.sdkVersion | <code>string</code> |
| [options.sdkName] | <code>string</code> |
| [options.ignoredParametersNames] | <code>string</code> |
| [options.undocumentedParametersNames] | <code>string</code> |

10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,20 @@ export default function useAPISWR<T extends Handler<any, any>>(
## openapi-ts-sdk-builder
<a name="module_openapi-ts-sdk-builder..generateSDKFromOpenAPI"></a>

### openapi-ts-sdk-builder~generateSDKFromOpenAPI(openAPIContent, sdkVersion) ⇒ <code>Promise.&lt;string&gt;</code>
### openapi-ts-sdk-builder~generateSDKFromOpenAPI(openAPIContent, options) ⇒ <code>Promise.&lt;string&gt;</code>
Build a JS SDK from an OpenAPI file

**Kind**: inner method of [<code>openapi-ts-sdk-builder</code>](#module_openapi-ts-sdk-builder)
**Returns**: <code>Promise.&lt;string&gt;</code> - The SDK JS code

| Param | Type |
| --- | --- |
| openAPIContent | <code>String</code> |
| sdkVersion | <code>String</code> |
| openAPIContent | <code>string</code> |
| options | <code>Object</code> |
| options.sdkVersion | <code>string</code> |
| [options.sdkName] | <code>string</code> |
| [options.ignoredParametersNames] | <code>string</code> |
| [options.undocumentedParametersNames] | <code>string</code> |


# Authors
Expand Down
6 changes: 6 additions & 0 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>;
export type Cookie = NonNullable<string>;
export type XApplicationVersion = NonNullable<string>;
}
}
export namespace GetDiagnostic {
Expand Down Expand Up @@ -266,6 +270,7 @@ async function getPing(
async function getDelay(
{
duration,
xApplicationVersion,
} : APITypes.GetDelay.Input,
options: AxiosRequestConfig = {}
) : Promise<Writeable<APITypes.GetDelay.Output>> {
Expand All @@ -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,
Expand Down
34 changes: 31 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' } },
},
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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();
});
});
35 changes: 21 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} 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);
Expand Down Expand Up @@ -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 `
/**
Expand All @@ -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 {${
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dfa6ffb

Please sign in to comment.