-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: abstract "api.use()" to "applyRequestHandlers"
- Loading branch information
1 parent
14b0ba7
commit 9892d86
Showing
3 changed files
with
38 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters