Skip to content

Commit 5baa849

Browse files
fix(deps): update dependency express to v5 (#140)
* fix(deps): update dependency express to v5 * adjust * parseint * adjust --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Moreno Feltscher <moreno@smartive.ch>
1 parent 73fd376 commit 5baa849

File tree

3 files changed

+248
-264
lines changed

3 files changed

+248
-264
lines changed

index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ route.post('/reset/calls', (_req, res) => {
114114

115115
route.post('/recordings', (req, res) => {
116116
console.log('Setting up recordings... info:', req.body);
117-
recordingsContext.active = req.body.active || false;
118-
recordingsContext.deleteBodyAttributesForHash = req.body.deleteBodyAttributesForHash || [];
119-
recordingsContext.forwardHeadersForRoute = req.body.forwardHeadersForRoute || [];
120-
recordingsContext.recordings = req.body.recordings || {};
121-
recordingsContext.deleteHeadersForHash = req.body.deleteHeadersForHash || [];
122-
recordingsContext.failedRequestsResponse = req.body.failedRequestsResponse;
117+
const body = req.body || {};
118+
recordingsContext.active = body.active || false;
119+
recordingsContext.deleteBodyAttributesForHash = body.deleteBodyAttributesForHash || [];
120+
recordingsContext.forwardHeadersForRoute = body.forwardHeadersForRoute || [];
121+
recordingsContext.recordings = body.recordings || {};
122+
recordingsContext.deleteHeadersForHash = body.deleteHeadersForHash || [];
123+
recordingsContext.failedRequestsResponse = body.failedRequestsResponse;
123124
res.sendStatus(204);
124125
});
125126

@@ -178,12 +179,12 @@ function deleteNestedProperty(obj, path) {
178179
}
179180
}
180181

181-
app.all('*', async (req, res) => {
182+
app.all('/*splat', async (req, res) => {
182183
const call = {
183184
method: req.method,
184185
headers: req.headers,
185186
url: req.url,
186-
body: req.body,
187+
body: req.body || {},
187188
};
188189
if (nextCallListeners.length) {
189190
nextCallListeners.forEach((listener) => listener(call));
@@ -192,15 +193,15 @@ app.all('*', async (req, res) => {
192193
calls.push(call);
193194
}
194195

195-
const stringifiedBody = typeof req.body === 'string' ? req.body : JSON.stringify(req.body, null, 2);
196+
const stringifiedBody = typeof req.body === 'string' ? req.body : JSON.stringify(req.body || {}, null, 2);
196197
for (const route of routes) {
197198
if (
198199
new RegExp(`^${route.request.match}$`).test(req.url) &&
199200
(!route.request.bodyMatch || new RegExp(`^${route.request.bodyMatch}$`, 's').test(stringifiedBody))
200201
) {
201202
console.log(`Call to ${req.url} matched ${route.request.match} ${route.request.bodyMatch || ''}`);
202203
const response = route.response;
203-
res.status(response.status || 200);
204+
res.status(typeof response.status === 'string' ? parseInt(response.status, 10) : response.status || 200);
204205
res.setHeader('Content-Type', response.contentType || 'application/json');
205206
const body = response.bodies ? response.bodies.shift() : response.body;
206207
res.send(response.contentType ? body : JSON.stringify(body));
@@ -212,7 +213,7 @@ app.all('*', async (req, res) => {
212213
}
213214
}
214215

215-
const obfuscatedReqBodyForHash = JSON.parse(JSON.stringify(req.body));
216+
const obfuscatedReqBodyForHash = JSON.parse(JSON.stringify(req.body || {}));
216217
recordingsContext.deleteBodyAttributesForHash.forEach((path) => {
217218
deleteNestedProperty(obfuscatedReqBodyForHash, path);
218219
});
@@ -257,7 +258,7 @@ app.all('*', async (req, res) => {
257258
});
258259
const status = proxyRes.status;
259260

260-
res.status(proxyRes.status);
261+
res.status(status);
261262
const contentType = proxyRes.headers.get('content-type');
262263
let body;
263264
if (contentType && contentType.includes('application/json')) {
@@ -294,7 +295,7 @@ app.all('*', async (req, res) => {
294295
if (typeof durationMs === 'number' && durationMs > 0) {
295296
await new Promise((r) => setTimeout(r, durationMs));
296297
}
297-
res.status(status);
298+
res.status(typeof status === 'string' ? parseInt(status, 10) : status);
298299
if (typeof body === 'object') {
299300
res.setHeader('Content-Type', 'application/json');
300301
res.send(body);

0 commit comments

Comments
 (0)