@mswjs/http-middleware
Advanced tools
Comparing version 0.10.1 to 0.10.2
@@ -15,29 +15,31 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createMiddleware = void 0; | ||
exports.createMiddleware = createMiddleware; | ||
const node_crypto_1 = __importDefault(require("node:crypto")); | ||
const node_stream_1 = require("node:stream"); | ||
const node_crypto_1 = __importDefault(require("node:crypto")); | ||
const msw_1 = require("msw"); | ||
const strict_event_emitter_1 = require("strict-event-emitter"); | ||
const encoder = new TextEncoder(); | ||
const emitter = new strict_event_emitter_1.Emitter(); | ||
function createMiddleware(...handlers) { | ||
return (req, res, next) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
const method = req.method || 'GET'; | ||
const serverOrigin = `${req.protocol}://${req.get('host')}`; | ||
const method = req.method || 'GET'; | ||
// Ensure the request body input passed to the MockedRequest | ||
// is always a string. Custom middleware like "express.json()" | ||
// may coerce "req.body" to be an Object. | ||
const requestBody = typeof req.body === 'string' ? req.body : JSON.stringify(req.body); | ||
const mockedRequest = new Request( | ||
const canRequestHaveBody = method !== 'HEAD' && method !== 'GET'; | ||
const fetchRequest = new Request( | ||
// Treat all relative URLs as the ones coming from the server. | ||
new URL(req.url, serverOrigin), { | ||
method: req.method, | ||
method, | ||
headers: new Headers(req.headers), | ||
credentials: 'omit', | ||
// Request with GET/HEAD method cannot have body. | ||
body: ['GET', 'HEAD'].includes(method) | ||
? undefined | ||
: encoder.encode(requestBody), | ||
// @ts-ignore Internal Undici property. | ||
duplex: canRequestHaveBody ? 'half' : undefined, | ||
body: canRequestHaveBody | ||
? req.readable | ||
? node_stream_1.Readable.toWeb(req) | ||
: ((_a = req.header('content-type')) === null || _a === void 0 ? void 0 : _a.includes('json')) | ||
? JSON.stringify(req.body) | ||
: req.body | ||
: undefined, | ||
}); | ||
yield (0, msw_1.handleRequest)(mockedRequest, node_crypto_1.default.randomUUID(), handlers, { | ||
yield (0, msw_1.handleRequest)(fetchRequest, node_crypto_1.default.randomUUID(), handlers, { | ||
onUnhandledRequest: () => null, | ||
@@ -52,21 +54,23 @@ }, emitter, { | ||
}, | ||
onMockedResponse: (mockedResponse) => __awaiter(this, void 0, void 0, function* () { | ||
const { status, statusText, headers } = mockedResponse; | ||
res.statusCode = status; | ||
res.statusMessage = statusText; | ||
headers.forEach((value, name) => { | ||
/** | ||
* @note Use `.appendHeader()` to support multi-value | ||
* response headers, like "Set-Cookie". | ||
*/ | ||
res.appendHeader(name, value); | ||
onMockedResponse(mockedResponse) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { status, statusText, headers } = mockedResponse; | ||
res.statusCode = status; | ||
res.statusMessage = statusText; | ||
headers.forEach((value, name) => { | ||
/** | ||
* @note Use `.appendHeader()` to support multi-value | ||
* response headers, like "Set-Cookie". | ||
*/ | ||
res.appendHeader(name, value); | ||
}); | ||
if (mockedResponse.body) { | ||
const stream = node_stream_1.Readable.fromWeb(mockedResponse.body); | ||
stream.pipe(res); | ||
} | ||
else { | ||
res.end(); | ||
} | ||
}); | ||
if (mockedResponse.body) { | ||
const stream = node_stream_1.Readable.fromWeb(mockedResponse.body); | ||
stream.pipe(res); | ||
} | ||
else { | ||
res.end(); | ||
} | ||
}), | ||
}, | ||
onPassthroughResponse() { | ||
@@ -78,2 +82,1 @@ next(); | ||
} | ||
exports.createMiddleware = createMiddleware; |
import express from 'express'; | ||
import { HttpHandler } from 'msw'; | ||
export declare function createServer(...handlers: Array<HttpHandler>): express.Express; | ||
type ParserOptions = Parameters<typeof express.raw>[0]; | ||
export declare function createServer(parserOptions?: ParserOptions, ...handlers: Array<HttpHandler>): express.Express; | ||
export {}; |
@@ -6,8 +6,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createServer = void 0; | ||
exports.createServer = createServer; | ||
const express_1 = __importDefault(require("express")); | ||
const middleware_1 = require("./middleware"); | ||
function createServer(...handlers) { | ||
function createServer(parserOptions, ...handlers) { | ||
const app = (0, express_1.default)(); | ||
app.use(express_1.default.json()); | ||
app.use((0, middleware_1.createMiddleware)(...handlers)); | ||
@@ -21,2 +20,1 @@ app.use((_req, res) => { | ||
} | ||
exports.createServer = createServer; |
{ | ||
"name": "@mswjs/http-middleware", | ||
"version": "0.10.1", | ||
"version": "0.10.2", | ||
"main": "lib/index.js", | ||
@@ -38,20 +38,15 @@ "typings": "lib/index.d.ts", | ||
"@types/express": "^4.17.17", | ||
"@types/jest": "^27.0.2", | ||
"@types/node": "^20.8.7", | ||
"@types/node-fetch": "^2.5.11", | ||
"@typescript-eslint/eslint-plugin": "^6.10.0", | ||
"@typescript-eslint/parser": "^6.10.0", | ||
"eslint": "^8.35.0", | ||
"eslint-config-prettier": "^8.7.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"@typescript-eslint/eslint-plugin": "^8.11.0", | ||
"@typescript-eslint/parser": "^8.11.0", | ||
"eslint": "^8.53.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-prettier": "^5.2.1", | ||
"husky": "^7.0.4", | ||
"jest": "^29.7.0", | ||
"jest-environment-jsdom": "^29.7.0", | ||
"lint-staged": "^11.2.6", | ||
"msw": "^2.0.0", | ||
"node-fetch": "^2.6.1", | ||
"prettier": "^2.8.4", | ||
"msw": "^2.5.1", | ||
"prettier": "^3.3.3", | ||
"rimraf": "^4.4.0", | ||
"ts-jest": "^29.1.1", | ||
"typescript": "^5.0.0" | ||
"typescript": "^5.6.3", | ||
"vitest": "^2.1.3" | ||
}, | ||
@@ -64,9 +59,9 @@ "peerDependencies": { | ||
}, | ||
"packageManager": "pnpm@7.12.2", | ||
"packageManager": "pnpm@8.15.6", | ||
"scripts": { | ||
"start": "tsc -w", | ||
"test": "jest", | ||
"start": "tsc -w -p ./tsconfig.src.json", | ||
"test": "vitest", | ||
"lint": "eslint \"{examples,src,test}/**/*.ts\"", | ||
"clean": "rimraf ./lib", | ||
"build": "pnpm clean && tsc", | ||
"build": "pnpm clean && tsc -p ./tsconfig.src.json", | ||
"example-build": "pnpm build && cd examples && rimraf ./lib && tsc && cp -R public lib/examples/public", | ||
@@ -73,0 +68,0 @@ "example:basic": "pnpm example-build && node examples/lib/examples/basic-server.js", |
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
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
10549
16
125