Skip to content

Commit

Permalink
fix: handle formData parameters in generated types
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Jul 4, 2024
1 parent 0e14810 commit 0448823
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',
);
}

Check warning on line 275 in packages/openapi-ts/src/utils/write/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/types.ts#L268-L275

Added lines #L268 - L275 were not covered by tests
const bodyParameters: OperationParameter = {
mediaType: null,

Check warning on line 277 in packages/openapi-ts/src/utils/write/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/types.ts#L277

Added line #L277 was not covered by tests
...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) {

Check warning on line 387 in packages/openapi-ts/src/utils/write/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/types.ts#L387

Added line #L387 was not covered by tests
// 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 0448823

Please sign in to comment.