From 044882399d56d1532c7125df2e69cfb3b5fb4f34 Mon Sep 17 00:00:00 2001 From: Lubos Date: Thu, 4 Jul 2024 02:11:00 +0100 Subject: [PATCH] fix: handle formData parameters in generated types --- .changeset/brown-carrots-yawn.md | 5 +++++ packages/openapi-ts/src/utils/write/types.ts | 15 ++++++++++----- .../v3_hey-api_client-axios/types.gen.ts.snap | 11 ++++++++++- .../v3_hey-api_client-fetch/types.gen.ts.snap | 11 ++++++++++- packages/openapi-ts/test/sample.cjs | 6 +++--- 5 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 .changeset/brown-carrots-yawn.md diff --git a/.changeset/brown-carrots-yawn.md b/.changeset/brown-carrots-yawn.md new file mode 100644 index 000000000..b4d5df63b --- /dev/null +++ b/.changeset/brown-carrots-yawn.md @@ -0,0 +1,5 @@ +--- +'@hey-api/openapi-ts': patch +--- + +fix: handle formData parameters in generated types diff --git a/packages/openapi-ts/src/utils/write/types.ts b/packages/openapi-ts/src/utils/write/types.ts index f6c9a4658..69c741a98 100644 --- a/packages/openapi-ts/src/utils/write/types.ts +++ b/packages/openapi-ts/src/utils/write/types.ts @@ -265,15 +265,20 @@ const processServiceTypes = ({ } if (operation.parameters.length > 0) { - const bodyParameter = operation.parameters - .filter((parameter) => parameter.in === 'body') - .sort(sorterByName)[0]; + let bodyParameter = operation.parameters.find( + (parameter) => parameter.in === 'body', + ); + if (!bodyParameter) { + bodyParameter = operation.parameters.find( + (parameter) => parameter.in === 'formData', + ); + } const bodyParameters: OperationParameter = { + mediaType: null, ...emptyModel, ...bodyParameter, in: 'body', isRequired: bodyParameter ? bodyParameter.isRequired : false, - // mediaType: null, name: 'body', prop: 'body', }; @@ -379,7 +384,7 @@ const processServiceTypes = ({ response.responseTypes.includes('error'), ); - if (isStandaloneClient(config)) { + if (isStandalone) { // create type export for operation error generateType({ client, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap index 27e0fb216..cc29c9fdc 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap @@ -1174,6 +1174,10 @@ export type PostApiRequestBodyData = { }; export type PostApiFormDataData = { + /** + * A reusable request body + */ + body?: ModelWithString; query?: { /** * This is a reusable parameter @@ -1414,7 +1418,12 @@ export type ComplexTypesResponse = Array; export type ComplexTypesError = unknown; -export type MultipartRequestData = unknown; +export type MultipartRequestData = { + body?: { + content?: (Blob | File); + data?: ModelWithString | null; + }; +}; export type MultipartResponseResponse = { file?: (Blob | File); diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap index 27e0fb216..cc29c9fdc 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap @@ -1174,6 +1174,10 @@ export type PostApiRequestBodyData = { }; export type PostApiFormDataData = { + /** + * A reusable request body + */ + body?: ModelWithString; query?: { /** * This is a reusable parameter @@ -1414,7 +1418,12 @@ export type ComplexTypesResponse = Array; export type ComplexTypesError = unknown; -export type MultipartRequestData = unknown; +export type MultipartRequestData = { + body?: { + content?: (Blob | File); + data?: ModelWithString | null; + }; +}; export type MultipartResponseResponse = { file?: (Blob | File); diff --git a/packages/openapi-ts/test/sample.cjs b/packages/openapi-ts/test/sample.cjs index 9a1a17627..0e214d30a 100644 --- a/packages/openapi-ts/test/sample.cjs +++ b/packages/openapi-ts/test/sample.cjs @@ -17,14 +17,14 @@ const main = async () => { }, services: { // asClass: true, - export: false, + // export: false, // name: '^Parameters', }, types: { // dates: 'types+transform', // enums: 'typescript', - include: - '^(ModelWithOneOfAndProperties|CompositionWithOneOfAndProperties)', + // include: + // '^(ModelWithOneOfAndProperties|CompositionWithOneOfAndProperties)', // name: 'PascalCase', }, // useOptions: false,