Skip to content

Commit ada6a19

Browse files
authored
feat: support runtime delay functions (#1281)
* feat: support runtime delay functions * ci: add regression test for runtime delay functions * refactor: rename delayCalcRuntime to delayMockFunction and improve docs * refactor: rename delayMockFunction to delayFunctionLazyExecute
1 parent 4ba63ba commit ada6a19

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@ Default Value: `1000`.
300300

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

303+
#### delayFunctionLazyExecute
304+
305+
Type: `boolean`.
306+
307+
Gives you the possibility to have functions that are passed to `delay` to be
308+
executed at runtime rather than when the mocks are generated.
309+
303310
#### useExamples
304311

305312
Type: `Boolean`.
@@ -1010,6 +1017,13 @@ module.exports = {
10101017
};
10111018
```
10121019

1020+
#### delayFunctionLazyExecute
1021+
1022+
Type: `boolean`.
1023+
1024+
Gives you the possibility to have functions that are passed to `delay` to be
1025+
executed at runtime rather than when the mocks are generated.
1026+
10131027
##### arrayMin
10141028

10151029
Type: `Number`.

packages/core/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ export type GlobalMockOptions = {
230230
useExamples?: boolean;
231231
// This is used to set the delay to your own custom value
232232
delay?: number | (() => number);
233+
// This is used to execute functions that are passed to the 'delay' argument
234+
// at runtime rather than build time.
235+
delayFunctionLazyExecute?: boolean;
233236
// This is used to set the base url to your own custom value
234237
baseUrl?: string;
235238
// This is used to set the locale of the faker library

packages/mock/src/delay.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import { GlobalMockOptions, NormalizedOverrideOutput } from '@orval/core';
33
export const getDelay = (
44
override?: NormalizedOverrideOutput,
55
options?: GlobalMockOptions,
6-
): number => {
6+
): GlobalMockOptions['delay'] => {
77
const overrideDelay =
88
typeof override?.mock?.delay === 'number'
99
? override?.mock?.delay
1010
: options?.delay;
11+
const delayFunctionLazyExecute =
12+
override?.mock?.delayFunctionLazyExecute ??
13+
options?.delayFunctionLazyExecute;
1114
switch (typeof overrideDelay) {
1215
case 'function':
13-
return overrideDelay();
16+
return delayFunctionLazyExecute ? overrideDelay : overrideDelay();
1417
case 'number':
1518
return overrideDelay;
1619
default:

packages/mock/src/msw/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ export const generateMSW = (
8484
? `export const ${getResponseMockFunctionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''})${mockData ? '' : `: ${returnType}`} => (${value})\n\n`
8585
: '';
8686

87+
const delayTime = getDelay(override, !isFunction(mock) ? mock : undefined);
8788
const handlerImplementation = `
8889
export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrideResponse?: ${returnType}` : ''}) => {
8990
return http.${verb}('${route}', async () => {
90-
await delay(${getDelay(override, !isFunction(mock) ? mock : undefined)});
91+
await delay(${isFunction(delayTime) ? `(${delayTime})()` : delayTime});
9192
return new HttpResponse(${
9293
isReturnHttpResponse
9394
? isTextPlain

tests/configs/default.config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,16 @@ export default defineConfig({
122122
},
123123
},
124124
},
125+
'runtime-mock-delay': {
126+
input: '../specifications/petstore.yaml',
127+
output: {
128+
mock: {
129+
delay: () => 400,
130+
delayFunctionLazyExecute: true,
131+
type: 'msw',
132+
},
133+
schemas: '../generated/default/runtime-mock-delay/model',
134+
target: '../generated/default/runtime-mock-delay/endpoints.ts',
135+
},
136+
},
125137
});

0 commit comments

Comments
 (0)