diff --git a/packages/msw-addon/src/applyRequestHandlers.ts b/packages/msw-addon/src/applyRequestHandlers.ts new file mode 100644 index 0000000..90af651 --- /dev/null +++ b/packages/msw-addon/src/applyRequestHandlers.ts @@ -0,0 +1,34 @@ +import type { RequestHandler } from 'msw' +import { api } from '@build-time/initialize' +import type { Context } from './decorator' + +export function applyRequestHandlers( + handlersListOrObject: Context['parameters']['msw'] +): void { + if (handlersListOrObject == null) { + return + } + + if (Array.isArray(handlersListOrObject) && handlersListOrObject.length > 0) { + // Support an Array of request handlers (backwards compatability). + api.use(...handlersListOrObject) + return + } + + if ('handlers' in handlersListOrObject && handlersListOrObject.handlers) { + // Support an Array named request handlers handlers + // or an Object of named request handlers with named arrays of handlers + const handlers = Object.values(handlersListOrObject.handlers) + .filter(Boolean) + .reduce( + (handlers, handlersList) => handlers.concat(handlersList), + [] + ) + + if (handlers.length > 0) { + api.use(...handlers) + } + + return + } +} diff --git a/packages/msw-addon/src/decorator.ts b/packages/msw-addon/src/decorator.ts index f94561d..52479c2 100644 --- a/packages/msw-addon/src/decorator.ts +++ b/packages/msw-addon/src/decorator.ts @@ -1,5 +1,5 @@ import type { RequestHandler } from 'msw' -import { api } from '@build-time/initialize' +import { applyRequestHandlers } from './applyRequestHandlers' export type MswParameters = { msw?: @@ -17,33 +17,6 @@ export const mswDecorator = any>( storyFn: Story, context: Context ) => { - const { - parameters: { msw }, - } = context - - if (api) { - api.resetHandlers() - - if (msw) { - if (Array.isArray(msw) && msw.length > 0) { - // Support an Array of request handlers (backwards compatability). - api.use(...msw) - } else if ('handlers' in msw && msw.handlers) { - // Support an Array named request handlers handlers - // or an Object of named request handlers with named arrays of handlers - const handlers = Object.values(msw.handlers) - .filter(Boolean) - .reduce( - (handlers, handlersList) => handlers.concat(handlersList), - [] - ) - - if (handlers.length > 0) { - api.use(...handlers) - } - } - } - } - + applyRequestHandlers(context.parameters.msw) return storyFn() } diff --git a/packages/msw-addon/src/loader.ts b/packages/msw-addon/src/loader.ts index 071162a..f1deefa 100644 --- a/packages/msw-addon/src/loader.ts +++ b/packages/msw-addon/src/loader.ts @@ -1,35 +1,8 @@ -import type { RequestHandler } from 'msw' import type { Context } from './decorator' -import { api } from '@build-time/initialize' +import { applyRequestHandlers } from './applyRequestHandlers' export const mswLoader = async (context: Context) => { - const { - parameters: { msw }, - } = context - - if (api) { - api.resetHandlers() - - if (msw) { - if (Array.isArray(msw) && msw.length > 0) { - // Support an Array of request handlers (backwards compatability). - api.use(...msw) - } else if ('handlers' in msw && msw.handlers) { - // Support an Array named request handlers handlers - // or an Object of named request handlers with named arrays of handlers - const handlers = Object.values(msw.handlers) - .filter(Boolean) - .reduce( - (handlers, handlersList) => handlers.concat(handlersList), - [] - ) - - if (handlers.length > 0) { - api.use(...handlers) - } - } - } - } + applyRequestHandlers(context.parameters.msw) if ( typeof window !== 'undefined' &&