diff --git a/docs/src/pages/reference/configuration/output.md b/docs/src/pages/reference/configuration/output.md index 78096f590..c81903acc 100644 --- a/docs/src/pages/reference/configuration/output.md +++ b/docs/src/pages/reference/configuration/output.md @@ -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`. @@ -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`. diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 970d16178..7009a2a13 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -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 diff --git a/packages/mock/src/delay.ts b/packages/mock/src/delay.ts index 2196e7bd1..feeb02a71 100644 --- a/packages/mock/src/delay.ts +++ b/packages/mock/src/delay.ts @@ -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: diff --git a/packages/mock/src/msw/index.ts b/packages/mock/src/msw/index.ts index f71742930..25de5dc6a 100644 --- a/packages/mock/src/msw/index.ts +++ b/packages/mock/src/msw/index.ts @@ -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 diff --git a/tests/configs/default.config.ts b/tests/configs/default.config.ts index 49f236bab..763faec64 100644 --- a/tests/configs/default.config.ts +++ b/tests/configs/default.config.ts @@ -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', + }, + }, });