-
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.
- Loading branch information
1 parent
bf62ea8
commit 676bec1
Showing
10 changed files
with
509 additions
and
17 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 |
---|---|---|
|
@@ -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,49 @@ | ||
import type { RequestHandler } from 'msw' | ||
import { api } from '@build-time/initialize' | ||
|
||
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 | ||
) => { | ||
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) | ||
} | ||
} | ||
} | ||
} | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { initialize, InitializeOptions } 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,17 @@ | ||
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 | ||
} |
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,17 @@ | ||
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 | ||
} |
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,17 @@ | ||
import type { RequestHandler } from 'msw' | ||
import { 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 | ||
} |
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,45 @@ | ||
import type { RequestHandler } from 'msw' | ||
import type { Context } from './decorator' | ||
import { api } from '@build-time/initialize' | ||
|
||
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) | ||
} | ||
} | ||
} | ||
} | ||
|
||
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 {} | ||
} |
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,51 @@ | ||
import { defineConfig } from 'tsup' | ||
|
||
const browser = defineConfig({ | ||
entry: { | ||
'index.browser': './src/index.new.ts', | ||
}, | ||
outDir: './build', | ||
target: ['chrome112'], | ||
format: 'cjs', | ||
esbuildOptions(options) { | ||
options.alias = { | ||
...(options.alias || {}), | ||
'@build-time/initialize': './src/initialize.browser.ts', | ||
} | ||
}, | ||
bundle: true, | ||
}) | ||
|
||
const node = defineConfig({ | ||
entry: { | ||
'index.node': './src/index.new.ts', | ||
}, | ||
outDir: './build', | ||
target: 'node18', | ||
format: 'cjs', | ||
esbuildOptions(options) { | ||
options.alias = { | ||
...(options.alias || {}), | ||
'@build-time/initialize': './src/initialize.node.ts', | ||
} | ||
}, | ||
bundle: true, | ||
}) | ||
|
||
const reactNative = defineConfig({ | ||
entry: { | ||
'index.react-native': './src/index.new.ts', | ||
}, | ||
outDir: './build', | ||
target: 'esnext', | ||
format: 'cjs', | ||
esbuildOptions(options) { | ||
options.alias = { | ||
...(options.alias || {}), | ||
'@build-time/initialize': './src/initialize.react-native.ts', | ||
} | ||
}, | ||
bundle: true, | ||
}) | ||
|
||
export default [browser, node, reactNative] |
Oops, something went wrong.