Skip to content

export operation's options as type #1700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ $ openapi --help
--exportServices <value> Write services to disk (default: true)
--exportModels <value> Write models to disk (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--exportOptions <value> Write function's options to disk (default: false)
--indent <value> Indentation options [4, 2, tab] (default: "4")
--postfixServices Service name postfix (default: "Service")
--postfixModels Model name postfix
Expand Down
2 changes: 2 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const params = program
.option('--exportServices <value>', 'Write services to disk', true)
.option('--exportModels <value>', 'Write models to disk', true)
.option('--exportSchemas <value>', 'Write schemas to disk', false)
.option('--exportOptions <value>', `Write function's options to disk`, false)
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
.option('--postfixServices <value>', 'Service name postfix', 'Service')
.option('--postfixModels <value>', 'Model name postfix')
Expand All @@ -41,6 +42,7 @@ if (OpenAPI) {
exportServices: JSON.parse(params.exportServices) === true,
exportModels: JSON.parse(params.exportModels) === true,
exportSchemas: JSON.parse(params.exportSchemas) === true,
exportOptions: JSON.parse(params.exportOptions) === true,
indent: params.indent,
postfixServices: params.postfixServices,
postfixModels: params.postfixModels,
Expand Down
2 changes: 2 additions & 0 deletions bin/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ describe('bin', () => {
'true',
'--exportSchemas',
'true',
'--exportOptions',
'true',
'--indent',
'4',
'--postfixServices',
Expand Down
1 change: 1 addition & 0 deletions src/client/interfaces/Operation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { OperationResponse } from './OperationResponse';
export interface Operation extends OperationParameters {
service: string;
name: string;
optionsTypeName: string;
summary: string | null;
description: string | null;
deprecated: boolean;
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type Options = {
exportServices?: boolean;
exportModels?: boolean;
exportSchemas?: boolean;
exportOptions?: boolean;
indent?: Indent;
postfixServices?: string;
postfixModels?: string;
Expand All @@ -44,6 +45,7 @@ export type Options = {
* @param exportServices Generate services
* @param exportModels Generate models
* @param exportSchemas Generate schemas
* @param exportOptions Generate function's options
* @param indent Indentation options (4, 2 or tab)
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
Expand All @@ -61,6 +63,7 @@ export const generate = async ({
exportServices = true,
exportModels = true,
exportSchemas = false,
exportOptions = false,
indent = Indent.SPACE_4,
postfixServices = 'Service',
postfixModels = '',
Expand Down Expand Up @@ -91,6 +94,7 @@ export const generate = async ({
exportServices,
exportModels,
exportSchemas,
exportOptions,
indent,
postfixServices,
postfixModels,
Expand All @@ -115,6 +119,7 @@ export const generate = async ({
exportServices,
exportModels,
exportSchemas,
exportOptions,
indent,
postfixServices,
postfixModels,
Expand Down
3 changes: 3 additions & 0 deletions src/openApi/v2/parser/getOperation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import camelCase from 'camelcase';

import type { Operation } from '../../../client/interfaces/Operation';
import type { OperationParameters } from '../../../client/interfaces/OperationParameters';
import type { OpenApi } from '../interfaces/OpenApi';
Expand Down Expand Up @@ -26,6 +28,7 @@ export const getOperation = (
const operation: Operation = {
service: serviceName,
name: operationName,
optionsTypeName: camelCase([operationName, 'Options'], { pascalCase: true }),
summary: op.summary || null,
description: op.description || null,
deprecated: op.deprecated === true,
Expand Down
3 changes: 3 additions & 0 deletions src/openApi/v3/parser/getOperation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import camelCase from 'camelcase';

import type { Operation } from '../../../client/interfaces/Operation';
import type { OperationParameters } from '../../../client/interfaces/OperationParameters';
import type { OpenApi } from '../interfaces/OpenApi';
Expand Down Expand Up @@ -29,6 +31,7 @@ export const getOperation = (
const operation: Operation = {
service: serviceName,
name: operationName,
optionsTypeName: camelCase([operationName, 'Options'], { pascalCase: true }),
summary: op.summary || null,
description: op.description || null,
deprecated: op.deprecated === true,
Expand Down
24 changes: 24 additions & 0 deletions src/templates/exportService.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ import { OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';
{{/if}}

{{#if @root.useOptions }}
{{#if @root.exportOptions }}
{{#each operations}}
{{#if parameters}}
export type {{{optionsTypeName}}} = {
{{#each parameters}}
{{#ifdef description deprecated}}
/**
{{#if description}}
* {{{escapeComment description}}}
{{/if}}
{{#if deprecated}}
* @deprecated
{{/if}}
*/
{{/ifdef}}
{{{name}}}{{>isRequired}}: {{>type}},
{{/each}}
};
{{/if}}
{{/each}}
{{/if}}
{{/if}}

{{#equals @root.httpClient 'angular'}}
@Injectable({
providedIn: 'root',
Expand Down
2 changes: 1 addition & 1 deletion src/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export { ${{{name}}} } from './schemas/${{{name}}}';
{{#if services}}

{{#each services}}
export { {{{name}}}{{{@root.postfixServices}}} } from './services/{{{name}}}{{{@root.postfixServices}}}';
export { {{{name}}}{{{@root.postfixServices}}}{{#if @root.useOptions}}{{#if @root.exportOptions}}{{#each operations}}{{#if parameters}}, {{{ optionsTypeName }}}{{/if}}{{/each}}{{/if}}{{/if}} } from './services/{{{name}}}{{{@root.postfixServices}}}';
{{/each}}
{{/if}}
{{/if}}
3 changes: 2 additions & 1 deletion src/templates/partials/parameters.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{#each parameters}}
{{{name}}}{{#if default}} = {{{default}}}{{/if}},
{{/each}}
}: {
}: {{#if @root.exportOptions~}}{{{ optionsTypeName }}}{{~else}}{
{{#each parameters}}
{{#ifdef description deprecated}}
/**
Expand All @@ -19,6 +19,7 @@
{{{name}}}{{>isRequired}}: {{>type}},
{{/each}}
}
{{/if}}
{{~else}}

{{#each parameters}}
Expand Down
1 change: 1 addition & 0 deletions src/utils/writeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('writeClient', () => {
true,
true,
true,
false,
Indent.SPACE_4,
'Service',
'AppClient'
Expand Down
6 changes: 5 additions & 1 deletion src/utils/writeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { writeClientServices } from './writeClientServices';
* @param exportServices Generate services
* @param exportModels Generate models
* @param exportSchemas Generate schemas
* @param exportSchemas Generate schemas
* @param exportOptions Generate function's options
* @param indent Indentation options (4, 2 or tab)
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
Expand All @@ -44,6 +44,7 @@ export const writeClient = async (
exportServices: boolean,
exportModels: boolean,
exportSchemas: boolean,
exportOptions: boolean,
indent: Indent,
postfixServices: string,
postfixModels: string,
Expand Down Expand Up @@ -76,6 +77,7 @@ export const writeClient = async (
httpClient,
useUnionTypes,
useOptions,
exportOptions,
indent,
postfixServices,
clientName
Expand Down Expand Up @@ -106,10 +108,12 @@ export const writeClient = async (
templates,
outputPath,
useUnionTypes,
useOptions,
exportCore,
exportServices,
exportModels,
exportSchemas,
exportOptions,
postfixServices,
postfixModels,
clientName
Expand Down
2 changes: 1 addition & 1 deletion src/utils/writeClientIndex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('writeClientIndex', () => {
},
};

await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service', '');
await writeClientIndex(client, templates, '/', true, true, true, true, true, true, true, 'Service', '');

expect(writeFile).toBeCalledWith(resolve('/', '/index.ts'), 'index');
});
Expand Down
6 changes: 6 additions & 0 deletions src/utils/writeClientIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import { sortServicesByName } from './sortServicesByName';
* @param templates The loaded handlebar templates
* @param outputPath Directory to write the generated files to
* @param useUnionTypes Use union types instead of enums
* @param useOptions Use options or arguments functions
* @param exportCore Generate core
* @param exportServices Generate services
* @param exportModels Generate models
* @param exportSchemas Generate schemas
* @param exportOptions Generate function's options
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
* @param clientName Custom client class name
Expand All @@ -28,10 +30,12 @@ export const writeClientIndex = async (
templates: Templates,
outputPath: string,
useUnionTypes: boolean,
useOptions: boolean,
exportCore: boolean,
exportServices: boolean,
exportModels: boolean,
exportSchemas: boolean,
exportOptions: boolean,
postfixServices: string,
postfixModels: string,
clientName?: string
Expand All @@ -41,6 +45,8 @@ export const writeClientIndex = async (
exportServices,
exportModels,
exportSchemas,
exportOptions,
useOptions,
useUnionTypes,
postfixServices,
postfixModels,
Expand Down
12 changes: 11 additions & 1 deletion src/utils/writeClientServices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ 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(resolve('/', '/UserService.ts'), `service${EOL}`);
});
Expand Down
3 changes: 3 additions & 0 deletions src/utils/writeClientServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { Templates } from './registerHandlebarTemplates';
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
* @param useUnionTypes Use union types instead of enums
* @param useOptions Use options or arguments functions
* @param exportOptions Generate function's options
* @param indent Indentation options (4, 2 or tab)
* @param postfix Service name postfix
* @param clientName Custom client class name
Expand All @@ -28,6 +29,7 @@ export const writeClientServices = async (
httpClient: HttpClient,
useUnionTypes: boolean,
useOptions: boolean,
exportOptions: boolean,
indent: Indent,
postfix: string,
clientName?: string
Expand All @@ -39,6 +41,7 @@ export const writeClientServices = async (
httpClient,
useUnionTypes,
useOptions,
exportOptions,
postfix,
exportClient: isDefined(clientName),
});
Expand Down
Loading