This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
routeTemplate.handlebars
189 lines (176 loc) · 7.31 KB
/
routeTemplate.handlebars
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
/*
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
|______||______||______||______||______||______||______||______||______||______||______|
___ _ _____ _ _
/ _ \ | | | __ \ | | | |
/ /_\ \ _ _ | |_ ___ | | \/ ___ _ __ ___ _ __ __ _ | |_ ___ __| |
| _ || | | || __|/ _ \ | | __ / _ \| '_ \ / _ \| '__|/ _` || __|/ _ \ / _` |
| | | || |_| || |_| (_) | | |_\ \| __/| | | || __/| | | (_| || |_| __/| (_| |
\_| |_/ \__,_| \__|\___/ \____/ \___||_| |_| \___||_| \__,_| \__|\___| \__,_|
______ _ _ _ ___ ___ _ _ __
| _ \ | \ | | | | | \/ | | |(_) / _|
| | | | ___ | \| | ___ | |_ | . . | ___ __| | _ | |_ _ _
| | | |/ _ \ | . ` | / _ \ | __| | |\/| | / _ \ / _` || || _|| | | |
| |/ /| (_) | | |\ || (_) || |_ | | | || (_) || (_| || || | | |_| |
|___/ \___/ \_| \_/ \___/ \__| \_| |_/ \___/ \__,_||_||_| \__, |
__/ |
|___/
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
|______||______||______||______||______||______||______||______||______||______||______|
Routes are generated from models and controllers
*/
/* tslint:disable */
import { Controller, ValidateParam, FieldErrors, ValidateError, TsoaRoute } from 'tsoa';
import { Container, Provider } from 'typescript-ioc';
import { CurrentUser } from './infrastructure/CurrentUser';
{{#if iocModule}}
import { iocContainer } from '{{iocModule}}';
{{/if}}
{{#each controllers}}
import { {{name}} } from '{{modulePath}}';
{{/each}}
{{#if authenticationModule}}
import { koaAuthentication } from '{{authenticationModule}}';
{{/if}}
const models: TsoaRoute.Models = {
{{#each models}}
"{{@key}}": {
{{#if enums}}
"enums": {{{json enums}}},
{{/if}}
{{#if properties}}
"properties": {
{{#each properties}}
"{{@key}}": {{{json this}}},
{{/each}}
},
{{/if}}
{{#if additionalProperties}}
"additionalProperties": {{{json additionalProperties}}},
{{/if}}
},
{{/each}}
};
export function RegisterRoutes(router: any) {
{{#each controllers}}
{{#each actions}}
router.{{method}}('{{fullPath}}',
{{#if security.length}}
authenticateMiddleware({{json security}}),
{{/if}}
async (context, next) => {
const args = {
{{#each parameters}}
{{@key}}: {{{json this}}},
{{/each}}
};
let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, context);
} catch (error) {
context.status = error.status || 500;
context.body = error;
return next();
}
{{#if ../../iocModule}}
const controller = iocContainer.get<{{../name}}>({{../name}});
{{else}}
// Create the currentUser from the context and bind it to the ioc container
const currentUserProvider : Provider={
get:()=> new CurrentUser(context.request.user)
}
Container.bind(CurrentUser).provider(currentUserProvider);
// Using the typescript-ioc container, retrieve controller
const controller = Container.get({{../name}}) as {{../name}};
{{!--
// New up new controller
const controller = new {{../name}}();
--}}
{{/if}}
const promise = controller.{{name}}.apply(controller, validatedArgs);
return promiseHandler(controller, promise, context, next);
});
{{/each}}
{{/each}}
{{#if useSecurity}}
function authenticateMiddleware(security: TsoaRoute.Security[] = []) {
return async (context: any, next: any) => {
let responded = 0;
let success = false;
for (const secMethod of security) {
try
{
const user = await koaAuthentication(context.request, secMethod.name, secMethod.scopes)
// only need to respond once
if (!success) {
success = true;
responded++;
context.request['user'] = user;
return next();
}
} catch (error) {
responded++;
// If no authentication was successful
if (responded == security.length && !success) {
context.throw(401, 'access_denied', `${error.message ? error.message : error}`);
}
}
}
}
}
{{/if}}
function isController(object: any): object is Controller {
return 'getHeaders' in object && 'getStatus' in object && 'setStatus' in object;
}
function promiseHandler(controllerObj: any, promise: Promise<any>, context: any, next: () => Promise<any>) {
return Promise.resolve(promise)
.then((data: any) => {
if (data || data === false) {
context.body = data;
context.status = 200;
} else {
context.status = 204;
}
if (isController(controllerObj)) {
const headers = controllerObj.getHeaders();
Object.keys(headers).forEach((name: string) => {
context.set(name, headers[name]);
});
const statusCode = controllerObj.getStatus();
if (statusCode) {
context.status = statusCode;
}
}
return next();
})
.catch((error: any) => {
context.status = error.status || 500;
context.body = error;
return next();
});
}
function getValidatedArgs(args: any, context: any): any[] {
const errorFields: FieldErrors = {};
const values = Object.keys(args).map(key => {
const name = args[key].name;
switch (args[key].in) {
case 'request':
return context.request;
case 'query':
return ValidateParam(args[key], context.request.query[name], models, name, errorFields)
case 'path':
return ValidateParam(args[key], context.params[name], models, name, errorFields)
case 'header':
return ValidateParam(args[key], context.request.headers[name], models, name, errorFields);
case 'body':
return ValidateParam(args[key], context.request.body, models, name, errorFields, name + '.');
case 'body-prop':
return ValidateParam(args[key], context.request.body[name], models, name, errorFields, 'body.');
}
});
if (Object.keys(errorFields).length > 0) {
throw new ValidateError(errorFields, '');
}
return values;
}
}