msw-storybook-addon
Advanced tools
Comparing version 2.0.0--canary.111.c10b8bb.0 to 2.0.0--canary.122.06f0c92.0
@@ -1,29 +0,1 @@ | ||
import { SetupWorkerApi, RequestHandler } from 'msw'; | ||
import { SetupServerApi } from 'msw/node'; | ||
type SetupApi = SetupWorkerApi | SetupServerApi; | ||
type InitializeOptions = Parameters<SetupWorkerApi['start']>[0] | Parameters<SetupServerApi['listen']>[0]; | ||
type MswParameters = { | ||
msw?: RequestHandler[] | { | ||
handlers: RequestHandler[] | Record<string, RequestHandler>; | ||
}; | ||
}; | ||
type Context = { | ||
parameters: MswParameters; | ||
}; | ||
declare function initialize(options?: InitializeOptions): Promise<SetupApi>; | ||
declare function getWorker(): SetupApi; | ||
/** | ||
* @deprecated Use mswLoader instead, as it is much more reliable. | ||
* ```diff | ||
* - import { mswDecorator } from 'msw-storybook-addon'; | ||
* + import { mswLoader } from 'msw-storybook-addon'; | ||
* | ||
* - export const decorators = [mswDecorator]; | ||
* + export const loaders = [mswLoader]; | ||
* ``` | ||
*/ | ||
declare const mswDecorator: <Story extends (...args: any[]) => any>(storyFn: Story, context: Context) => any; | ||
declare const mswLoader: (context: Context) => Promise<{}>; | ||
export { InitializeOptions, MswParameters, SetupApi, getWorker, initialize, mswDecorator, mswLoader }; | ||
export * from './mswDecorator'; |
@@ -1,75 +0,17 @@ | ||
'use strict'; | ||
var isNodeProcess = require('is-node-process'); | ||
var msw = require('msw'); | ||
// src/index.ts | ||
var IS_BROWSER = !isNodeProcess.isNodeProcess(); | ||
var api; | ||
var workerPromise; | ||
async function initialize(options) { | ||
const defaultHandlers = [ | ||
msw.rest.get("/hot-update/*", (req) => req.passthrough()), | ||
msw.rest.get("/node_modules/*", (req) => req.passthrough()) | ||
]; | ||
if (IS_BROWSER) { | ||
const { setupWorker } = await import('msw'); | ||
const worker = setupWorker(...defaultHandlers); | ||
workerPromise = worker.start(options); | ||
api = worker; | ||
} else { | ||
const { setupServer } = await import('msw/node'); | ||
const server = setupServer(...defaultHandlers); | ||
workerPromise = Promise.resolve(server.listen(options)); | ||
api = server; | ||
} | ||
return api; | ||
} | ||
function getWorker() { | ||
if (api === void 0) { | ||
throw new Error( | ||
`[MSW] Failed to retrieve the worker: no active worker found. Did you forget to call "initialize"?` | ||
); | ||
} | ||
return api; | ||
} | ||
var setupHandlers = (msw) => { | ||
if (api) { | ||
api.resetHandlers(); | ||
if (msw) { | ||
if (Array.isArray(msw) && msw.length > 0) { | ||
api.use(...msw); | ||
} else if ("handlers" in msw && msw.handlers) { | ||
const handlers = Object.values(msw.handlers).filter(Boolean).reduce( | ||
(handlers2, handlersList) => handlers2.concat(handlersList), | ||
[] | ||
); | ||
if (handlers.length > 0) { | ||
api.use(...handlers); | ||
} | ||
} | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var mswDecorator = (storyFn, context) => { | ||
const { | ||
parameters: { msw } | ||
} = context; | ||
setupHandlers(msw); | ||
return storyFn(); | ||
}; | ||
var mswLoader = async (context) => { | ||
const { | ||
parameters: { msw } | ||
} = context; | ||
setupHandlers(msw); | ||
if (workerPromise) { | ||
await workerPromise; | ||
} | ||
return {}; | ||
}; | ||
exports.getWorker = getWorker; | ||
exports.initialize = initialize; | ||
exports.mswDecorator = mswDecorator; | ||
exports.mswLoader = mswLoader; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./mswDecorator"), exports); |
{ | ||
"name": "msw-storybook-addon", | ||
"description": "Mock API requests in Storybook with Mock Service Worker.", | ||
"version": "2.0.0--canary.111.c10b8bb.0", | ||
"version": "2.0.0--canary.122.06f0c92.0", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"typings": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsup", | ||
"dev": "yarn build --watch", | ||
"build": "tsc", | ||
"prepublishOnly": "yarn build", | ||
@@ -15,14 +14,4 @@ "release": "auto shipit" | ||
"files": [ | ||
"dist/**/*", | ||
"README.md", | ||
"*.js", | ||
"*.d.ts" | ||
"dist/" | ||
], | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"repository": { | ||
@@ -54,7 +43,7 @@ "type": "git", | ||
"auto": "^10.32.2", | ||
"tsup": "^6.6.3", | ||
"typescript": "^5.0.4" | ||
"typescript": "^5.2.2", | ||
"msw": "^2.0.9" | ||
}, | ||
"peerDependencies": { | ||
"msw": ">=0.35.0 <2.0.0" | ||
"msw": "^2.0.9" | ||
}, | ||
@@ -61,0 +50,0 @@ "storybook": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10473
5
183
1
1