Skip to content

Commit 89edbc5

Browse files
committed
Add 'inlineRequestOverrides' option to easily modify individual requests
1 parent cef1ca6 commit 89edbc5

File tree

11 files changed

+40
-6
lines changed

11 files changed

+40
-6
lines changed

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const params = program
1616
.option('--name <value>', 'Custom client class name')
1717
.option('--useOptions', 'Use options instead of arguments')
1818
.option('--useUnionTypes', 'Use union types instead of enums')
19+
.option('--inlineRequestOverrides', 'Permit specifying request options on a per-invocation basis')
1920
.option('--exportCore <value>', 'Write core files to disk', true)
2021
.option('--exportServices <value>', 'Write services to disk', true)
2122
.option('--exportModels <value>', 'Write models to disk', true)
@@ -35,6 +36,7 @@ if (OpenAPI) {
3536
httpClient: params.client,
3637
clientName: params.name,
3738
useOptions: params.useOptions,
39+
inlineRequestOverrides: params.inlineRequestOverrides,
3840
useUnionTypes: params.useUnionTypes,
3941
exportCore: JSON.parse(params.exportCore) === true,
4042
exportServices: JSON.parse(params.exportServices) === true,

bin/index.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('bin', () => {
2323
'--client',
2424
'fetch',
2525
'--useOptions',
26+
'--inlineRequestOverrides',
2627
'--useUnionTypes',
2728
'--exportCore',
2829
'true',

docs/basic-usage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ $ openapi --help
1212
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
1313
--name <value> Custom client class name
1414
--useOptions Use options instead of arguments
15+
--inlineRequestOverrides Override request options on a per-invocation basis
1516
--useUnionTypes Use union types instead of enums
1617
--exportCore <value> Write core files to disk (default: true)
1718
--exportServices <value> Write services to disk (default: true)

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "openapi-typescript-codegen",
3-
"version": "0.23.0",
2+
"name": "@parrotmac/openapi-typescript-codegen",
3+
"version": "0.23.0-hack5",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
6-
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",
6+
"homepage": "https://github.com/parrotmac/openapi-typescript-codegen",
77
"repository": {
88
"type": "git",
9-
"url": "git+https://github.com/ferdikoomen/openapi-typescript-codegen.git"
9+
"url": "git+https://github.com/parrotmac/openapi-typescript-codegen.git"
1010
},
1111
"bugs": {
1212
"url": "https://github.com/ferdikoomen/openapi-typescript-codegen/issues"
@@ -31,10 +31,13 @@
3131
"email": "[email protected]"
3232
}
3333
],
34+
"publishConfig": {
35+
"registry":"https://registry.npmjs.org"
36+
},
3437
"main": "dist/index.js",
3538
"types": "types/index.d.ts",
3639
"bin": {
37-
"openapi": "bin/index.js"
40+
"custom-openapi": "bin/index.js"
3841
},
3942
"files": [
4043
"bin/index.js",

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type Options = {
1818
httpClient?: HttpClient;
1919
clientName?: string;
2020
useOptions?: boolean;
21+
inlineRequestOverrides?: boolean;
2122
useUnionTypes?: boolean;
2223
exportCore?: boolean;
2324
exportServices?: boolean;
@@ -39,6 +40,7 @@ export type Options = {
3940
* @param clientName Custom client class name
4041
* @param useOptions Use options or arguments functions
4142
* @param useUnionTypes Use union types instead of enums
43+
* @param inlineRequestOverrides Provide a place within service methods to define arbitrary request modifications
4244
* @param exportCore Generate core client classes
4345
* @param exportServices Generate services
4446
* @param exportModels Generate models
@@ -54,6 +56,7 @@ export const generate = async ({
5456
httpClient = HttpClient.FETCH,
5557
clientName,
5658
useOptions = false,
59+
inlineRequestOverrides = false,
5760
useUnionTypes = false,
5861
exportCore = true,
5962
exportServices = true,
@@ -83,6 +86,7 @@ export const generate = async ({
8386
output,
8487
httpClient,
8588
useOptions,
89+
inlineRequestOverrides,
8690
useUnionTypes,
8791
exportCore,
8892
exportServices,
@@ -106,6 +110,7 @@ export const generate = async ({
106110
output,
107111
httpClient,
108112
useOptions,
113+
inlineRequestOverrides,
109114
useUnionTypes,
110115
exportCore,
111116
exportServices,

src/templates/exportService.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import type { BaseHttpRequest } from '../core/BaseHttpRequest';
3030
import { OpenAPI } from '../core/OpenAPI';
3131
import { request as __request } from '../core/request';
3232
{{/if}}
33+
{{#if @root.inlineRequestOverrides~}}
34+
import type { ApiRequestOptions } from '../core/ApiRequestOptions';
35+
{{/if}}
3336

3437
{{#equals @root.httpClient 'angular'}}
3538
@Injectable()
@@ -143,6 +146,9 @@ export class {{{name}}}{{{@root.postfix}}} {
143146
{{/each}}
144147
},
145148
{{/if}}
149+
{{#if @root.inlineRequestOverrides}}
150+
..._requestOverrides,
151+
{{/if}}
146152
});
147153
}
148154

src/templates/partials/parameters.hbs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
{{#each parameters}}
55
{{{name}}}{{#if default}} = {{{default}}}{{/if}},
66
{{/each}}
7+
{{#if @root.inlineRequestOverrides~}}
8+
_requestOverrides,
9+
{{/if}}
710
}: {
811
{{#each parameters}}
912
{{#ifdef description deprecated}}
@@ -18,11 +21,19 @@
1821
{{/ifdef}}
1922
{{{name}}}{{>isRequired}}: {{>type}},
2023
{{/each}}
24+
_requestOverrides?: Partial<ApiRequestOptions>,
2125
}
2226
{{~else}}
2327

2428
{{#each parameters}}
2529
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
2630
{{/each}}
2731
{{/if}}
32+
{{~else}}
33+
{{#if @root.inlineRequestOverrides~}}{
34+
_requestOverrides,
35+
}: {
36+
_requestOverrides?: Partial<ApiRequestOptions>,
37+
}
38+
{{/if}}
2839
{{/if}}

src/utils/writeClient.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe('writeClient', () => {
4343
HttpClient.FETCH,
4444
false,
4545
false,
46+
false,
4647
true,
4748
true,
4849
true,

src/utils/writeClient.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const writeClient = async (
3838
output: string,
3939
httpClient: HttpClient,
4040
useOptions: boolean,
41+
inlineRequestOverrides: boolean,
4142
useUnionTypes: boolean,
4243
exportCore: boolean,
4344
exportServices: boolean,
@@ -74,6 +75,7 @@ export const writeClient = async (
7475
httpClient,
7576
useUnionTypes,
7677
useOptions,
78+
inlineRequestOverrides,
7779
indent,
7880
postfix,
7981
clientName

src/utils/writeClientServices.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('writeClientServices', () => {
3939
},
4040
};
4141

42-
await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4, 'Service');
42+
await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, false, Indent.SPACE_4, 'Service');
4343

4444
expect(writeFile).toBeCalledWith('/UserService.ts', `service${EOL}`);
4545
});

0 commit comments

Comments
 (0)