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

Commit

Permalink
Add useGet generator
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Apr 23, 2019
1 parent cd3ce81 commit adb1ae6
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 37 deletions.
16 changes: 15 additions & 1 deletion src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,20 @@ 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<${genericsTypes}>, "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);
`;
}

if (headerParams.map(({ name }) => name.toLocaleLowerCase()).includes("prefer")) {
output += `export type Poll${componentName}Props = Omit<PollProps<${genericsTypes}>, "path">${
paramsInPath.length ? ` & {${paramsTypes}}` : ""
Expand Down Expand Up @@ -481,7 +495,7 @@ const importOpenApi = async (

const imports = [];
if (haveGet) {
imports.push("Get", "GetProps");
imports.push("Get", "GetProps", "useGet", "UseGetProps");
}
if (haveMutate) {
imports.push("Mutate", "MutateProps");
Expand Down
12 changes: 11 additions & 1 deletion src/scripts/tests/__snapshots__/import-open-api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`scripts/import-open-api should parse correctly petstore-expanded.yaml 1
"/* Generated by restful-react */
import React from \\"react\\";
import { Get, GetProps, Mutate, MutateProps } from \\"restful-react\\";
import { Get, GetProps, useGet, UseGetProps, Mutate, MutateProps } from \\"restful-react\\";
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
Expand All @@ -26,6 +26,11 @@ export const FindPets = (props: FindPetsProps) => (
/>
);
export type UseFindPetsProps = Omit<UseGetProps<Pet[], Error, FindPetsQueryParams>, \\"path\\">;
export const useFindPets = (props: UseFindPetsProps) => useGet<Pet[], Error, FindPetsQueryParams>(\`/pets\`, props);
export type AddPetProps = Omit<MutateProps<Pet, Error, void, NewPet>, \\"path\\" | \\"verb\\">;
Expand All @@ -49,6 +54,11 @@ export const FindPetById = ({id, ...props}: FindPetByIdProps) => (
/>
);
export type UseFindPetByIdProps = Omit<UseGetProps<Pet, Error, void>, \\"path\\"> & {id: number};
export const useFindPetById = ({id, ...props}: UseFindPetByIdProps) => useGet<Pet, Error, void>(\`/pets/\${id}\`, props);
export type DeletePetProps = Omit<MutateProps<void, Error, void, void>, \\"path\\" | \\"verb\\">;
Expand Down
Loading

0 comments on commit adb1ae6

Please sign in to comment.