Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Extract requestBody type if inline
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Jan 28, 2020
1 parent 1f9452b commit a6919e1
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export const generateRestfulComponent = (
const responseTypes = getResReqTypes(Object.entries(operation.responses).filter(isOk)) || "void";
const errorTypes = getResReqTypes(Object.entries(operation.responses).filter(isError)) || "unknown";
const requestBodyTypes = getResReqTypes([["body", operation.requestBody!]]);
const needARequestBodyComponent = requestBodyTypes.includes("{");
const needAResponseComponent = responseTypes.includes("{");

/**
Expand Down Expand Up @@ -349,7 +350,13 @@ export const generateRestfulComponent = (
}`
: `${needAResponseComponent ? componentName + "Response" : responseTypes}, ${errorTypes}, ${
queryParamsType ? componentName + "QueryParams" : "void"
}, ${verb === "delete" && lastParamInTheRoute ? "string" : requestBodyTypes}`;
}, ${
verb === "delete" && lastParamInTheRoute
? "string"
: needARequestBodyComponent
? componentName + "RequestBody"
: requestBodyTypes
}`;

const genericsTypesForHooksProps =
verb === "get"
Expand All @@ -358,7 +365,13 @@ export const generateRestfulComponent = (
}`
: `${needAResponseComponent ? componentName + "Response" : responseTypes}, ${
queryParamsType ? componentName + "QueryParams" : "void"
}, ${verb === "delete" && lastParamInTheRoute ? "string" : requestBodyTypes}`;
}, ${
verb === "delete" && lastParamInTheRoute
? "string"
: needARequestBodyComponent
? componentName + "RequestBody"
: requestBodyTypes
}`;

const customPropsEntries = Object.entries(customProps);

Expand All @@ -376,6 +389,12 @@ export ${
export interface ${componentName}QueryParams {
${queryParamsType};
}
`
: ""
}${
needARequestBodyComponent
? `
export interface ${componentName}RequestBody ${requestBodyTypes}
`
: ""
}
Expand Down
86 changes: 86 additions & 0 deletions src/scripts/tests/import-open-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,92 @@ describe("scripts/import-open-api", () => {
"
`);
});

it("should generate a request body type if it's inline in the specs", () => {
const operation: OperationObject = {
summary: "Update use case details",
operationId: "updateUseCase",
tags: ["use-case"],
parameters: [
{
name: "useCaseId",
in: "path",
required: true,
description: "The id of the use case",
schema: { type: "string", format: "uuid" },
},
],
requestBody: {
required: true,
content: {
"application/json": {
schema: {
type: "object",
required: ["name"],
properties: {
name: {
type: "string",
description: "The use case name",
},
description: {
type: "string",
},
},
},
},
},
},
responses: {
"204": {
description: "Use case updated",
content: { "application/json": { schema: { $ref: "#/components/schemas/UseCaseResponse" } } },
},
default: {
description: "unexpected error",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/APIError" },
example: { errors: ["msg1", "msg2"] },
},
},
},
},
};

expect(generateRestfulComponent(operation, "put", "/use-cases/{useCaseId}", [])).toMatchInlineSnapshot(`
"
export interface UpdateUseCaseRequestBody {
/**
* The use case name
*/
name: string;
description?: string;
}
export type UpdateUseCaseProps = Omit<MutateProps<UseCaseResponse, APIError, void, UpdateUseCaseRequestBody>, \\"path\\" | \\"verb\\"> & {useCaseId: string};
/**
* Update use case details
*/
export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => (
<Mutate<UseCaseResponse, APIError, void, UpdateUseCaseRequestBody>
verb=\\"PUT\\"
path={\`/use-cases/\${useCaseId}\`}
{...props}
/>
);
export type UseUpdateUseCaseProps = Omit<UseMutateProps<UseCaseResponse, void, UpdateUseCaseRequestBody>, \\"path\\" | \\"verb\\"> & {useCaseId: string};
/**
* Update use case details
*/
export const useUpdateUseCase = ({useCaseId, ...props}: UseUpdateUseCaseProps) => useMutate<UseCaseResponse, APIError, void, UpdateUseCaseRequestBody>(\\"PUT\\", \`/use-cases/\${useCaseId}\`, props);
"
`);
});

it("should generate a proper ComponentResponse type if the type is custom", () => {
const operation: OperationObject = {
summary: "Update use case details",
Expand Down

0 comments on commit a6919e1

Please sign in to comment.