Skip to content

Commit

Permalink
fix(swr): fix to use Array as key for useMutation (#1546)
Browse files Browse the repository at this point in the history
* chore: remove unnecessary body params by swr key function

* feat: add query params and update mutation key to Array

* fix: updated `useMutation` argument to use `Key` type

* chore: update sample app
  • Loading branch information
soartec-lab authored Jul 28, 2024
1 parent 05286d3 commit 31f2f94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
19 changes: 12 additions & 7 deletions packages/swr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const generateSwrMutationArguments = ({
swrBodyType: string;
httpClient: OutputHttpClient;
}) => {
const definition = `SWRMutationConfiguration<Awaited<ReturnType<typeof ${operationName}>>, TError, string, ${swrBodyType}, Awaited<ReturnType<typeof ${operationName}>>> & { swrKey?: string }`;
const definition = `SWRMutationConfiguration<Awaited<ReturnType<typeof ${operationName}>>, TError, Key, ${swrBodyType}, Awaited<ReturnType<typeof ${operationName}>>> & { swrKey?: string }`;

if (!isRequestOptions) {
return `swrOptions?: ${definition}`;
Expand Down Expand Up @@ -418,8 +418,8 @@ const generateSwrHook = (
const swrKeyFn = `
export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
queryParams ? ', ...(params ? [params]: [])' : ''
}${body.implementation ? `, ${body.implementation}` : ''}] as const;
\n`;
}] as const;
`;

const swrKeyLoaderFnName = camel(
`get-${operationName}-infinite-key-loader`,
Expand Down Expand Up @@ -458,7 +458,8 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
props.filter(
(prop) =>
prop.type === GetterPropType.PARAM ||
prop.type === GetterPropType.NAMED_PATH_PARAMS,
prop.type === GetterPropType.NAMED_PATH_PARAMS ||
prop.type === GetterPropType.QUERY_PARAM,
),
'implementation',
);
Expand Down Expand Up @@ -506,7 +507,8 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
.filter(
(prop) =>
prop.type === GetterPropType.PARAM ||
prop.type === GetterPropType.NAMED_PATH_PARAMS,
prop.type === GetterPropType.NAMED_PATH_PARAMS ||
prop.type === GetterPropType.QUERY_PARAM,
)
.map((prop) => {
if (prop.type === GetterPropType.NAMED_PATH_PARAMS) {
Expand All @@ -518,7 +520,10 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
.join(',');

const swrKeyFnName = camel(`get-${operationName}-mutation-key`);
const swrMutationKeyFn = `export const ${swrKeyFnName} = (${queryKeyProps}) => \`${route}\` as const;\n`;
const swrMutationKeyFn = `export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
queryParams ? ', ...(params ? [params]: [])' : ''
}] as const;
`;

const swrMutationFetcherName = camel(
`get-${operationName}-mutation-fetcher`,
Expand Down Expand Up @@ -553,7 +558,7 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${

const swrMutationFetcherFn = `
export const ${swrMutationFetcherName} = (${swrProps} ${swrMutationFetcherOptions}) => {
return (_: string, ${swrMutationFetcherArg}: { arg: ${swrBodyType} }): ${swrMutationFetcherType} => {
return (_: Key, ${swrMutationFetcherArg}: { arg: ${swrBodyType} }): ${swrMutationFetcherType} => {
return ${operationName}(${httpFnProperties}${
swrMutationFetcherOptions.length
? (httpFnProperties.length ? ', ' : '') + 'options'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ export const createPets = (
};

export const getCreatePetsMutationFetcher = (version: number = 1) => {
return (_: string, { arg }: { arg: CreatePetsBody }): Promise<Pet> => {
return (_: Key, { arg }: { arg: CreatePetsBody }): Promise<Pet> => {
return createPets(arg, version);
};
};
export const getCreatePetsMutationKey = (version: number = 1) =>
`/v${version}/pets` as const;
[`/v${version}/pets`] as const;

export type CreatePetsMutationResult = NonNullable<
Awaited<ReturnType<typeof createPets>>
Expand All @@ -110,7 +110,7 @@ export const useCreatePets = <TError = Error>(
swr?: SWRMutationConfiguration<
Awaited<ReturnType<typeof createPets>>,
TError,
string,
Key,
CreatePetsBody,
Awaited<ReturnType<typeof createPets>>
> & { swrKey?: string };
Expand Down

0 comments on commit 31f2f94

Please sign in to comment.