express-response-middleware 2.0.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.commitlintrc.json +3 -0
- package/.github/workflows/release.yml +4 -4
- package/.github/workflows/runTest.yml +11 -8
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +1 -0
- package/.husky/pre-push +1 -0
- package/CHANGELOG.md +21 -0
- package/README.md +3 -1
- package/biome.json +36 -0
- package/dist/cjs/index.cjs +17 -20
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +24 -0
- package/dist/esm/index.d.ts +8 -8
- package/dist/esm/index.mjs +17 -20
- package/dist/esm/index.mjs.map +1 -1
- package/package.json +36 -13
- package/pnpm-workspace.yaml +3 -0
- package/renovate.json +4 -1
- package/{rollup.config.mjs → rollup.config.js} +8 -5
- package/src/endMiddleware.ts +31 -31
- package/src/index.ts +5 -6
- package/src/jsonMiddleware.ts +38 -38
- package/src/jsonpMiddleware.ts +39 -39
- package/src/sendMiddleware.ts +25 -25
- package/src/tests/endMiddleware.test.ts +64 -55
- package/src/tests/jsonMiddleware.test.ts +166 -122
- package/src/tests/jsonpMiddleware.test.ts +154 -112
- package/src/tests/mixAndMatch.test.ts +119 -111
- package/src/tests/sendMiddleware.test.ts +144 -112
- package/src/tests/testHelpers/sleep.ts +2 -1
- package/src/tests/writeMiddleware.test.ts +157 -114
- package/src/utils/asyncLocalStorage.ts +2 -2
- package/src/utils/errorHandler.ts +3 -4
- package/src/writeMiddleware.ts +44 -38
- package/tsconfig.json +1 -1
- package/vitest.config.ts +2 -2
- package/.prettierrc.json +0 -8
- package/dist/cjs/index.d.ts +0 -24
- package/index.d.ts +0 -69
|
@@ -1,83 +1,113 @@
|
|
|
1
|
-
import express, { ErrorRequestHandler } from 'express'
|
|
2
|
-
import request from 'supertest'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import express, { type ErrorRequestHandler } from 'express';
|
|
2
|
+
import request from 'supertest';
|
|
3
|
+
import { expect } from 'vitest';
|
|
4
|
+
import { type TransformJson, jsonMiddleware } from '../index.js';
|
|
5
|
+
import { sleep } from './testHelpers/sleep.js';
|
|
5
6
|
|
|
6
7
|
describe('jsonMiddleware', () => {
|
|
7
|
-
const noop: TransformJson = () => {}
|
|
8
|
+
const noop: TransformJson = () => {};
|
|
8
9
|
|
|
9
|
-
const noopAsync: TransformJson = async json => {
|
|
10
|
-
await sleep()
|
|
11
|
-
return json
|
|
12
|
-
}
|
|
10
|
+
const noopAsync: TransformJson = async (json) => {
|
|
11
|
+
await sleep();
|
|
12
|
+
return json;
|
|
13
|
+
};
|
|
13
14
|
|
|
14
|
-
const inspect: TransformJson<any> = json => {
|
|
15
|
-
json
|
|
16
|
-
|
|
17
|
-
}
|
|
15
|
+
const inspect: TransformJson<any> = (json) => {
|
|
16
|
+
return { ...json, inspected_by: 'me' };
|
|
17
|
+
};
|
|
18
18
|
|
|
19
|
-
const
|
|
20
|
-
json.
|
|
21
|
-
|
|
22
|
-
}
|
|
19
|
+
const inspectMutate: TransformJson<any> = (json) => {
|
|
20
|
+
json.inspected_by = 'me';
|
|
21
|
+
};
|
|
23
22
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
;
|
|
27
|
-
|
|
23
|
+
const inspect2: TransformJson<any> = (json) => {
|
|
24
|
+
json.inspected_by_2 = 'him';
|
|
25
|
+
return json;
|
|
26
|
+
};
|
|
28
27
|
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
const inspectMutateAsync: TransformJson = async (json) => {
|
|
29
|
+
await sleep();
|
|
30
|
+
(json as any).inspected_by = 'me';
|
|
31
|
+
await sleep();
|
|
32
|
+
};
|
|
31
33
|
|
|
32
|
-
const
|
|
33
|
-
await sleep()
|
|
34
|
-
|
|
35
|
-
await sleep()
|
|
34
|
+
const inspectAsync: TransformJson = async (json) => {
|
|
35
|
+
await sleep();
|
|
36
|
+
const res = { ...(json as object), inspected_by: 'me' };
|
|
37
|
+
await sleep();
|
|
36
38
|
|
|
37
|
-
return
|
|
38
|
-
}
|
|
39
|
+
return res;
|
|
40
|
+
};
|
|
39
41
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
const inspectAsync2: TransformJson = async (json) => {
|
|
43
|
+
await sleep();
|
|
44
|
+
(json as any).inspected_by_2 = 'him';
|
|
45
|
+
await sleep();
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
json.foo.bar.hopefully.fails()
|
|
47
|
-
await sleep()
|
|
47
|
+
return json;
|
|
48
|
+
};
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const error: TransformJson<any> = (json) => {
|
|
51
|
+
json.foo.bar.hopefully.fails();
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const errorAsync: TransformJson<any> = async (json) => {
|
|
55
|
+
await sleep();
|
|
56
|
+
json.foo.bar.hopefully.fails();
|
|
57
|
+
await sleep();
|
|
58
|
+
|
|
59
|
+
return json;
|
|
60
|
+
};
|
|
51
61
|
|
|
52
62
|
const noPermission: TransformJson = (_json, _req, res) => {
|
|
53
|
-
res.status(403).send('no permissions')
|
|
54
|
-
}
|
|
63
|
+
res.status(403).send('no permissions');
|
|
64
|
+
};
|
|
55
65
|
|
|
56
66
|
const noPermissionAsync: TransformJson = async (json, _req, res) => {
|
|
57
|
-
await sleep()
|
|
58
|
-
res.status(403).send('no permissions')
|
|
59
|
-
await sleep()
|
|
67
|
+
await sleep();
|
|
68
|
+
res.status(403).send('no permissions');
|
|
69
|
+
await sleep();
|
|
60
70
|
|
|
61
|
-
return Promise.resolve(json)
|
|
62
|
-
}
|
|
71
|
+
return Promise.resolve(json);
|
|
72
|
+
};
|
|
63
73
|
|
|
64
|
-
it.each([inspect, inspectAsync])(
|
|
74
|
+
it.each([inspect, inspectAsync, inspectMutate, inspectMutateAsync])(
|
|
65
75
|
'should return the JSON result',
|
|
66
|
-
async handler => {
|
|
76
|
+
async (handler) => {
|
|
67
77
|
const server = express()
|
|
68
78
|
.use(jsonMiddleware(handler))
|
|
69
|
-
.get('/', (_req, res) =>
|
|
70
|
-
|
|
79
|
+
.get('/', (_req, res) => {
|
|
80
|
+
res.status(200).json({ a: 'a' }).end();
|
|
81
|
+
});
|
|
82
|
+
const response = await request(server).get('/');
|
|
71
83
|
|
|
72
|
-
const expected = { a: 'a', inspected_by: 'me' }
|
|
84
|
+
const expected = { a: 'a', inspected_by: 'me' };
|
|
73
85
|
|
|
74
|
-
expect(response.status).toStrictEqual(200)
|
|
75
|
-
expect(response.body).toStrictEqual(expected)
|
|
86
|
+
expect(response.status).toStrictEqual(200);
|
|
87
|
+
expect(response.body).toStrictEqual(expected);
|
|
76
88
|
expect(response.headers['content-length']).toStrictEqual(
|
|
77
|
-
JSON.stringify(expected).length.toString()
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
)
|
|
89
|
+
JSON.stringify(expected).length.toString(),
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
it.each([inspect, inspectAsync])(
|
|
95
|
+
'should not call if header is already sent',
|
|
96
|
+
async (handler) => {
|
|
97
|
+
const handlerSpy = vi.fn(handler);
|
|
98
|
+
|
|
99
|
+
const server = express()
|
|
100
|
+
.use(jsonMiddleware(handlerSpy))
|
|
101
|
+
.get('/', (_req, res) => {
|
|
102
|
+
res.end();
|
|
103
|
+
res.status(200).json({ a: 'a' }).end();
|
|
104
|
+
});
|
|
105
|
+
const response = await request(server).get('/');
|
|
106
|
+
|
|
107
|
+
expect(handlerSpy).not.toHaveBeenCalled();
|
|
108
|
+
expect(response.status).toStrictEqual(200);
|
|
109
|
+
},
|
|
110
|
+
);
|
|
81
111
|
|
|
82
112
|
it.each([
|
|
83
113
|
[inspect, inspect2],
|
|
@@ -88,122 +118,136 @@ describe('jsonMiddleware', () => {
|
|
|
88
118
|
const server = express()
|
|
89
119
|
.use(jsonMiddleware(handler))
|
|
90
120
|
.use(jsonMiddleware(handler2))
|
|
91
|
-
.get('/', (_req, res) =>
|
|
92
|
-
|
|
121
|
+
.get('/', (_req, res) => {
|
|
122
|
+
res.status(200).json({ a: 'a' }).end();
|
|
123
|
+
});
|
|
124
|
+
const response = await request(server).get('/');
|
|
93
125
|
|
|
94
|
-
const expected = { a: 'a', inspected_by: 'me', inspected_by_2: 'him' }
|
|
126
|
+
const expected = { a: 'a', inspected_by: 'me', inspected_by_2: 'him' };
|
|
95
127
|
|
|
96
|
-
expect(response.status).toStrictEqual(200)
|
|
97
|
-
expect(response.body).toStrictEqual(expected)
|
|
128
|
+
expect(response.status).toStrictEqual(200);
|
|
129
|
+
expect(response.body).toStrictEqual(expected);
|
|
98
130
|
expect(response.headers['content-length']).toStrictEqual(
|
|
99
|
-
JSON.stringify(expected).length.toString()
|
|
100
|
-
)
|
|
101
|
-
})
|
|
131
|
+
JSON.stringify(expected).length.toString(),
|
|
132
|
+
);
|
|
133
|
+
});
|
|
102
134
|
|
|
103
135
|
it.each([inspect, inspectAsync])(
|
|
104
136
|
'should call callback with an error',
|
|
105
|
-
async handler => {
|
|
137
|
+
async (handler) => {
|
|
106
138
|
const server = express()
|
|
107
139
|
.use(jsonMiddleware(handler))
|
|
108
|
-
.get('/', (_req, res) =>
|
|
109
|
-
|
|
140
|
+
.get('/', (_req, res) => {
|
|
141
|
+
res.status(404).json({ a: 'a' }).end();
|
|
142
|
+
});
|
|
143
|
+
const response = await request(server).get('/');
|
|
110
144
|
|
|
111
|
-
const expected = { a: 'a', inspected_by: 'me' }
|
|
145
|
+
const expected = { a: 'a', inspected_by: 'me' };
|
|
112
146
|
|
|
113
|
-
expect(response.status).toStrictEqual(404)
|
|
114
|
-
expect(response.body).toStrictEqual(expected)
|
|
147
|
+
expect(response.status).toStrictEqual(404);
|
|
148
|
+
expect(response.body).toStrictEqual(expected);
|
|
115
149
|
expect(response.headers['content-length']).toStrictEqual(
|
|
116
|
-
JSON.stringify(expected).length.toString()
|
|
117
|
-
)
|
|
118
|
-
}
|
|
119
|
-
)
|
|
150
|
+
JSON.stringify(expected).length.toString(),
|
|
151
|
+
);
|
|
152
|
+
},
|
|
153
|
+
);
|
|
120
154
|
|
|
121
155
|
it.each([inspect, inspectAsync])(
|
|
122
156
|
'should return the JSON result from a res.send',
|
|
123
|
-
async handler => {
|
|
157
|
+
async (handler) => {
|
|
124
158
|
const server = express()
|
|
125
159
|
.use(jsonMiddleware(handler))
|
|
126
|
-
.get('/', (_req, res) =>
|
|
127
|
-
|
|
160
|
+
.get('/', (_req, res) => {
|
|
161
|
+
res.status(200).send({ a: 'a' }).end();
|
|
162
|
+
});
|
|
163
|
+
const response = await request(server).get('/');
|
|
128
164
|
|
|
129
|
-
|
|
165
|
+
const expected = { a: 'a', inspected_by: 'me' };
|
|
130
166
|
|
|
131
|
-
expect(response.status).toStrictEqual(200)
|
|
132
|
-
expect(response.body).toStrictEqual(expected)
|
|
167
|
+
expect(response.status).toStrictEqual(200);
|
|
168
|
+
expect(response.body).toStrictEqual(expected);
|
|
133
169
|
expect(response.headers['content-length']).toStrictEqual(
|
|
134
|
-
JSON.stringify(expected).length.toString()
|
|
135
|
-
)
|
|
136
|
-
}
|
|
137
|
-
)
|
|
170
|
+
JSON.stringify(expected).length.toString(),
|
|
171
|
+
);
|
|
172
|
+
},
|
|
173
|
+
);
|
|
138
174
|
|
|
139
175
|
it.each([noop, noopAsync])(
|
|
140
176
|
'should return a as application/json',
|
|
141
|
-
async handler => {
|
|
177
|
+
async (handler) => {
|
|
142
178
|
const server = express()
|
|
143
179
|
.use(jsonMiddleware(handler))
|
|
144
|
-
.get('/', (_req, res) =>
|
|
145
|
-
|
|
180
|
+
.get('/', (_req, res) => {
|
|
181
|
+
res.status(200).json(42).end();
|
|
182
|
+
});
|
|
183
|
+
const response = await request(server).get('/');
|
|
146
184
|
|
|
147
|
-
expect(response.status).toStrictEqual(200)
|
|
148
|
-
expect(response.text).toStrictEqual('42')
|
|
185
|
+
expect(response.status).toStrictEqual(200);
|
|
186
|
+
expect(response.text).toStrictEqual('42');
|
|
149
187
|
expect(response.headers).toStrictEqual(
|
|
150
188
|
expect.objectContaining({
|
|
151
189
|
'content-type': 'application/json; charset=utf-8',
|
|
152
|
-
})
|
|
153
|
-
)
|
|
154
|
-
}
|
|
155
|
-
)
|
|
190
|
+
}),
|
|
191
|
+
);
|
|
192
|
+
},
|
|
193
|
+
);
|
|
156
194
|
|
|
157
195
|
it.each([noPermission, noPermissionAsync])(
|
|
158
196
|
'should abort if a response is sent',
|
|
159
|
-
async handler => {
|
|
197
|
+
async (handler) => {
|
|
160
198
|
const server = express()
|
|
161
199
|
.use(jsonMiddleware(handler))
|
|
162
|
-
.get('/', (_req, res) =>
|
|
163
|
-
|
|
200
|
+
.get('/', (_req, res) => {
|
|
201
|
+
res.status(200).json({ a: 'a' }).end();
|
|
202
|
+
});
|
|
203
|
+
const response = await request(server).get('/');
|
|
164
204
|
|
|
165
|
-
expect(response.status).toStrictEqual(403)
|
|
166
|
-
expect(response.text).toStrictEqual('no permissions')
|
|
205
|
+
expect(response.status).toStrictEqual(403);
|
|
206
|
+
expect(response.text).toStrictEqual('no permissions');
|
|
167
207
|
expect(response.headers).toStrictEqual(
|
|
168
208
|
expect.objectContaining({
|
|
169
209
|
'content-type': 'text/html; charset=utf-8',
|
|
170
|
-
})
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
)
|
|
210
|
+
}),
|
|
211
|
+
);
|
|
212
|
+
},
|
|
213
|
+
);
|
|
174
214
|
|
|
175
215
|
it.each([error, errorAsync])(
|
|
176
216
|
'should 500 on a synchronous exception',
|
|
177
|
-
async handler => {
|
|
178
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
179
|
-
res.status(500).send(err.message).end()
|
|
217
|
+
async (handler) => {
|
|
218
|
+
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {
|
|
219
|
+
res.status(500).send(err.message).end();
|
|
220
|
+
};
|
|
180
221
|
const server = express()
|
|
181
222
|
.use(errorHandler)
|
|
182
223
|
.use(jsonMiddleware(handler))
|
|
183
|
-
.get('/', (_req, res) =>
|
|
224
|
+
.get('/', (_req, res) => {
|
|
225
|
+
res.status(200).json({ a: 'a' }).end();
|
|
226
|
+
});
|
|
184
227
|
|
|
185
|
-
const response = await request(server).get('/')
|
|
228
|
+
const response = await request(server).get('/');
|
|
186
229
|
|
|
187
|
-
expect(response.status).toStrictEqual(500)
|
|
188
|
-
}
|
|
189
|
-
)
|
|
230
|
+
expect(response.status).toStrictEqual(500);
|
|
231
|
+
},
|
|
232
|
+
);
|
|
190
233
|
|
|
191
234
|
it.each([error, errorAsync])(
|
|
192
235
|
'should 500 on an asynchronous exception',
|
|
193
|
-
async handler => {
|
|
194
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
195
|
-
res.status(500).send(err.message).end()
|
|
236
|
+
async (handler) => {
|
|
237
|
+
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {
|
|
238
|
+
res.status(500).send(err.message).end();
|
|
239
|
+
};
|
|
196
240
|
const server = express()
|
|
197
241
|
.use(errorHandler)
|
|
198
242
|
.use(jsonMiddleware(handler))
|
|
199
243
|
.get('/', (_req, res) => {
|
|
200
244
|
process.nextTick(() => {
|
|
201
|
-
res.status(200).json({ a: 'a' }).end()
|
|
202
|
-
})
|
|
203
|
-
})
|
|
204
|
-
const response = await request(server).get('/')
|
|
205
|
-
|
|
206
|
-
expect(response.status).toStrictEqual(500)
|
|
207
|
-
}
|
|
208
|
-
)
|
|
209
|
-
})
|
|
245
|
+
res.status(200).json({ a: 'a' }).end();
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
const response = await request(server).get('/');
|
|
249
|
+
|
|
250
|
+
expect(response.status).toStrictEqual(500);
|
|
251
|
+
},
|
|
252
|
+
);
|
|
253
|
+
});
|