diff --git a/bin/index.js b/bin/index.js index decf79420..712b5c133 100755 --- a/bin/index.js +++ b/bin/index.js @@ -20,6 +20,7 @@ const params = program .option('--exportServices ', 'Write services to disk', true) .option('--exportModels ', 'Write models to disk', true) .option('--exportSchemas ', 'Write schemas to disk', false) + .option('--exportIndex ', 'Write index file to disk (default: exportCore || exportServices || exportModels || exportSchemas)') .option('--indent ', 'Indentation options [4, 2, tabs]', '4') .option('--postfix ', 'Service name postfix', 'Service') .option('--request ', 'Path to custom request file') @@ -40,6 +41,7 @@ if (OpenAPI) { exportServices: JSON.parse(params.exportServices) === true, exportModels: JSON.parse(params.exportModels) === true, exportSchemas: JSON.parse(params.exportSchemas) === true, + exportIndex: params.exportIndex ? JSON.parse(params.exportIndex) : undefined, indent: params.indent, postfix: params.postfix, request: params.request, diff --git a/bin/index.spec.js b/bin/index.spec.js index 289631812..4cc986a27 100755 --- a/bin/index.spec.js +++ b/bin/index.spec.js @@ -32,6 +32,8 @@ describe('bin', () => { 'true', '--exportSchemas', 'true', + '--exportIndex', + 'true', '--indent', '4', '--postfix', diff --git a/package-lock.json b/package-lock.json index 79f00627b..16c36b856 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "openapi-typescript-codegen", - "version": "0.20.1", + "version": "0.21.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "openapi-typescript-codegen", - "version": "0.20.1", + "version": "0.21.0", "license": "MIT", "dependencies": { "camelcase": "^6.3.0", diff --git a/src/index.ts b/src/index.ts index ef7a8b1bf..fbe1be5ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,6 +23,7 @@ export type Options = { exportServices?: boolean; exportModels?: boolean; exportSchemas?: boolean; + exportIndex?: boolean; indent?: Indent; postfix?: string; request?: string; @@ -43,6 +44,7 @@ export type Options = { * @param exportServices Generate services * @param exportModels Generate models * @param exportSchemas Generate schemas + * @param exportSchemas Generate index file * @param indent Indentation options (4, 2 or tab) * @param postfix Service name postfix * @param request Path to custom request file @@ -59,6 +61,7 @@ export const generate = async ({ exportServices = true, exportModels = true, exportSchemas = false, + exportIndex, indent = Indent.SPACE_4, postfix = 'Service', request, @@ -88,6 +91,7 @@ export const generate = async ({ exportServices, exportModels, exportSchemas, + exportIndex, indent, postfix, clientName, @@ -111,6 +115,7 @@ export const generate = async ({ exportServices, exportModels, exportSchemas, + exportIndex, indent, postfix, clientName, diff --git a/src/templates/exportService.hbs b/src/templates/exportService.hbs index 2fdd9af58..a5fd839f8 100644 --- a/src/templates/exportService.hbs +++ b/src/templates/exportService.hbs @@ -31,120 +31,90 @@ import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; {{/if}} -{{#equals @root.httpClient 'angular'}} -@Injectable() -{{/equals}} -export class {{{name}}}{{{@root.postfix}}} { - {{#if @root.exportClient}} - - constructor(public readonly httpRequest: BaseHttpRequest) {} - {{else}} - {{#equals @root.httpClient 'angular'}} - - constructor(public readonly http: HttpClient) {} - {{/equals}} - {{/if}} - - {{#each operations}} - /** - {{#if deprecated}} - * @deprecated - {{/if}} - {{#if summary}} - * {{{escapeComment summary}}} - {{/if}} - {{#if description}} - * {{{escapeComment description}}} - {{/if}} - {{#unless @root.useOptions}} - {{#if parameters}} - {{#each parameters}} - * @param {{{name}}} {{#if description}}{{{escapeComment description}}}{{/if}} - {{/each}} - {{/if}} - {{/unless}} - {{#each results}} - * @returns {{{type}}} {{#if description}}{{{escapeComment description}}}{{/if}} - {{/each}} - * @throws ApiError - */ - {{#if @root.exportClient}} - {{#equals @root.httpClient 'angular'}} - public {{{name}}}({{>parameters}}): Observable<{{>result}}> { - return this.httpRequest.request({ - {{else}} - public {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> { - return this.httpRequest.request({ - {{/equals}} - {{else}} - {{#equals @root.httpClient 'angular'}} - public {{{name}}}({{>parameters}}): Observable<{{>result}}> { - return __request(OpenAPI, this.http, { - {{else}} - public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> { - return __request(OpenAPI, { - {{/equals}} - {{/if}} - method: '{{{method}}}', - url: '{{{path}}}', - {{#if parametersPath}} - path: { - {{#each parametersPath}} - '{{{prop}}}': {{{name}}}, - {{/each}} - }, - {{/if}} - {{#if parametersCookie}} - cookies: { - {{#each parametersCookie}} - '{{{prop}}}': {{{name}}}, - {{/each}} - }, - {{/if}} - {{#if parametersHeader}} - headers: { - {{#each parametersHeader}} - '{{{prop}}}': {{{name}}}, - {{/each}} - }, - {{/if}} - {{#if parametersQuery}} - query: { - {{#each parametersQuery}} - '{{{prop}}}': {{{name}}}, - {{/each}} - }, - {{/if}} - {{#if parametersForm}} - formData: { - {{#each parametersForm}} - '{{{prop}}}': {{{name}}}, - {{/each}} - }, - {{/if}} - {{#if parametersBody}} - {{#equals parametersBody.in 'formData'}} - formData: {{{parametersBody.name}}}, - {{/equals}} - {{#equals parametersBody.in 'body'}} - body: {{{parametersBody.name}}}, - {{/equals}} - {{#if parametersBody.mediaType}} - mediaType: '{{{parametersBody.mediaType}}}', - {{/if}} - {{/if}} - {{#if responseHeader}} - responseHeader: '{{{responseHeader}}}', - {{/if}} - {{#if errors}} - errors: { - {{#each errors}} - {{{code}}}: `{{{escapeDescription description}}}`, - {{/each}} - }, - {{/if}} - }); - } +{{#each operations}} +/** +{{#if deprecated}} + * @deprecated +{{/if}} +{{#if summary}} + * {{{escapeComment summary}}} +{{/if}} +{{#if description}} + * {{{escapeComment description}}} +{{/if}} +{{#unless @root.useOptions}} +{{#if parameters}} +{{#each parameters}} + * @param {{{name}}} {{#if description}}{{{escapeComment description}}}{{/if}} +{{/each}} +{{/if}} +{{/unless}} +{{#each results}} + * @returns {{{type}}} {{#if description}}{{{escapeComment description}}}{{/if}} +{{/each}} + * @throws ApiError + */ +export const {{{name}}} = ({{>parameters}}): CancelablePromise<{{>result}}> => { + return __request(OpenAPI, { + method: '{{{method}}}', + url: '{{{path}}}', + {{#if parametersPath}} + path: { + {{#each parametersPath}} + '{{{prop}}}': {{{name}}}, + {{/each}} + }, + {{/if}} + {{#if parametersCookie}} + cookies: { + {{#each parametersCookie}} + '{{{prop}}}': {{{name}}}, + {{/each}} + }, + {{/if}} + {{#if parametersHeader}} + headers: { + {{#each parametersHeader}} + '{{{prop}}}': {{{name}}}, + {{/each}} + }, + {{/if}} + {{#if parametersQuery}} + query: { + {{#each parametersQuery}} + '{{{prop}}}': {{{name}}}, + {{/each}} + }, + {{/if}} + {{#if parametersForm}} + formData: { + {{#each parametersForm}} + '{{{prop}}}': {{{name}}}, + {{/each}} + }, + {{/if}} + {{#if parametersBody}} + {{#equals parametersBody.in 'formData'}} + formData: {{{parametersBody.name}}}, + {{/equals}} + {{#equals parametersBody.in 'body'}} + body: {{{parametersBody.name}}}, + {{/equals}} + {{#if parametersBody.mediaType}} + mediaType: '{{{parametersBody.mediaType}}}', + {{/if}} + {{/if}} + {{#if responseHeader}} + responseHeader: '{{{responseHeader}}}', + {{/if}} + {{#if errors}} + errors: { + {{#each errors}} + {{{code}}}: `{{{escapeDescription description}}}`, + {{/each}} + }, + {{/if}} + }); +}; - {{/each}} -} +{{/each}} diff --git a/src/utils/writeClient.spec.ts b/src/utils/writeClient.spec.ts index 3c06a95a5..0999e6da9 100644 --- a/src/utils/writeClient.spec.ts +++ b/src/utils/writeClient.spec.ts @@ -47,6 +47,7 @@ describe('writeClient', () => { true, true, true, + undefined, Indent.SPACE_4, 'Service', 'AppClient' diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index a0ffc1821..3f97d1d45 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -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 exportIndex Generate index file * @param indent Indentation options (4, 2 or tab) * @param postfix Service name postfix * @param clientName Custom client class name @@ -43,6 +43,7 @@ export const writeClient = async ( exportServices: boolean, exportModels: boolean, exportSchemas: boolean, + exportIndex: boolean | undefined, indent: Indent, postfix: string, clientName?: string, @@ -97,7 +98,7 @@ export const writeClient = async ( await writeClientClass(client, templates, outputPath, httpClient, clientName, indent, postfix); } - if (exportCore || exportServices || exportSchemas || exportModels) { + if (exportIndex ?? (exportCore || exportServices || exportSchemas || exportModels)) { await mkdir(outputPath); await writeClientIndex( client,