/
ffi.ts
52 lines (46 loc) · 1.52 KB
/
ffi.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
/* eslint-disable import/first */
import { forEachObjIndexed } from 'ramda';
import { ConsumerInteraction } from '@pact-foundation/pact-core/src/consumer/index';
import { isArray } from 'util';
import { TemplateHeaders, V3Request, V3Response } from './types';
import { matcherValueOrString } from './matchers';
import { MatchersV3 } from '../v3';
export const setRequestDetails = (
interaction: ConsumerInteraction,
req: V3Request
): void => {
interaction.withRequest(req.method, matcherValueOrString(req.path));
forEachObjIndexed((v, k) => {
interaction.withRequestHeader(k, 0, matcherValueOrString(v));
}, req.headers);
forEachObjIndexed((v, k) => {
if (isArray(v)) {
(v as unknown[]).forEach((vv, i) => {
interaction.withQuery(k, i, matcherValueOrString(vv));
});
} else {
interaction.withQuery(k, 0, matcherValueOrString(v));
}
}, req.query);
};
export const setResponseDetails = (
interaction: ConsumerInteraction,
res: V3Response
): void => {
interaction.withStatus(res.status);
forEachObjIndexed((v, k) => {
interaction.withResponseHeader(k, 0, matcherValueOrString(v));
}, res.headers);
};
export const contentTypeFromHeaders = (
headers: TemplateHeaders | undefined,
defaultContentType: string
): string => {
let contentType: string | MatchersV3.Matcher<string> = defaultContentType;
forEachObjIndexed((v, k) => {
if (`${k}`.toLowerCase() === 'content-type') {
contentType = matcherValueOrString(v);
}
}, headers || {});
return contentType;
};