/
proxy.js
72 lines (62 loc) · 1.88 KB
/
proxy.js
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
63
64
65
66
67
68
69
70
71
72
export default proxy;
import { URL } from "url";
import { Router } from "express";
import { createProxyMiddleware } from "http-proxy-middleware";
import validateRequest from "./request-validation-middleware.js";
function proxy(state, { target }) {
const middleware = Router();
const { protocol, hostname } = new URL(target);
middleware.use(
`/${hostname}/:fixturesId`,
validateRequest.bind(null, state),
createProxyMiddleware({
target: target,
router(req) {
const fixturesId = req.path.split("/")[2];
const target = `${protocol}//fixtures-${fixturesId}.${hostname}`;
return target;
},
changeOrigin: true,
logLevel: state.logLevel,
pathRewrite: {
"^/[^/]+/[\\w]+/": "/",
},
onError(error, request, response) {
/* istanbul ignore if */
if (error.message.indexOf("Nock: No match for request") !== 0) {
return response.end(
JSON.stringify({
error: error.message,
})
);
}
response.writeHead(404, {
"Content-Type": "application/json; charset=utf-8",
});
const [expected] = error.message
.substr("Nock: No match for request ".length)
.split(" Got instead ");
response.end(
JSON.stringify(
{
error: "Nock: No match for request",
detail: {
expected: JSON.parse(expected),
},
},
null,
2
) + "\n"
);
},
onProxyRes(proxyRes, request, response) {
const mock = state.cachimo.get(request.params.fixturesId);
if (mock.isDone()) {
state.cachimo.remove(request.params.fixturesId);
state.log.debug(`Fixtures "${request.params.fixturesId}" completed`);
}
},
})
);
return middleware;
}