-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: use conditional exports * chore: keep "dist" as build directory * chore: remove custom webpackFinal config * chore: prevent browser-style resolution in jsdom tests * chore: remove old code * chore: use "index.ts" * fix: use environment-based tsconfig.json * chore: remove unused imports * fix: abstract "api.use()" to "applyRequestHandlers" * read getWorker export * add types for getWorker --------- Co-authored-by: Yann Braga <yannbf@gmail.com>
- Loading branch information
1 parent
bf62ea8
commit 37dfc82
Showing
20 changed files
with
619 additions
and
198 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
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
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
# production | ||
/dist | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { RequestHandler } from 'msw' | ||
import { applyRequestHandlers } from './applyRequestHandlers' | ||
|
||
export type MswParameters = { | ||
msw?: | ||
| RequestHandler[] | ||
| { | ||
handlers: RequestHandler[] | Record<string, RequestHandler> | ||
} | ||
} | ||
|
||
export type Context = { | ||
parameters: MswParameters | ||
} | ||
|
||
export const mswDecorator = <Story extends (...args: any[]) => any>( | ||
storyFn: Story, | ||
context: Context | ||
) => { | ||
applyRequestHandlers(context.parameters.msw) | ||
return storyFn() | ||
} |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './mswDecorator'; | ||
export { initialize, InitializeOptions, getWorker } from '@build-time/initialize' | ||
export * from './decorator' | ||
export * from './loader' |
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,27 @@ | ||
import type { RequestHandler } from 'msw' | ||
import type { SetupWorker } from 'msw/browser' | ||
import { setupWorker } from 'msw/browser' | ||
|
||
export let api: SetupWorker | ||
|
||
export type InitializeOptions = Parameters<SetupWorker['start']>[0] | ||
|
||
export function initialize( | ||
options?: InitializeOptions, | ||
initialHandlers: RequestHandler[] = [] | ||
): SetupWorker { | ||
const worker = setupWorker(...initialHandlers) | ||
worker.start(options) | ||
api = worker | ||
return worker | ||
} | ||
|
||
export function getWorker(): SetupWorker { | ||
if (api === undefined) { | ||
throw new Error( | ||
`[MSW] Failed to retrieve the worker: no active worker found. Did you forget to call "initialize"?` | ||
) | ||
} | ||
|
||
return api | ||
} |
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,16 @@ | ||
import type { RequestHandler, SetupApi, LifeCycleEventsMap } from 'msw' | ||
import type { SetupWorker } from 'msw/browser' | ||
import type { SetupServer } from 'msw/node' | ||
|
||
export declare var api: SetupApi<LifeCycleEventsMap> | ||
|
||
export type InitializeOptions = | ||
| Parameters<SetupServer['listen']>[0] | ||
| Parameters<SetupWorker['start']>[0] | ||
|
||
export declare function initialize( | ||
options?: InitializeOptions, | ||
initialHandlers?: RequestHandler[] | ||
): SetupApi<LifeCycleEventsMap> | ||
|
||
export declare function getWorker(): typeof api |
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,27 @@ | ||
import type { RequestHandler } from 'msw' | ||
import type { SetupServer } from 'msw/node' | ||
import { setupServer } from 'msw/node' | ||
|
||
export let api: SetupServer | ||
|
||
export type InitializeOptions = Parameters<SetupServer['listen']>[0] | ||
|
||
export function initialize( | ||
options?: InitializeOptions, | ||
initialHandlers: RequestHandler[] = [] | ||
): SetupServer { | ||
const server = setupServer(...initialHandlers) | ||
server.listen(options) | ||
api = server | ||
return server | ||
} | ||
|
||
export function getWorker(): SetupServer { | ||
if (api === undefined) { | ||
throw new Error( | ||
`[MSW] Failed to retrieve the worker: no active worker found. Did you forget to call "initialize"?` | ||
) | ||
} | ||
|
||
return api | ||
} |
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,27 @@ | ||
import type { RequestHandler } from 'msw' | ||
import type { SetupServer } from 'msw/node' | ||
import { setupServer } from 'msw/native' | ||
|
||
export let api: SetupServer | ||
|
||
export type InitializeOptions = Parameters<SetupServer['listen']>[0] | ||
|
||
export function initialize( | ||
options?: InitializeOptions, | ||
initialHandlers: RequestHandler[] = [] | ||
): SetupServer { | ||
const server = setupServer(...initialHandlers) | ||
server.listen(options) | ||
api = server | ||
return server | ||
} | ||
|
||
export function getWorker(): SetupServer { | ||
if (api === undefined) { | ||
throw new Error( | ||
`[MSW] Failed to retrieve the worker: no active worker found. Did you forget to call "initialize"?` | ||
) | ||
} | ||
|
||
return api | ||
} |
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,18 @@ | ||
import type { Context } from './decorator' | ||
import { applyRequestHandlers } from './applyRequestHandlers' | ||
|
||
export const mswLoader = async (context: Context) => { | ||
applyRequestHandlers(context.parameters.msw) | ||
|
||
if ( | ||
typeof window !== 'undefined' && | ||
'navigator' in window && | ||
navigator.serviceWorker.controller | ||
) { | ||
// No need to rely on the MSW Promise exactly | ||
// since only 1 worker can control 1 scope at a time. | ||
await navigator.serviceWorker.ready | ||
} | ||
|
||
return {} | ||
} |
Oops, something went wrong.