Skip to content

Commit

Permalink
Merge pull request #746 from hey-api/fix/client-fetch-body
Browse files Browse the repository at this point in the history
fix: handle formData parameters in generated types
  • Loading branch information
mrlubos authored Jul 4, 2024
2 parents 0e14810 + 0448823 commit 24ed655
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-carrots-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: handle formData parameters in generated types
15 changes: 10 additions & 5 deletions packages/openapi-ts/src/utils/write/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand Down Expand Up @@ -379,7 +384,7 @@ const processServiceTypes = ({
response.responseTypes.includes('error'),
);

if (isStandaloneClient(config)) {
if (isStandalone) {
// create type export for operation error
generateType({
client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,10 @@ export type PostApiRequestBodyData = {
};

export type PostApiFormDataData = {
/**
* A reusable request body
*/
body?: ModelWithString;
query?: {
/**
* This is a reusable parameter
Expand Down Expand Up @@ -1414,7 +1418,12 @@ export type ComplexTypesResponse = Array<ModelWithString>;

export type ComplexTypesError = unknown;

export type MultipartRequestData = unknown;
export type MultipartRequestData = {
body?: {
content?: (Blob | File);
data?: ModelWithString | null;
};
};

export type MultipartResponseResponse = {
file?: (Blob | File);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,10 @@ export type PostApiRequestBodyData = {
};

export type PostApiFormDataData = {
/**
* A reusable request body
*/
body?: ModelWithString;
query?: {
/**
* This is a reusable parameter
Expand Down Expand Up @@ -1414,7 +1418,12 @@ export type ComplexTypesResponse = Array<ModelWithString>;

export type ComplexTypesError = unknown;

export type MultipartRequestData = unknown;
export type MultipartRequestData = {
body?: {
content?: (Blob | File);
data?: ModelWithString | null;
};
};

export type MultipartResponseResponse = {
file?: (Blob | File);
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-ts/test/sample.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 24ed655

Please sign in to comment.