From 393a6d8eb59ed75c3caf5b3aed5ca2bad1a4bb23 Mon Sep 17 00:00:00 2001 From: Alfred Jonsson Date: Thu, 9 May 2024 07:45:26 +0200 Subject: [PATCH] fix(msw): make delay optional (#1360) Co-authored-by: Alfred Jonsson --- docs/src/pages/reference/configuration/output.md | 10 ++++++---- packages/core/src/types.ts | 4 ++-- packages/mock/src/delay.ts | 3 ++- packages/mock/src/msw/index.ts | 10 +++++++--- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/docs/src/pages/reference/configuration/output.md b/docs/src/pages/reference/configuration/output.md index bb93e0be0..67a6a5739 100644 --- a/docs/src/pages/reference/configuration/output.md +++ b/docs/src/pages/reference/configuration/output.md @@ -324,11 +324,12 @@ Use to specify the mock type you want to generate. #### delay -Type: `Number | Function`. +Type: `Number | Function | false`. 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. +Use to specify the delay time for the mock. It can either be a fixed number, false or a function that returns a number. +Setting delay to false removes the delay call completely. #### delayFunctionLazyExecute @@ -1125,9 +1126,10 @@ module.exports = { ##### delay -Type: `number` or `Function`. +Type: `number`, `Function` or `false`. -Give you the possibility to set delay time for mock. It can either be a fixed number or a function that returns a number. +Give you the possibility to set delay time for mock. It can either be a fixed number, false or a function that returns a number. +Setting delay to false removes the delay call completely. Default Value: `1000` diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index e0ed88125..796608fb2 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -239,8 +239,8 @@ export type GlobalMockOptions = { useExamples?: boolean; // This is used to generate mocks for all http responses defined in the OpenAPI specification generateEachHttpStatus?: boolean; - // This is used to set the delay to your own custom value - delay?: number | (() => number); + // This is used to set the delay to your own custom value, or pass false to disable delay + delay?: false | number | (() => number); // This is used to execute functions that are passed to the 'delay' argument // at runtime rather than build time. delayFunctionLazyExecute?: boolean; diff --git a/packages/mock/src/delay.ts b/packages/mock/src/delay.ts index feeb02a71..b8b01bde4 100644 --- a/packages/mock/src/delay.ts +++ b/packages/mock/src/delay.ts @@ -5,7 +5,7 @@ export const getDelay = ( options?: GlobalMockOptions, ): GlobalMockOptions['delay'] => { const overrideDelay = - typeof override?.mock?.delay === 'number' + override?.mock?.delay !== undefined ? override?.mock?.delay : options?.delay; const delayFunctionLazyExecute = @@ -15,6 +15,7 @@ export const getDelay = ( case 'function': return delayFunctionLazyExecute ? overrideDelay : overrideDelay(); case 'number': + case 'boolean': return overrideDelay; default: return 1000; diff --git a/packages/mock/src/msw/index.ts b/packages/mock/src/msw/index.ts index fbf7dc4a6..249426b48 100644 --- a/packages/mock/src/msw/index.ts +++ b/packages/mock/src/msw/index.ts @@ -94,11 +94,15 @@ const generateDefinition = ( ? `export const ${getResponseMockFunctionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''})${mockData ? '' : `: ${returnType}`} => (${value})\n\n` : ''; - const delayTime = getDelay(override, !isFunction(mock) ? mock : undefined); + const delay = getDelay(override, !isFunction(mock) ? mock : undefined); const handlerImplementation = ` export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrideResponse?: ${returnType}` : ''}) => { - return http.${verb}('${route}', async () => { - await delay(${isFunction(delayTime) ? `(${delayTime})()` : delayTime}); + return http.${verb}('${route}', ${ + delay === false + ? '() => {' + : `async () => { + await delay(${isFunction(delay) ? `(${delay})()` : delay});` + } return new HttpResponse(${ isReturnHttpResponse ? isTextPlain