msw-storybook-addon
Advanced tools
Comparing version 1.8.0 to 2.0.0--canary.111.4ed5190.0
@@ -1,1 +0,29 @@ | ||
export * from './mswDecorator'; | ||
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 }; |
@@ -1,13 +0,75 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (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); | ||
'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); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./mswDecorator"), exports); | ||
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; |
{ | ||
"name": "msw-storybook-addon", | ||
"description": "Mock API requests in Storybook with Mock Service Worker.", | ||
"version": "1.8.0", | ||
"version": "2.0.0--canary.111.4ed5190.0", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsup", | ||
"dev": "yarn build --watch", | ||
"build": "tsc", | ||
"prepublishOnly": "yarn build", | ||
@@ -14,4 +15,14 @@ "release": "auto shipit" | ||
"files": [ | ||
"dist/" | ||
"dist/**/*", | ||
"README.md", | ||
"*.js", | ||
"*.d.ts" | ||
], | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"repository": { | ||
@@ -43,3 +54,4 @@ "type": "git", | ||
"auto": "^10.32.2", | ||
"typescript": "^4.5.2" | ||
"tsup": "^6.6.3", | ||
"typescript": "^5.0.4" | ||
}, | ||
@@ -46,0 +58,0 @@ "peerDependencies": { |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
0
6470
4
4
167
1
1