Skip to content

Commit

Permalink
fix: abstract "api.use()" to "applyRequestHandlers"
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jan 19, 2024
1 parent 14b0ba7 commit 9892d86
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 58 deletions.
34 changes: 34 additions & 0 deletions packages/msw-addon/src/applyRequestHandlers.ts
Original file line number Diff line number Diff line change
@@ -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<RequestHandler[]>(
(handlers, handlersList) => handlers.concat(handlersList),
[]
)

if (handlers.length > 0) {
api.use(...handlers)
}

return
}
}
31 changes: 2 additions & 29 deletions packages/msw-addon/src/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RequestHandler } from 'msw'
import { api } from '@build-time/initialize'
import { applyRequestHandlers } from './applyRequestHandlers'

export type MswParameters = {
msw?:
Expand All @@ -17,33 +17,6 @@ export const mswDecorator = <Story extends (...args: any[]) => 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<RequestHandler[]>(
(handlers, handlersList) => handlers.concat(handlersList),
[]
)

if (handlers.length > 0) {
api.use(...handlers)
}
}
}
}

applyRequestHandlers(context.parameters.msw)
return storyFn()
}
31 changes: 2 additions & 29 deletions packages/msw-addon/src/loader.ts
Original file line number Diff line number Diff line change
@@ -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<RequestHandler[]>(
(handlers, handlersList) => handlers.concat(handlersList),
[]
)

if (handlers.length > 0) {
api.use(...handlers)
}
}
}
}
applyRequestHandlers(context.parameters.msw)

if (
typeof window !== 'undefined' &&
Expand Down

0 comments on commit 9892d86

Please sign in to comment.