Skip to content

Commit

Permalink
feat: add support for path params is useMutate
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbansal authored and fabien0102 committed Jun 4, 2020
1 parent 63fe965 commit 96fd119
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Get.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface GetProps<TData, TError, TQueryParams, TPathParams> {
/** Options passed into the fetch call. */
requestOptions?: RestfulReactProviderProps["requestOptions"];
/**
* Query parameters
* Path parameters
*/
pathParams?: TPathParams;
/**
Expand Down
4 changes: 2 additions & 2 deletions src/Mutate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface MutateState<TData, TError> {
* is a named class because it is useful in
* debugging.
*/
class ContextlessMutate<TData, TError, TQueryParams, TRequestBody, TPathParams = unknown> extends React.Component<
class ContextlessMutate<TData, TError, TQueryParams, TRequestBody, TPathParams> extends React.Component<
MutateProps<TData, TError, TQueryParams, TRequestBody, TPathParams> & InjectedProps,
MutateState<TData, TError>
> {
Expand Down Expand Up @@ -279,7 +279,7 @@ function Mutate<
<RestfulReactConsumer>
{contextProps => (
<RestfulReactProvider {...contextProps} parentPath={composePath(contextProps.parentPath, props.path!)}>
<ContextlessMutate<TData, TError, TQueryParams, TRequestBody>
<ContextlessMutate<TData, TError, TQueryParams, TRequestBody, TPathParams>
{...contextProps}
{...props}
queryParams={{ ...contextProps.queryParams, ...props.queryParams } as TQueryParams}
Expand Down
24 changes: 15 additions & 9 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`);
}
Expand Down Expand Up @@ -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"
});
`;

Expand Down
12 changes: 9 additions & 3 deletions src/scripts/tests/__snapshots__/import-open-api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export type UseFindPetsProps = Omit<UseGetProps<Pet[], FindPetsQueryParams, void
* Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.
*
*/
export const useFindPets = (props: UseFindPetsProps) => useGet<Pet[], Error, FindPetsQueryParams, void>(\`/pets\`, { ...props });
export const useFindPets = (props: UseFindPetsProps) => useGet<Pet[], Error, FindPetsQueryParams, void>(\`/pets\`, props);
export type AddPetProps = Omit<MutateProps<Pet, Error, void, NewPet, void>, \\"path\\" | \\"verb\\">;
Expand All @@ -118,10 +118,13 @@ export type UseAddPetProps = Omit<UseMutateProps<Pet, void, NewPet, void>, \\"pa
/**
* Creates a new pet in the store. Duplicates are allowed
*/
export const useAddPet = (props: UseAddPetProps) => useMutate<Pet, Error, void, NewPet, void>(\\"POST\\", \`/pets\`, { ...props });
export const useAddPet = (props: UseAddPetProps) => useMutate<Pet, Error, void, NewPet, void>(\\"POST\\", \`/pets\`, props);
export interface FindPetByIdPathParams {
/**
* ID of pet to fetch
*/
id: number
}
Expand Down Expand Up @@ -163,10 +166,13 @@ export type UseDeletePetProps = Omit<UseMutateProps<void, void, number, void>, \
/**
* deletes a single pet based on the ID supplied
*/
export const useDeletePet = (props: UseDeletePetProps) => useMutate<void, Error, void, number, void>(\\"DELETE\\", \`/pets\`, { ...props });
export const useDeletePet = (props: UseDeletePetProps) => useMutate<void, Error, void, number, void>(\\"DELETE\\", \`/pets\`, props);
export interface UpdatePetPathParams {
/**
* ID of pet to update
*/
id: number
}
Expand Down
43 changes: 32 additions & 11 deletions src/scripts/tests/import-open-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ describe("scripts/import-open-api", () => {
/**
* List all fields for the use case schema
*/
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, void, void>(\`/fields\`, { ...props });
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, void, void>(\`/fields\`, props);
"
`);
Expand Down Expand Up @@ -903,7 +903,7 @@ describe("scripts/import-open-api", () => {
*
* This is a longer description to describe my endpoint
*/
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, unknown, void, void>(\`/fields\`, { ...props });
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, unknown, void, void>(\`/fields\`, props);
"
`);
Expand Down Expand Up @@ -941,7 +941,7 @@ describe("scripts/import-open-api", () => {
/**
* List all fields for the use case schema
*/
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, unknown, void, void>(\`/fields\`, { ...props });
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, unknown, void, void>(\`/fields\`, props);
"
`);
Expand Down Expand Up @@ -992,7 +992,7 @@ describe("scripts/import-open-api", () => {
/**
* List all fields for the use case schema
*/
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, void, void>(\`/fields\`, { ...props });
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, void, void>(\`/fields\`, props);
"
`);
Expand Down Expand Up @@ -1069,7 +1069,7 @@ describe("scripts/import-open-api", () => {
/**
* List all fields for the use case schema
*/
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, ListFieldsQueryParams, void>(\`/fields\`, { ...props });
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, ListFieldsQueryParams, void>(\`/fields\`, props);
"
`);
Expand Down Expand Up @@ -1152,7 +1152,7 @@ describe("scripts/import-open-api", () => {
/**
* List all fields for the use case schema
*/
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, ListFieldsQueryParams, void>(\`/fields\`, { ...props });
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, ListFieldsQueryParams, void>(\`/fields\`, props);
"
`);
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -1281,6 +1284,9 @@ describe("scripts/import-open-api", () => {
).toMatchInlineSnapshot(`
"
export interface ListFieldsPathParams {
/**
* The id of the project
*/
id: string
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -1533,6 +1545,9 @@ describe("scripts/import-open-api", () => {
}
export interface UpdateUseCasePathParams {
/**
* The id of the use case
*/
useCaseId: string
}
Expand Down Expand Up @@ -1628,6 +1643,9 @@ describe("scripts/import-open-api", () => {
}
export interface UpdateUseCasePathParams {
/**
* The id of the use case
*/
useCaseId: string
}
Expand Down Expand Up @@ -1712,7 +1730,7 @@ describe("scripts/import-open-api", () => {
/**
* Delete use case
*/
export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate<void, APIError, void, string, void>(\\"DELETE\\", \`/use-cases\`, { ...props });
export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate<void, APIError, void, string, void>(\\"DELETE\\", \`/use-cases\`, props);
"
`);
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -1841,7 +1862,7 @@ describe("scripts/import-open-api", () => {
/**
* Delete use case
*/
export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate<void, APIError, void, number, void>(\\"DELETE\\", \`/use-cases\`, { ...props });
export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate<void, APIError, void, number, void>(\\"DELETE\\", \`/use-cases\`, props);
"
`);
Expand Down Expand Up @@ -1899,7 +1920,7 @@ describe("scripts/import-open-api", () => {
/**
* Delete use case
*/
export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate<void, APIError, void, UseCaseId, void>(\\"DELETE\\", \`/use-cases\`, { ...props });
export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate<void, APIError, void, UseCaseId, void>(\\"DELETE\\", \`/use-cases\`, props);
"
`);
Expand Down Expand Up @@ -1955,7 +1976,7 @@ describe("scripts/import-open-api", () => {
/**
* List all fields for the use case schema
*/
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, void, void>(\`/fields\`, { ...props });
export const useListFields = (props: UseListFieldsProps) => useGet<FieldListResponse, APIError, void, void>(\`/fields\`, props);
export type PollListFieldsProps = Omit<PollProps<FieldListResponse, APIError, void, void>, \\"path\\">;
Expand Down Expand Up @@ -2010,7 +2031,7 @@ describe("scripts/import-open-api", () => {
/**
* List all fields for the use case schema
*/
export const useListFields = (props: UseListFieldsProps) => useGet<void, APIError, void, void>(\`/fields\`, { ...props });
export const useListFields = (props: UseListFieldsProps) => useGet<void, APIError, void, void>(\`/fields\`, props);
"
`);
Expand Down

0 comments on commit 96fd119

Please sign in to comment.