Skip to content

Commit 83878eb

Browse files
committed
feat: add export options
1 parent 1bbbfdc commit 83878eb

18 files changed

+4611
-5
lines changed

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const params = program
2020
.option('--exportServices <value>', 'Write services to disk', true)
2121
.option('--exportModels <value>', 'Write models to disk', true)
2222
.option('--exportSchemas <value>', 'Write schemas to disk', false)
23+
.option('--exportOptions <value>', `Write function's options to disk`, false)
2324
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
2425
.option('--postfixServices <value>', 'Service name postfix', 'Service')
2526
.option('--postfixModels <value>', 'Model name postfix')
@@ -41,6 +42,7 @@ if (OpenAPI) {
4142
exportServices: JSON.parse(params.exportServices) === true,
4243
exportModels: JSON.parse(params.exportModels) === true,
4344
exportSchemas: JSON.parse(params.exportSchemas) === true,
45+
exportOptions: JSON.parse(params.exportOptions) === true,
4446
indent: params.indent,
4547
postfixServices: params.postfixServices,
4648
postfixModels: params.postfixModels,

bin/index.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ describe('bin', () => {
3232
'true',
3333
'--exportSchemas',
3434
'true',
35+
'--exportOptions',
36+
'true',
3537
'--indent',
3638
'4',
3739
'--postfixServices',

src/client/interfaces/Operation.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { OperationResponse } from './OperationResponse';
55
export interface Operation extends OperationParameters {
66
service: string;
77
name: string;
8+
optionsTypeName: string;
89
summary: string | null;
910
description: string | null;
1011
deprecated: boolean;

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type Options = {
2323
exportServices?: boolean;
2424
exportModels?: boolean;
2525
exportSchemas?: boolean;
26+
exportOptions?: boolean;
2627
indent?: Indent;
2728
postfixServices?: string;
2829
postfixModels?: string;
@@ -44,6 +45,7 @@ export type Options = {
4445
* @param exportServices Generate services
4546
* @param exportModels Generate models
4647
* @param exportSchemas Generate schemas
48+
* @param exportOptions Generate function's options
4749
* @param indent Indentation options (4, 2 or tab)
4850
* @param postfixServices Service name postfix
4951
* @param postfixModels Model name postfix
@@ -61,6 +63,7 @@ export const generate = async ({
6163
exportServices = true,
6264
exportModels = true,
6365
exportSchemas = false,
66+
exportOptions = false,
6467
indent = Indent.SPACE_4,
6568
postfixServices = 'Service',
6669
postfixModels = '',
@@ -91,6 +94,7 @@ export const generate = async ({
9194
exportServices,
9295
exportModels,
9396
exportSchemas,
97+
exportOptions,
9498
indent,
9599
postfixServices,
96100
postfixModels,
@@ -115,6 +119,7 @@ export const generate = async ({
115119
exportServices,
116120
exportModels,
117121
exportSchemas,
122+
exportOptions,
118123
indent,
119124
postfixServices,
120125
postfixModels,

src/openApi/v2/parser/getOperation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import camelCase from 'camelcase';
2+
13
import type { Operation } from '../../../client/interfaces/Operation';
24
import type { OperationParameters } from '../../../client/interfaces/OperationParameters';
35
import type { OpenApi } from '../interfaces/OpenApi';
@@ -26,6 +28,7 @@ export const getOperation = (
2628
const operation: Operation = {
2729
service: serviceName,
2830
name: operationName,
31+
optionsTypeName: camelCase([operationName, 'Options'], { pascalCase: true }),
2932
summary: op.summary || null,
3033
description: op.description || null,
3134
deprecated: op.deprecated === true,

src/openApi/v3/parser/getOperation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import camelCase from 'camelcase';
2+
13
import type { Operation } from '../../../client/interfaces/Operation';
24
import type { OperationParameters } from '../../../client/interfaces/OperationParameters';
35
import type { OpenApi } from '../interfaces/OpenApi';
@@ -29,6 +31,7 @@ export const getOperation = (
2931
const operation: Operation = {
3032
service: serviceName,
3133
name: operationName,
34+
optionsTypeName: camelCase([operationName, 'Options'], { pascalCase: true }),
3235
summary: op.summary || null,
3336
description: op.description || null,
3437
deprecated: op.deprecated === true,

src/templates/exportService.hbs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ import { OpenAPI } from '../core/OpenAPI';
3131
import { request as __request } from '../core/request';
3232
{{/if}}
3333

34+
{{#if @root.useOptions }}
35+
{{#if @root.exportOptions }}
36+
{{#each operations}}
37+
{{#if parameters}}
38+
export type {{{optionsTypeName}}} = {
39+
{{#each parameters}}
40+
{{#ifdef description deprecated}}
41+
/**
42+
{{#if description}}
43+
* {{{escapeComment description}}}
44+
{{/if}}
45+
{{#if deprecated}}
46+
* @deprecated
47+
{{/if}}
48+
*/
49+
{{/ifdef}}
50+
{{{name}}}{{>isRequired}}: {{>type}},
51+
{{/each}}
52+
};
53+
{{/if}}
54+
{{/each}}
55+
{{/if}}
56+
{{/if}}
57+
3458
{{#equals @root.httpClient 'angular'}}
3559
@Injectable({
3660
providedIn: 'root',

src/templates/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export { ${{{name}}} } from './schemas/${{{name}}}';
4141
{{#if services}}
4242

4343
{{#each services}}
44-
export { {{{name}}}{{{@root.postfixServices}}} } from './services/{{{name}}}{{{@root.postfixServices}}}';
44+
export { {{{name}}}{{{@root.postfixServices}}}{{#if @root.useOptions}}{{#if @root.exportOptions}}{{#each operations}}{{#if parameters}}, {{{ optionsTypeName }}}{{/if}}{{/each}}{{/if}}{{/if}} } from './services/{{{name}}}{{{@root.postfixServices}}}';
4545
{{/each}}
4646
{{/if}}
4747
{{/if}}

src/templates/partials/parameters.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{#each parameters}}
55
{{{name}}}{{#if default}} = {{{default}}}{{/if}},
66
{{/each}}
7-
}: {
7+
}: {{#if @root.exportOptions~}}{{{ optionsTypeName }}}{{~else}}{
88
{{#each parameters}}
99
{{#ifdef description deprecated}}
1010
/**
@@ -19,6 +19,7 @@
1919
{{{name}}}{{>isRequired}}: {{>type}},
2020
{{/each}}
2121
}
22+
{{/if}}
2223
{{~else}}
2324

2425
{{#each parameters}}

src/utils/writeClient.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('writeClient', () => {
4747
true,
4848
true,
4949
true,
50+
false,
5051
Indent.SPACE_4,
5152
'Service',
5253
'AppClient'

src/utils/writeClient.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { writeClientServices } from './writeClientServices';
2626
* @param exportServices Generate services
2727
* @param exportModels Generate models
2828
* @param exportSchemas Generate schemas
29-
* @param exportSchemas Generate schemas
29+
* @param exportOptions Generate function's options
3030
* @param indent Indentation options (4, 2 or tab)
3131
* @param postfixServices Service name postfix
3232
* @param postfixModels Model name postfix
@@ -44,6 +44,7 @@ export const writeClient = async (
4444
exportServices: boolean,
4545
exportModels: boolean,
4646
exportSchemas: boolean,
47+
exportOptions: boolean,
4748
indent: Indent,
4849
postfixServices: string,
4950
postfixModels: string,
@@ -76,6 +77,7 @@ export const writeClient = async (
7677
httpClient,
7778
useUnionTypes,
7879
useOptions,
80+
exportOptions,
7981
indent,
8082
postfixServices,
8183
clientName
@@ -106,10 +108,12 @@ export const writeClient = async (
106108
templates,
107109
outputPath,
108110
useUnionTypes,
111+
useOptions,
109112
exportCore,
110113
exportServices,
111114
exportModels,
112115
exportSchemas,
116+
exportOptions,
113117
postfixServices,
114118
postfixModels,
115119
clientName

src/utils/writeClientIndex.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('writeClientIndex', () => {
3636
},
3737
};
3838

39-
await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service', '');
39+
await writeClientIndex(client, templates, '/', true, true, true, true, true, true, true, 'Service', '');
4040

4141
expect(writeFile).toBeCalledWith(resolve('/', '/index.ts'), 'index');
4242
});

src/utils/writeClientIndex.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import { sortServicesByName } from './sortServicesByName';
1515
* @param templates The loaded handlebar templates
1616
* @param outputPath Directory to write the generated files to
1717
* @param useUnionTypes Use union types instead of enums
18+
* @param useOptions Use options or arguments functions
1819
* @param exportCore Generate core
1920
* @param exportServices Generate services
2021
* @param exportModels Generate models
2122
* @param exportSchemas Generate schemas
23+
* @param exportOptions Generate function's options
2224
* @param postfixServices Service name postfix
2325
* @param postfixModels Model name postfix
2426
* @param clientName Custom client class name
@@ -28,10 +30,12 @@ export const writeClientIndex = async (
2830
templates: Templates,
2931
outputPath: string,
3032
useUnionTypes: boolean,
33+
useOptions: boolean,
3134
exportCore: boolean,
3235
exportServices: boolean,
3336
exportModels: boolean,
3437
exportSchemas: boolean,
38+
exportOptions: boolean,
3539
postfixServices: string,
3640
postfixModels: string,
3741
clientName?: string
@@ -41,6 +45,8 @@ export const writeClientIndex = async (
4145
exportServices,
4246
exportModels,
4347
exportSchemas,
48+
exportOptions,
49+
useOptions,
4450
useUnionTypes,
4551
postfixServices,
4652
postfixModels,

src/utils/writeClientServices.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ describe('writeClientServices', () => {
4040
},
4141
};
4242

43-
await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4, 'Service');
43+
await writeClientServices(
44+
services,
45+
templates,
46+
'/',
47+
HttpClient.FETCH,
48+
false,
49+
false,
50+
false,
51+
Indent.SPACE_4,
52+
'Service'
53+
);
4454

4555
expect(writeFile).toBeCalledWith(resolve('/', '/UserService.ts'), `service${EOL}`);
4656
});

src/utils/writeClientServices.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { Templates } from './registerHandlebarTemplates';
1717
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
1818
* @param useUnionTypes Use union types instead of enums
1919
* @param useOptions Use options or arguments functions
20+
* @param exportOptions Generate function's options
2021
* @param indent Indentation options (4, 2 or tab)
2122
* @param postfix Service name postfix
2223
* @param clientName Custom client class name
@@ -28,6 +29,7 @@ export const writeClientServices = async (
2829
httpClient: HttpClient,
2930
useUnionTypes: boolean,
3031
useOptions: boolean,
32+
exportOptions: boolean,
3133
indent: Indent,
3234
postfix: string,
3335
clientName?: string
@@ -39,6 +41,7 @@ export const writeClientServices = async (
3941
httpClient,
4042
useUnionTypes,
4143
useOptions,
44+
exportOptions,
4245
postfix,
4346
exportClient: isDefined(clientName),
4447
});

0 commit comments

Comments
 (0)