From 74ef879961737b309a5284952aea951f43c0edae Mon Sep 17 00:00:00 2001 From: Shodai Suzuki Date: Sat, 17 Feb 2024 11:44:11 +0900 Subject: [PATCH] feat(swr): add `swrOptions` property to `output.override.swr` (#1223) --- .../pages/reference/configuration/output.md | 24 +++++++++++++++++++ packages/core/src/types.ts | 1 + packages/swr/src/index.ts | 6 +++-- tests/configs/swr.config.ts | 18 ++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/docs/src/pages/reference/configuration/output.md b/docs/src/pages/reference/configuration/output.md index c30ab08e5..296dcd48e 100644 --- a/docs/src/pages/reference/configuration/output.md +++ b/docs/src/pages/reference/configuration/output.md @@ -817,6 +817,30 @@ Type: `Boolean`. Use to generate a useSWRInfinite custom hook. +##### swrOptions + +Type: `Object`. + +Use to override the `useSwr` options. Check available options [here](https://swr.vercel.app/docs/api#options) + +Example: + +```js +module.exports = { + petstore: { + output: { + override: { + swr: { + swrOptions: { + dedupingInterval: 10000, + }, + }, + }, + }, + }, +}; +``` + #### mock Type: `Object`. diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index c879cb15e..37f20acda 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -362,6 +362,7 @@ export type AngularOptions = { export type SwrOptions = { options?: any; useInfinite?: boolean; + swrOptions?: any; }; export type InputTransformerFn = (spec: OpenAPIObject) => OpenAPIObject; diff --git a/packages/swr/src/index.ts b/packages/swr/src/index.ts index 3b9ca82ba..ac190b012 100644 --- a/packages/swr/src/index.ts +++ b/packages/swr/src/index.ts @@ -378,6 +378,8 @@ ${doc}export const ${camel( }\n` : ''; + const defaultSwrOptions = { ...swrOptions.swrOptions, ...swrOptions.options }; + const useSwrImplementation = ` export type ${pascal( operationName, @@ -418,9 +420,9 @@ ${doc}export const ${camel(`use-${operationName}`)} = ( }); const ${queryResultVarName} = useSwr>, TError>(swrKey, swrFn, ${ - swrOptions.options + defaultSwrOptions ? `{ - ${stringify(swrOptions.options)?.slice(1, -1)} + ${stringify(defaultSwrOptions)?.slice(1, -1)} ...swrOptions }` : 'swrOptions' diff --git a/tests/configs/swr.config.ts b/tests/configs/swr.config.ts index 4d3b44792..c79d2ed9b 100644 --- a/tests/configs/swr.config.ts +++ b/tests/configs/swr.config.ts @@ -107,4 +107,22 @@ export default defineConfig({ }, }, }, + petstoreOverrideSwr: { + output: { + target: '../generated/swr/petstore-override-swr/endpoints.ts', + schemas: '../generated/swr/petstore-override-swr/model', + client: 'swr', + override: { + swr: { + useInfinite: true, + swrOptions: { + dedupingInterval: 10000, + }, + }, + }, + }, + input: { + target: '../specifications/petstore.yaml', + }, + }, });