Skip to content

Commit

Permalink
fix(swr): correct second mutator parameter when using custom mutator (#…
Browse files Browse the repository at this point in the history
…1412)

Co-authored-by: Alfred Jonsson <alfred.jonsson@decerno.se>
  • Loading branch information
AllieJonsson and Alfred Jonsson authored May 29, 2024
1 parent 1a13970 commit 1624e6a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 9 additions & 2 deletions packages/swr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ const generateSwrHook = (
summary,
deprecated,
}: GeneratorVerbOptions,
{ route }: GeneratorOptions,
{ route, context }: GeneratorOptions,
) => {
const isRequestOptions = override?.requestOptions !== false;
const doc = jsDoc({ summary, deprecated });
Expand Down Expand Up @@ -695,8 +695,15 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
const swrMutationFetcherType = mutator
? `Promise<${response.definition.success || 'unknown'}>`
: `Promise<AxiosResponse<${response.definition.success || 'unknown'}>>`;
const swrMutationFetcherOptionType = !mutator
? 'AxiosRequestConfig'
: mutator.hasSecondArg
? `SecondParameter<typeof ${mutator.name}>`
: '';
const swrMutationFetcherOptions =
!mutator && isRequestOptions ? 'options?: AxiosRequestConfig' : '';
isRequestOptions && swrMutationFetcherOptionType
? `options${context.output.optionsParamRequired ? '' : '?'}: ${swrMutationFetcherOptionType}`
: '';

const swrMutationFetcherArg = props.some(
(prop) => prop.type === GetterPropType.BODY,
Expand Down
6 changes: 3 additions & 3 deletions tests/configs/swr.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default defineConfig({
mock: true,
override: {
mutator: {
path: '../mutators/custom-instance.ts',
path: '../mutators/multi-arguments.ts',
name: 'customInstance',
},
},
Expand All @@ -73,8 +73,8 @@ export default defineConfig({
},
customClient: {
output: {
target: '../generated/swr/mutator/endpoints.ts',
schemas: '../generated/swr/mutator/model',
target: '../generated/swr/custom-client/endpoints.ts',
schemas: '../generated/swr/custom-client/model',
client: 'swr',
mock: true,
override: {
Expand Down

0 comments on commit 1624e6a

Please sign in to comment.