Skip to content

Commit 7feae35

Browse files
AllieJonssonAlfred Jonsson
and
Alfred Jonsson
authoredApr 23, 2024
fix(swr): strongly type args in swr mutation fetchers (#1324)
Co-authored-by: Alfred Jonsson <alfred.jonsson@decerno.se>
1 parent d7fa66d commit 7feae35

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed
 

‎packages/swr/src/index.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,14 @@ const generateSwrMutationArguments = ({
248248
operationName,
249249
isRequestOptions,
250250
mutator,
251+
swrBodyType,
251252
}: {
252253
operationName: string;
253254
isRequestOptions: boolean;
254255
mutator?: GeneratorMutator;
256+
swrBodyType: string;
255257
}) => {
256-
const definition = `SWRMutationConfiguration<Awaited<ReturnType<typeof ${operationName}>>, TError, string, Arguments, Awaited<ReturnType<typeof ${operationName}>>> & { swrKey?: string }`;
258+
const definition = `SWRMutationConfiguration<Awaited<ReturnType<typeof ${operationName}>>, TError, string, ${swrBodyType}, Awaited<ReturnType<typeof ${operationName}>>> & { swrKey?: string }`;
257259

258260
if (!isRequestOptions) {
259261
return `swrOptions?: ${definition}`;
@@ -448,6 +450,7 @@ const generateSwrMutationImplementation = ({
448450
mutator,
449451
swrOptions,
450452
doc,
453+
swrBodyType,
451454
}: {
452455
isRequestOptions: boolean;
453456
operationName: string;
@@ -461,6 +464,7 @@ const generateSwrMutationImplementation = ({
461464
mutator?: GeneratorMutator;
462465
swrOptions: SwrOptions;
463466
doc?: string;
467+
swrBodyType: string;
464468
}) => {
465469
const hasParamReservedWord = props.some(
466470
(prop: GetterProp) => prop.name === 'query',
@@ -488,6 +492,7 @@ ${doc}export const ${camel(`use-${operationName}`)} = <TError = ${errorType}>(
488492
operationName,
489493
isRequestOptions,
490494
mutator,
495+
swrBodyType,
491496
})}) => {
492497
493498
${
@@ -659,7 +664,7 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
659664
if (prop.type === GetterPropType.NAMED_PATH_PARAMS) {
660665
return prop.destructured;
661666
} else if (prop.type === GetterPropType.BODY) {
662-
return `arg as ${prop.implementation.split(': ')[1]}`;
667+
return `arg`;
663668
} else {
664669
return prop.name;
665670
}
@@ -699,9 +704,14 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
699704
? '{ arg }'
700705
: '__';
701706

707+
const swrBodyType =
708+
props
709+
.find((prop) => prop.type === GetterPropType.BODY)
710+
?.implementation.split(': ')[1] ?? 'Arguments';
711+
702712
const swrMutationFetcherFn = `
703713
export const ${swrMutationFetcherName} = (${swrProps} ${swrMutationFetcherOptions}) => {
704-
return (_: string, ${swrMutationFetcherArg}: { arg: Arguments }): ${swrMutationFetcherType} => {
714+
return (_: string, ${swrMutationFetcherArg}: { arg: ${swrBodyType} }): ${swrMutationFetcherType} => {
705715
return ${operationName}(${httpFnProperties}${
706716
swrMutationFetcherOptions.length
707717
? (httpFnProperties.length ? ', ' : '') + 'options'
@@ -723,6 +733,7 @@ export const ${swrMutationFetcherName} = (${swrProps} ${swrMutationFetcherOption
723733
mutator,
724734
swrOptions: override.swr,
725735
doc,
736+
swrBodyType,
726737
});
727738

728739
return swrMutationFetcherFn + swrMutationKeyFn + swrImplementation;

0 commit comments

Comments
 (0)
Please sign in to comment.