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

Commit

Permalink
Use type from specs for delete operations
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Mar 22, 2020
1 parent 24c99f1 commit eb569d4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,22 @@ export const generateRestfulComponent = (
})
.join(";\n ");

// Retrieve the type of the param for delete verb
const lastParamInTheRouteDefinition = operation.parameters
? (operation.parameters.find(p => {
if (isReference(p)) return false;
return p.name === lastParamInTheRoute;
}) as ParameterObject | undefined) // Reference is not possible
: { schema: { type: "string" } };

const lastParamInTheRouteType =
lastParamInTheRouteDefinition &&
!isReference(lastParamInTheRouteDefinition) &&
!isReference(lastParamInTheRouteDefinition.schema) &&
lastParamInTheRouteDefinition.schema
? lastParamInTheRouteDefinition.schema.type
: "string";

const genericsTypes =
verb === "get"
? `${needAResponseComponent ? componentName + "Response" : responseTypes}, ${errorTypes}, ${
Expand All @@ -369,7 +385,7 @@ export const generateRestfulComponent = (
queryParamsType ? componentName + "QueryParams" : "void"
}, ${
verb === "delete" && lastParamInTheRoute
? "string"
? lastParamInTheRouteType
: needARequestBodyComponent
? componentName + "RequestBody"
: requestBodyTypes
Expand All @@ -384,7 +400,7 @@ export const generateRestfulComponent = (
queryParamsType ? componentName + "QueryParams" : "void"
}, ${
verb === "delete" && lastParamInTheRoute
? "string"
? lastParamInTheRouteType
: needARequestBodyComponent
? componentName + "RequestBody"
: requestBodyTypes
Expand Down
8 changes: 4 additions & 4 deletions src/scripts/tests/__snapshots__/import-open-api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,25 @@ export type UseFindPetByIdProps = Omit<UseGetProps<Pet, void>, \\"path\\"> & {id
export const useFindPetById = ({id, ...props}: UseFindPetByIdProps) => useGet<Pet, Error, void>(\`/pets/\${id}\`, props);
export type DeletePetProps = Omit<MutateProps<void, Error, void, string>, \\"path\\" | \\"verb\\">;
export type DeletePetProps = Omit<MutateProps<void, Error, void, integer>, \\"path\\" | \\"verb\\">;
/**
* deletes a single pet based on the ID supplied
*/
export const DeletePet = (props: DeletePetProps) => (
<Mutate<void, Error, void, string>
<Mutate<void, Error, void, integer>
verb=\\"DELETE\\"
path={\`/pets\`}
{...props}
/>
);
export type UseDeletePetProps = Omit<UseMutateProps<void, void, string>, \\"path\\" | \\"verb\\">;
export type UseDeletePetProps = Omit<UseMutateProps<void, void, integer>, \\"path\\" | \\"verb\\">;
/**
* deletes a single pet based on the ID supplied
*/
export const useDeletePet = (props: UseDeletePetProps) => useMutate<void, Error, void, string>(\\"DELETE\\", \`/pets\`, props);
export const useDeletePet = (props: UseDeletePetProps) => useMutate<void, Error, void, integer>(\\"DELETE\\", \`/pets\`, props);
export type UpdatePetProps = Omit<MutateProps<Pet, Error, void, UpdatePetRequestRequestBody>, \\"path\\" | \\"verb\\"> & {id: number};
Expand Down
62 changes: 62 additions & 0 deletions src/scripts/tests/import-open-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,68 @@ describe("scripts/import-open-api", () => {
"
`);
});
it("should take the type from open-api specs", () => {
const operation: OperationObject = {
summary: "Delete use case",
operationId: "deleteUseCase",
tags: ["use-case"],
parameters: [
{
name: "tenantId",
in: "path",
required: true,
description: "The id of the Contiamo tenant",
schema: { type: "string" },
},
{
name: "useCaseId",
in: "path",
required: true,
description: "The id of the use case",
schema: { type: "number", format: "uuid" },
},
],
responses: {
"204": {
description: "Empty response",
},
default: {
description: "unexpected error",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/APIError" },
example: { errors: ["msg1", "msg2"] },
},
},
},
},
};

expect(generateRestfulComponent(operation, "delete", "/use-cases/{useCaseId}", [])).toMatchInlineSnapshot(`
"
export type DeleteUseCaseProps = Omit<MutateProps<void, APIError, void, number>, \\"path\\" | \\"verb\\">;
/**
* Delete use case
*/
export const DeleteUseCase = (props: DeleteUseCaseProps) => (
<Mutate<void, APIError, void, number>
verb=\\"DELETE\\"
path={\`/use-cases\`}
{...props}
/>
);
export type UseDeleteUseCaseProps = Omit<UseMutateProps<void, void, number>, \\"path\\" | \\"verb\\">;
/**
* Delete use case
*/
export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate<void, APIError, void, number>(\\"DELETE\\", \`/use-cases\`, props);
"
`);
});

it("should generate a Poll compoment if the `prefer` token is present", () => {
const operation: OperationObject = {
Expand Down

0 comments on commit eb569d4

Please sign in to comment.