diff --git a/src/scripts/import-open-api.ts b/src/scripts/import-open-api.ts index dfae6947..253e3b8d 100644 --- a/src/scripts/import-open-api.ts +++ b/src/scripts/import-open-api.ts @@ -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 @@ -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, "path"${ - verb === "get" ? "" : ` | "verb"` - }>${paramsInPath.length ? ` & {${paramsTypes}}` : ""}; + output += `export type Use${componentName}Props = Omit, "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, "path">${ diff --git a/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap b/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap index 2ffe2bea..d7ddc618 100644 --- a/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap +++ b/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap @@ -43,6 +43,11 @@ export const AddPet = (props: AddPetProps) => ( /> ); +export type UseAddPetProps = Omit, \\"path\\" | \\"verb\\">; + + +export const useAddPet = (props: UseAddPetProps) => useMutate(\\"POST\\", \`/pets\`, props); + export type FindPetByIdProps = Omit, \\"path\\"> & {id: number}; @@ -60,16 +65,21 @@ export type UseFindPetByIdProps = Omit, \\"path\\"> & {id export const useFindPetById = ({id, ...props}: UseFindPetByIdProps) => useGet(\`/pets/\${id}\`, props); -export type DeletePetProps = Omit, \\"path\\" | \\"verb\\">; +export type DeletePetProps = Omit, \\"path\\" | \\"verb\\">; export const DeletePet = (props: DeletePetProps) => ( - + verb=\\"DELETE\\" path={\`/pets\`} {...props} /> ); +export type UseDeletePetProps = Omit, \\"path\\" | \\"verb\\">; + + +export const useDeletePet = (props: UseDeletePetProps) => useMutate(\\"DELETE\\", \`/pets\`, props); + " `; diff --git a/src/scripts/tests/import-open-api.test.ts b/src/scripts/tests/import-open-api.test.ts index 28514db6..93e3cf6b 100644 --- a/src/scripts/tests/import-open-api.test.ts +++ b/src/scripts/tests/import-open-api.test.ts @@ -505,24 +505,24 @@ export interface JobRunResponse {} }; expect(generateRestfulComponent(operation, "get", "/fields", [])).toMatchInlineSnapshot(` -" -export type ListFieldsProps = Omit, \\"path\\">; + " + export type ListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const ListFields = (props: ListFieldsProps) => ( - - path={\`/fields\`} - {...props} - /> -); + // List all fields for the use case schema + export const ListFields = (props: ListFieldsProps) => ( + + path={\`/fields\`} + {...props} + /> + ); -export type UseListFieldsProps = Omit, \\"path\\">; + export type UseListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); + // List all fields for the use case schema + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); -" -`); + " + `); }); it("should add a fallback if the error is not defined", () => { @@ -539,24 +539,24 @@ export const useListFields = (props: UseListFieldsProps) => useGet, \\"path\\">; + " + export type ListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const ListFields = (props: ListFieldsProps) => ( - - path={\`/fields\`} - {...props} - /> -); + // List all fields for the use case schema + export const ListFields = (props: ListFieldsProps) => ( + + path={\`/fields\`} + {...props} + /> + ); -export type UseListFieldsProps = Omit, \\"path\\">; + export type UseListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); + // List all fields for the use case schema + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); -" -`); + " + `); }); it("should remove duplicate types", () => { @@ -586,24 +586,24 @@ export const useListFields = (props: UseListFieldsProps) => useGet, \\"path\\">; + " + export type ListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const ListFields = (props: ListFieldsProps) => ( - - path={\`/fields\`} - {...props} - /> -); + // List all fields for the use case schema + export const ListFields = (props: ListFieldsProps) => ( + + path={\`/fields\`} + {...props} + /> + ); -export type UseListFieldsProps = Omit, \\"path\\">; + export type UseListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); + // List all fields for the use case schema + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); -" -`); + " + `); }); it("should deal with parameters in query", () => { @@ -648,26 +648,26 @@ export const useListFields = (props: UseListFieldsProps) => useGet, \\"path\\">; + export type ListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const ListFields = (props: ListFieldsProps) => ( - - path={\`/fields\`} - {...props} - /> -); + // List all fields for the use case schema + export const ListFields = (props: ListFieldsProps) => ( + + path={\`/fields\`} + {...props} + /> + ); -export type UseListFieldsProps = Omit, \\"path\\">; + export type UseListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); + // List all fields for the use case schema + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); -" -`); + " + `); }); it("should deal with parameters in query (root level)", () => { const operation: OperationObject = { @@ -718,26 +718,26 @@ export const useListFields = (props: UseListFieldsProps) => useGet, \\"path\\">; + export type ListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const ListFields = (props: ListFieldsProps) => ( - - path={\`/fields\`} - {...props} - /> -); + // List all fields for the use case schema + export const ListFields = (props: ListFieldsProps) => ( + + path={\`/fields\`} + {...props} + /> + ); -export type UseListFieldsProps = Omit, \\"path\\">; + export type UseListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); + // List all fields for the use case schema + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); -" -`); + " + `); }); it("should deal with parameters in path", () => { @@ -783,24 +783,24 @@ export const useListFields = (props: UseListFieldsProps) => useGet, \\"path\\"> & {id: string}; + " + export type ListFieldsProps = Omit, \\"path\\"> & {id: string}; -// List all fields for the use case schema -export const ListFields = ({id, ...props}: ListFieldsProps) => ( - - path={\`/fields/\${id}\`} - {...props} - /> -); + // List all fields for the use case schema + export const ListFields = ({id, ...props}: ListFieldsProps) => ( + + path={\`/fields/\${id}\`} + {...props} + /> + ); -export type UseListFieldsProps = Omit, \\"path\\"> & {id: string}; + export type UseListFieldsProps = Omit, \\"path\\"> & {id: string}; -// List all fields for the use case schema -export const useListFields = ({id, ...props}: UseListFieldsProps) => useGet(\`/fields/\${id}\`, props); + // List all fields for the use case schema + export const useListFields = ({id, ...props}: UseListFieldsProps) => useGet(\`/fields/\${id}\`, props); -" -`); + " + `); }); it("should deal with parameters in path (root level)", () => { @@ -853,24 +853,24 @@ export const useListFields = ({id, ...props}: UseListFieldsProps) => useGet, \\"path\\"> & {id: string}; + " + export type ListFieldsProps = Omit, \\"path\\"> & {id: string}; -// List all fields for the use case schema -export const ListFields = ({id, ...props}: ListFieldsProps) => ( - - path={\`/fields/\${id}\`} - {...props} - /> -); + // List all fields for the use case schema + export const ListFields = ({id, ...props}: ListFieldsProps) => ( + + path={\`/fields/\${id}\`} + {...props} + /> + ); -export type UseListFieldsProps = Omit, \\"path\\"> & {id: string}; + export type UseListFieldsProps = Omit, \\"path\\"> & {id: string}; -// List all fields for the use case schema -export const useListFields = ({id, ...props}: UseListFieldsProps) => useGet(\`/fields/\${id}\`, props); + // List all fields for the use case schema + export const useListFields = ({id, ...props}: UseListFieldsProps) => useGet(\`/fields/\${id}\`, props); -" -`); + " + `); }); it("should generate a Mutate type component", () => { @@ -916,20 +916,25 @@ export const useListFields = ({id, ...props}: UseListFieldsProps) => useGet, \\"path\\" | \\"verb\\"> & {useCaseId: string}; - -// Update use case details -export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => ( - - verb=\\"PUT\\" - path={\`/use-cases/\${useCaseId}\`} - {...props} - /> -); - -" -`); + " + export type UpdateUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; + + // Update use case details + export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => ( + + verb=\\"PUT\\" + path={\`/use-cases/\${useCaseId}\`} + {...props} + /> + ); + + export type UseUpdateUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; + + // Update use case details + export const useUpdateUseCase = ({useCaseId, ...props}: UseUpdateUseCaseProps) => useMutate(\\"PUT\\", \`/use-cases/\${useCaseId}\`, props); + + " + `); }); it("should generate a proper ComponentResponse type if the type is custom", () => { const operation: OperationObject = { @@ -989,22 +994,27 @@ export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => ( }; expect(generateRestfulComponent(operation, "put", "/use-cases/{useCaseId}", [])).toMatchInlineSnapshot(` -" -export interface UpdateUseCaseResponse {id: string; name?: string} + " + export interface UpdateUseCaseResponse {id: string; name?: string} -export type UpdateUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; + export type UpdateUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; -// Update use case details -export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => ( - - verb=\\"PUT\\" - path={\`/use-cases/\${useCaseId}\`} - {...props} - /> -); + // Update use case details + export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => ( + + verb=\\"PUT\\" + path={\`/use-cases/\${useCaseId}\`} + {...props} + /> + ); -" -`); + export type UseUpdateUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; + + // Update use case details + export const useUpdateUseCase = ({useCaseId, ...props}: UseUpdateUseCaseProps) => useMutate(\\"PUT\\", \`/use-cases/\${useCaseId}\`, props); + + " + `); }); it("should ignore 3xx responses", () => { @@ -1068,22 +1078,27 @@ export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => ( }; expect(generateRestfulComponent(operation, "put", "/use-cases/{useCaseId}", [])).toMatchInlineSnapshot(` -" -export interface UpdateUseCaseResponse {id: string; name?: string} + " + export interface UpdateUseCaseResponse {id: string; name?: string} -export type UpdateUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; + export type UpdateUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; -// Update use case details -export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => ( - - verb=\\"PUT\\" - path={\`/use-cases/\${useCaseId}\`} - {...props} - /> -); + // Update use case details + export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => ( + + verb=\\"PUT\\" + path={\`/use-cases/\${useCaseId}\`} + {...props} + /> + ); -" -`); + export type UseUpdateUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; + + // Update use case details + export const useUpdateUseCase = ({useCaseId, ...props}: UseUpdateUseCaseProps) => useMutate(\\"PUT\\", \`/use-cases/\${useCaseId}\`, props); + + " + `); }); it("should ignore the last param of a delete call (the id is give after)", () => { @@ -1124,20 +1139,25 @@ export const UpdateUseCase = ({useCaseId, ...props}: UpdateUseCaseProps) => ( }; expect(generateRestfulComponent(operation, "delete", "/use-cases/{useCaseId}", [])).toMatchInlineSnapshot(` -" -export type DeleteUseCaseProps = Omit, \\"path\\" | \\"verb\\">; - -// Delete use case -export const DeleteUseCase = (props: DeleteUseCaseProps) => ( - - verb=\\"DELETE\\" - path={\`/use-cases\`} - {...props} - /> -); - -" -`); + " + export type DeleteUseCaseProps = Omit, \\"path\\" | \\"verb\\">; + + // Delete use case + export const DeleteUseCase = (props: DeleteUseCaseProps) => ( + + verb=\\"DELETE\\" + path={\`/use-cases\`} + {...props} + /> + ); + + export type UseDeleteUseCaseProps = Omit, \\"path\\" | \\"verb\\">; + + // Delete use case + export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate(\\"DELETE\\", \`/use-cases\`, props); + + " + `); }); it("should only remove the last params in delete operation", () => { const operation: OperationObject = { @@ -1177,20 +1197,25 @@ export const DeleteUseCase = (props: DeleteUseCaseProps) => ( }; expect(generateRestfulComponent(operation, "delete", "/use-cases/{useCaseId}/secret", [])).toMatchInlineSnapshot(` -" -export type DeleteUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; - -// Delete use case -export const DeleteUseCase = ({useCaseId, ...props}: DeleteUseCaseProps) => ( - - verb=\\"DELETE\\" - path={\`/use-cases/\${useCaseId}/secret\`} - {...props} - /> -); - -" -`); + " + export type DeleteUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; + + // Delete use case + export const DeleteUseCase = ({useCaseId, ...props}: DeleteUseCaseProps) => ( + + verb=\\"DELETE\\" + path={\`/use-cases/\${useCaseId}/secret\`} + {...props} + /> + ); + + export type UseDeleteUseCaseProps = Omit, \\"path\\" | \\"verb\\"> & {useCaseId: string}; + + // Delete use case + export const useDeleteUseCase = ({useCaseId, ...props}: UseDeleteUseCaseProps) => useMutate(\\"DELETE\\", \`/use-cases/\${useCaseId}/secret\`, props); + + " + `); }); it("should generate a Poll compoment if the `prefer` token is present", () => { @@ -1225,34 +1250,34 @@ export const DeleteUseCase = ({useCaseId, ...props}: DeleteUseCaseProps) => ( }; expect(generateRestfulComponent(operation, "get", "/fields", [])).toMatchInlineSnapshot(` -" -export type ListFieldsProps = Omit, \\"path\\">; - -// List all fields for the use case schema -export const ListFields = (props: ListFieldsProps) => ( - - path={\`/fields\`} - {...props} - /> -); - -export type UseListFieldsProps = Omit, \\"path\\">; - -// List all fields for the use case schema -export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); - -export type PollListFieldsProps = Omit, \\"path\\">; - -// List all fields for the use case schema (long polling) -export const PollListFields = (props: PollListFieldsProps) => ( - - path={\`/fields\`} - {...props} - /> -); - -" -`); + " + export type ListFieldsProps = Omit, \\"path\\">; + + // List all fields for the use case schema + export const ListFields = (props: ListFieldsProps) => ( + + path={\`/fields\`} + {...props} + /> + ); + + export type UseListFieldsProps = Omit, \\"path\\">; + + // List all fields for the use case schema + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); + + export type PollListFieldsProps = Omit, \\"path\\">; + + // List all fields for the use case schema (long polling) + export const PollListFields = (props: PollListFieldsProps) => ( + + path={\`/fields\`} + {...props} + /> + ); + + " + `); }); }); it("should deal with no 2xx response case", () => { @@ -1276,23 +1301,23 @@ export const PollListFields = (props: PollListFieldsProps) => ( }; expect(generateRestfulComponent(operation, "get", "/fields", [])).toMatchInlineSnapshot(` -" -export type ListFieldsProps = Omit, \\"path\\">; + " + export type ListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const ListFields = (props: ListFieldsProps) => ( - - path={\`/fields\`} - {...props} - /> -); + // List all fields for the use case schema + export const ListFields = (props: ListFieldsProps) => ( + + path={\`/fields\`} + {...props} + /> + ); -export type UseListFieldsProps = Omit, \\"path\\">; + export type UseListFieldsProps = Omit, \\"path\\">; -// List all fields for the use case schema -export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); + // List all fields for the use case schema + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); -" -`); + " + `); }); });