@deepkit/http 1.0.1-alpha.68 → 1.0.1-alpha.72

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.
Files changed (121) hide show
  1. package/dist/cjs/src/decorator.d.ts +8 -6
  2. package/dist/cjs/src/decorator.js +49 -12
  3. package/dist/cjs/src/decorator.js.map +1 -1
  4. package/dist/cjs/src/filter.d.ts +3 -3
  5. package/dist/cjs/src/filter.js +3 -3
  6. package/dist/cjs/src/filter.js.map +1 -1
  7. package/dist/cjs/src/http.d.ts +49 -11
  8. package/dist/cjs/src/http.js +128 -37
  9. package/dist/cjs/src/http.js.map +1 -1
  10. package/dist/cjs/src/kernel.d.ts +3 -3
  11. package/dist/cjs/src/kernel.js +1 -1
  12. package/dist/cjs/src/kernel.js.map +1 -1
  13. package/dist/cjs/src/model.d.ts +11 -3
  14. package/dist/cjs/src/model.js +58 -26
  15. package/dist/cjs/src/model.js.map +1 -1
  16. package/dist/cjs/src/module.js +5 -3
  17. package/dist/cjs/src/module.js.map +1 -1
  18. package/dist/cjs/src/router.d.ts +84 -11
  19. package/dist/cjs/src/router.js +280 -94
  20. package/dist/cjs/src/router.js.map +1 -1
  21. package/dist/cjs/src/static-serving.js +15 -8
  22. package/dist/cjs/src/static-serving.js.map +1 -1
  23. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  24. package/dist/esm/src/decorator.d.ts +8 -6
  25. package/dist/esm/src/decorator.js +47 -11
  26. package/dist/esm/src/decorator.js.map +1 -1
  27. package/dist/esm/src/filter.d.ts +3 -3
  28. package/dist/esm/src/filter.js +4 -4
  29. package/dist/esm/src/filter.js.map +1 -1
  30. package/dist/esm/src/http.d.ts +49 -11
  31. package/dist/esm/src/http.js +101 -23
  32. package/dist/esm/src/http.js.map +1 -1
  33. package/dist/esm/src/kernel.d.ts +3 -3
  34. package/dist/esm/src/kernel.js +2 -2
  35. package/dist/esm/src/kernel.js.map +1 -1
  36. package/dist/esm/src/model.d.ts +11 -3
  37. package/dist/esm/src/model.js +47 -15
  38. package/dist/esm/src/model.js.map +1 -1
  39. package/dist/esm/src/module.js +5 -3
  40. package/dist/esm/src/module.js.map +1 -1
  41. package/dist/esm/src/router.d.ts +84 -11
  42. package/dist/esm/src/router.js +278 -94
  43. package/dist/esm/src/router.js.map +1 -1
  44. package/dist/esm/src/static-serving.js +16 -9
  45. package/dist/esm/src/static-serving.js.map +1 -1
  46. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  47. package/package.json +17 -14
  48. package/src/decorator.ts +9 -6
  49. package/src/filter.ts +4 -4
  50. package/src/http.ts +88 -15
  51. package/src/kernel.ts +2 -2
  52. package/src/model.ts +35 -6
  53. package/src/module.ts +5 -3
  54. package/src/router.ts +286 -73
  55. package/src/static-serving.ts +6 -3
  56. package/coverage/clover.xml +0 -1086
  57. package/coverage/coverage-final.json +0 -12
  58. package/coverage/lcov-report/base.css +0 -224
  59. package/coverage/lcov-report/block-navigation.js +0 -79
  60. package/coverage/lcov-report/favicon.png +0 -0
  61. package/coverage/lcov-report/index.html +0 -126
  62. package/coverage/lcov-report/prettify.css +0 -1
  63. package/coverage/lcov-report/prettify.js +0 -2
  64. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  65. package/coverage/lcov-report/sorter.js +0 -170
  66. package/coverage/lcov-report/src/controllers.ts.html +0 -113
  67. package/coverage/lcov-report/src/decorator.ts.html +0 -1280
  68. package/coverage/lcov-report/src/filter.ts.html +0 -734
  69. package/coverage/lcov-report/src/http.ts.html +0 -2192
  70. package/coverage/lcov-report/src/index.html +0 -246
  71. package/coverage/lcov-report/src/kernel.ts.html +0 -260
  72. package/coverage/lcov-report/src/middleware.ts.html +0 -428
  73. package/coverage/lcov-report/src/model.ts.html +0 -937
  74. package/coverage/lcov-report/src/module.config.ts.html +0 -89
  75. package/coverage/lcov-report/src/module.ts.html +0 -245
  76. package/coverage/lcov-report/src/router.ts.html +0 -2318
  77. package/coverage/lcov-report/tests/index.html +0 -111
  78. package/coverage/lcov-report/tests/utils.ts.html +0 -176
  79. package/coverage/lcov.info +0 -2225
  80. package/dist/cjs/tests/filter.spec.d.ts +0 -1
  81. package/dist/cjs/tests/filter.spec.js +0 -251
  82. package/dist/cjs/tests/filter.spec.js.map +0 -1
  83. package/dist/cjs/tests/middleware.spec.d.ts +0 -1
  84. package/dist/cjs/tests/middleware.spec.js +0 -471
  85. package/dist/cjs/tests/middleware.spec.js.map +0 -1
  86. package/dist/cjs/tests/model.spec.d.ts +0 -1
  87. package/dist/cjs/tests/model.spec.js +0 -23
  88. package/dist/cjs/tests/model.spec.js.map +0 -1
  89. package/dist/cjs/tests/module.spec.d.ts +0 -1
  90. package/dist/cjs/tests/module.spec.js +0 -46
  91. package/dist/cjs/tests/module.spec.js.map +0 -1
  92. package/dist/cjs/tests/router.spec.d.ts +0 -1
  93. package/dist/cjs/tests/router.spec.js +0 -987
  94. package/dist/cjs/tests/router.spec.js.map +0 -1
  95. package/dist/cjs/tests/utils.d.ts +0 -8
  96. package/dist/cjs/tests/utils.js +0 -30
  97. package/dist/cjs/tests/utils.js.map +0 -1
  98. package/dist/esm/tests/filter.spec.d.ts +0 -1
  99. package/dist/esm/tests/filter.spec.js +0 -249
  100. package/dist/esm/tests/filter.spec.js.map +0 -1
  101. package/dist/esm/tests/middleware.spec.d.ts +0 -1
  102. package/dist/esm/tests/middleware.spec.js +0 -469
  103. package/dist/esm/tests/middleware.spec.js.map +0 -1
  104. package/dist/esm/tests/model.spec.d.ts +0 -1
  105. package/dist/esm/tests/model.spec.js +0 -21
  106. package/dist/esm/tests/model.spec.js.map +0 -1
  107. package/dist/esm/tests/module.spec.d.ts +0 -1
  108. package/dist/esm/tests/module.spec.js +0 -44
  109. package/dist/esm/tests/module.spec.js.map +0 -1
  110. package/dist/esm/tests/router.spec.d.ts +0 -1
  111. package/dist/esm/tests/router.spec.js +0 -985
  112. package/dist/esm/tests/router.spec.js.map +0 -1
  113. package/dist/esm/tests/utils.d.ts +0 -8
  114. package/dist/esm/tests/utils.js +0 -26
  115. package/dist/esm/tests/utils.js.map +0 -1
  116. package/tests/filter.spec.ts +0 -181
  117. package/tests/middleware.spec.ts +0 -440
  118. package/tests/model.spec.ts +0 -26
  119. package/tests/module.spec.ts +0 -32
  120. package/tests/router.spec.ts +0 -781
  121. package/tests/utils.ts +0 -32
@@ -7,10 +7,17 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  import { __ΩSerializationOptions as __ΩSerializationOptions } from '@deepkit/type';
8
8
  import { __ΩClassType as __ΩClassType } from '@deepkit/core';
9
9
  import { __ΩMiddlewareRegistryEntry as __ΩMiddlewareRegistryEntry } from '@deepkit/app';
10
+ import { __ΩHttpDecorator as __ΩHttpDecorator } from './decorator';
10
11
  import { __ΩLoggerInterface as __ΩLoggerInterface } from '@deepkit/logger';
11
12
  import { __ΩHttpMiddlewareFn as __ΩHttpMiddlewareFn } from './middleware';
12
13
  import { __ΩHttpRequestQuery as __ΩHttpRequestQuery } from './model';
13
14
  import { __ΩHttpRequestResolvedParameters as __ΩHttpRequestResolvedParameters } from './model';
15
+ function __assignType(fn, args) {
16
+ fn.__type = args;
17
+ return fn;
18
+ }
19
+ const __ΩPartial = ['T', 'l+e#!e"!fRb!Pde"!gN#"'];
20
+ const __ΩRecord = ['K', 'T', 'l\'e#"Rb!b"Pde"!N#!'];
14
21
  /*
15
22
  * Deepkit Framework
16
23
  * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
@@ -21,10 +28,10 @@ import { __ΩHttpRequestResolvedParameters as __ΩHttpRequestResolvedParameters
21
28
  * You should have received a copy of the MIT License along with this program.
22
29
  */
23
30
  import { asyncOperation, CompilerContext, getClassName, isClass, isObject, urlJoin } from '@deepkit/core';
24
- import { assertType, entity, findMember, getSerializeFunction, getValidatorFunction, metaAnnotation, ReflectionClass, ReflectionParameter, serializer, Serializer, stringifyType, typeToObject, ValidationError } from '@deepkit/type';
31
+ import { assertType, entity, findMember, getSerializeFunction, getValidatorFunction, metaAnnotation, ReflectionClass, ReflectionFunction, ReflectionKind, ReflectionParameter, serializer, Serializer, stringifyType, typeToObject, ValidationError } from '@deepkit/type';
25
32
  // @ts-ignore
26
33
  import formidable from 'formidable';
27
- import { httpClass } from './decorator';
34
+ import { HttpAction, httpClass, HttpController } from './decorator';
28
35
  import { BodyValidationError, getRegExp, HttpRequest, ValidatedBody } from './model';
29
36
  import { InjectorContext, InjectorModule, TagRegistry } from '@deepkit/injector';
30
37
  import { Logger } from '@deepkit/logger';
@@ -33,6 +40,7 @@ import { MiddlewareRegistry } from '@deepkit/app';
33
40
  import { HttpMiddlewareConfig } from './middleware';
34
41
  //@ts-ignore
35
42
  import qs from 'qs';
43
+ import { isArray } from '@deepkit/core';
36
44
  const __ΩRouteParameterResolverForInjector = [() => InjectorContext, 'injector', '', 'PP7!2"P"F"F`J/#'];
37
45
  export { __ΩRouteParameterResolverForInjector as __ΩRouteParameterResolverForInjector };
38
46
  const __ΩResolvedController = [() => __ΩRouteParameterResolverForInjector, 'parameters', () => RouteConfig, 'routeConfig', () => UploadedFile, 'uploadedFiles', () => InjectorContext, 'injector', () => __ΩHttpMiddlewareFn, 'fn', 'timeout', '', 'middlewares', 'Pn!4"P7#4$P&P7%LM4&PP7\'2(Pn)4*\'4+MF/,4-8M'];
@@ -43,12 +51,10 @@ UploadedFile = __decorate([
43
51
  entity.name('@deepkit/UploadedFile')
44
52
  ], UploadedFile);
45
53
  export { UploadedFile };
46
- const __ΩRouteControllerAction = [() => InjectorModule, 'module', () => __ΩClassType, 'controller', 'methodName', 'PP"7!4"8n#4$&4%M'];
47
- export { __ΩRouteControllerAction as __ΩRouteControllerAction };
48
- function getRouterControllerActionName(action) {
49
- return `${getClassName(action.controller)}.${action.methodName}`;
50
- }
51
- getRouterControllerActionName.__type = [() => __ΩRouteControllerAction, 'action', 'getRouterControllerActionName', 'Pn!2"&/#'];
54
+ const __ΩRouteFunctionControllerAction = ["function", 'type', () => InjectorModule, 'module', 'args', '', 'fn', 'P.!4"P"7#4$8P"@2%"/&4\'M'];
55
+ export { __ΩRouteFunctionControllerAction as __ΩRouteFunctionControllerAction };
56
+ const __ΩRouteClassControllerAction = ["controller", 'type', () => InjectorModule, 'module', () => __ΩClassType, 'controller', 'methodName', 'P.!4"P"7#4$8n%4&&4\'M'];
57
+ export { __ΩRouteClassControllerAction as __ΩRouteClassControllerAction };
52
58
  export class RouteConfig {
53
59
  constructor(name, httpMethods, path, action, internal = false) {
54
60
  this.name = name;
@@ -69,6 +75,11 @@ export class RouteConfig {
69
75
  */
70
76
  this.data = new Map();
71
77
  }
78
+ getReflectionFunction() {
79
+ return this.action.type === 'controller' ?
80
+ ReflectionClass.from(this.action.controller).getMethod(this.action.methodName)
81
+ : ReflectionFunction.from(this.action.fn);
82
+ }
72
83
  getSchemaForResponse(statusCode) {
73
84
  if (!this.responses.length)
74
85
  return;
@@ -85,7 +96,7 @@ export class RouteConfig {
85
96
  return path;
86
97
  }
87
98
  }
88
- RouteConfig.__type = ['baseUrl', function () { return ''; }, 'statusCode', 'description', 'type', 'responses', function () { return []; }, function () { return ''; }, 'groups', function () { return []; }, 'category', function () { return ''; }, 'returnType', () => __ΩSerializationOptions, 'serializationOptions', () => Serializer, 'serializer', () => InjectorModule, 'module', () => __ΩClassType, 'resolverForToken', function () { return new Map(); }, () => HttpMiddlewareConfig, 'config', () => InjectorModule, 'middlewares', function () { return []; }, () => __ΩClassType, 'resolverForParameterName', function () { return new Map(); }, 'name', 'httpMethods', 'path', () => __ΩRouteControllerAction, 'action', 'internal', () => false, 'constructor', 'getSchemaForResponse', 'getFullPath', '&3!>"P\'4#&4$"4%8MF3&>\'&3$>(&F3)>*&3+>,"3-8n.3/8P70318P"72338"n4E35>6PP7748P"7943MF3:>;&n<E3=>>P&2?:&F2@:&2A:nB2C:)2D:>E"0FP\'2#P"-J0GP&0H5'];
99
+ RouteConfig.__type = ['baseUrl', function () { return ''; }, 'statusCode', 'description', 'type', 'responses', function () { return []; }, function () { return ''; }, 'groups', function () { return []; }, 'category', function () { return ''; }, 'returnType', () => __ΩSerializationOptions, 'serializationOptions', () => Serializer, 'serializer', () => InjectorModule, 'module', () => __ΩClassType, 'resolverForToken', function () { return new Map(); }, () => HttpMiddlewareConfig, 'config', () => InjectorModule, 'middlewares', function () { return []; }, () => __ΩClassType, 'resolverForParameterName', function () { return new Map(); }, 'name', 'httpMethods', 'path', () => __ΩRouteClassControllerAction, () => __ΩRouteFunctionControllerAction, 'action', 'internal', () => false, 'constructor', () => ReflectionFunction, 'getReflectionFunction', 'getSchemaForResponse', 'getFullPath', '&3!>"P\'4#&4$"4%8MF3&>\'&3$>(&F3)>*&3+>,"3-8n.3/8P70318P"72338"n4E35>6PP7748P"79438MF3:>;&n<E3=>>P&2?:&F2@:&2A:PnBnCJ2D:)2E:>F"0GPP7H0IP\'2#P"-J0JP&0K5'];
89
100
  class ParsedRoute {
90
101
  constructor(routeConfig) {
91
102
  this.routeConfig = routeConfig;
@@ -121,9 +132,9 @@ class ParsedRouteParameter {
121
132
  }
122
133
  getType() {
123
134
  if (this.bodyValidation) {
124
- assertType(this.parameter.type, 20 /* class */);
125
- const valueType = findMember('value', this.parameter.type);
126
- if (!valueType || valueType.kind !== 15 /* property */)
135
+ assertType(this.parameter.type, ReflectionKind.class);
136
+ const valueType = findMember('value', this.parameter.type.types);
137
+ if (!valueType || valueType.kind !== ReflectionKind.property)
127
138
  throw new Error(`No property value found at ${stringifyType(this.parameter.type)}`);
128
139
  return valueType.type;
129
140
  }
@@ -155,26 +166,29 @@ ParsedRouteParameter.__type = ['regexPosition', () => ReflectionParameter, 'para
155
166
  function parseRoutePathToRegex(routeConfig) {
156
167
  const parameterNames = {};
157
168
  let path = routeConfig.getFullPath();
158
- const method = ReflectionClass.from(routeConfig.action.controller).getMethod(routeConfig.action.methodName);
169
+ const fn = routeConfig.getReflectionFunction();
159
170
  let argumentIndex = 0;
160
- path = path.replace(/:(\w+)/g, Object.assign((a, name) => {
171
+ path = path.replace(/:(\w+)/g, __assignType((a, name) => {
161
172
  parameterNames[name] = argumentIndex;
162
173
  argumentIndex++;
163
- const parameter = method.getParameterOrUndefined(name);
174
+ const parameter = fn.getParameterOrUndefined(name);
164
175
  if (parameter) {
165
176
  const regExp = getRegExp(parameter.type);
166
- if (regExp)
177
+ if (regExp instanceof RegExp) {
178
+ return '(' + regExp.source + ')';
179
+ }
180
+ else if (regExp) {
167
181
  return '(' + regExp + ')';
182
+ }
168
183
  }
169
184
  return String.raw `([^/]+)`;
170
- }, { __type: ['a', 'name', '', 'P"2!"2""/#'] }));
185
+ }, ['a', 'name', '', 'P"2!"2""/#']));
171
186
  return { regex: path, parameterNames };
172
187
  }
173
188
  parseRoutePathToRegex.__type = [() => RouteConfig, 'routeConfig', 'regex', 'parameterNames', 'parseRoutePathToRegex', 'PP7!2"P&4#P&\'LM4$M/%'];
174
189
  export function parseRouteControllerAction(routeConfig) {
175
- const schema = ReflectionClass.from(routeConfig.action.controller);
176
190
  const parsedRoute = new ParsedRoute(routeConfig);
177
- const methodArgumentProperties = schema.getMethodParameters(routeConfig.action.methodName);
191
+ const methodArgumentProperties = routeConfig.getReflectionFunction().getParameters();
178
192
  const parsedPath = parseRoutePathToRegex(routeConfig);
179
193
  parsedRoute.regex = parsedPath.regex;
180
194
  parsedRoute.pathParameterNames = parsedPath.parameterNames;
@@ -198,14 +212,14 @@ export { __ΩRouteParameterResolverContext as __ΩRouteParameterResolverContext
198
212
  function filterMiddlewaresForRoute(middlewareRawConfigs, routeConfig, fullPath) {
199
213
  const middlewares = middlewareRawConfigs.slice(0);
200
214
  middlewares.push(...routeConfig.middlewares);
201
- const middlewareConfigs = middlewares.filter(Object.assign((v) => {
215
+ const middlewareConfigs = middlewares.filter(__assignType((v) => {
202
216
  var _a;
203
217
  if (!(v.config instanceof HttpMiddlewareConfig))
204
218
  return false;
205
- if (v.config.controllers.length && !v.config.controllers.includes(routeConfig.action.controller)) {
219
+ if (v.config.controllers.length && routeConfig.action.type === 'controller' && !v.config.controllers.includes(routeConfig.action.controller)) {
206
220
  return false;
207
221
  }
208
- if (v.config.excludeControllers.length && v.config.excludeControllers.includes(routeConfig.action.controller)) {
222
+ if (v.config.excludeControllers.length && routeConfig.action.type === 'controller' && v.config.excludeControllers.includes(routeConfig.action.controller)) {
209
223
  return false;
210
224
  }
211
225
  if (v.config.modules.length && (!routeConfig.module || !v.config.modules.includes(routeConfig.module))) {
@@ -219,16 +233,22 @@ function filterMiddlewaresForRoute(middlewareRawConfigs, routeConfig, fullPath)
219
233
  if (v.config.selfModule && v.module.id !== ((_a = routeConfig.module) === null || _a === void 0 ? void 0 : _a.id))
220
234
  return false;
221
235
  if (v.config.routeNames.length) {
236
+ let found = false;
222
237
  for (const name of v.config.routeNames) {
223
238
  if (name.includes('*')) {
224
239
  const regex = new RegExp('^' + name.replace(/\*/g, '.*') + '$');
225
- if (!regex.test(routeConfig.name))
226
- return false;
240
+ if (regex.test(routeConfig.name)) {
241
+ found = true;
242
+ break;
243
+ }
227
244
  }
228
- else if (name !== routeConfig.name) {
229
- return false;
245
+ else if (name === routeConfig.name) {
246
+ found = true;
247
+ break;
230
248
  }
231
249
  }
250
+ if (!found)
251
+ return false;
232
252
  }
233
253
  if (v.config.excludeRouteNames.length) {
234
254
  for (const name of v.config.excludeRouteNames) {
@@ -261,50 +281,251 @@ function filterMiddlewaresForRoute(middlewareRawConfigs, routeConfig, fullPath)
261
281
  }
262
282
  }
263
283
  return true;
264
- }, { __type: ['v', '', 'P"2!"/"'] }));
265
- middlewareConfigs.sort(Object.assign((a, b) => {
284
+ }, ['v', '', 'P"2!"/"']));
285
+ middlewareConfigs.sort(__assignType((a, b) => {
266
286
  return a.config.order - b.config.order;
267
- }, { __type: ['a', 'b', '', 'P"2!"2""/#'] }));
287
+ }, ['a', 'b', '', 'P"2!"2""/#']));
268
288
  return middlewareConfigs;
269
289
  }
270
290
  filterMiddlewaresForRoute.__type = [() => __ΩMiddlewareRegistryEntry, 'middlewareRawConfigs', () => RouteConfig, 'routeConfig', 'fullPath', () => HttpMiddlewareConfig, 'config', () => InjectorModule, 'module', 'filterMiddlewaresForRoute', 'Pn!F2"P7#2$&2%PP7&4\'P"7(4)MF/*'];
271
- export class Router {
272
- constructor(controllers, logger, tagRegistry, middlewareRegistry = new MiddlewareRegistry) {
291
+ const __ΩHttpRouterFunctionOptions = ['path', 'name', 'methods', 'description', 'category', 'groups', () => __ΩRecord, 'data', 'baseUrl', () => HttpMiddlewareConfig, '', 'middlewares', () => Serializer, 'serializer', () => __ΩSerializationOptions, 'serializationOptions', () => __ΩClassType, 'resolverForToken', () => __ΩClassType, 'resolverForParameterName', 'statusCode', 'type', 'responses', 'P&4!&4"8&F4#8&4$8&4%8&F4&8""o\'#4(8&4)8PP7*/+F4,8P7-4.8n/408"n1E428&n3E448P\'45&4$"468MF478M'];
292
+ export { __ΩHttpRouterFunctionOptions as __ΩHttpRouterFunctionOptions };
293
+ function convertOptions(methods, pathOrOptions, defaultOptions) {
294
+ const options = 'string' === typeof pathOrOptions ? { path: pathOrOptions } : pathOrOptions;
295
+ if (options.methods)
296
+ return options;
297
+ return { ...options, methods };
298
+ }
299
+ convertOptions.__type = ['methods', () => __ΩHttpRouterFunctionOptions, 'pathOrOptions', () => __ΩPartial, () => __ΩHttpRouterFunctionOptions, 'defaultOptions', () => __ΩHttpRouterFunctionOptions, 'convertOptions', 'P&F2!P&n"J2#n%o$"2&n\'/('];
300
+ export class HttpRouterRegistryFunctionRegistrar {
301
+ constructor() {
302
+ this.defaultOptions = {};
303
+ }
304
+ /**
305
+ * Returns a new registrar object with default options that apply to each registered route through this registrar.
306
+ *
307
+ * ```typescript
308
+ * const registry: HttpRouterRegistry = ...;
309
+ *
310
+ * const secretRegistry = registry.forOptions({groups: ['secret']});
311
+ *
312
+ * secretRegistry.get('/admin/groups', () => {
313
+ * });
314
+ *
315
+ * secretRegistry.get('/admin/users', () => {
316
+ * });
317
+ * ```
318
+ */
319
+ forOptions(options) {
320
+ var _a;
321
+ const that = this;
322
+ return new (_a = class extends HttpRouterRegistryFunctionRegistrar {
323
+ constructor() {
324
+ super(...arguments);
325
+ this.defaultOptions = options;
326
+ }
327
+ addRoute(routeConfig) {
328
+ that.addRoute(routeConfig);
329
+ }
330
+ },
331
+ _a.__type = [() => HttpRouterRegistryFunctionRegistrar, () => RouteConfig, 'routeConfig', 'addRoute', 'P7!PP7"2#"0$5'],
332
+ _a);
333
+ }
334
+ any(pathOrOptions, callback) {
335
+ this.register(convertOptions([], pathOrOptions, this.defaultOptions), callback);
336
+ }
337
+ add(decorator, callback) {
338
+ const data = decorator(Object, '_');
339
+ const action = isArray(data) ? data.find(__assignType(v => v instanceof HttpAction, ['v', '', 'P"2!"/"'])) : undefined;
340
+ if (!action)
341
+ throw new Error('No HttpAction available');
342
+ const fn = ReflectionFunction.from(callback);
343
+ const routeConfig = createRouteConfigFromHttpAction({
344
+ type: 'function',
345
+ fn: callback,
346
+ }, action);
347
+ routeConfig.returnType = fn.getReturnType();
348
+ this.addRoute(routeConfig);
349
+ }
350
+ get(pathOrOptions, callback) {
351
+ this.register(convertOptions(['GET'], pathOrOptions, this.defaultOptions), callback);
352
+ }
353
+ post(pathOrOptions, callback) {
354
+ this.register(convertOptions(['POST'], pathOrOptions, this.defaultOptions), callback);
355
+ }
356
+ put(pathOrOptions, callback) {
357
+ this.register(convertOptions(['PUT'], pathOrOptions, this.defaultOptions), callback);
358
+ }
359
+ patch(pathOrOptions, callback) {
360
+ this.register(convertOptions(['PATCH'], pathOrOptions, this.defaultOptions), callback);
361
+ }
362
+ delete(pathOrOptions, callback) {
363
+ this.register(convertOptions(['DELETE'], pathOrOptions, this.defaultOptions), callback);
364
+ }
365
+ options(pathOrOptions, callback) {
366
+ this.register(convertOptions(['OPTIONS'], pathOrOptions, this.defaultOptions), callback);
367
+ }
368
+ trace(pathOrOptions, callback) {
369
+ this.register(convertOptions(['TRACE'], pathOrOptions, this.defaultOptions), callback);
370
+ }
371
+ head(pathOrOptions, callback) {
372
+ this.register(convertOptions(['HEAD'], pathOrOptions, this.defaultOptions), callback);
373
+ }
374
+ register(options, callback, module) {
375
+ const fn = ReflectionFunction.from(callback);
376
+ const routeConfig = new RouteConfig(options.name || '', options.methods || [], options.path, {
377
+ type: 'function',
378
+ fn: callback,
379
+ });
380
+ routeConfig.module = module;
381
+ if (options.responses)
382
+ routeConfig.responses = options.responses;
383
+ if (options.description)
384
+ routeConfig.description = options.description;
385
+ if (options.category)
386
+ routeConfig.category = options.category;
387
+ if (options.groups)
388
+ routeConfig.groups = options.groups;
389
+ if (options.data)
390
+ routeConfig.data = new Map(Object.entries(options.data));
391
+ if (options.baseUrl)
392
+ routeConfig.baseUrl = options.baseUrl;
393
+ if (options.middlewares) {
394
+ routeConfig.middlewares = options.middlewares.map(__assignType(v => {
395
+ return { config: v(), module };
396
+ }, ['v', '', 'P"2!"/"']));
397
+ }
398
+ if (options.resolverForToken) {
399
+ for (const item of options.resolverForToken)
400
+ routeConfig.resolverForToken.set(...item);
401
+ }
402
+ if (options.resolverForToken) {
403
+ for (const item of options.resolverForToken)
404
+ routeConfig.resolverForToken.set(...item);
405
+ }
406
+ if (options.resolverForParameterName) {
407
+ for (const item of options.resolverForParameterName)
408
+ routeConfig.resolverForParameterName.set(...item);
409
+ }
410
+ routeConfig.serializer = options.serializer;
411
+ routeConfig.serializationOptions = options.serializationOptions;
412
+ routeConfig.returnType = fn.getReturnType();
413
+ this.addRoute(routeConfig);
414
+ }
415
+ }
416
+ HttpRouterRegistryFunctionRegistrar.__type = [() => __ΩPartial, () => __ΩHttpRouterFunctionOptions, 'defaultOptions', function () { return {}; }, () => RouteConfig, 'routeConfig', 'addRoute', () => __ΩPartial, () => __ΩHttpRouterFunctionOptions, 'options', () => HttpRouterRegistryFunctionRegistrar, 'forOptions', () => __ΩHttpRouterFunctionOptions, 'pathOrOptions', 'args', '', 'callback', 'any', () => __ΩHttpDecorator, 'decorator', 'add', () => __ΩHttpRouterFunctionOptions, 'get', () => __ΩHttpRouterFunctionOptions, 'post', () => __ΩHttpRouterFunctionOptions, 'put', () => __ΩHttpRouterFunctionOptions, 'patch', () => __ΩHttpRouterFunctionOptions, 'delete', () => __ΩHttpRouterFunctionOptions, () => __ΩHttpRouterFunctionOptions, 'trace', () => __ΩHttpRouterFunctionOptions, 'head', () => __ΩHttpRouterFunctionOptions, () => InjectorModule, 'module', 'register', 'n"o!"3#<>$PP7%2&$0\'=Pn)o("2*P7+0,PP&n-J2.P"@2/"/021"02Pn324P"@2/"/021"05PP&n6J2.P"@2/"/021"07PP&n8J2.P"@2/"/021"09PP&n:J2.P"@2/"/021"0;PP&n<J2.P"@2/"/021"0=PP&n>J2.P"@2/"/021"0?PP&n@J2.P"@2/"/021"0*PP&nAJ2.P"@2/"/021"0BPP&nCJ2.P"@2/"/021"0DPnE2*P"@2/"/021P"7F2G8"0H;5'];
417
+ function createRouteConfigFromHttpAction(routeAction, action, module, controller) {
418
+ const routeConfig = new RouteConfig(action.name, action.httpMethods, action.path, routeAction);
419
+ routeConfig.responses = action.responses;
420
+ routeConfig.description = action.description;
421
+ routeConfig.category = action.category;
422
+ routeConfig.groups = action.groups;
423
+ routeConfig.data = new Map(action.data);
424
+ if (controller) {
425
+ routeConfig.baseUrl = controller.baseUrl;
426
+ routeConfig.middlewares = controller.middlewares.map(__assignType(v => {
427
+ return { config: v(), module };
428
+ }, ['v', '', 'P"2!"/"']));
429
+ routeConfig.resolverForToken = new Map(controller.resolverForToken);
430
+ routeConfig.resolverForParameterName = new Map(controller.resolverForParameterName);
431
+ }
432
+ routeConfig.middlewares.push(...action.middlewares.map(__assignType(v => {
433
+ return { config: v(), module };
434
+ }, ['v', '', 'P"2!"/"'])));
435
+ for (const item of action.resolverForToken)
436
+ routeConfig.resolverForToken.set(...item);
437
+ for (const item of action.resolverForParameterName)
438
+ routeConfig.resolverForParameterName.set(...item);
439
+ routeConfig.serializer = action.serializer;
440
+ routeConfig.serializationOptions = action.serializationOptions;
441
+ return routeConfig;
442
+ }
443
+ createRouteConfigFromHttpAction.__type = [() => __ΩRouteClassControllerAction, () => __ΩRouteFunctionControllerAction, 'routeAction', () => HttpAction, 'action', () => InjectorModule, 'module', () => HttpController, 'controller', 'createRouteConfigFromHttpAction', 'PPn!n"J2#P7$2%P"7&2\'8P7(2)8"/*'];
444
+ export class HttpRouterRegistry extends HttpRouterRegistryFunctionRegistrar {
445
+ constructor() {
446
+ super(...arguments);
447
+ this.routes = [];
448
+ this.buildId = 1;
449
+ }
450
+ getBuildId() {
451
+ return this.buildId;
452
+ }
453
+ getRoutes() {
454
+ return this.routes;
455
+ }
456
+ addRouteForController(controller, module) {
457
+ const controllerData = httpClass._fetch(controller);
458
+ if (!controllerData)
459
+ throw new Error(`Http controller class ${getClassName(controller)} has no @http.controller decorator.`);
460
+ const schema = ReflectionClass.from(controller);
461
+ for (const action of controllerData.getActions()) {
462
+ const routeAction = {
463
+ type: 'controller',
464
+ controller,
465
+ module,
466
+ methodName: action.methodName
467
+ };
468
+ const routeConfig = createRouteConfigFromHttpAction(routeAction, action, module, controllerData);
469
+ routeConfig.module = module;
470
+ if (schema.hasMethod(action.methodName))
471
+ routeConfig.returnType = schema.getMethod(action.methodName).getReturnType();
472
+ this.addRoute(routeConfig);
473
+ }
474
+ }
475
+ addRoute(routeConfig) {
476
+ this.routes.push(routeConfig);
477
+ this.buildId++;
478
+ }
479
+ }
480
+ HttpRouterRegistry.__type = [() => HttpRouterRegistryFunctionRegistrar, () => RouteConfig, 'routes', function () { return []; }, 'buildId', function () { return 1; }, 'getBuildId', () => RouteConfig, 'getRoutes', () => __ΩClassType, 'controller', () => InjectorModule, 'module', 'addRouteForController', () => RouteConfig, 'routeConfig', 'addRoute', 'P7!P7"F3#<>$\'3%;>&P\'0\'PP7(F0)Pn*2+P"7,2-"0.PP7/20"015'];
481
+ export class HttpRouter {
482
+ constructor(controllers, logger, tagRegistry, middlewareRegistry = new MiddlewareRegistry, registry = new HttpRouterRegistry) {
273
483
  this.logger = logger;
274
484
  this.middlewareRegistry = middlewareRegistry;
275
- this.routes = [];
485
+ this.registry = registry;
486
+ this.buildId = 0;
276
487
  for (const controller of controllers.controllers) {
277
488
  this.addRouteForController(controller.controller, controller.module);
278
489
  }
279
490
  }
280
- parseBody(req, files) {
491
+ parseBody(req, foundFiles) {
281
492
  const form = formidable({
282
493
  multiples: true,
283
494
  hash: 'sha1',
284
- enabledPlugins: ['octetstream', 'querystring', 'json'],
285
495
  });
286
- return asyncOperation(Object.assign((resolve, reject) => {
496
+ return asyncOperation(__assignType((resolve, reject) => {
287
497
  if (req.body) {
288
498
  return resolve(req.body);
289
499
  }
290
- form.parse(req, Object.assign((err, fields, files) => {
500
+ form.parse(req, __assignType((err, fields, files) => {
291
501
  if (err) {
292
502
  reject(err);
293
503
  }
294
504
  else {
295
- const body = req.body = { ...fields, ...files };
505
+ for (const [name, file] of Object.entries(files)) {
506
+ if (file.size === 0)
507
+ continue;
508
+ foundFiles[name] = {
509
+ size: file.size,
510
+ path: file.filepath,
511
+ name: file.originalFilename,
512
+ type: file.mimetype,
513
+ lastModifiedDate: file.lastModifiedDate,
514
+ };
515
+ }
516
+ const body = req.body = { ...fields, ...foundFiles };
296
517
  resolve(body);
297
518
  }
298
- }, { __type: ['err', 'fields', 'files', '', 'P"2!"2""2#"/$'] }));
299
- }, { __type: ['resolve', 'reject', '', 'P"2!"2""/#'] }));
519
+ }, ['err', 'fields', 'files', '', 'P"2!"2""2#"/$']));
520
+ }, ['resolve', 'reject', '', 'P"2!"2""/#']));
300
521
  }
301
522
  getRoutes() {
302
- return this.routes;
523
+ return this.registry.getRoutes();
303
524
  }
304
525
  static forControllers(controllers, tagRegistry = new TagRegistry(), middlewareRegistry = new MiddlewareRegistry(), module = new InjectorModule()) {
305
- return new this(new HttpControllers(controllers.map(Object.assign(v => {
526
+ return new this(new HttpControllers(controllers.map(__assignType(v => {
306
527
  return isClass(v) ? { controller: v, module } : v;
307
- }, { __type: ['v', '', 'P"2!"/"'] }))), new Logger([], []), tagRegistry, middlewareRegistry);
528
+ }, ['v', '', 'P"2!"/"']))), new Logger([], []), tagRegistry, middlewareRegistry);
308
529
  }
309
530
  getRouteCode(compiler, routeConfig) {
310
531
  const routeConfigVar = compiler.reserveVariable('routeConfigVar', routeConfig);
@@ -355,7 +576,7 @@ export class Router {
355
576
  else {
356
577
  parameterNames.push(`parameters.${parameter.parameter.name}`);
357
578
  if (parameter.isPartOfPath()) {
358
- if (parameter.parameter.type.kind !== 20 /* class */) {
579
+ if (parameter.parameter.type.kind !== ReflectionKind.class) {
359
580
  const converted = getSerializeFunction(parameter.parameter.parameter, serializer.deserializeRegistry, undefined, parameter.getName());
360
581
  const converterVar = compiler.reserveVariable('argumentConverter', converted);
361
582
  setParameters.push(`parameters.${parameter.parameter.name} = ${converterVar}(_match[${1 + (parameter.regexPosition || 0)}], {loosely: true});`);
@@ -367,7 +588,7 @@ export class Router {
367
588
  setParameters.push(`parameters.${parameter.parameter.name} = _match[${1 + (parameter.regexPosition || 0)}];`);
368
589
  }
369
590
  }
370
- const injectorToken = parameter.parameter.type.kind === 20 /* class */ ? parameter.parameter.type.classType : undefined;
591
+ const injectorToken = parameter.parameter.type.kind === ReflectionKind.class ? parameter.parameter.type.classType : undefined;
371
592
  const injectorTokenVar = compiler.reserveVariable('classType', injectorToken);
372
593
  const parameterResolverFoundVar = compiler.reserveVariable('parameterResolverFound', false);
373
594
  setParameters.push(`${parameterResolverFoundVar} = false;`);
@@ -391,7 +612,7 @@ export class Router {
391
612
  ${parameterResolverFoundVar} = true;
392
613
  parameters.${parameter.parameter.name} = await ${instance}.resolve({
393
614
  token: ${injectorTokenVar},
394
- routeConfig: ${routeConfigVar},
615
+ route: ${routeConfigVar},
395
616
  request: request,
396
617
  name: ${JSON.stringify(parameter.parameter.name)},
397
618
  value: parameters.${parameter.parameter.name},
@@ -458,9 +679,9 @@ export class Router {
458
679
  }
459
680
  let methodCheck = '';
460
681
  if (routeConfig.httpMethods.length) {
461
- methodCheck = '(' + routeConfig.httpMethods.map(Object.assign(v => {
682
+ methodCheck = '(' + routeConfig.httpMethods.map(__assignType(v => {
462
683
  return `_method === '${v.toUpperCase()}'`;
463
- }, { __type: ['v', '', 'P"2!"/"'] })).join(' || ') + ') && ';
684
+ }, ['v', '', 'P"2!"/"'])).join(' || ') + ') && ';
464
685
  }
465
686
  return `
466
687
  //=> ${routeConfig.httpMethods.join(',')} ${path}
@@ -472,14 +693,14 @@ export class Router {
472
693
  getRouteUrlResolveCode(compiler, routeConfig) {
473
694
  const parsedRoute = parseRouteControllerAction(routeConfig);
474
695
  let url = routeConfig.getFullPath();
475
- url = url.replace(/:(\w+)/g, Object.assign((a, name) => {
696
+ url = url.replace(/:(\w+)/g, __assignType((a, name) => {
476
697
  return `\${parameters.${name}}`;
477
- }, { __type: ['a', 'name', '', 'P"2!"2""/#'] }));
698
+ }, ['a', 'name', '', 'P"2!"2""/#']));
478
699
  const modify = [];
479
700
  for (const parameter of parsedRoute.getParameters()) {
480
701
  if (parameter.query || parameter.queries) {
481
702
  const queryPath = parameter.typePath === undefined && parameter.query ? parameter.parameter.name : parameter.typePath || '';
482
- if (parameter.parameter.type.kind === 20 /* class */ || parameter.parameter.type.kind === 30 /* objectLiteral */) {
703
+ if (parameter.parameter.type.kind === ReflectionKind.class || parameter.parameter.type.kind === ReflectionKind.objectLiteral) {
483
704
  for (const property of ReflectionClass.from(parameter.parameter.type).getProperties()) {
484
705
  const accessor = `parameters.${parameter.getName()}?.${property.name}`;
485
706
  const thisPath = queryPath ? queryPath + '.' + property.name : property.name;
@@ -501,55 +722,18 @@ export class Router {
501
722
  `;
502
723
  }
503
724
  addRoute(routeConfig) {
504
- this.routes.push(routeConfig);
505
- this.fn = undefined;
725
+ this.registry.addRoute(routeConfig);
506
726
  }
507
727
  addRouteForController(controller, module) {
508
- const data = httpClass._fetch(controller);
509
- if (!data)
510
- throw new Error(`Http controller class ${getClassName(controller)} has no @http.controller decorator.`);
511
- const schema = ReflectionClass.from(controller);
512
- for (const action of data.getActions()) {
513
- const routeConfig = new RouteConfig(action.name, action.httpMethods, action.path, {
514
- controller,
515
- module,
516
- methodName: action.methodName
517
- });
518
- routeConfig.module = module;
519
- routeConfig.responses = action.responses;
520
- routeConfig.description = action.description;
521
- routeConfig.category = action.category;
522
- routeConfig.groups = action.groups;
523
- routeConfig.data = new Map(action.data);
524
- routeConfig.baseUrl = data.baseUrl;
525
- routeConfig.middlewares = data.middlewares.map(Object.assign(v => {
526
- return { config: v(), module };
527
- }, { __type: ['v', '', 'P"2!"/"'] }));
528
- routeConfig.middlewares.push(...action.middlewares.map(Object.assign(v => {
529
- return { config: v(), module };
530
- }, { __type: ['v', '', 'P"2!"/"'] })));
531
- for (const item of action.resolverForToken)
532
- routeConfig.resolverForToken.set(...item);
533
- routeConfig.resolverForToken = new Map(data.resolverForToken);
534
- for (const item of action.resolverForToken)
535
- routeConfig.resolverForToken.set(...item);
536
- routeConfig.resolverForParameterName = new Map(data.resolverForParameterName);
537
- for (const item of action.resolverForParameterName)
538
- routeConfig.resolverForParameterName.set(...item);
539
- routeConfig.serializationOptions = action.serializationOptions;
540
- routeConfig.serializer = action.serializer;
541
- routeConfig.serializer = action.serializer;
542
- if (schema.hasMethod(action.methodName))
543
- routeConfig.returnType = schema.getMethod(action.methodName).getReturnType();
544
- this.addRoute(routeConfig);
545
- }
728
+ this.registry.addRouteForController(controller, module);
546
729
  }
547
730
  build() {
731
+ this.buildId = this.registry.getBuildId();
548
732
  const compiler = new CompilerContext;
549
733
  compiler.context.set('ValidationError', ValidationError);
550
734
  compiler.context.set('qs', qs);
551
735
  const code = [];
552
- for (const route of this.routes) {
736
+ for (const route of this.getRoutes()) {
553
737
  code.push(this.getRouteCode(compiler, route));
554
738
  }
555
739
  return compiler.build(`
@@ -566,7 +750,7 @@ export class Router {
566
750
  buildUrlResolver() {
567
751
  const compiler = new CompilerContext;
568
752
  const code = [];
569
- for (const route of this.routes) {
753
+ for (const route of this.getRoutes()) {
570
754
  code.push(this.getRouteUrlResolveCode(compiler, route));
571
755
  }
572
756
  return compiler.build(`
@@ -583,7 +767,7 @@ export class Router {
583
767
  return this.resolveFn(routeName, parameters);
584
768
  }
585
769
  resolveRequest(request) {
586
- if (!this.fn) {
770
+ if (!this.fn || this.buildId !== this.registry.getBuildId()) {
587
771
  this.fn = this.build();
588
772
  }
589
773
  return this.fn(request);
@@ -593,5 +777,5 @@ export class Router {
593
777
  return this.resolveRequest({ url, method });
594
778
  }
595
779
  }
596
- Router.__type = [() => HttpRequest, 'request', () => __ΩResolvedController, '', 'fn', 'name', 'parameters', 'resolveFn', () => RouteConfig, 'routes', function () { return []; }, () => HttpRequest, 'req', () => UploadedFile, 'files', 'parseBody', () => HttpControllers, 'controllers', () => __ΩLoggerInterface, 'logger', () => TagRegistry, 'tagRegistry', () => MiddlewareRegistry, 'middlewareRegistry', () => new MiddlewareRegistry, 'constructor', () => RouteConfig, 'getRoutes', () => __ΩClassType, () => InjectorModule, 'module', () => __ΩClassType, 'controller', () => TagRegistry, () => new TagRegistry(), () => MiddlewareRegistry, () => new MiddlewareRegistry(), () => InjectorModule, () => new InjectorModule(), () => Router, 'forControllers', () => CompilerContext, 'compiler', () => RouteConfig, 'routeConfig', 'getRouteCode', () => CompilerContext, () => RouteConfig, 'getRouteUrlResolveCode', () => RouteConfig, 'addRoute', () => __ΩClassType, () => InjectorModule, 'addRouteForController', () => HttpRequest, () => __ΩResolvedController, 'build', 'buildUrlResolver', 'routeName', () => ({}), 'resolveUrl', () => HttpRequest, () => __ΩResolvedController, 'resolveRequest', 'method', 'url', () => __ΩResolvedController, 'resolve', 'PP7!2"Pn#-J/$3%8<P&2&P&"LM2\'&/$3(8<P7)F3*<>+PP7,2-P&P7.LM2/"00;PP7122n324;P7526P7728;>9"0:PP7;F0<PPn=PP"7>4?n@4AMJF22P7B26>CP7D28>EP"7F2?>GP7H0IPP7J2KP7L2M&0N<PP7O2KP7P2M&0Q<PP7R2M"0SPnT2AP"7U2?"0VPPP7W2"PnX-J/$0Y<P"0Z<P&2[P&"LM2\'>\\&0]PP7^2"Pn_-J0`P&2a&2bPnc-J0d5'];
780
+ HttpRouter.__type = [() => HttpRequest, 'request', () => __ΩResolvedController, '', 'fn', 'buildId', function () { return 0; }, 'name', 'parameters', 'resolveFn', () => HttpRequest, 'req', () => UploadedFile, 'foundFiles', 'parseBody', () => HttpControllers, 'controllers', () => __ΩLoggerInterface, 'logger', () => TagRegistry, 'tagRegistry', () => MiddlewareRegistry, 'middlewareRegistry', () => new MiddlewareRegistry, () => HttpRouterRegistry, 'registry', () => new HttpRouterRegistry, 'constructor', () => RouteConfig, 'getRoutes', () => __ΩClassType, () => InjectorModule, 'module', () => __ΩClassType, 'controller', () => TagRegistry, () => new TagRegistry(), () => MiddlewareRegistry, () => new MiddlewareRegistry(), () => InjectorModule, () => new InjectorModule(), () => HttpRouter, 'forControllers', () => CompilerContext, 'compiler', () => RouteConfig, 'routeConfig', 'getRouteCode', () => CompilerContext, () => RouteConfig, 'getRouteUrlResolveCode', () => RouteConfig, 'addRoute', () => __ΩClassType, () => InjectorModule, 'addRouteForController', () => HttpRequest, () => __ΩResolvedController, 'build', 'buildUrlResolver', 'routeName', () => ({}), 'resolveUrl', () => HttpRequest, () => __ΩResolvedController, 'resolveRequest', 'method', 'url', () => __ΩResolvedController, 'resolve', 'PP7!2"Pn#-J/$3%8<\'3&<>\'P&2(P&"LM2)&/$3*8<PP7+2,P&P7-LM2."0/;PP7021n223;P7425P7627;>8P792:;>;"0<PP7=F0>PPn?PP"7@4AnB4CMJF21P7D25>EP7F27>GP"7H2A>IP7J0KPP7L2MP7N2O&0P<PP7Q2MP7R2O&0S<PP7T2O"0UPnV2CP"7W2A"0XPPP7Y2"PnZ-J/$0[<P"0\\<P&2]P&"LM2)>^&0_PP7`2"Pna-J0bP&2c&2dPne-J0f5'];
597
781
  //# sourceMappingURL=router.js.map