Skip to content

Commit 052cfdb

Browse files
committed
feat: add export options
1 parent 1bbbfdc commit 052cfdb

19 files changed

+4612
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ $ openapi --help
4646
--exportServices <value> Write services to disk (default: true)
4747
--exportModels <value> Write models to disk (default: true)
4848
--exportSchemas <value> Write schemas to disk (default: false)
49+
--exportOptions <value> Write function's options to disk (default: false)
4950
--indent <value> Indentation options [4, 2, tab] (default: "4")
5051
--postfixServices Service name postfix (default: "Service")
5152
--postfixModels Model name postfix

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}}

0 commit comments

Comments
 (0)