Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support runtime delay functions #1281

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ Default Value: `1000`.

Use to specify the delay time for the mock. It can either be a fixed number or a function that returns a number.

#### delayFunctionLazyExecute

Type: `boolean`.

Gives you the possibility to have functions that are passed to `delay` to be
executed at runtime rather than when the mocks are generated.

#### useExamples

Type: `Boolean`.
Expand Down Expand Up @@ -1010,6 +1017,13 @@ module.exports = {
};
```

#### delayFunctionLazyExecute

Type: `boolean`.

Gives you the possibility to have functions that are passed to `delay` to be
executed at runtime rather than when the mocks are generated.

##### arrayMin

Type: `Number`.
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ export type GlobalMockOptions = {
useExamples?: boolean;
// This is used to set the delay to your own custom value
delay?: number | (() => number);
// This is used to execute functions that are passed to the 'delay' argument
// at runtime rather than build time.
delayFunctionLazyExecute?: boolean;
// This is used to set the base url to your own custom value
baseUrl?: string;
// This is used to set the locale of the faker library
Expand Down
7 changes: 5 additions & 2 deletions packages/mock/src/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { GlobalMockOptions, NormalizedOverrideOutput } from '@orval/core';
export const getDelay = (
override?: NormalizedOverrideOutput,
options?: GlobalMockOptions,
): number => {
): GlobalMockOptions['delay'] => {
const overrideDelay =
typeof override?.mock?.delay === 'number'
? override?.mock?.delay
: options?.delay;
const delayFunctionLazyExecute =
override?.mock?.delayFunctionLazyExecute ??
options?.delayFunctionLazyExecute;
switch (typeof overrideDelay) {
case 'function':
return overrideDelay();
return delayFunctionLazyExecute ? overrideDelay : overrideDelay();
case 'number':
return overrideDelay;
default:
Expand Down
3 changes: 2 additions & 1 deletion packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ export const generateMSW = (
? `export const ${getResponseMockFunctionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''})${mockData ? '' : `: ${returnType}`} => (${value})\n\n`
: '';

const delayTime = getDelay(override, !isFunction(mock) ? mock : undefined);
const handlerImplementation = `
export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrideResponse?: ${returnType}` : ''}) => {
return http.${verb}('${route}', async () => {
await delay(${getDelay(override, !isFunction(mock) ? mock : undefined)});
await delay(${isFunction(delayTime) ? `(${delayTime})()` : delayTime});
return new HttpResponse(${
isReturnHttpResponse
? isTextPlain
Expand Down
12 changes: 12 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,16 @@ export default defineConfig({
},
},
},
'runtime-mock-delay': {
input: '../specifications/petstore.yaml',
output: {
mock: {
delay: () => 400,
delayFunctionLazyExecute: true,
type: 'msw',
},
schemas: '../generated/default/runtime-mock-delay/model',
target: '../generated/default/runtime-mock-delay/endpoints.ts',
},
},
});
Loading