resting-squirrel-controller 2.6.0 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +224 -224
- package/dist/controller.d.ts +257 -257
- package/dist/controller.js +373 -379
- package/dist/controller.js.map +1 -1
- package/dist/decorators/controller-options.d.ts +17 -17
- package/dist/decorators/controller-options.js +15 -15
- package/dist/decorators/deprecated.d.ts +3 -3
- package/dist/decorators/deprecated.js +10 -10
- package/dist/decorators/methods.d.ts +6 -6
- package/dist/decorators/methods.js +107 -107
- package/dist/decorators/options.d.ts +25 -25
- package/dist/decorators/options.js +17 -17
- package/dist/decorators/resource.d.ts +3 -3
- package/dist/decorators/resource.js +7 -7
- package/dist/decorators/version.d.ts +3 -3
- package/dist/decorators/version.js +7 -7
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -4
- package/dist/utils.d.ts +194 -193
- package/dist/utils.js +31 -31
- package/package.json +66 -66
- package/index.js +0 -1
- package/tslint.json +0 -78
package/dist/controller.js
CHANGED
|
@@ -1,380 +1,374 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
-
var t = {};
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
-
t[p[i]] = s[p[i]];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
const path = require("path");
|
|
24
|
-
const resting_squirrel_1 = require("resting-squirrel");
|
|
25
|
-
const resting_squirrel_dto_1 = require("resting-squirrel-dto");
|
|
26
|
-
const controller_options_1 = require("./decorators/controller-options");
|
|
27
|
-
const deprecated_1 = require("./decorators/deprecated");
|
|
28
|
-
const methods_1 = require("./decorators/methods");
|
|
29
|
-
const options_1 = require("./decorators/options");
|
|
30
|
-
const resource_1 = require("./decorators/resource");
|
|
31
|
-
const version_1 = require("./decorators/version");
|
|
32
|
-
const utils_1 = require("./utils");
|
|
33
|
-
class E {
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* The endpoint is executed with `PUT` method.
|
|
37
|
-
*/
|
|
38
|
-
E.put = methods_1.put;
|
|
39
|
-
/**
|
|
40
|
-
* The endpoint is executed with `GET` method.
|
|
41
|
-
*/
|
|
42
|
-
E.get = methods_1.get;
|
|
43
|
-
/**
|
|
44
|
-
* The endpoint is executed with `POST` method.
|
|
45
|
-
*/
|
|
46
|
-
E.post = methods_1.post;
|
|
47
|
-
/**
|
|
48
|
-
* The endpoint is executed with `DELETE` method.
|
|
49
|
-
*/
|
|
50
|
-
E.delete = methods_1.del;
|
|
51
|
-
/**
|
|
52
|
-
* The endpoint is executed with `HEAD` method.
|
|
53
|
-
*/
|
|
54
|
-
E.head = methods_1.head;
|
|
55
|
-
/**
|
|
56
|
-
* Defines options to the endpoint.
|
|
57
|
-
*/
|
|
58
|
-
E.options = options_1.default;
|
|
59
|
-
/**
|
|
60
|
-
* Define specific option to the endpoint.
|
|
61
|
-
*/
|
|
62
|
-
E.option = (option, value) => E.options({ [option]: value });
|
|
63
|
-
/**
|
|
64
|
-
* Sets the `auth` option of the endpoint.
|
|
65
|
-
*/
|
|
66
|
-
E.auth = (auth) => E.options({ auth });
|
|
67
|
-
/**
|
|
68
|
-
* Sets the `params` option to the endpoint using DTO classes.
|
|
69
|
-
*/
|
|
70
|
-
E.params = (params, optionalParams = [], omit = []) => E.options({ params, optionalParams, omitParams: omit });
|
|
71
|
-
/**
|
|
72
|
-
* Sets the `response` option to the endpoint using DTO classes.
|
|
73
|
-
*/
|
|
74
|
-
E.response = (response, omit = []) => E.options({ response, omitResponse: omit });
|
|
75
|
-
/**
|
|
76
|
-
* Sets the `params` and `response` options to the endpoint using DTO classes.
|
|
77
|
-
*/
|
|
78
|
-
E.dto = (dto, optionalParams = [], omitParams = [], omitResponse = []) => E.options({ params: dto, response: dto, optionalParams, omitParams, omitResponse });
|
|
79
|
-
/**
|
|
80
|
-
* Sets the `errors` option to the endpoint.
|
|
81
|
-
*/
|
|
82
|
-
E.errors = (errors) => E.options({ errors });
|
|
83
|
-
/**
|
|
84
|
-
* Sets the `description` option to the endpoint.
|
|
85
|
-
*/
|
|
86
|
-
E.description = (description) => E.options({ description });
|
|
87
|
-
/**
|
|
88
|
-
* Sets the `hideDocs` option to `true` to the endpoint.
|
|
89
|
-
*/
|
|
90
|
-
// tslint:disable-next-line: member-ordering
|
|
91
|
-
E.hideDocs = E.option('hideDocs', true);
|
|
92
|
-
/**
|
|
93
|
-
* Sets the `args` option to the endpoint.
|
|
94
|
-
*/
|
|
95
|
-
E.args = (args) => E.options({ args });
|
|
96
|
-
/**
|
|
97
|
-
* Sets the `requireApiKey` option to the endpoint.
|
|
98
|
-
*/
|
|
99
|
-
E.requireApiKey = (requireApiKey) => E.options({ requireApiKey });
|
|
100
|
-
/**
|
|
101
|
-
* Sets the `excludedApiKey` option to the endpoint.
|
|
102
|
-
*/
|
|
103
|
-
E.excludedApiKeys = (excludedApiKeys) => E.options({ excludedApiKeys });
|
|
104
|
-
/**
|
|
105
|
-
* Sets the `timeout` option to the endpoint.
|
|
106
|
-
*/
|
|
107
|
-
E.timeout = (timeout) => E.options({ timeout });
|
|
108
|
-
/**
|
|
109
|
-
* Sets the `props` option to the endpoint.
|
|
110
|
-
*/
|
|
111
|
-
E.props = (props) => E.options({ props });
|
|
112
|
-
/**
|
|
113
|
-
* Sets the endpoint as empty. It returns 204 status code.
|
|
114
|
-
*/
|
|
115
|
-
// tslint:disable-next-line: member-ordering
|
|
116
|
-
E.emptyResponse = E.response(null);
|
|
117
|
-
/**
|
|
118
|
-
* Sets the endpoint as deprecated.
|
|
119
|
-
*/
|
|
120
|
-
// tslint:disable-next-line: member-ordering
|
|
121
|
-
E.deprecated = deprecated_1.default;
|
|
122
|
-
// tslint:disable-next-line: member-ordering
|
|
123
|
-
E.redirect = E.options({ redirect: true });
|
|
124
|
-
// tslint:disable-next-line: max-classes-per-file
|
|
125
|
-
class Controller {
|
|
126
|
-
constructor(app) {
|
|
127
|
-
this._app = app;
|
|
128
|
-
Controller._controllers.push(this);
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Registers all found controllers in the directory to the application.
|
|
132
|
-
*
|
|
133
|
-
* @param app The instance of the application.
|
|
134
|
-
* @param directory Path to the directory where the controllers are located.
|
|
135
|
-
*/
|
|
136
|
-
static registerDirectory(app, directory) {
|
|
137
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
-
const files = yield utils_1.fs.readdir(directory);
|
|
139
|
-
for (const file of files) {
|
|
140
|
-
const filename = path.resolve(directory, file);
|
|
141
|
-
if ((yield utils_1.fs.stat(filename)).isDirectory()) {
|
|
142
|
-
yield this.registerDirectory(app, filename);
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
try {
|
|
146
|
-
const M = (0, utils_1.requireModule)(filename);
|
|
147
|
-
if (M && M.prototype && M.prototype instanceof this) {
|
|
148
|
-
M.register(app);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
catch (e) {
|
|
152
|
-
if (file.indexOf('.js.map') >= 0 || file.indexOf('.d.ts') >= 0) {
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
console.error(file, e);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
static register(app) {
|
|
161
|
-
new this(app).register();
|
|
162
|
-
}
|
|
163
|
-
static getControllers() {
|
|
164
|
-
return this._controllers;
|
|
165
|
-
}
|
|
166
|
-
register() {
|
|
167
|
-
const version = this.getVersion();
|
|
168
|
-
for (const endpoint of this.getEndpoints()) {
|
|
169
|
-
let e;
|
|
170
|
-
if (version !== undefined) {
|
|
171
|
-
e = this._app.registerRoute(endpoint.method, version, this.getRoute(endpoint.route), this.getOptions(endpoint.propertyKey), endpoint.callback);
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
e = this._app.registerRoute(endpoint.method, this.getRoute(endpoint.route), this.getOptions(endpoint.propertyKey), endpoint.callback);
|
|
175
|
-
}
|
|
176
|
-
if (this.isDeprecated(endpoint.propertyKey)) {
|
|
177
|
-
e.deprecate();
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
beforeExecution(req, res) {
|
|
182
|
-
return __awaiter(this, void 0, void 0, function* () { });
|
|
183
|
-
}
|
|
184
|
-
getEndpoints() {
|
|
185
|
-
return this.__endpoints__ || [];
|
|
186
|
-
}
|
|
187
|
-
getVersion() {
|
|
188
|
-
return this.constructor.__version__;
|
|
189
|
-
}
|
|
190
|
-
getResource() {
|
|
191
|
-
return this.constructor.__resource__;
|
|
192
|
-
}
|
|
193
|
-
getOptions(propertyKey) {
|
|
194
|
-
const t = this;
|
|
195
|
-
const controllerOptions = this.constructor.__options__;
|
|
196
|
-
if (!t.__options__) {
|
|
197
|
-
return {};
|
|
198
|
-
}
|
|
199
|
-
if (!t.__options__[propertyKey]) {
|
|
200
|
-
return {};
|
|
201
|
-
}
|
|
202
|
-
let options = t.__options__[propertyKey];
|
|
203
|
-
options = Object.assign(Object.assign(Object.assign({}, controllerOptions), options), { errors: [
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (
|
|
241
|
-
return
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (response
|
|
247
|
-
return
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
Controller.
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
Controller.
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
*
|
|
273
|
-
* @deprecated
|
|
274
|
-
*/
|
|
275
|
-
Controller.
|
|
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
|
-
Controller.
|
|
339
|
-
/**
|
|
340
|
-
* Sets the `
|
|
341
|
-
* @deprecated
|
|
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
|
-
Controller.
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
// tslint:disable-next-line: member-ordering
|
|
375
|
-
Controller.emptyResponse = Controller.response(null);
|
|
376
|
-
// #endregion
|
|
377
|
-
// #endregion
|
|
378
|
-
// tslint:disable-next-line: member-ordering
|
|
379
|
-
Controller._controllers = [];
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
const path = require("path");
|
|
24
|
+
const resting_squirrel_1 = require("resting-squirrel");
|
|
25
|
+
const resting_squirrel_dto_1 = require("resting-squirrel-dto");
|
|
26
|
+
const controller_options_1 = require("./decorators/controller-options");
|
|
27
|
+
const deprecated_1 = require("./decorators/deprecated");
|
|
28
|
+
const methods_1 = require("./decorators/methods");
|
|
29
|
+
const options_1 = require("./decorators/options");
|
|
30
|
+
const resource_1 = require("./decorators/resource");
|
|
31
|
+
const version_1 = require("./decorators/version");
|
|
32
|
+
const utils_1 = require("./utils");
|
|
33
|
+
class E {
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The endpoint is executed with `PUT` method.
|
|
37
|
+
*/
|
|
38
|
+
E.put = methods_1.put;
|
|
39
|
+
/**
|
|
40
|
+
* The endpoint is executed with `GET` method.
|
|
41
|
+
*/
|
|
42
|
+
E.get = methods_1.get;
|
|
43
|
+
/**
|
|
44
|
+
* The endpoint is executed with `POST` method.
|
|
45
|
+
*/
|
|
46
|
+
E.post = methods_1.post;
|
|
47
|
+
/**
|
|
48
|
+
* The endpoint is executed with `DELETE` method.
|
|
49
|
+
*/
|
|
50
|
+
E.delete = methods_1.del;
|
|
51
|
+
/**
|
|
52
|
+
* The endpoint is executed with `HEAD` method.
|
|
53
|
+
*/
|
|
54
|
+
E.head = methods_1.head;
|
|
55
|
+
/**
|
|
56
|
+
* Defines options to the endpoint.
|
|
57
|
+
*/
|
|
58
|
+
E.options = options_1.default;
|
|
59
|
+
/**
|
|
60
|
+
* Define specific option to the endpoint.
|
|
61
|
+
*/
|
|
62
|
+
E.option = (option, value) => E.options({ [option]: value });
|
|
63
|
+
/**
|
|
64
|
+
* Sets the `auth` option of the endpoint.
|
|
65
|
+
*/
|
|
66
|
+
E.auth = (auth) => E.options({ auth });
|
|
67
|
+
/**
|
|
68
|
+
* Sets the `params` option to the endpoint using DTO classes.
|
|
69
|
+
*/
|
|
70
|
+
E.params = (params, optionalParams = [], omit = []) => E.options({ params, optionalParams, omitParams: omit });
|
|
71
|
+
/**
|
|
72
|
+
* Sets the `response` option to the endpoint using DTO classes.
|
|
73
|
+
*/
|
|
74
|
+
E.response = (response, omit = []) => E.options({ response, omitResponse: omit });
|
|
75
|
+
/**
|
|
76
|
+
* Sets the `params` and `response` options to the endpoint using DTO classes.
|
|
77
|
+
*/
|
|
78
|
+
E.dto = (dto, optionalParams = [], omitParams = [], omitResponse = []) => E.options({ params: dto, response: dto, optionalParams, omitParams, omitResponse });
|
|
79
|
+
/**
|
|
80
|
+
* Sets the `errors` option to the endpoint.
|
|
81
|
+
*/
|
|
82
|
+
E.errors = (errors) => E.options({ errors });
|
|
83
|
+
/**
|
|
84
|
+
* Sets the `description` option to the endpoint.
|
|
85
|
+
*/
|
|
86
|
+
E.description = (description) => E.options({ description });
|
|
87
|
+
/**
|
|
88
|
+
* Sets the `hideDocs` option to `true` to the endpoint.
|
|
89
|
+
*/
|
|
90
|
+
// tslint:disable-next-line: member-ordering
|
|
91
|
+
E.hideDocs = E.option('hideDocs', true);
|
|
92
|
+
/**
|
|
93
|
+
* Sets the `args` option to the endpoint.
|
|
94
|
+
*/
|
|
95
|
+
E.args = (args) => E.options({ args });
|
|
96
|
+
/**
|
|
97
|
+
* Sets the `requireApiKey` option to the endpoint.
|
|
98
|
+
*/
|
|
99
|
+
E.requireApiKey = (requireApiKey) => E.options({ requireApiKey });
|
|
100
|
+
/**
|
|
101
|
+
* Sets the `excludedApiKey` option to the endpoint.
|
|
102
|
+
*/
|
|
103
|
+
E.excludedApiKeys = (excludedApiKeys) => E.options({ excludedApiKeys });
|
|
104
|
+
/**
|
|
105
|
+
* Sets the `timeout` option to the endpoint.
|
|
106
|
+
*/
|
|
107
|
+
E.timeout = (timeout) => E.options({ timeout });
|
|
108
|
+
/**
|
|
109
|
+
* Sets the `props` option to the endpoint.
|
|
110
|
+
*/
|
|
111
|
+
E.props = (props) => E.options({ props });
|
|
112
|
+
/**
|
|
113
|
+
* Sets the endpoint as empty. It returns 204 status code.
|
|
114
|
+
*/
|
|
115
|
+
// tslint:disable-next-line: member-ordering
|
|
116
|
+
E.emptyResponse = E.response(null);
|
|
117
|
+
/**
|
|
118
|
+
* Sets the endpoint as deprecated.
|
|
119
|
+
*/
|
|
120
|
+
// tslint:disable-next-line: member-ordering
|
|
121
|
+
E.deprecated = deprecated_1.default;
|
|
122
|
+
// tslint:disable-next-line: member-ordering
|
|
123
|
+
E.redirect = E.options({ redirect: true });
|
|
124
|
+
// tslint:disable-next-line: max-classes-per-file
|
|
125
|
+
class Controller {
|
|
126
|
+
constructor(app) {
|
|
127
|
+
this._app = app;
|
|
128
|
+
Controller._controllers.push(this);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Registers all found controllers in the directory to the application.
|
|
132
|
+
*
|
|
133
|
+
* @param app The instance of the application.
|
|
134
|
+
* @param directory Path to the directory where the controllers are located.
|
|
135
|
+
*/
|
|
136
|
+
static registerDirectory(app, directory) {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
const files = yield utils_1.fs.readdir(directory);
|
|
139
|
+
for (const file of files) {
|
|
140
|
+
const filename = path.resolve(directory, file);
|
|
141
|
+
if ((yield utils_1.fs.stat(filename)).isDirectory()) {
|
|
142
|
+
yield this.registerDirectory(app, filename);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const M = (0, utils_1.requireModule)(filename);
|
|
147
|
+
if (M && M.prototype && M.prototype instanceof this) {
|
|
148
|
+
M.register(app);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
if (file.indexOf('.js.map') >= 0 || file.indexOf('.d.ts') >= 0) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
console.error(file, e);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
static register(app) {
|
|
161
|
+
new this(app).register();
|
|
162
|
+
}
|
|
163
|
+
static getControllers() {
|
|
164
|
+
return this._controllers;
|
|
165
|
+
}
|
|
166
|
+
register() {
|
|
167
|
+
const version = this.getVersion();
|
|
168
|
+
for (const endpoint of this.getEndpoints()) {
|
|
169
|
+
let e;
|
|
170
|
+
if (version !== undefined) {
|
|
171
|
+
e = this._app.registerRoute(endpoint.method, version, this.getRoute(endpoint.route), this.getOptions(endpoint.propertyKey), endpoint.callback);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
e = this._app.registerRoute(endpoint.method, this.getRoute(endpoint.route), this.getOptions(endpoint.propertyKey), endpoint.callback);
|
|
175
|
+
}
|
|
176
|
+
if (this.isDeprecated(endpoint.propertyKey)) {
|
|
177
|
+
e.deprecate();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
beforeExecution(req, res) {
|
|
182
|
+
return __awaiter(this, void 0, void 0, function* () { });
|
|
183
|
+
}
|
|
184
|
+
getEndpoints() {
|
|
185
|
+
return this.__endpoints__ || [];
|
|
186
|
+
}
|
|
187
|
+
getVersion() {
|
|
188
|
+
return this.constructor.__version__;
|
|
189
|
+
}
|
|
190
|
+
getResource() {
|
|
191
|
+
return this.constructor.__resource__;
|
|
192
|
+
}
|
|
193
|
+
getOptions(propertyKey) {
|
|
194
|
+
const t = this;
|
|
195
|
+
const controllerOptions = this.constructor.__options__;
|
|
196
|
+
if (!t.__options__) {
|
|
197
|
+
return {};
|
|
198
|
+
}
|
|
199
|
+
if (!t.__options__[propertyKey]) {
|
|
200
|
+
return {};
|
|
201
|
+
}
|
|
202
|
+
let options = t.__options__[propertyKey];
|
|
203
|
+
options = Object.assign(Object.assign(Object.assign({}, controllerOptions), options), { errors: [...((controllerOptions === null || controllerOptions === void 0 ? void 0 : controllerOptions.errors) || []), ...((options === null || options === void 0 ? void 0 : options.errors) || [])], props: Object.assign(Object.assign({}, controllerOptions === null || controllerOptions === void 0 ? void 0 : controllerOptions.props), options === null || options === void 0 ? void 0 : options.props) });
|
|
204
|
+
const { args, params, response, optionalParams, omitParams, omitResponse } = options, restOptions = __rest(options, ["args", "params", "response", "optionalParams", "omitParams", "omitResponse"]);
|
|
205
|
+
return Object.assign(Object.assign({}, restOptions), { args: args ? (args instanceof Array ? args : args.toArray()) : undefined, params: this._getParamsArray(params, optionalParams, omitParams), response: response instanceof resting_squirrel_1.Response.Base ? response : this._getResponseArray(response, omitResponse) });
|
|
206
|
+
}
|
|
207
|
+
isDeprecated(propertyKey) {
|
|
208
|
+
const t = this;
|
|
209
|
+
if (!t.__deprecated__) {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
return t.__deprecated__.includes(propertyKey);
|
|
213
|
+
}
|
|
214
|
+
getRoute(route) {
|
|
215
|
+
let resource = this.getResource();
|
|
216
|
+
if (!resource) {
|
|
217
|
+
return route;
|
|
218
|
+
}
|
|
219
|
+
if (resource.indexOf('/') !== 0) {
|
|
220
|
+
resource = `/${resource}`;
|
|
221
|
+
}
|
|
222
|
+
if (route.indexOf('/') !== 0) {
|
|
223
|
+
route = `/${route}`;
|
|
224
|
+
}
|
|
225
|
+
if (route.lastIndexOf('/') === route.length - 1) {
|
|
226
|
+
route = route.substring(0, route.length - 1);
|
|
227
|
+
}
|
|
228
|
+
return `${resource}${route}`;
|
|
229
|
+
}
|
|
230
|
+
_getParamsArray(params, optional = [], omit = []) {
|
|
231
|
+
if (!params) {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
if (params.prototype instanceof resting_squirrel_dto_1.RequestDto) {
|
|
235
|
+
return params.toArray(optional, omit);
|
|
236
|
+
}
|
|
237
|
+
return resting_squirrel_dto_1.default.toParams(params, optional, omit);
|
|
238
|
+
}
|
|
239
|
+
_getResponseArray(response, omit = []) {
|
|
240
|
+
if (response === null) {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
if (!response) {
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
if (response.prototype instanceof resting_squirrel_dto_1.ResponseDto) {
|
|
247
|
+
return response.toArray();
|
|
248
|
+
}
|
|
249
|
+
return resting_squirrel_dto_1.default.toResponse(response, omit);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
exports.default = Controller;
|
|
253
|
+
// #region Decorators
|
|
254
|
+
/**
|
|
255
|
+
* Sets the version to the `Controller` class. All endpoints will have this version.
|
|
256
|
+
*/
|
|
257
|
+
Controller.version = version_1.default;
|
|
258
|
+
/**
|
|
259
|
+
* @alias version
|
|
260
|
+
*/
|
|
261
|
+
Controller.v = Controller.version;
|
|
262
|
+
Controller.controllerOptions = controller_options_1.default;
|
|
263
|
+
Controller.resource = resource_1.default;
|
|
264
|
+
Controller.Endpoint = E;
|
|
265
|
+
/**
|
|
266
|
+
* Marks the endpoint on the method as deprecated.
|
|
267
|
+
* @deprecated
|
|
268
|
+
*/
|
|
269
|
+
Controller.deprecated = deprecated_1.default;
|
|
270
|
+
// #region Methods
|
|
271
|
+
/**
|
|
272
|
+
* The endpoint is executed with `PUT` method.
|
|
273
|
+
* @deprecated
|
|
274
|
+
*/
|
|
275
|
+
Controller.put = methods_1.put;
|
|
276
|
+
/**
|
|
277
|
+
* The endpoint is executed with `GET` method.
|
|
278
|
+
* @deprecated
|
|
279
|
+
*/
|
|
280
|
+
Controller.get = methods_1.get;
|
|
281
|
+
/**
|
|
282
|
+
* The endpoint is executed with `POST` method.
|
|
283
|
+
* @deprecated
|
|
284
|
+
*/
|
|
285
|
+
Controller.post = methods_1.post;
|
|
286
|
+
/**
|
|
287
|
+
* The endpoint is executed with `DELETE` method.
|
|
288
|
+
* @deprecated
|
|
289
|
+
*/
|
|
290
|
+
Controller.delete = methods_1.del;
|
|
291
|
+
// #endregion
|
|
292
|
+
// #region Options
|
|
293
|
+
/**
|
|
294
|
+
* Defines options to the endpoint.
|
|
295
|
+
* @deprecated
|
|
296
|
+
*/
|
|
297
|
+
Controller.options = options_1.default;
|
|
298
|
+
/**
|
|
299
|
+
* Define specific option to the endpoint.
|
|
300
|
+
* @deprecated
|
|
301
|
+
*/
|
|
302
|
+
Controller.option = (option, value) => Controller.options({ [option]: value });
|
|
303
|
+
/**
|
|
304
|
+
* Sets the `auth` option of the endpoint.
|
|
305
|
+
* @deprecated
|
|
306
|
+
*/
|
|
307
|
+
Controller.auth = (auth) => Controller.options({ auth });
|
|
308
|
+
/**
|
|
309
|
+
* Sets the `params` option to the endpoint using DTO classes.
|
|
310
|
+
* @deprecated
|
|
311
|
+
*/
|
|
312
|
+
Controller.params = (params, optionalParams = [], omit = []) => Controller.options({ params, optionalParams, omitParams: omit });
|
|
313
|
+
/**
|
|
314
|
+
* Sets the `response` option to the endpoint using DTO classes.
|
|
315
|
+
* @deprecated
|
|
316
|
+
*/
|
|
317
|
+
Controller.response = (response, omit = []) => Controller.options({ response, omitResponse: omit });
|
|
318
|
+
/**
|
|
319
|
+
* Sets the `params` and `response` options to the endpoint using DTO classes.
|
|
320
|
+
* @deprecated
|
|
321
|
+
*/
|
|
322
|
+
Controller.dto = (dto, optionalParams = [], omitParams = [], omitResponse = []) => Controller.options({ params: dto, response: dto, optionalParams, omitParams, omitResponse });
|
|
323
|
+
/**
|
|
324
|
+
* Sets the `errors` option to the endpoint.
|
|
325
|
+
* @deprecated
|
|
326
|
+
*/
|
|
327
|
+
Controller.errors = (errors) => Controller.options({ errors });
|
|
328
|
+
/**
|
|
329
|
+
* Sets the `description` option to the endpoint.
|
|
330
|
+
* @deprecated
|
|
331
|
+
*/
|
|
332
|
+
Controller.description = (description) => Controller.options({ description });
|
|
333
|
+
/**
|
|
334
|
+
* Sets the `hideDocs` option to `true` to the endpoint.
|
|
335
|
+
* @deprecated
|
|
336
|
+
*/
|
|
337
|
+
// tslint:disable-next-line: member-ordering
|
|
338
|
+
Controller.hideDocs = Controller.option('hideDocs', true);
|
|
339
|
+
/**
|
|
340
|
+
* Sets the `args` option to the endpoint.
|
|
341
|
+
* @deprecated
|
|
342
|
+
*/
|
|
343
|
+
Controller.args = (args) => Controller.options({ args });
|
|
344
|
+
/**
|
|
345
|
+
* Sets the `requireApiKey` option to the endpoint.
|
|
346
|
+
* @deprecated
|
|
347
|
+
*/
|
|
348
|
+
Controller.requireApiKey = (requireApiKey) => Controller.options({ requireApiKey });
|
|
349
|
+
/**
|
|
350
|
+
* Sets the `excludedApiKey` option to the endpoint.
|
|
351
|
+
* @deprecated
|
|
352
|
+
*/
|
|
353
|
+
Controller.excludedApiKeys = (excludedApiKeys) => Controller.options({ excludedApiKeys });
|
|
354
|
+
/**
|
|
355
|
+
* Sets the `timeout` option to the endpoint.
|
|
356
|
+
* @deprecated
|
|
357
|
+
*/
|
|
358
|
+
Controller.timeout = (timeout) => Controller.options({ timeout });
|
|
359
|
+
/**
|
|
360
|
+
* Sets the `props` option to the endpoint.
|
|
361
|
+
* @deprecated
|
|
362
|
+
*/
|
|
363
|
+
Controller.props = (props) => Controller.options({ props });
|
|
364
|
+
/**
|
|
365
|
+
* Sets the endpoint as empty. It returns 204 status code.
|
|
366
|
+
* @deprecated
|
|
367
|
+
*/
|
|
368
|
+
// tslint:disable-next-line: member-ordering
|
|
369
|
+
Controller.emptyResponse = Controller.response(null);
|
|
370
|
+
// #endregion
|
|
371
|
+
// #endregion
|
|
372
|
+
// tslint:disable-next-line: member-ordering
|
|
373
|
+
Controller._controllers = [];
|
|
380
374
|
//# sourceMappingURL=controller.js.map
|