@pactflow/pact-msw-adapter
Version:
> Generate pact contracts from the recorded mock service worker interactions.
58 lines (57 loc) • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertMswMatchToPact = exports.readBody = void 0;
const lodash_1 = require("lodash");
const pjson = require("../package.json");
const readBody = async (input) => {
// so we don't reread body somewhere
const clone = input.clone();
if (clone.body === null)
return undefined;
const contentType = clone.headers.get("content-type");
if (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith("application/json")) {
return clone.json();
}
else if (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith("multipart/form-data")) {
return clone.formData();
}
// default to text
return clone.text();
};
exports.readBody = readBody;
const convertMswMatchToPact = async ({ consumer, provider, matches, headers, }) => {
const pactFile = {
consumer: { name: consumer },
provider: { name: provider },
interactions: await Promise.all(matches.map(async (match) => {
var _a, _b, _c;
return ({
description: match.requestId,
providerState: "",
request: {
method: match.request.method,
path: new URL(match.request.url).pathname,
headers: (0, lodash_1.omit)(Object.fromEntries(match.request.headers.entries()), (_a = headers === null || headers === void 0 ? void 0 : headers.excludeHeaders) !== null && _a !== void 0 ? _a : []),
body: await (0, exports.readBody)(match.request),
query: (_b = new URL(match.request.url).search) === null || _b === void 0 ? void 0 : _b.split("?")[1]
},
response: {
status: match.response.status,
headers: (0, lodash_1.omit)(Object.fromEntries(match.response.headers.entries()), (_c = headers === null || headers === void 0 ? void 0 : headers.excludeHeaders) !== null && _c !== void 0 ? _c : []),
body: await (0, exports.readBody)(match.response),
},
});
})),
metadata: {
pactSpecification: {
version: "2.0.0",
},
client: {
name: "pact-msw-adapter",
version: pjson.version,
},
},
};
return pactFile;
};
exports.convertMswMatchToPact = convertMswMatchToPact;