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

Commit

Permalink
Generate useMutate from openAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed May 17, 2019
1 parent 1a3e52b commit f0b4171
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 222 deletions.
20 changes: 10 additions & 10 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,16 @@ export const generateRestfulComponent = (
}`
: `${needAResponseComponent ? componentName + "Response" : responseTypes}, ${errorTypes}, ${
queryParamsType ? componentName + "QueryParams" : "void"
}, ${requestBodyTypes}`;
}, ${verb === "delete" ? "string" : requestBodyTypes}`;

const genericsTypesWithoutError =
const genericsTypesForHooksProps =
verb === "get"
? `${needAResponseComponent ? componentName + "Response" : responseTypes}, ${
queryParamsType ? componentName + "QueryParams" : "void"
}`
: `${needAResponseComponent ? componentName + "Response" : responseTypes}, ${
queryParamsType ? componentName + "QueryParams" : "void"
}, ${requestBodyTypes}`;
}`;

let output = `${
needAResponseComponent
Expand Down Expand Up @@ -366,18 +366,18 @@ export const ${componentName} = (${
`;

// Hooks version
if (verb === "get" /* TODO: Remove this condition after `useMutate` implementation */) {
output += `export type Use${componentName}Props = Omit<Use${Component}Props<${genericsTypesWithoutError}>, "path"${
verb === "get" ? "" : ` | "verb"`
}>${paramsInPath.length ? ` & {${paramsTypes}}` : ""};
output += `export type Use${componentName}Props = Omit<Use${Component}Props<${genericsTypesForHooksProps}>, "path"${
verb === "get" ? "" : ` | "verb"`
}>${paramsInPath.length ? ` & {${paramsTypes}}` : ""};
${operation.summary ? "// " + operation.summary : ""}
export const use${componentName} = (${
paramsInPath.length ? `{${paramsInPath.join(", ")}, ...props}` : "props"
}: Use${componentName}Props) => use${Component}<${genericsTypes}>(\`${route}\`, props);
paramsInPath.length ? `{${paramsInPath.join(", ")}, ...props}` : "props"
}: Use${componentName}Props) => use${Component}<${genericsTypes}>(${
verb === "get" ? "" : `"${verb.toUpperCase()}", `
}\`${route}\`, props);
`;
}

if (headerParams.map(({ name }) => name.toLocaleLowerCase()).includes("prefer")) {
output += `export type Poll${componentName}Props = Omit<PollProps<${genericsTypes}>, "path">${
Expand Down
14 changes: 12 additions & 2 deletions src/scripts/tests/__snapshots__/import-open-api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export const AddPet = (props: AddPetProps) => (
/>
);
export type UseAddPetProps = Omit<UseMutateProps<Pet, void>, \\"path\\" | \\"verb\\">;
export const useAddPet = (props: UseAddPetProps) => useMutate<Pet, Error, void, NewPet>(\\"POST\\", \`/pets\`, props);
export type FindPetByIdProps = Omit<GetProps<Pet, Error, void>, \\"path\\"> & {id: number};
Expand All @@ -60,16 +65,21 @@ 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, void>, \\"path\\" | \\"verb\\">;
export type DeletePetProps = Omit<MutateProps<void, Error, void, string>, \\"path\\" | \\"verb\\">;
export const DeletePet = (props: DeletePetProps) => (
<Mutate<void, Error, void, void>
<Mutate<void, Error, void, string>
verb=\\"DELETE\\"
path={\`/pets\`}
{...props}
/>
);
export type UseDeletePetProps = Omit<UseMutateProps<void, void>, \\"path\\" | \\"verb\\">;
export const useDeletePet = (props: UseDeletePetProps) => useMutate<void, Error, void, string>(\\"DELETE\\", \`/pets\`, props);
"
`;
Loading

0 comments on commit f0b4171

Please sign in to comment.