Skip to content

Commit

Permalink
feat(swr): add swrOptions property to output.override.swr (orval-…
Browse files Browse the repository at this point in the history
  • Loading branch information
soartec-lab authored Feb 17, 2024
1 parent 97552e4 commit 74ef879
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,30 @@ Type: `Boolean`.

Use to generate a <a href="https://swr.vercel.app/docs/pagination#useswrinfinite" target="_blank">useSWRInfinite</a> 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`.
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ export type AngularOptions = {
export type SwrOptions = {
options?: any;
useInfinite?: boolean;
swrOptions?: any;
};

export type InputTransformerFn = (spec: OpenAPIObject) => OpenAPIObject;
Expand Down
6 changes: 4 additions & 2 deletions packages/swr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ ${doc}export const ${camel(
}\n`
: '';

const defaultSwrOptions = { ...swrOptions.swrOptions, ...swrOptions.options };

const useSwrImplementation = `
export type ${pascal(
operationName,
Expand Down Expand Up @@ -418,9 +420,9 @@ ${doc}export const ${camel(`use-${operationName}`)} = <TError = ${errorType}>(
});
const ${queryResultVarName} = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(swrKey, swrFn, ${
swrOptions.options
defaultSwrOptions
? `{
${stringify(swrOptions.options)?.slice(1, -1)}
${stringify(defaultSwrOptions)?.slice(1, -1)}
...swrOptions
}`
: 'swrOptions'
Expand Down
18 changes: 18 additions & 0 deletions tests/configs/swr.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
});

0 comments on commit 74ef879

Please sign in to comment.