From 89edbc5f8ddc9e057cf14c25bf25c9efe7927e61 Mon Sep 17 00:00:00 2001 From: Isaac Parker Date: Thu, 8 Sep 2022 18:49:33 +0000 Subject: [PATCH 1/2] Add 'inlineRequestOverrides' option to easily modify individual requests --- bin/index.js | 2 ++ bin/index.spec.js | 1 + docs/basic-usage.md | 1 + package.json | 13 ++++++++----- src/index.ts | 5 +++++ src/templates/exportService.hbs | 6 ++++++ src/templates/partials/parameters.hbs | 11 +++++++++++ src/utils/writeClient.spec.ts | 1 + src/utils/writeClient.ts | 2 ++ src/utils/writeClientServices.spec.ts | 2 +- src/utils/writeClientServices.ts | 2 ++ 11 files changed, 40 insertions(+), 6 deletions(-) diff --git a/bin/index.js b/bin/index.js index decf79420..51f9c3332 100755 --- a/bin/index.js +++ b/bin/index.js @@ -16,6 +16,7 @@ const params = program .option('--name ', 'Custom client class name') .option('--useOptions', 'Use options instead of arguments') .option('--useUnionTypes', 'Use union types instead of enums') + .option('--inlineRequestOverrides', 'Permit specifying request options on a per-invocation basis') .option('--exportCore ', 'Write core files to disk', true) .option('--exportServices ', 'Write services to disk', true) .option('--exportModels ', 'Write models to disk', true) @@ -35,6 +36,7 @@ if (OpenAPI) { httpClient: params.client, clientName: params.name, useOptions: params.useOptions, + inlineRequestOverrides: params.inlineRequestOverrides, useUnionTypes: params.useUnionTypes, exportCore: JSON.parse(params.exportCore) === true, exportServices: JSON.parse(params.exportServices) === true, diff --git a/bin/index.spec.js b/bin/index.spec.js index 289631812..0b256069b 100755 --- a/bin/index.spec.js +++ b/bin/index.spec.js @@ -23,6 +23,7 @@ describe('bin', () => { '--client', 'fetch', '--useOptions', + '--inlineRequestOverrides', '--useUnionTypes', '--exportCore', 'true', diff --git a/docs/basic-usage.md b/docs/basic-usage.md index 5b110f2ac..8947c3825 100644 --- a/docs/basic-usage.md +++ b/docs/basic-usage.md @@ -12,6 +12,7 @@ $ openapi --help -c, --client HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch") --name Custom client class name --useOptions Use options instead of arguments + --inlineRequestOverrides Override request options on a per-invocation basis --useUnionTypes Use union types instead of enums --exportCore Write core files to disk (default: true) --exportServices Write services to disk (default: true) diff --git a/package.json b/package.json index ad532b3f5..39226c31d 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { - "name": "openapi-typescript-codegen", - "version": "0.23.0", + "name": "@parrotmac/openapi-typescript-codegen", + "version": "0.23.0-hack5", "description": "Library that generates Typescript clients based on the OpenAPI specification.", "author": "Ferdi Koomen", - "homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen", + "homepage": "https://github.com/parrotmac/openapi-typescript-codegen", "repository": { "type": "git", - "url": "git+https://github.com/ferdikoomen/openapi-typescript-codegen.git" + "url": "git+https://github.com/parrotmac/openapi-typescript-codegen.git" }, "bugs": { "url": "https://github.com/ferdikoomen/openapi-typescript-codegen/issues" @@ -31,10 +31,13 @@ "email": "info@madebyferdi.com" } ], + "publishConfig": { + "registry":"https://registry.npmjs.org" + }, "main": "dist/index.js", "types": "types/index.d.ts", "bin": { - "openapi": "bin/index.js" + "custom-openapi": "bin/index.js" }, "files": [ "bin/index.js", diff --git a/src/index.ts b/src/index.ts index ef7a8b1bf..ed30e08f5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,7 @@ export type Options = { httpClient?: HttpClient; clientName?: string; useOptions?: boolean; + inlineRequestOverrides?: boolean; useUnionTypes?: boolean; exportCore?: boolean; exportServices?: boolean; @@ -39,6 +40,7 @@ export type Options = { * @param clientName Custom client class name * @param useOptions Use options or arguments functions * @param useUnionTypes Use union types instead of enums + * @param inlineRequestOverrides Provide a place within service methods to define arbitrary request modifications * @param exportCore Generate core client classes * @param exportServices Generate services * @param exportModels Generate models @@ -54,6 +56,7 @@ export const generate = async ({ httpClient = HttpClient.FETCH, clientName, useOptions = false, + inlineRequestOverrides = false, useUnionTypes = false, exportCore = true, exportServices = true, @@ -83,6 +86,7 @@ export const generate = async ({ output, httpClient, useOptions, + inlineRequestOverrides, useUnionTypes, exportCore, exportServices, @@ -106,6 +110,7 @@ export const generate = async ({ output, httpClient, useOptions, + inlineRequestOverrides, useUnionTypes, exportCore, exportServices, diff --git a/src/templates/exportService.hbs b/src/templates/exportService.hbs index 2fdd9af58..30358d43a 100644 --- a/src/templates/exportService.hbs +++ b/src/templates/exportService.hbs @@ -30,6 +30,9 @@ import type { BaseHttpRequest } from '../core/BaseHttpRequest'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; {{/if}} +{{#if @root.inlineRequestOverrides~}} +import type { ApiRequestOptions } from '../core/ApiRequestOptions'; +{{/if}} {{#equals @root.httpClient 'angular'}} @Injectable() @@ -143,6 +146,9 @@ export class {{{name}}}{{{@root.postfix}}} { {{/each}} }, {{/if}} + {{#if @root.inlineRequestOverrides}} + ..._requestOverrides, + {{/if}} }); } diff --git a/src/templates/partials/parameters.hbs b/src/templates/partials/parameters.hbs index 57ab5a7d1..d1cbb3930 100644 --- a/src/templates/partials/parameters.hbs +++ b/src/templates/partials/parameters.hbs @@ -4,6 +4,9 @@ {{#each parameters}} {{{name}}}{{#if default}} = {{{default}}}{{/if}}, {{/each}} +{{#if @root.inlineRequestOverrides~}} +_requestOverrides, +{{/if}} }: { {{#each parameters}} {{#ifdef description deprecated}} @@ -18,6 +21,7 @@ {{/ifdef}} {{{name}}}{{>isRequired}}: {{>type}}, {{/each}} + _requestOverrides?: Partial, } {{~else}} @@ -25,4 +29,11 @@ {{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}}, {{/each}} {{/if}} +{{~else}} +{{#if @root.inlineRequestOverrides~}}{ + _requestOverrides, +}: { + _requestOverrides?: Partial, +} +{{/if}} {{/if}} diff --git a/src/utils/writeClient.spec.ts b/src/utils/writeClient.spec.ts index 3c06a95a5..2bfc3e284 100644 --- a/src/utils/writeClient.spec.ts +++ b/src/utils/writeClient.spec.ts @@ -43,6 +43,7 @@ describe('writeClient', () => { HttpClient.FETCH, false, false, + false, true, true, true, diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index a0ffc1821..f369a6521 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -38,6 +38,7 @@ export const writeClient = async ( output: string, httpClient: HttpClient, useOptions: boolean, + inlineRequestOverrides: boolean, useUnionTypes: boolean, exportCore: boolean, exportServices: boolean, @@ -74,6 +75,7 @@ export const writeClient = async ( httpClient, useUnionTypes, useOptions, + inlineRequestOverrides, indent, postfix, clientName diff --git a/src/utils/writeClientServices.spec.ts b/src/utils/writeClientServices.spec.ts index b7ebbfe6c..0aac11219 100644 --- a/src/utils/writeClientServices.spec.ts +++ b/src/utils/writeClientServices.spec.ts @@ -39,7 +39,7 @@ describe('writeClientServices', () => { }, }; - await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4, 'Service'); + await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, false, Indent.SPACE_4, 'Service'); expect(writeFile).toBeCalledWith('/UserService.ts', `service${EOL}`); }); diff --git a/src/utils/writeClientServices.ts b/src/utils/writeClientServices.ts index 2f95341d2..90503afc5 100644 --- a/src/utils/writeClientServices.ts +++ b/src/utils/writeClientServices.ts @@ -28,6 +28,7 @@ export const writeClientServices = async ( httpClient: HttpClient, useUnionTypes: boolean, useOptions: boolean, + inlineRequestOverrides: boolean, indent: Indent, postfix: string, clientName?: string @@ -39,6 +40,7 @@ export const writeClientServices = async ( httpClient, useUnionTypes, useOptions, + inlineRequestOverrides, postfix, exportClient: isDefined(clientName), }); From ff235a6a2bb0c98baa3dfd8fc83a53d2a1be9521 Mon Sep 17 00:00:00 2001 From: Isaac Parker Date: Thu, 8 Sep 2022 12:54:46 -0600 Subject: [PATCH 2/2] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 7c3a945c1..8c8d76ff2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +## See https://github.com/parrotmac/openapi-typescript-codegen/commit/89edbc5f8ddc9e057cf14c25bf25c9efe7927e61 for the _raison d'etre_ of this fork. + + # OpenAPI Typescript Codegen [![NPM][npm-image]][npm-url]