-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconvertMswMatchToPact.ts
62 lines (61 loc) · 1.73 KB
/
convertMswMatchToPact.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { PactFile, MswMatch } from "./pactMswAdapter";
import { omit } from "lodash";
const pjson = require("../package.json");
export const convertMswMatchToPact = ({
consumer,
provider,
matches,
headers,
}: {
consumer: string;
provider: string;
matches: MswMatch[];
headers?: { excludeHeaders: string[] | undefined };
}): PactFile => {
const pactFile: PactFile = {
consumer: { name: consumer },
provider: { name: provider },
interactions: matches.map((match) => ({
description: match.request.id,
providerState: "",
request: {
method: match.request.method,
path: match.request.url.pathname,
headers: headers?.excludeHeaders
? omit(
Object.fromEntries(match.request.headers.entries()),
headers.excludeHeaders
)
: Object.fromEntries(match.request.headers.entries()),
body: match.request.body || undefined,
query: match.request.url.search
? match.request.url.search.split("?")[1]
: undefined,
},
response: {
status: match.response.status,
headers: headers?.excludeHeaders
? omit(
Object.fromEntries(match.response.headers.entries()),
headers.excludeHeaders
)
: Object.fromEntries(match.response.headers.entries()),
body: match.body
? match.response.headers.get("content-type")?.includes("json")
? JSON.parse(match.body)
: match.body
: undefined,
},
})),
metadata: {
pactSpecification: {
version: "2.0.0",
},
client: {
name: "pact-msw-adapter",
version: pjson.version,
},
},
};
return pactFile;
};