forked from typestack/routing-controllers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoaDriver.ts
431 lines (372 loc) · 15.6 KB
/
KoaDriver.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import {Action} from "../../Action";
import {ActionMetadata} from "../../metadata/ActionMetadata";
import {BaseDriver} from "../BaseDriver";
import {MiddlewareMetadata} from "../../metadata/MiddlewareMetadata";
import {ParamMetadata} from "../../metadata/ParamMetadata";
import {UseMetadata} from "../../metadata/UseMetadata";
import {KoaMiddlewareInterface} from "./KoaMiddlewareInterface";
import {AuthorizationCheckerNotDefinedError} from "../../error/AuthorizationCheckerNotDefinedError";
import {AccessDeniedError} from "../../error/AccessDeniedError";
import {isPromiseLike} from "../../util/isPromiseLike";
import {IGetFromContainer} from "../../container";
import {RoleChecker} from "../../RoleChecker";
import {AuthorizationRequiredError} from "../../error/AuthorizationRequiredError";
import {HttpError, NotFoundError} from "../../index";
const cookie = require("cookie");
const templateUrl = require("template-url");
/**
* Integration with koa framework.
*/
export class KoaDriver extends BaseDriver {
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(public getFromContainer: IGetFromContainer, public koa?: any, public router?: any) {
super();
this.loadKoa();
this.loadRouter();
this.app = this.koa;
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Initializes the things driver needs before routes and middleware registration.
*/
initialize() {
const bodyParser = require("koa-bodyparser");
this.koa.use(bodyParser({
extendTypes: {
text: ["application/xml", "text/xml"],
},
enableTypes: ["json", "form", "text"],
}));
if (this.cors) {
const cors = require("kcors");
if (this.cors === true) {
this.koa.use(cors());
} else {
this.koa.use(cors(this.cors));
}
}
}
/**
* Registers middleware that run before controller actions.
*/
registerMiddleware(middleware: MiddlewareMetadata): void {
if ((middleware.instance as KoaMiddlewareInterface).use) {
this.koa.use(function (ctx: any, next: any) {
return (middleware.instance as KoaMiddlewareInterface).use(ctx, next);
});
}
}
/**
* Registers action in the driver.
*/
registerAction(actionMetadata: ActionMetadata, executeCallback: (options: Action) => any): void {
// middlewares required for this action
const defaultMiddlewares: any[] = [];
if (actionMetadata.isAuthorizedUsed) {
defaultMiddlewares.push((context: any, next: Function) => {
if (!this.authorizationChecker)
throw new AuthorizationCheckerNotDefinedError();
const action: Action = { request: context.request, response: context.response, context, next };
try {
const checkResult = actionMetadata.authorizedRoles instanceof Function ?
this.getFromContainer<RoleChecker>(actionMetadata.authorizedRoles).check(action) :
this.authorizationChecker(action, actionMetadata.authorizedRoles);
const handleError = (result: any) => {
if (!result) {
let error = actionMetadata.authorizedRoles.length === 0 ? new AuthorizationRequiredError(action) : new AccessDeniedError(action);
return this.handleError(error, actionMetadata, action);
} else {
return next();
}
};
if (isPromiseLike(checkResult)) {
return checkResult
.then(result => handleError(result))
.catch(error => this.handleError(error, actionMetadata, action));
} else {
return handleError(checkResult);
}
} catch (error) {
return this.handleError(error, actionMetadata, action);
}
});
}
if (actionMetadata.isFileUsed || actionMetadata.isFilesUsed) {
const multer = this.loadMulter();
actionMetadata.params
.filter(param => param.type === "file")
.forEach(param => {
defaultMiddlewares.push(multer(param.extraOptions).single(param.name));
});
actionMetadata.params
.filter(param => param.type === "files")
.forEach(param => {
defaultMiddlewares.push(multer(param.extraOptions).array(param.name));
});
defaultMiddlewares.push(this.fixMulterRequestAssignment);
}
// user used middlewares
const uses = actionMetadata.controllerMetadata.uses.concat(actionMetadata.uses);
const beforeMiddlewares = this.prepareMiddlewares(uses.filter(use => !use.afterAction));
const afterMiddlewares = this.prepareMiddlewares(uses.filter(use => use.afterAction));
// prepare route and route handler function
const route = ActionMetadata.appendBaseRoute(this.routePrefix, actionMetadata.fullRoute);
const routeHandler = (context: any, next: () => Promise<any>) => {
const options: Action = {request: context.request, response: context.response, context, next};
return executeCallback(options);
};
// finally register action in koa
this.router[actionMetadata.type.toLowerCase()](...[
route,
...beforeMiddlewares,
...defaultMiddlewares,
routeHandler,
...afterMiddlewares
]);
}
/**
* Registers all routes in the framework.
*/
registerRoutes() {
this.koa.use(this.router.routes());
this.koa.use(this.router.allowedMethods());
}
/**
* Gets param from the request.
*/
getParamFromRequest(actionOptions: Action, param: ParamMetadata): any {
const context = actionOptions.context;
const request: any = actionOptions.request;
switch (param.type) {
case "body":
return request.body;
case "body-param":
return request.body[param.name];
case "param":
return context.params[param.name];
case "params":
return context.params;
case "session":
if (param.name)
return context.session[param.name];
return context.session;
case "state":
if (param.name)
return context.state[param.name];
return context.state;
case "query":
return context.query[param.name];
case "queries":
return context.query;
case "file":
return actionOptions.context.req.file;
case "files":
return actionOptions.context.req.files;
case "header":
return context.headers[param.name.toLowerCase()];
case "headers":
return request.headers;
case "cookie":
if (!context.headers.cookie) return;
const cookies = cookie.parse(context.headers.cookie);
return cookies[param.name];
case "cookies":
if (!request.headers.cookie) return {};
return cookie.parse(request.headers.cookie);
}
}
/**
* Handles result of successfully executed controller action.
*/
handleSuccess(result: any, action: ActionMetadata, options: Action): void {
// if the action returned the context or the response object itself, short-circuits
if (result && (result === options.response || result === options.context)) {
return options.next();
}
// transform result if needed
result = this.transformResult(result, action, options);
if (action.redirect) { // if redirect is set then do it
if (typeof result === "string") {
options.response.redirect(result);
} else if (result instanceof Object) {
options.response.redirect(templateUrl(action.redirect, result));
} else {
options.response.redirect(action.redirect);
}
} else if (action.renderedTemplate) { // if template is set then render it // TODO: not working in koa
const renderOptions = result && result instanceof Object ? result : {};
this.koa.use(async function (ctx: any, next: any) {
await ctx.render(action.renderedTemplate, renderOptions);
});
}
else if (result === undefined) { // throw NotFoundError on undefined response
if (action.undefinedResultCode instanceof Function) {
throw new (action.undefinedResultCode as any)(options);
} else if (!action.undefinedResultCode) {
throw new NotFoundError();
}
}
else if (result === null) { // send null response
if (action.nullResultCode instanceof Function)
throw new (action.nullResultCode as any)(options);
options.response.body = null;
}
else if (result instanceof Uint8Array) { // check if it's binary data (typed array)
options.response.body = Buffer.from(result as any);
}
else { // send regular result
if (result instanceof Object) {
options.response.body = result;
} else {
options.response.body = result;
}
}
// set http status code
if (result === undefined && action.undefinedResultCode) {
options.response.status = action.undefinedResultCode;
}
else if (result === null && action.nullResultCode) {
options.response.status = action.nullResultCode;
} else if (action.successHttpCode) {
options.response.status = action.successHttpCode;
} else if (options.response.body === null) {
options.response.status = 204;
}
// apply http headers
Object.keys(action.headers).forEach(name => {
options.response.set(name, action.headers[name]);
});
return options.next();
}
/**
* Handles result of failed executed controller action.
*/
handleError(error: any, action: ActionMetadata | undefined, options: Action) {
return new Promise((resolve, reject) => {
if (this.isDefaultErrorHandlingEnabled) {
// apply http headers
if (action) {
Object.keys(action.headers).forEach(name => {
options.response.set(name, action.headers[name]);
});
}
// send error content
if (action && action.isJsonTyped) {
options.response.body = this.processJsonError(error);
} else {
options.response.body = this.processTextError(error);
}
// set http status
if (error instanceof HttpError && error.httpCode) {
options.response.status = error.httpCode;
} else {
options.response.status = 500;
}
return resolve();
}
return reject(error);
});
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Creates middlewares from the given "use"-s.
*/
protected prepareMiddlewares(uses: UseMetadata[]) {
const middlewareFunctions: Function[] = [];
uses.forEach(use => {
if (use.middleware.prototype && use.middleware.prototype.use) { // if this is function instance of MiddlewareInterface
middlewareFunctions.push((context: any, next: (err?: any) => Promise<any>) => {
try {
const useResult = (this.getFromContainer(use.middleware) as KoaMiddlewareInterface).use(context, next);
if (isPromiseLike(useResult)) {
useResult.catch((error: any) => {
this.handleError(error, undefined, {
request: context.req,
response: context.res,
context,
next
});
return error;
});
}
return useResult;
} catch (error) {
this.handleError(error, undefined, {
request: context.request,
response: context.response,
context,
next
});
}
});
} else {
middlewareFunctions.push(use.middleware);
}
});
return middlewareFunctions;
}
/**
* Dynamically loads koa and required koa-router module.
*/
protected loadKoa() {
if (require) {
if (!this.koa) {
try {
this.koa = new (require("koa"))();
} catch (e) {
throw new Error("koa package was not found installed. Try to install it: npm install koa@next --save");
}
}
} else {
throw new Error("Cannot load koa. Try to install all required dependencies.");
}
}
/**
* Dynamically loads koa-router module.
*/
private loadRouter() {
if (require) {
if (!this.router) {
try {
this.router = new (require("koa-router"))();
} catch (e) {
throw new Error("koa-router package was not found installed. Try to install it: npm install koa-router@next --save");
}
}
} else {
throw new Error("Cannot load koa. Try to install all required dependencies.");
}
}
/**
* Dynamically loads koa-multer module.
*/
private loadMulter() {
try {
return require("koa-multer");
} catch (e) {
throw new Error("koa-multer package was not found installed. Try to install it: npm install koa-multer --save");
}
}
/**
* This middleware fixes a bug on koa-multer implementation.
*
* This bug should be fixed by koa-multer PR #15: https://github.com/koa-modules/multer/pull/15
*/
private async fixMulterRequestAssignment(ctx: any, next: Function) {
if ("request" in ctx) {
if (ctx.req.body) ctx.request.body = ctx.req.body;
if (ctx.req.file) ctx.request.file = ctx.req.file;
if (ctx.req.files) {
ctx.request.files = ctx.req.files;
ctx.files = ctx.req.files;
}
}
return await next();
}
}