From d5ccc8cf3b3d0d1b420b753057460ef99ee26f4a Mon Sep 17 00:00:00 2001 From: Shodai Suzuki Date: Sun, 18 Feb 2024 12:37:04 +0900 Subject: [PATCH] feat(swr): add `swrMutationOptions` property to `output.override.swr` (#1225) --- .../pages/reference/configuration/output.md | 24 +++++++++++++++++++ packages/core/src/types.ts | 1 + packages/swr/src/index.ts | 4 ++-- tests/configs/swr.config.ts | 3 +++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/docs/src/pages/reference/configuration/output.md b/docs/src/pages/reference/configuration/output.md index 296dcd48e..de6bb7a1c 100644 --- a/docs/src/pages/reference/configuration/output.md +++ b/docs/src/pages/reference/configuration/output.md @@ -841,6 +841,30 @@ module.exports = { }; ``` +##### swrMutationOptions + +Type: `Object`. + +Use to override the `useSWRMutation` options. Check available options [here](https://swr.vercel.app/docs/mutation#useswrmutation-parameters) + +Example: + +```js +module.exports = { + petstore: { + output: { + override: { + swr: { + swrMutationOptions: { + revalidate: true, + }, + }, + }, + }, + }, +}; +``` + #### mock Type: `Object`. diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 37f20acda..62193a1b0 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -363,6 +363,7 @@ export type SwrOptions = { options?: any; useInfinite?: boolean; swrOptions?: any; + swrMutationOptions?: any; }; export type InputTransformerFn = (spec: OpenAPIObject) => OpenAPIObject; diff --git a/packages/swr/src/index.ts b/packages/swr/src/index.ts index ac190b012..c9a8e8769 100644 --- a/packages/swr/src/index.ts +++ b/packages/swr/src/index.ts @@ -518,9 +518,9 @@ ${doc}export const ${camel(`use-${operationName}`)} = ( }); const ${queryResultVarName} = useSWRMutation(swrKey, swrFn, ${ - swrOptions.options + swrOptions.swrMutationOptions ? `{ - ${stringify(swrOptions.options)?.slice(1, -1)} + ${stringify(swrOptions.swrMutationOptions)?.slice(1, -1)} ...swrOptions }` : 'swrOptions' diff --git a/tests/configs/swr.config.ts b/tests/configs/swr.config.ts index c79d2ed9b..3fba4b34a 100644 --- a/tests/configs/swr.config.ts +++ b/tests/configs/swr.config.ts @@ -118,6 +118,9 @@ export default defineConfig({ swrOptions: { dedupingInterval: 10000, }, + swrMutationOptions: { + revalidate: true, + }, }, }, },