diff --git a/src/Get.tsx b/src/Get.tsx index ea76b449..fbf07b7f 100644 --- a/src/Get.tsx +++ b/src/Get.tsx @@ -79,7 +79,7 @@ export interface GetProps { /** Options passed into the fetch call. */ requestOptions?: RestfulReactProviderProps["requestOptions"]; /** - * Query parameters + * Path parameters */ pathParams?: TPathParams; /** diff --git a/src/Mutate.tsx b/src/Mutate.tsx index a9d9e621..c68a276a 100644 --- a/src/Mutate.tsx +++ b/src/Mutate.tsx @@ -115,7 +115,7 @@ export interface MutateState { * is a named class because it is useful in * debugging. */ -class ContextlessMutate extends React.Component< +class ContextlessMutate extends React.Component< MutateProps & InjectedProps, MutateState > { @@ -279,7 +279,7 @@ function Mutate< {contextProps => ( - + {...contextProps} {...props} queryParams={{ ...contextProps.queryParams, ...props.queryParams } as TQueryParams} diff --git a/src/scripts/import-open-api.ts b/src/scripts/import-open-api.ts index 56213f79..66fd21b9 100644 --- a/src/scripts/import-open-api.ts +++ b/src/scripts/import-open-api.ts @@ -343,8 +343,10 @@ export const generateRestfulComponent = ( const paramsTypes = paramsInPath .map(p => { try { - const { name, required, schema } = pathParams.find(i => i.name === p)!; - return `${name}${required ? "" : "?"}: ${resolveValue(schema!)}`; + const { name, required, schema, description } = pathParams.find(i => i.name === p)!; + return `${description ? formatDescription(description, 2) : ""}${name}${required ? "" : "?"}: ${resolveValue( + schema!, + )}`; } catch (err) { throw new Error(`The path params ${p} can't be found in parameters (${operation.operationId})`); } @@ -486,13 +488,17 @@ ${description}export const use${componentName} = (${ paramsInPath.length ? `({ ${paramsInPath.join(", ")} }: ${componentName}PathParams) => \`${route}\`` : `\`${route}\`` - }, { ${ - customPropsEntries.length - ? `{ ${customPropsEntries - .map(([key, value]) => `${key}:${reactPropsValueToObjectValue(value || "")}`) - .join(", ")}, ` - : "" - }${paramsInPath.length ? `pathParams: { ${paramsInPath.join(", ")} }, ` : ""}...props }); + }, ${ + customPropsEntries.length || paramsInPath.length + ? `{ ${ + customPropsEntries.length + ? `${customPropsEntries + .map(([key, value]) => `${key}:${reactPropsValueToObjectValue(value || "")}`) + .join(", ")},` + : "" + }${paramsInPath.length ? `pathParams: { ${paramsInPath.join(", ")} },` : ""} ...props }` + : "props" + }); `; 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 a7e63ba0..d1c19fca 100644 --- a/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap +++ b/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap @@ -97,7 +97,7 @@ export type UseFindPetsProps = Omit useGet(\`/pets\`, { ...props }); +export const useFindPets = (props: UseFindPetsProps) => useGet(\`/pets\`, props); export type AddPetProps = Omit, \\"path\\" | \\"verb\\">; @@ -118,10 +118,13 @@ export type UseAddPetProps = Omit, \\"pa /** * Creates a new pet in the store. Duplicates are allowed */ -export const useAddPet = (props: UseAddPetProps) => useMutate(\\"POST\\", \`/pets\`, { ...props }); +export const useAddPet = (props: UseAddPetProps) => useMutate(\\"POST\\", \`/pets\`, props); export interface FindPetByIdPathParams { + /** + * ID of pet to fetch + */ id: number } @@ -163,10 +166,13 @@ export type UseDeletePetProps = Omit, \ /** * deletes a single pet based on the ID supplied */ -export const useDeletePet = (props: UseDeletePetProps) => useMutate(\\"DELETE\\", \`/pets\`, { ...props }); +export const useDeletePet = (props: UseDeletePetProps) => useMutate(\\"DELETE\\", \`/pets\`, props); export interface UpdatePetPathParams { + /** + * ID of pet to update + */ id: number } diff --git a/src/scripts/tests/import-open-api.test.ts b/src/scripts/tests/import-open-api.test.ts index 7c83ec14..a8e11fd8 100644 --- a/src/scripts/tests/import-open-api.test.ts +++ b/src/scripts/tests/import-open-api.test.ts @@ -860,7 +860,7 @@ describe("scripts/import-open-api", () => { /** * List all fields for the use case schema */ - export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, { ...props }); + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); " `); @@ -903,7 +903,7 @@ describe("scripts/import-open-api", () => { * * This is a longer description to describe my endpoint */ - export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, { ...props }); + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); " `); @@ -941,7 +941,7 @@ describe("scripts/import-open-api", () => { /** * List all fields for the use case schema */ - export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, { ...props }); + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); " `); @@ -992,7 +992,7 @@ describe("scripts/import-open-api", () => { /** * List all fields for the use case schema */ - export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, { ...props }); + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); " `); @@ -1069,7 +1069,7 @@ describe("scripts/import-open-api", () => { /** * List all fields for the use case schema */ - export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, { ...props }); + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); " `); @@ -1152,7 +1152,7 @@ describe("scripts/import-open-api", () => { /** * List all fields for the use case schema */ - export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, { ...props }); + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); " `); @@ -1203,6 +1203,9 @@ describe("scripts/import-open-api", () => { expect(generateRestfulComponent(operation, "get", "/fields/{id}", [])).toMatchInlineSnapshot(` " export interface ListFieldsPathParams { + /** + * The id of the project + */ id: string } @@ -1281,6 +1284,9 @@ describe("scripts/import-open-api", () => { ).toMatchInlineSnapshot(` " export interface ListFieldsPathParams { + /** + * The id of the project + */ id: string } @@ -1352,6 +1358,9 @@ describe("scripts/import-open-api", () => { expect(generateRestfulComponent(operation, "put", "/use-cases/{useCaseId}", [])).toMatchInlineSnapshot(` " export interface UpdateUseCasePathParams { + /** + * The id of the use case + */ useCaseId: string } @@ -1433,6 +1442,9 @@ describe("scripts/import-open-api", () => { expect(generateRestfulComponent(operation, "put", "/use-cases/{useCaseId}", [])).toMatchInlineSnapshot(` " export interface UpdateUseCasePathParams { + /** + * The id of the use case + */ useCaseId: string } @@ -1533,6 +1545,9 @@ describe("scripts/import-open-api", () => { } export interface UpdateUseCasePathParams { + /** + * The id of the use case + */ useCaseId: string } @@ -1628,6 +1643,9 @@ describe("scripts/import-open-api", () => { } export interface UpdateUseCasePathParams { + /** + * The id of the use case + */ useCaseId: string } @@ -1712,7 +1730,7 @@ describe("scripts/import-open-api", () => { /** * Delete use case */ - export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate(\\"DELETE\\", \`/use-cases\`, { ...props }); + export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate(\\"DELETE\\", \`/use-cases\`, props); " `); @@ -1757,6 +1775,9 @@ describe("scripts/import-open-api", () => { expect(generateRestfulComponent(operation, "delete", "/use-cases/{useCaseId}/secret", [])).toMatchInlineSnapshot(` " export interface DeleteUseCasePathParams { + /** + * The id of the use case + */ useCaseId: string } @@ -1841,7 +1862,7 @@ describe("scripts/import-open-api", () => { /** * Delete use case */ - export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate(\\"DELETE\\", \`/use-cases\`, { ...props }); + export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate(\\"DELETE\\", \`/use-cases\`, props); " `); @@ -1899,7 +1920,7 @@ describe("scripts/import-open-api", () => { /** * Delete use case */ - export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate(\\"DELETE\\", \`/use-cases\`, { ...props }); + export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate(\\"DELETE\\", \`/use-cases\`, props); " `); @@ -1955,7 +1976,7 @@ describe("scripts/import-open-api", () => { /** * List all fields for the use case schema */ - export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, { ...props }); + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); export type PollListFieldsProps = Omit, \\"path\\">; @@ -2010,7 +2031,7 @@ describe("scripts/import-open-api", () => { /** * List all fields for the use case schema */ - export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, { ...props }); + export const useListFields = (props: UseListFieldsProps) => useGet(\`/fields\`, props); " `);