Skip to content

Commit

Permalink
Merge pull request #267 from hey-api/fix/client-remove-default-params
Browse files Browse the repository at this point in the history
feat(client): do not send default params
  • Loading branch information
mrlubos authored Apr 5, 2024
2 parents 9235d63 + 82f7619 commit b81dd9a
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 204 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-toes-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

fix(client): do not send default params
1 change: 1 addition & 0 deletions packages/openapi-ts/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function handlebarsPlugin(): Plugin {
const templateSpec = handlebars.precompile(template, {
knownHelpers: {
camelCase: true,
dataDestructure: true,
dataParameters: true,
enumKey: true,
enumName: true,
Expand Down
8 changes: 4 additions & 4 deletions packages/openapi-ts/src/templates/exportService.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
39 changes: 0 additions & 39 deletions packages/openapi-ts/src/templates/partials/dataDestructure.hbs

This file was deleted.

1 change: 1 addition & 0 deletions packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
48 changes: 45 additions & 3 deletions packages/openapi-ts/src/utils/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,15 +461,7 @@ export class DefaultsService {
* @throws ApiError
*/
public static callWithDefaultParameters(data: DefaultsData['CallWithDefaultParameters']): CancelablePromise<void> {
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',
Expand All @@ -489,15 +481,7 @@ export class DefaultsService {
public static callWithDefaultOptionalParameters(
data: DefaultsData['CallWithDefaultOptionalParameters'] = {}
): CancelablePromise<void> {
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',
Expand All @@ -517,13 +501,13 @@ export class DefaultsService {
public static callToTestOrderOfParams(data: DefaultsData['CallToTestOrderOfParams']): CancelablePromise<void> {
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',
Expand Down Expand Up @@ -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, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,15 +731,7 @@ export class DefaultsService {
public static callWithDefaultParameters(
data: DefaultsData['CallWithDefaultParameters'] = {}
): CancelablePromise<void> {
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',
Expand All @@ -759,15 +751,7 @@ export class DefaultsService {
public static callWithDefaultOptionalParameters(
data: DefaultsData['CallWithDefaultOptionalParameters'] = {}
): CancelablePromise<void> {
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',
Expand All @@ -787,13 +771,13 @@ export class DefaultsService {
public static callToTestOrderOfParams(data: DefaultsData['CallToTestOrderOfParams']): CancelablePromise<void> {
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',
Expand Down Expand Up @@ -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, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,15 +767,7 @@ export class DefaultsService {
* @throws ApiError
*/
public callWithDefaultParameters(data: DefaultsData['CallWithDefaultParameters'] = {}): Observable<void> {
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',
Expand All @@ -795,15 +787,7 @@ export class DefaultsService {
public callWithDefaultOptionalParameters(
data: DefaultsData['CallWithDefaultOptionalParameters'] = {}
): Observable<void> {
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',
Expand All @@ -823,13 +807,13 @@ export class DefaultsService {
public callToTestOrderOfParams(data: DefaultsData['CallToTestOrderOfParams']): Observable<void> {
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',
Expand Down Expand Up @@ -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, {
Expand Down
Loading

0 comments on commit b81dd9a

Please sign in to comment.