Skip to content

Commit 74ef879

Browse files
authoredFeb 17, 2024··
feat(swr): add swrOptions property to output.override.swr (#1223)
1 parent 97552e4 commit 74ef879

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed
 

‎docs/src/pages/reference/configuration/output.md

+24
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,30 @@ Type: `Boolean`.
817817

818818
Use to generate a <a href="https://swr.vercel.app/docs/pagination#useswrinfinite" target="_blank">useSWRInfinite</a> custom hook.
819819

820+
##### swrOptions
821+
822+
Type: `Object`.
823+
824+
Use to override the `useSwr` options. Check available options [here](https://swr.vercel.app/docs/api#options)
825+
826+
Example:
827+
828+
```js
829+
module.exports = {
830+
petstore: {
831+
output: {
832+
override: {
833+
swr: {
834+
swrOptions: {
835+
dedupingInterval: 10000,
836+
},
837+
},
838+
},
839+
},
840+
},
841+
};
842+
```
843+
820844
#### mock
821845

822846
Type: `Object`.

‎packages/core/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ export type AngularOptions = {
362362
export type SwrOptions = {
363363
options?: any;
364364
useInfinite?: boolean;
365+
swrOptions?: any;
365366
};
366367

367368
export type InputTransformerFn = (spec: OpenAPIObject) => OpenAPIObject;

‎packages/swr/src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ ${doc}export const ${camel(
378378
}\n`
379379
: '';
380380

381+
const defaultSwrOptions = { ...swrOptions.swrOptions, ...swrOptions.options };
382+
381383
const useSwrImplementation = `
382384
export type ${pascal(
383385
operationName,
@@ -418,9 +420,9 @@ ${doc}export const ${camel(`use-${operationName}`)} = <TError = ${errorType}>(
418420
});
419421
420422
const ${queryResultVarName} = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(swrKey, swrFn, ${
421-
swrOptions.options
423+
defaultSwrOptions
422424
? `{
423-
${stringify(swrOptions.options)?.slice(1, -1)}
425+
${stringify(defaultSwrOptions)?.slice(1, -1)}
424426
...swrOptions
425427
}`
426428
: 'swrOptions'

‎tests/configs/swr.config.ts

+18
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,22 @@ export default defineConfig({
107107
},
108108
},
109109
},
110+
petstoreOverrideSwr: {
111+
output: {
112+
target: '../generated/swr/petstore-override-swr/endpoints.ts',
113+
schemas: '../generated/swr/petstore-override-swr/model',
114+
client: 'swr',
115+
override: {
116+
swr: {
117+
useInfinite: true,
118+
swrOptions: {
119+
dedupingInterval: 10000,
120+
},
121+
},
122+
},
123+
},
124+
input: {
125+
target: '../specifications/petstore.yaml',
126+
},
127+
},
110128
});

0 commit comments

Comments
 (0)
Please sign in to comment.