This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
/
request_middleware.ts
294 lines (281 loc) · 9.91 KB
/
request_middleware.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import * as express from "express";
import * as winston from "winston";
import { Either, isLeft } from "fp-ts/lib/Either";
import {
IResponse,
ResponseErrorInternal
} from "italia-ts-commons/lib/responses";
export type RequestHandler<R> = (
request: express.Request
) => Promise<IResponse<R>>;
/**
* Transforms a typesafe RequestHandler into an Express Request Handler.
*
* Failed promises will be mapped to 500 errors handled by ResponseErrorGeneric.
*/
export function wrapRequestHandler<R>(
handler: RequestHandler<R>
): express.RequestHandler {
return (request, response, _) => {
return handler(request).then(
r => {
r.apply(response);
winston.log(
"debug",
`wrapRequestHandler|SUCCESS|${request.url}|${r.kind}`
);
},
e => {
ResponseErrorInternal(e).apply(response);
winston.log("debug", `wrapRequestHandler|ERROR|${request.url}|${e}`);
}
);
};
}
/**
* Interface for implementing a request middleware.
*
* A RequestMiddleware is just a function that validates a request or
* extracts some object out of it.
* The middleware returns a promise that will resolve to a value that gets
* passed to the handler.
* In case the validation fails, the middleware rejects the promise (the
* value of the error is discarded). In this case the processing of the
* following middlewares will not happen.
* Finally, when called, the middleware has full access to the request and
* the response objects. Access to the response object is particulary useful
* for returning error messages when the validation fails.
*/
export type IRequestMiddleware<R, T> = (
request: express.Request
) => Promise<Either<IResponse<R>, T>>;
//
// The following are the type definitions for withRequestMiddlewares(...)
// Each overloaded type provided a type safe signature of withRequestMiddlewares with
// a certain number of middlewares. This is useful for enforcing the constraint that
// the handler should have the same number of parameters as the number of middlewares
// and each parameter must be of the same type returned by the corresponding middleware.
//
export function withRequestMiddlewares<R1, T1>(
v1: IRequestMiddleware<R1, T1>
): <RH>(handler: (v1: T1) => Promise<IResponse<RH>>) => RequestHandler<RH | R1>;
export function withRequestMiddlewares<R1, R2, T1, T2>(
v1: IRequestMiddleware<R1, T1>,
v2: IRequestMiddleware<R2, T2>
): <RH>(
handler: (v1: T1, v2: T2) => Promise<IResponse<RH>>
) => RequestHandler<RH | R1 | R2>;
export function withRequestMiddlewares<R1, R2, R3, T1, T2, T3>(
v1: IRequestMiddleware<R1, T1>,
v2: IRequestMiddleware<R2, T2>,
v3: IRequestMiddleware<R3, T3>
): <RH>(
handler: (v1: T1, v2: T2, v3: T3) => Promise<IResponse<RH>>
) => RequestHandler<RH | R1 | R2 | R3>;
export function withRequestMiddlewares<R1, R2, R3, R4, T1, T2, T3, T4>(
v1: IRequestMiddleware<R1, T1>,
v2: IRequestMiddleware<R2, T2>,
v3: IRequestMiddleware<R3, T3>,
v4: IRequestMiddleware<R4, T4>
): <RH>(
handler: (v1: T1, v2: T2, v3: T3, v4: T4) => Promise<IResponse<RH>>
) => RequestHandler<RH | R1 | R2 | R3 | R4>;
export function withRequestMiddlewares<R1, R2, R3, R4, R5, T1, T2, T3, T4, T5>(
v1: IRequestMiddleware<R1, T1>,
v2: IRequestMiddleware<R2, T2>,
v3: IRequestMiddleware<R3, T3>,
v4: IRequestMiddleware<R4, T4>,
v5: IRequestMiddleware<R5, T5>
): <RH>(
handler: (v1: T1, v2: T2, v3: T3, v4: T4, v5: T5) => Promise<IResponse<RH>>
) => RequestHandler<RH | R1 | R2 | R3 | R4 | R5>;
export function withRequestMiddlewares<
R1,
R2,
R3,
R4,
R5,
R6,
T1,
T2,
T3,
T4,
T5,
T6
>(
v1: IRequestMiddleware<R1, T1>,
v2: IRequestMiddleware<R2, T2>,
v3: IRequestMiddleware<R3, T3>,
v4: IRequestMiddleware<R4, T4>,
v5: IRequestMiddleware<R5, T5>,
v6: IRequestMiddleware<R6, T6>
): <RH>(
handler: (
v1: T1,
v2: T2,
v3: T3,
v4: T4,
v5: T5,
v6: T6
) => Promise<IResponse<RH>>
) => RequestHandler<RH | R1 | R2 | R3 | R4 | R5 | R6>;
/**
* Returns a request handler wrapped with the provided middlewares.
*
* The wrapper will process the request with each provided middleware in sequence.
* Each middleware will return a response or a value.
* When a response gets returned, the response gets sent back to the client and the
* processing stops.
* When all the provided middlewares complete by returning a value, all the values
* gets passed to the custom handler that in turn will return a response.
* That final response gets sent to the client.
*/
// TODO: Refactor this function to reduce its Cognitive Complexity
/* tslint:disable-next-line:cognitive-complexity */
export function withRequestMiddlewares<
R1,
R2,
R3,
R4,
R5,
R6,
T1,
T2,
T3,
T4,
T5,
T6
>(
v1: IRequestMiddleware<R1, T1>,
v2?: IRequestMiddleware<R2, T2>,
v3?: IRequestMiddleware<R3, T3>,
v4?: IRequestMiddleware<R4, T4>,
v5?: IRequestMiddleware<R5, T5>,
v6?: IRequestMiddleware<R6, T6>
): <RH>(
handler: (
v1: T1,
v2?: T2,
v3?: T3,
v4?: T4,
v5?: T5,
v6?: T6
) => Promise<IResponse<RH>>
) => RequestHandler<R1 | R2 | R3 | R4 | R5 | R6 | RH> {
return <RH>(
handler: (
v1: T1,
v2?: T2,
v3?: T3,
v4?: T4,
v5?: T5,
v6?: T6
) => Promise<IResponse<RH>>
) => {
// The outer promise with resolve to a type that can either be the the type returned
// by the handler or one of the types returned by any of the middlewares (i.e., when
// a middleware returns an error response).
return request =>
new Promise<IResponse<R1 | R2 | R3 | R4 | R5 | R6 | RH>>(
(resolve, reject) => {
// we execute each middleware in sequence, stopping at the first middleware that is
// undefined or when a middleware returns an error response.
// when we find an undefined middleware, we call the handler with all the results of
// the executed middlewares
v1(request).then(r1 => {
if (isLeft(r1)) {
// 1st middleware returned a response
// stop processing the middlewares
resolve(r1.value);
} else if (v2 !== undefined) {
// 1st middleware returned a value
// process 2nd middleware
v2(request).then(r2 => {
if (isLeft(r2)) {
// 2nd middleware returned a response
// stop processing the middlewares
resolve(r2.value);
} else if (v3 !== undefined) {
// process 3rd middleware
v3(request).then(r3 => {
if (isLeft(r3)) {
// 3rd middleware returned a response
// stop processing the middlewares
resolve(r3.value);
} else if (v4 !== undefined) {
v4(request).then(r4 => {
if (isLeft(r4)) {
// 4th middleware returned a response
// stop processing the middlewares
resolve(r4.value);
} else if (v5 !== undefined) {
v5(request).then(r5 => {
if (isLeft(r5)) {
// 5th middleware returned a response
// stop processing the middlewares
resolve(r5.value);
} else if (v6 !== undefined) {
v6(request).then(r6 => {
if (isLeft(r6)) {
// 6th middleware returned a response
// stop processing the middlewares
resolve(r6.value);
} else {
// 6th middleware returned a value
// run handler
handler(
r1.value,
r2.value,
r3.value,
r4.value,
r5.value,
r6.value
).then(resolve, reject);
}
}, reject);
} else {
// 5th middleware returned a value
// run handler
handler(
r1.value,
r2.value,
r3.value,
r4.value,
r5.value
).then(resolve, reject);
}
}, reject);
} else {
// 4th middleware returned a value
// run handler
handler(r1.value, r2.value, r3.value, r4.value).then(
resolve,
reject
);
}
}, reject);
} else {
// 3rd middleware returned a value
// run handler
handler(r1.value, r2.value, r3.value).then(
resolve,
reject
);
}
}, reject);
} else {
// 2nd middleware returned a value
// run handler
handler(r1.value, r2.value).then(resolve, reject);
}
}, reject);
} else {
// 1st middleware returned a value
// run handler
handler(r1.value).then(resolve, reject);
}
}, reject);
}
);
};
}