-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
PlatformHandler.ts
273 lines (233 loc) · 7.3 KB
/
PlatformHandler.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
import {AnyToPromiseStatus, isFunction, isStream} from "@tsed/core";
import {Inject, Injectable, InjectorService, Provider, ProviderScope} from "@tsed/di";
import {$log} from "@tsed/logger";
import {ArgScope, HandlerWithScope, PlatformParams} from "@tsed/platform-params";
import {PlatformResponseFilter} from "@tsed/platform-response-filter";
import {AnyToPromiseWithCtx} from "../domain/AnyToPromiseWithCtx";
import {EndpointMetadata} from "../domain/EndpointMetadata";
import {HandlerMetadata} from "../domain/HandlerMetadata";
import {PlatformContext} from "../domain/PlatformContext";
import {HandlerType} from "../interfaces/HandlerType";
import {PlatformRouteWithoutHandlers} from "../interfaces/PlatformRouteOptions";
import {createHandlerMetadata} from "../utils/createHandlerMetadata";
import {setResponseHeaders} from "../utils/setResponseHeaders";
export interface OnRequestOptions {
$ctx: PlatformContext;
metadata: HandlerMetadata;
handler: HandlerWithScope;
next?: any;
err?: any;
[key: string]: any;
}
/**
* Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).
* @platform
*/
@Injectable({
scope: ProviderScope.SINGLETON
})
export class PlatformHandler {
@Inject()
protected responseFilter: PlatformResponseFilter;
constructor(protected injector: InjectorService, protected params: PlatformParams) {}
/**
* Create a native middleware based on the given metadata and return an instance of AnyToPromiseWithCtx
* @param input
* @param options
*/
createHandler(input: EndpointMetadata | HandlerMetadata | any, options: PlatformRouteWithoutHandlers = {}) {
return this.createRawHandler(createHandlerMetadata(this.injector, input, options));
}
/**
* Create injectable handler from the given provider
* @param provider
* @param propertyKey
*/
createCustomHandler(provider: Provider, propertyKey: string) {
const handler = this.compileHandler(
new HandlerMetadata({
token: provider.provide,
target: provider.useClass,
type: HandlerType.CUSTOM,
scope: provider.scope,
propertyKey
})
);
return async ($ctx: PlatformContext, next?: any) =>
handler({
$ctx,
next
});
}
/**
* Send the response to the consumer.
* @param data
* @param ctx
* @protected
*/
async flush(data: any, ctx: PlatformContext) {
const {response} = ctx;
if (!response.isDone()) {
data = await this.responseFilter.serialize(data, ctx);
data = await this.responseFilter.transform(data, ctx);
response.body(data);
}
}
compileHandler(metadata: HandlerMetadata) {
if (metadata.type === HandlerType.CTX_FN) {
return async (scope: Record<string, any>) => {
// @ts-ignore
return this.onCtxRequest({
...scope,
handler: () => metadata.handler(scope.$ctx),
metadata
});
};
}
const promise = this.params.compileHandler<PlatformContext>({
token: metadata.token,
propertyKey: metadata.propertyKey,
getCustomArgs: metadata.injectable ? undefined : this.getDefaultArgs(metadata)
});
return async (scope: Record<string, any>) => {
const handler = await promise;
// @ts-ignore
return this.onRequest({
...scope,
handler,
metadata
});
};
}
protected async onCtxRequest(requestOptions: OnRequestOptions): Promise<any> {
await requestOptions.handler(requestOptions);
return this.next(requestOptions);
}
/**
* Call handler when a request his handle
* @param requestOptions
*/
protected async onRequest(requestOptions: OnRequestOptions): Promise<any> {
const {$ctx, metadata, err, handler} = requestOptions;
// istanbul ignore next
if (!$ctx || $ctx.isDone()) {
$log.error({
name: "HEADERS_SENT",
message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${metadata.toString()}`
});
return;
}
const resolver = new AnyToPromiseWithCtx({$ctx, err});
return this.injector.runInContext($ctx, async () => {
try {
const {state, data, status, headers} = await resolver.call(handler);
if (state === AnyToPromiseStatus.RESOLVED) {
if (status) {
$ctx.response.status(status);
}
if (headers) {
$ctx.response.setHeaders(headers);
}
if (data !== undefined) {
$ctx.data = data;
}
// Can be canceled by the handler itself
return await this.onSuccess($ctx.data, requestOptions);
}
} catch (er) {
return this.onError(er, requestOptions);
}
});
}
protected async onError(er: Error, requestOptions: OnRequestOptions) {
const {next, $ctx, metadata} = requestOptions;
$ctx.data = er;
if (!next) {
throw er;
}
// istanbul ignore next
if (!$ctx || $ctx?.isDone()) {
$log.warn({
name: "HEADERS_SENT",
message: `An error was caught after the headers were sent to the client. The error comes from the handler: ${metadata.toString()}`,
stack: er.stack,
origin: er,
request_id: $ctx.id
});
return;
}
return next && next(er);
}
/**
* Manage success scenario
* @param data
* @param requestOptions
* @protected
*/
protected async onSuccess(data: any, requestOptions: OnRequestOptions) {
const {metadata, $ctx, next} = requestOptions;
// istanbul ignore next
if ($ctx?.isDone()) {
return;
}
// set headers each times that an endpoint is called
if (metadata.type === HandlerType.ENDPOINT) {
this.setHeaders($ctx);
}
// call returned middleware
if (isFunction(data) && !isStream(data)) {
return this.callReturnedMiddleware(data, $ctx, next);
}
if (metadata.isFinal()) {
return this.flush(data, $ctx);
}
return this.next(requestOptions);
}
/**
* Call the returned middleware by the handler.
* @param middleware
* @param ctx
* @param next
* @protected
*/
protected callReturnedMiddleware(middleware: any, ctx: PlatformContext, next: any) {
return middleware(ctx.getRequest(), ctx.getResponse(), next);
}
/**
* create Raw handler
* @param metadata
*/
protected createRawHandler(metadata: HandlerMetadata) {
if ([HandlerType.RAW_ERR_FN, HandlerType.RAW_FN].includes(metadata.type)) {
return metadata.handler;
}
const handler = this.compileHandler(metadata);
return async (request: any, response: any, next: any) => {
return handler({
next,
$ctx: request.$ctx
});
};
}
/**
* Set response headers
* @param ctx
* @protected
*/
protected setHeaders(ctx: PlatformContext) {
return setResponseHeaders(ctx);
}
protected next(requestOptions: OnRequestOptions) {
const {$ctx, next} = requestOptions;
return !$ctx.response.isDone() && next && next();
}
protected getDefaultArgs(metadata: HandlerMetadata) {
return async (scope: ArgScope<PlatformContext>) =>
[
metadata.hasErrorParam && scope.err,
scope.$ctx.request.request,
scope.$ctx.response.response,
metadata.hasNextFunction && scope.next
].filter(Boolean);
}
}