-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathRoutingControllers.ts
188 lines (171 loc) · 6.61 KB
/
RoutingControllers.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
import { Action } from './Action';
import { ActionParameterHandler } from './ActionParameterHandler';
import { getFromContainer } from './container';
import { BaseDriver } from './driver/BaseDriver';
import { InterceptorInterface } from './InterceptorInterface';
import { MetadataBuilder } from './metadata-builder/MetadataBuilder';
import { ActionMetadata } from './metadata/ActionMetadata';
import { InterceptorMetadata } from './metadata/InterceptorMetadata';
import { RoutingControllersOptions } from './RoutingControllersOptions';
import { isPromiseLike } from './util/isPromiseLike';
import { runInSequence } from './util/runInSequence';
/**
* Registers controllers and middlewares in the given server framework.
*/
export class RoutingControllers<T extends BaseDriver> {
// -------------------------------------------------------------------------
// Private properties
// -------------------------------------------------------------------------
/**
* Used to check and handle controller action parameters.
*/
private parameterHandler: ActionParameterHandler<T>;
/**
* Used to build metadata objects for controllers and middlewares.
*/
private metadataBuilder: MetadataBuilder;
/**
* Global interceptors run on each controller action.
*/
private interceptors: InterceptorMetadata[] = [];
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(
private driver: T,
private options: RoutingControllersOptions
) {
this.parameterHandler = new ActionParameterHandler(driver);
this.metadataBuilder = new MetadataBuilder(options);
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Initializes the things driver needs before routes and middleware registration.
*/
initialize(): this {
this.driver.initialize();
return this;
}
/**
* Registers all given interceptors.
*/
registerInterceptors(classes?: Function[]): this {
const interceptors = this.metadataBuilder
.buildInterceptorMetadata(classes)
.sort((middleware1, middleware2) => middleware1.priority - middleware2.priority)
.reverse();
this.interceptors.push(...interceptors);
return this;
}
/**
* Registers all given controllers and actions from those controllers.
*/
registerControllers(classes?: Function[]): this {
const controllers = this.metadataBuilder.buildControllerMetadata(classes);
controllers.forEach(controller => {
controller.actions.forEach(actionMetadata => {
const interceptorFns = this.prepareInterceptors([
...this.interceptors,
...actionMetadata.controllerMetadata.interceptors,
...actionMetadata.interceptors,
]);
this.driver.registerAction(actionMetadata, (action: Action) => {
return this.executeAction(actionMetadata, action, interceptorFns);
});
});
});
this.driver.registerRoutes();
return this;
}
/**
* Registers post-execution middlewares in the driver.
*/
registerMiddlewares(type: 'before' | 'after', classes?: Function[]): this {
this.metadataBuilder
.buildMiddlewareMetadata(classes)
.filter(middleware => middleware.global && middleware.type === type)
.sort((middleware1, middleware2) => middleware2.priority - middleware1.priority)
.forEach(middleware => this.driver.registerMiddleware(middleware, this.options));
return this;
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Executes given controller action.
*/
protected executeAction(actionMetadata: ActionMetadata, action: Action, interceptorFns: Function[]) {
// compute all parameters
const paramsPromises = actionMetadata.params
.sort((param1, param2) => param1.index - param2.index)
.map(param => this.parameterHandler.handle(action, param));
// after all parameters are computed
return Promise.all(paramsPromises)
.then(params => {
// execute action and handle result
const allParams = actionMetadata.appendParams ? actionMetadata.appendParams(action).concat(params) : params;
const result = actionMetadata.methodOverride
? actionMetadata.methodOverride(actionMetadata, action, allParams)
: actionMetadata.callMethod(allParams, action);
return this.handleCallMethodResult(result, actionMetadata, action, interceptorFns);
})
.catch(error => {
// otherwise simply handle error without action execution
return this.driver.handleError(error, actionMetadata, action);
});
}
/**
* Handles result of the action method execution.
*/
protected handleCallMethodResult(
result: any,
action: ActionMetadata,
options: Action,
interceptorFns: Function[]
): any {
if (isPromiseLike(result)) {
return result
.then((data: any) => {
return this.handleCallMethodResult(data, action, options, interceptorFns);
})
.catch((error: any) => {
return this.driver.handleError(error, action, options);
});
} else {
if (interceptorFns) {
const awaitPromise = runInSequence(interceptorFns, interceptorFn => {
const interceptedResult = interceptorFn(options, result);
if (isPromiseLike(interceptedResult)) {
return interceptedResult.then((resultFromPromise: any) => {
result = resultFromPromise;
});
} else {
result = interceptedResult;
return Promise.resolve();
}
});
return awaitPromise
.then(() => this.driver.handleSuccess(result, action, options))
.catch(error => this.driver.handleError(error, action, options));
} else {
return this.driver.handleSuccess(result, action, options);
}
}
}
/**
* Creates interceptors from the given "use interceptors".
*/
protected prepareInterceptors(uses: InterceptorMetadata[]): Function[] {
return uses.map(use => {
if (use.interceptor.prototype && use.interceptor.prototype.intercept) {
// if this is function instance of InterceptorInterface
return function (action: Action, result: any) {
return getFromContainer<InterceptorInterface>(use.interceptor, action).intercept(action, result);
};
}
return use.interceptor;
});
}
}