diff --git a/.changeset/pretty-toes-return.md b/.changeset/pretty-toes-return.md new file mode 100644 index 000000000..8ca01481c --- /dev/null +++ b/.changeset/pretty-toes-return.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": minor +--- + +fix(client): do not send default params diff --git a/packages/openapi-ts/rollup.config.ts b/packages/openapi-ts/rollup.config.ts index 87db9bfcf..47a63d6c7 100644 --- a/packages/openapi-ts/rollup.config.ts +++ b/packages/openapi-ts/rollup.config.ts @@ -24,6 +24,7 @@ export function handlebarsPlugin(): Plugin { const templateSpec = handlebars.precompile(template, { knownHelpers: { camelCase: true, + dataDestructure: true, dataParameters: true, enumKey: true, enumName: true, diff --git a/packages/openapi-ts/src/templates/exportService.hbs b/packages/openapi-ts/src/templates/exportService.hbs index eb0052caa..b28459f65 100644 --- a/packages/openapi-ts/src/templates/exportService.hbs +++ b/packages/openapi-ts/src/templates/exportService.hbs @@ -40,21 +40,21 @@ export class {{{name}}}{{{@root.$config.postfixServices}}} { {{#if @root.$config.name}} {{#equals @root.$config.client 'angular'}} public {{{name}}}{{>operationTypes}}({{>operationParameters}}): Observable<{{>operationResult}}> { - {{>dataDestructure}} + {{{dataDestructure this}}} return this.httpRequest.request({ {{else}} public {{{name}}}{{>operationTypes}}({{>operationParameters}}): CancelablePromise<{{>operationResult}}> { - {{>dataDestructure}} + {{{dataDestructure this}}} return this.httpRequest.request({ {{/equals}} {{else}} {{#equals @root.$config.client 'angular'}} public {{{name}}}{{>operationTypes}}({{>operationParameters}}): Observable<{{>operationResult}}> { - {{>dataDestructure}} + {{{dataDestructure this}}} return __request({{>requestConfig}}, this.http, { {{else}} public static {{{name}}}{{>operationTypes}}({{>operationParameters}}): CancelablePromise<{{>operationResult}}> { - {{>dataDestructure}} + {{{dataDestructure this}}} return __request({{>requestConfig}}, { {{/equals}} {{/if}} diff --git a/packages/openapi-ts/src/templates/partials/dataDestructure.hbs b/packages/openapi-ts/src/templates/partials/dataDestructure.hbs deleted file mode 100644 index 9b88a43b6..000000000 --- a/packages/openapi-ts/src/templates/partials/dataDestructure.hbs +++ /dev/null @@ -1,39 +0,0 @@ -{{~#if @root.$config.name~}} -{{#if @root.$config.useOptions~}} -{{#if parameters}} -const { -{{#each parameters}} -{{{name}}}{{#if default}} = {{{default}}}{{/if}}, -{{/each}} -} = data; -{{/if}} -{{/if}} -{{else~}} -{{#if @root.$config.useOptions~}} -{{~#notEquals @root.$config.serviceResponse 'generics'~}} -{{~#if parameters~}} -const { -{{#if @root.$config.experimental}} -query, -{{/if}} -{{#each parameters}} -{{#if @root.$config.experimental}} -{{#notEquals in 'query'}} -{{{name}}}{{#if default}} = {{{default}}}{{/if}}, -{{/notEquals}} -{{else}} -{{{name}}}{{#if default}} = {{{default}}}{{/if}}, -{{/if}} -{{/each}} -} = data; -{{/if}} -{{else}} -const { -{{#each parameters}} -{{{name}}}{{#if default}} = {{{default}}}{{/if}}, -{{/each}} - ...overrides -} = data; -{{/notEquals}} -{{/if}} -{{/if}} diff --git a/packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts b/packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts index c81ea5b45..e9dda61ec 100644 --- a/packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts +++ b/packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts @@ -36,6 +36,7 @@ describe('registerHandlebarHelpers', () => { ); const helpers = Object.keys(Handlebars.helpers); expect(helpers).toContain('camelCase'); + expect(helpers).toContain('dataDestructure'); expect(helpers).toContain('dataParameters'); expect(helpers).toContain('enumKey'); expect(helpers).toContain('enumName'); diff --git a/packages/openapi-ts/src/utils/handlebars.ts b/packages/openapi-ts/src/utils/handlebars.ts index d926d5c61..c9ec10b41 100644 --- a/packages/openapi-ts/src/utils/handlebars.ts +++ b/packages/openapi-ts/src/utils/handlebars.ts @@ -2,7 +2,7 @@ import camelCase from 'camelcase'; import Handlebars from 'handlebars/runtime'; import { EOL } from 'os'; -import type { Model, OperationParameter, Service } from '../openApi'; +import type { Model, Operation, OperationParameter, Service } from '../openApi'; import templateClient from '../templates/client.hbs'; import angularGetHeaders from '../templates/core/angular/getHeaders.hbs'; import angularGetRequestBody from '../templates/core/angular/getRequestBody.hbs'; @@ -53,7 +53,6 @@ import templateExportSchema from '../templates/exportSchema.hbs'; import templateExportService from '../templates/exportService.hbs'; import templateIndex from '../templates/index.hbs'; import partialBase from '../templates/partials/base.hbs'; -import partialDataDestructure from '../templates/partials/dataDestructure.hbs'; import partialExportComposition from '../templates/partials/exportComposition.hbs'; import partialExportEnum from '../templates/partials/exportEnum.hbs'; import partialExportInterface from '../templates/partials/exportInterface.hbs'; @@ -93,6 +92,46 @@ const escapeComment = (value: string) => .replace(/\/\*/g, '*') .replace(/\r?\n(.*)/g, (_, w) => `${EOL} * ${w.trim()}`); +const dataDestructure = (config: Config, operation: Operation) => { + if (config.name) { + if (config.useOptions) { + if (operation.parameters.length) { + return `const { + ${operation.parameters.map(parameter => parameter.name).join(',\n')} + } = data;`; + } + } + } else { + if (config.useOptions) { + if (config.serviceResponse !== 'generics') { + if (operation.parameters.length) { + return `const { + ${config.experimental ? 'query,' : ''} + ${operation.parameters + .map(parameter => { + if (config.experimental) { + if (parameter.in !== 'query') { + return parameter.name; + } + } else { + return parameter.name; + } + }) + .filter(Boolean) + .join(',\n')} + } = data;`; + } + } else { + return `const { + ${operation.parameters.map(parameter => parameter.name).join(',\n')} + ...overrides + } = data;`; + } + } + } + return ''; +}; + const dataParameters = (config: Config, parameters: OperationParameter[]) => { if (config.experimental) { let output = parameters @@ -197,6 +236,10 @@ export const operationDataType = (config: Config, service: Service) => { export const registerHandlebarHelpers = (config: Config, client: Client): void => { Handlebars.registerHelper('camelCase', camelCase); + Handlebars.registerHelper('dataDestructure', function (operation: Operation) { + return dataDestructure(config, operation); + }); + Handlebars.registerHelper('dataParameters', function (parameters: OperationParameter[]) { return dataParameters(config, parameters); }); @@ -363,7 +406,6 @@ export const registerHandlebarTemplates = (config: Config, client: Client): Temp // Partials for the generations of the models, services, etc. Handlebars.registerPartial('base', Handlebars.template(partialBase)); - Handlebars.registerPartial('dataDestructure', Handlebars.template(partialDataDestructure)); Handlebars.registerPartial('exportComposition', Handlebars.template(partialExportComposition)); Handlebars.registerPartial('exportEnum', Handlebars.template(partialExportEnum)); Handlebars.registerPartial('exportInterface', Handlebars.template(partialExportInterface)); diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v2/services.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v2/services.ts.snap index 5910a0afc..bd6f2b0d1 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v2/services.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v2/services.ts.snap @@ -461,15 +461,7 @@ export class DefaultsService { * @throws ApiError */ public static callWithDefaultParameters(data: DefaultsData['CallWithDefaultParameters']): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, { method: 'GET', url: '/api/v{api-version}/defaults', @@ -489,15 +481,7 @@ export class DefaultsService { public static callWithDefaultOptionalParameters( data: DefaultsData['CallWithDefaultOptionalParameters'] = {} ): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, { method: 'POST', url: '/api/v{api-version}/defaults', @@ -517,13 +501,13 @@ export class DefaultsService { public static callToTestOrderOfParams(data: DefaultsData['CallToTestOrderOfParams']): CancelablePromise { const { parameterStringWithNoDefault, - parameterOptionalStringWithDefault = 'Hello World!', - parameterOptionalStringWithEmptyDefault = '', + parameterOptionalStringWithDefault, + parameterOptionalStringWithEmptyDefault, parameterOptionalStringWithNoDefault, - parameterStringWithDefault = 'Hello World!', - parameterStringWithEmptyDefault = '', + parameterStringWithDefault, + parameterStringWithEmptyDefault, parameterStringNullableWithNoDefault, - parameterStringNullableWithDefault = null, + parameterStringNullableWithDefault, } = data; return __request(OpenAPI, { method: 'PUT', @@ -773,10 +757,10 @@ export class TypesService { parameterArray, parameterDictionary, parameterEnum, - parameterNumber = 123, - parameterString = 'default', - parameterBoolean = true, - parameterObject = null, + parameterNumber, + parameterString, + parameterBoolean, + parameterObject, id, } = data; return __request(OpenAPI, { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.ts.snap index 5233bd2a5..fd6c2b4e9 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/services.ts.snap @@ -731,15 +731,7 @@ export class DefaultsService { public static callWithDefaultParameters( data: DefaultsData['CallWithDefaultParameters'] = {} ): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, { method: 'GET', url: '/api/v{api-version}/defaults', @@ -759,15 +751,7 @@ export class DefaultsService { public static callWithDefaultOptionalParameters( data: DefaultsData['CallWithDefaultOptionalParameters'] = {} ): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, { method: 'POST', url: '/api/v{api-version}/defaults', @@ -787,13 +771,13 @@ export class DefaultsService { public static callToTestOrderOfParams(data: DefaultsData['CallToTestOrderOfParams']): CancelablePromise { const { parameterStringWithNoDefault, - parameterOptionalStringWithDefault = 'Hello World!', - parameterOptionalStringWithEmptyDefault = '', + parameterOptionalStringWithDefault, + parameterOptionalStringWithEmptyDefault, parameterOptionalStringWithNoDefault, - parameterStringWithDefault = 'Hello World!', - parameterStringWithEmptyDefault = '', + parameterStringWithDefault, + parameterStringWithEmptyDefault, parameterStringNullableWithNoDefault, - parameterStringNullableWithDefault = null, + parameterStringNullableWithDefault, } = data; return __request(OpenAPI, { method: 'PUT', @@ -1045,10 +1029,10 @@ export class TypesService { parameterArray, parameterDictionary, parameterEnum, - parameterNumber = 123, - parameterString = 'default', - parameterBoolean = true, - parameterObject = null, + parameterNumber, + parameterString, + parameterBoolean, + parameterObject, id, } = data; return __request(OpenAPI, { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.ts.snap index 6e253eadb..5ba4856f8 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.ts.snap @@ -767,15 +767,7 @@ export class DefaultsService { * @throws ApiError */ public callWithDefaultParameters(data: DefaultsData['CallWithDefaultParameters'] = {}): Observable { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, this.http, { method: 'GET', url: '/api/v{api-version}/defaults', @@ -795,15 +787,7 @@ export class DefaultsService { public callWithDefaultOptionalParameters( data: DefaultsData['CallWithDefaultOptionalParameters'] = {} ): Observable { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, this.http, { method: 'POST', url: '/api/v{api-version}/defaults', @@ -823,13 +807,13 @@ export class DefaultsService { public callToTestOrderOfParams(data: DefaultsData['CallToTestOrderOfParams']): Observable { const { parameterStringWithNoDefault, - parameterOptionalStringWithDefault = 'Hello World!', - parameterOptionalStringWithEmptyDefault = '', + parameterOptionalStringWithDefault, + parameterOptionalStringWithEmptyDefault, parameterOptionalStringWithNoDefault, - parameterStringWithDefault = 'Hello World!', - parameterStringWithEmptyDefault = '', + parameterStringWithDefault, + parameterStringWithEmptyDefault, parameterStringNullableWithNoDefault, - parameterStringNullableWithDefault = null, + parameterStringNullableWithDefault, } = data; return __request(OpenAPI, this.http, { method: 'PUT', @@ -1119,10 +1103,10 @@ export class TypesService { parameterArray, parameterDictionary, parameterEnum, - parameterNumber = 123, - parameterString = 'default', - parameterBoolean = true, - parameterObject = null, + parameterNumber, + parameterString, + parameterBoolean, + parameterObject, id, } = data; return __request(OpenAPI, this.http, { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.ts.snap index 51c64e850..2d25d7990 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.ts.snap @@ -740,15 +740,7 @@ export class DefaultsService { * @throws ApiError */ public callWithDefaultParameters(data: DefaultsData['CallWithDefaultParameters'] = {}): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return this.httpRequest.request({ method: 'GET', url: '/api/v{api-version}/defaults', @@ -768,15 +760,7 @@ export class DefaultsService { public callWithDefaultOptionalParameters( data: DefaultsData['CallWithDefaultOptionalParameters'] = {} ): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return this.httpRequest.request({ method: 'POST', url: '/api/v{api-version}/defaults', @@ -796,13 +780,13 @@ export class DefaultsService { public callToTestOrderOfParams(data: DefaultsData['CallToTestOrderOfParams']): CancelablePromise { const { parameterStringWithNoDefault, - parameterOptionalStringWithDefault = 'Hello World!', - parameterOptionalStringWithEmptyDefault = '', + parameterOptionalStringWithDefault, + parameterOptionalStringWithEmptyDefault, parameterOptionalStringWithNoDefault, - parameterStringWithDefault = 'Hello World!', - parameterStringWithEmptyDefault = '', + parameterStringWithDefault, + parameterStringWithEmptyDefault, parameterStringNullableWithNoDefault, - parameterStringNullableWithDefault = null, + parameterStringNullableWithDefault, } = data; return this.httpRequest.request({ method: 'PUT', @@ -1068,10 +1052,10 @@ export class TypesService { parameterArray, parameterDictionary, parameterEnum, - parameterNumber = 123, - parameterString = 'default', - parameterBoolean = true, - parameterObject = null, + parameterNumber, + parameterString, + parameterBoolean, + parameterObject, id, } = data; return this.httpRequest.request({ diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.ts.snap index 5233bd2a5..fd6c2b4e9 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.ts.snap @@ -731,15 +731,7 @@ export class DefaultsService { public static callWithDefaultParameters( data: DefaultsData['CallWithDefaultParameters'] = {} ): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, { method: 'GET', url: '/api/v{api-version}/defaults', @@ -759,15 +751,7 @@ export class DefaultsService { public static callWithDefaultOptionalParameters( data: DefaultsData['CallWithDefaultOptionalParameters'] = {} ): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, { method: 'POST', url: '/api/v{api-version}/defaults', @@ -787,13 +771,13 @@ export class DefaultsService { public static callToTestOrderOfParams(data: DefaultsData['CallToTestOrderOfParams']): CancelablePromise { const { parameterStringWithNoDefault, - parameterOptionalStringWithDefault = 'Hello World!', - parameterOptionalStringWithEmptyDefault = '', + parameterOptionalStringWithDefault, + parameterOptionalStringWithEmptyDefault, parameterOptionalStringWithNoDefault, - parameterStringWithDefault = 'Hello World!', - parameterStringWithEmptyDefault = '', + parameterStringWithDefault, + parameterStringWithEmptyDefault, parameterStringNullableWithNoDefault, - parameterStringNullableWithDefault = null, + parameterStringNullableWithDefault, } = data; return __request(OpenAPI, { method: 'PUT', @@ -1045,10 +1029,10 @@ export class TypesService { parameterArray, parameterDictionary, parameterEnum, - parameterNumber = 123, - parameterString = 'default', - parameterBoolean = true, - parameterObject = null, + parameterNumber, + parameterString, + parameterBoolean, + parameterObject, id, } = data; return __request(OpenAPI, { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/services.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/services.ts.snap index 9f9d6af3c..e9dcd6725 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/services.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_options/services.ts.snap @@ -92,15 +92,7 @@ export class DefaultsService { public static callWithDefaultParameters( data: DefaultsData['CallWithDefaultParameters'] = {} ): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, { method: 'GET', url: '/api/v{api-version}/defaults', @@ -120,15 +112,7 @@ export class DefaultsService { public static callWithDefaultOptionalParameters( data: DefaultsData['CallWithDefaultOptionalParameters'] = {} ): CancelablePromise { - const { - parameterString = 'Hello World!', - parameterNumber = 123, - parameterBoolean = true, - parameterEnum = 'Success', - parameterModel = { - prop: 'Hello World!', - }, - } = data; + const { parameterString, parameterNumber, parameterBoolean, parameterEnum, parameterModel } = data; return __request(OpenAPI, { method: 'POST', url: '/api/v{api-version}/defaults', @@ -148,13 +132,13 @@ export class DefaultsService { public static callToTestOrderOfParams(data: DefaultsData['CallToTestOrderOfParams']): CancelablePromise { const { parameterStringWithNoDefault, - parameterOptionalStringWithDefault = 'Hello World!', - parameterOptionalStringWithEmptyDefault = '', + parameterOptionalStringWithDefault, + parameterOptionalStringWithEmptyDefault, parameterOptionalStringWithNoDefault, - parameterStringWithDefault = 'Hello World!', - parameterStringWithEmptyDefault = '', + parameterStringWithDefault, + parameterStringWithEmptyDefault, parameterStringNullableWithNoDefault, - parameterStringNullableWithDefault = null, + parameterStringNullableWithDefault, } = data; return __request(OpenAPI, { method: 'PUT',