@deepkit/http 1.0.1-alpha.100

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/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/dist/.gitkeep +0 -0
  4. package/dist/cjs/index.d.ts +14 -0
  5. package/dist/cjs/index.js +31 -0
  6. package/dist/cjs/index.js.map +1 -0
  7. package/dist/cjs/src/controllers.d.ts +13 -0
  8. package/dist/cjs/src/controllers.js +17 -0
  9. package/dist/cjs/src/controllers.js.map +1 -0
  10. package/dist/cjs/src/decorator.d.ts +241 -0
  11. package/dist/cjs/src/decorator.js +422 -0
  12. package/dist/cjs/src/decorator.js.map +1 -0
  13. package/dist/cjs/src/filter.d.ts +49 -0
  14. package/dist/cjs/src/filter.js +215 -0
  15. package/dist/cjs/src/filter.js.map +1 -0
  16. package/dist/cjs/src/http-app.d.ts +8 -0
  17. package/dist/cjs/src/http-app.js +16 -0
  18. package/dist/cjs/src/http-app.js.map +1 -0
  19. package/dist/cjs/src/http.d.ts +278 -0
  20. package/dist/cjs/src/http.js +878 -0
  21. package/dist/cjs/src/http.js.map +1 -0
  22. package/dist/cjs/src/kernel.d.ts +16 -0
  23. package/dist/cjs/src/kernel.js +79 -0
  24. package/dist/cjs/src/kernel.js.map +1 -0
  25. package/dist/cjs/src/logger.d.ts +7 -0
  26. package/dist/cjs/src/logger.js +34 -0
  27. package/dist/cjs/src/logger.js.map +1 -0
  28. package/dist/cjs/src/middleware.d.ts +61 -0
  29. package/dist/cjs/src/middleware.js +95 -0
  30. package/dist/cjs/src/middleware.js.map +1 -0
  31. package/dist/cjs/src/model.d.ts +165 -0
  32. package/dist/cjs/src/model.js +398 -0
  33. package/dist/cjs/src/model.js.map +1 -0
  34. package/dist/cjs/src/module.config.d.ts +84 -0
  35. package/dist/cjs/src/module.config.js +14 -0
  36. package/dist/cjs/src/module.config.js.map +1 -0
  37. package/dist/cjs/src/module.d.ts +14 -0
  38. package/dist/cjs/src/module.js +124 -0
  39. package/dist/cjs/src/module.js.map +1 -0
  40. package/dist/cjs/src/request-parser.d.ts +36 -0
  41. package/dist/cjs/src/request-parser.js +305 -0
  42. package/dist/cjs/src/request-parser.js.map +1 -0
  43. package/dist/cjs/src/router.d.ts +230 -0
  44. package/dist/cjs/src/router.js +617 -0
  45. package/dist/cjs/src/router.js.map +1 -0
  46. package/dist/cjs/src/static-serving.d.ts +32 -0
  47. package/dist/cjs/src/static-serving.js +157 -0
  48. package/dist/cjs/src/static-serving.js.map +1 -0
  49. package/dist/cjs/src/utils.d.ts +1 -0
  50. package/dist/cjs/src/utils.js +13 -0
  51. package/dist/cjs/src/utils.js.map +1 -0
  52. package/dist/cjs/tsconfig.tsbuildinfo +1 -0
  53. package/dist/esm/index.d.ts +14 -0
  54. package/dist/esm/index.js +15 -0
  55. package/dist/esm/index.js.map +1 -0
  56. package/dist/esm/package.json +1 -0
  57. package/dist/esm/src/controllers.d.ts +13 -0
  58. package/dist/esm/src/controllers.js +13 -0
  59. package/dist/esm/src/controllers.js.map +1 -0
  60. package/dist/esm/src/decorator.d.ts +241 -0
  61. package/dist/esm/src/decorator.js +414 -0
  62. package/dist/esm/src/decorator.js.map +1 -0
  63. package/dist/esm/src/filter.d.ts +49 -0
  64. package/dist/esm/src/filter.js +209 -0
  65. package/dist/esm/src/filter.js.map +1 -0
  66. package/dist/esm/src/http-app.d.ts +8 -0
  67. package/dist/esm/src/http-app.js +12 -0
  68. package/dist/esm/src/http-app.js.map +1 -0
  69. package/dist/esm/src/http.d.ts +278 -0
  70. package/dist/esm/src/http.js +841 -0
  71. package/dist/esm/src/http.js.map +1 -0
  72. package/dist/esm/src/kernel.d.ts +16 -0
  73. package/dist/esm/src/kernel.js +75 -0
  74. package/dist/esm/src/kernel.js.map +1 -0
  75. package/dist/esm/src/logger.d.ts +7 -0
  76. package/dist/esm/src/logger.js +30 -0
  77. package/dist/esm/src/logger.js.map +1 -0
  78. package/dist/esm/src/middleware.d.ts +61 -0
  79. package/dist/esm/src/middleware.js +90 -0
  80. package/dist/esm/src/middleware.js.map +1 -0
  81. package/dist/esm/src/model.d.ts +165 -0
  82. package/dist/esm/src/model.js +363 -0
  83. package/dist/esm/src/model.js.map +1 -0
  84. package/dist/esm/src/module.config.d.ts +84 -0
  85. package/dist/esm/src/module.config.js +10 -0
  86. package/dist/esm/src/module.config.js.map +1 -0
  87. package/dist/esm/src/module.d.ts +14 -0
  88. package/dist/esm/src/module.js +120 -0
  89. package/dist/esm/src/module.js.map +1 -0
  90. package/dist/esm/src/request-parser.d.ts +36 -0
  91. package/dist/esm/src/request-parser.js +295 -0
  92. package/dist/esm/src/request-parser.js.map +1 -0
  93. package/dist/esm/src/router.d.ts +230 -0
  94. package/dist/esm/src/router.js +605 -0
  95. package/dist/esm/src/router.js.map +1 -0
  96. package/dist/esm/src/static-serving.d.ts +32 -0
  97. package/dist/esm/src/static-serving.js +149 -0
  98. package/dist/esm/src/static-serving.js.map +1 -0
  99. package/dist/esm/src/utils.d.ts +1 -0
  100. package/dist/esm/src/utils.js +9 -0
  101. package/dist/esm/src/utils.js.map +1 -0
  102. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -0
  103. package/index.ts +14 -0
  104. package/package.json +67 -0
  105. package/src/controllers.ts +11 -0
  106. package/src/decorator.ts +411 -0
  107. package/src/filter.ts +224 -0
  108. package/src/http-app.ts +11 -0
  109. package/src/http.ts +828 -0
  110. package/src/kernel.ts +79 -0
  111. package/src/logger.ts +20 -0
  112. package/src/middleware.ts +116 -0
  113. package/src/model.ts +392 -0
  114. package/src/module.config.ts +95 -0
  115. package/src/module.ts +116 -0
  116. package/src/request-parser.ts +346 -0
  117. package/src/router.ts +747 -0
  118. package/src/static-serving.ts +153 -0
  119. package/src/utils.ts +5 -0
  120. package/tsconfig.esm.json +36 -0
  121. package/tsconfig.json +53 -0
@@ -0,0 +1,411 @@
1
+ /*
2
+ * Deepkit Framework
3
+ * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the MIT License.
7
+ *
8
+ * You should have received a copy of the MIT License along with this program.
9
+ */
10
+
11
+ import { ClassType, isClass, urlJoin } from '@deepkit/core';
12
+ import {
13
+ ClassDecoratorResult,
14
+ createClassDecoratorContext,
15
+ createPropertyDecoratorContext,
16
+ DecoratorAndFetchSignature,
17
+ DualDecorator,
18
+ ExtractApiDataType,
19
+ ExtractClass,
20
+ isDecoratorContext,
21
+ mergeDecorator,
22
+ PropertyDecoratorFn,
23
+ ReceiveType,
24
+ resolveReceiveType,
25
+ SerializationOptions,
26
+ Serializer,
27
+ Type,
28
+ UnionToIntersection
29
+ } from '@deepkit/type';
30
+ import { RouteParameterResolver } from './router.js';
31
+ import { httpMiddleware, HttpMiddleware, HttpMiddlewareConfig, HttpMiddlewareFn } from './middleware.js';
32
+
33
+ type HttpActionMiddleware = (() => HttpMiddlewareConfig) | ClassType<HttpMiddleware> | HttpMiddlewareFn;
34
+
35
+ function isMiddlewareClassTypeOrFn(v: HttpActionMiddleware): v is ClassType<HttpMiddleware> | HttpMiddlewareFn {
36
+ return isClass(v) || !isDecoratorContext(httpMiddleware, v);
37
+ }
38
+
39
+ export class HttpController {
40
+ baseUrl: string = '';
41
+ actions = new Set<HttpAction>();
42
+ groups: string[] = [];
43
+
44
+ middlewares: (() => HttpMiddlewareConfig)[] = [];
45
+
46
+ resolverForToken: Map<any, ClassType> = new Map();
47
+ resolverForParameterName: Map<string, ClassType> = new Map();
48
+
49
+ private actionsProcessed = new Set<HttpAction>();
50
+
51
+ getUrl(action: HttpAction): string {
52
+ return urlJoin('/', this.baseUrl, action.path);
53
+ }
54
+
55
+ addAction(action: HttpAction) {
56
+ this.actions.add(action);
57
+ }
58
+
59
+ getActions(): Set<HttpAction> {
60
+ for (const action of this.actions) {
61
+ if (!this.actionsProcessed.has(action)) {
62
+ action.groups = [
63
+ ...this.groups,
64
+ ...action.groups.filter((g) => !this.groups.includes(g)),
65
+ ];
66
+ this.actionsProcessed.add(action);
67
+ }
68
+ }
69
+
70
+ return this.actions;
71
+ }
72
+
73
+ removeAction(methodName: string): void {
74
+ const action = this.getAction(methodName);
75
+ this.actions.delete(action);
76
+ this.actionsProcessed.delete(action);
77
+ }
78
+
79
+ getAction(methodName: string): HttpAction {
80
+ for (const a of this.getActions()) {
81
+ if (a.methodName === methodName) return a;
82
+ }
83
+ throw new Error(`No action with methodName ${methodName} found`);
84
+ }
85
+ }
86
+
87
+ export class HttpActionParameter {
88
+ name: string = '';
89
+ type?: 'body' | 'query' | 'queries';
90
+
91
+ /**
92
+ * undefined = propertyName, '' === root, else given path
93
+ */
94
+ typePath?: string;
95
+ optional: boolean = false;
96
+ }
97
+
98
+ export class HttpAction {
99
+ name: string = '';
100
+ description: string = '';
101
+ category: string = '';
102
+ path: string = '';
103
+ httpMethods: string[] = [];
104
+ methodName: string = '';
105
+ groups: string[] = [];
106
+ serializer?: Serializer;
107
+ middlewares: (() => HttpMiddlewareConfig)[] = [];
108
+ serializationOptions?: SerializationOptions;
109
+
110
+ resolverForToken: Map<any, ClassType> = new Map();
111
+ resolverForParameterName: Map<string, ClassType> = new Map();
112
+
113
+ /**
114
+ * An arbitrary data container the user can use to store app specific settings/values.
115
+ */
116
+ data = new Map<any, any>();
117
+
118
+ responses: { statusCode: number, description: string, type?: Type }[] = [];
119
+ }
120
+
121
+ export class HttpControllerDecorator {
122
+ t = new HttpController;
123
+
124
+ controller(baseUrl: string = '') {
125
+ this.t.baseUrl = baseUrl;
126
+ }
127
+
128
+ group(...group: string[]) {
129
+ this.t.groups.push(...group);
130
+ }
131
+
132
+ middleware(...middlewares: HttpActionMiddleware[]) {
133
+ this.t.middlewares.push(...middlewares.map(v => isMiddlewareClassTypeOrFn(v) ? httpMiddleware.for(v) : v));
134
+ }
135
+
136
+ /**
137
+ * Adds a parameter resolver for parameters based on the class type. Use .resolveParameterByName() for name-based resolving.
138
+ *
139
+ * ```typescript
140
+ *
141
+ * class UserResolver {
142
+ * resolve(context: RouteParameterResolverContext): any | Promise<any> {
143
+ * return new User();
144
+ * }
145
+ * }
146
+ *
147
+ * @http.resolveParameter(User, UserResolver)
148
+ * class MyController {
149
+ *
150
+ * @http.GET()
151
+ * myAction(user: User) {
152
+ * }
153
+ * }
154
+ *
155
+ * new App({providers: [UserResolver]}).run();
156
+ * ```
157
+ */
158
+ resolveParameter(classType: ClassType | string | any, resolver: ClassType<RouteParameterResolver>) {
159
+ this.t.resolverForToken.set(classType, resolver);
160
+ }
161
+
162
+ /**
163
+ * Adds a parameter resolver for parameters based on its name. Use .resolveParameter() for class-based resolving.
164
+ *
165
+ * ```typescript
166
+ *
167
+ * class UserResolver {
168
+ * resolve(context: RouteParameterResolverContext): any | Promise<any> {
169
+ * return new User();
170
+ * }
171
+ * }
172
+ *
173
+ * @http.resolveParameterByName('user', UserResolver)
174
+ * class MyController {
175
+ *
176
+ * @http.GET()
177
+ * myAction(user: User) {
178
+ * }
179
+ * }
180
+ *
181
+ * new App({providers: [UserResolver]}).run();
182
+ * ```
183
+ */
184
+ resolveParameterByName(name: string, resolver: ClassType<RouteParameterResolver>) {
185
+ this.t.resolverForParameterName.set(name, resolver);
186
+ }
187
+
188
+ setAction(action: HttpAction) {
189
+ this.t.addAction(action);
190
+ }
191
+ }
192
+
193
+ export const httpClass: ClassDecoratorResult<typeof HttpControllerDecorator> = createClassDecoratorContext(HttpControllerDecorator);
194
+
195
+ export class HttpActionDecorator {
196
+ t = new HttpAction;
197
+
198
+ onDecorator(target: ClassType, property: string | undefined, parameterIndexOrDescriptor?: any) {
199
+ if (!property) return;
200
+ if (target === Object) return;
201
+ this.t.methodName = property;
202
+ httpClass.setAction(this.t)(target);
203
+ }
204
+
205
+ name(name: string) {
206
+ this.t.name = name;
207
+ }
208
+
209
+ description(description: string) {
210
+ this.t.description = description;
211
+ }
212
+
213
+ serialization(options: SerializationOptions) {
214
+ this.t.serializationOptions = options;
215
+ }
216
+
217
+ serializer(serializer: Serializer) {
218
+ this.t.serializer = serializer;
219
+ }
220
+
221
+ middleware(...middlewares: HttpActionMiddleware[]) {
222
+ this.t.middlewares.push(...middlewares.map(v => isMiddlewareClassTypeOrFn(v) ? httpMiddleware.for(v) : v));
223
+ }
224
+
225
+ /**
226
+ * Allows to change the HttpAction object and composite multiple properties into one function.
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * const authGroup = Symbol('authGroup');
231
+ *
232
+ * function authGroup(group: 'admin' | 'user') {
233
+ * return (action: HttpAction) => {
234
+ * action.data.set(authGroup, group);
235
+ * };
236
+ * }
237
+ *
238
+ * class My Controller {
239
+ * @http.GET('/assets').use(authGroup('admin'))
240
+ * assets() {}
241
+ * }
242
+ * ```
243
+ */
244
+ use(use: (action: HttpAction) => void) {
245
+ use(this.t);
246
+ }
247
+
248
+ /**
249
+ * Arbitrary value container that can be read in RouterParameterResolver and all
250
+ * HTTP workflow events (like authentication).
251
+ *
252
+ * @example
253
+ * ```typescript
254
+ * class My Controller {
255
+ * @http.GET('/assets').data('authGroup', 'admin')
256
+ * assets() {}
257
+ * }
258
+ * ```
259
+ */
260
+ data(name: string, value: any) {
261
+ this.t.data.set(name, value);
262
+ }
263
+
264
+ category(category: string) {
265
+ this.t.category = category;
266
+ }
267
+
268
+ group(...group: string[]) {
269
+ this.t.groups.push(...group);
270
+ }
271
+
272
+ GET(path: string = '') {
273
+ this.t.httpMethods.push('GET');
274
+ if (path) this.t.path = path;
275
+ }
276
+
277
+ HEAD(path: string = '') {
278
+ this.t.httpMethods.push('HEAD');
279
+ if (path) this.t.path = path;
280
+ }
281
+
282
+ POST(path: string = '') {
283
+ this.t.httpMethods.push('POST');
284
+ if (path) this.t.path = path;
285
+ }
286
+
287
+ PUT(path: string = '') {
288
+ this.t.httpMethods.push('PUT');
289
+ if (path) this.t.path = path;
290
+ }
291
+
292
+ DELETE(path: string = '') {
293
+ this.t.httpMethods.push('DELETE');
294
+ if (path) this.t.path = path;
295
+ }
296
+
297
+ OPTIONS(path: string = '') {
298
+ this.t.httpMethods.push('OPTIONS');
299
+ if (path) this.t.path = path;
300
+ }
301
+
302
+ TRACE(path: string = '') {
303
+ this.t.httpMethods.push('TRACE');
304
+ if (path) this.t.path = path;
305
+ }
306
+
307
+ PATCH(path: string = '') {
308
+ this.t.httpMethods.push('PATCH');
309
+ if (path) this.t.path = path;
310
+ }
311
+
312
+ ANY(path: string = '') {
313
+ this.t.httpMethods = [];
314
+ if (path) this.t.path = path;
315
+ }
316
+
317
+ /**
318
+ * Adds additional information about what HTTP status codes are available in this route.
319
+ * You can add additionally a description and a response body type.
320
+ *
321
+ * The type is used for serialization for responses with the given statusCode.
322
+ *
323
+ * This information is available in Deepkit API console.
324
+ *
325
+ * ```typescript
326
+ *
327
+ * http.GET().response<boolean>(200, 'All ok')
328
+ *
329
+ * interface User {
330
+ * username: string;
331
+ * }
332
+ * http.GET().response<User>(200, 'User object')
333
+ *
334
+ * interface HttpErrorMessage {
335
+ * error: string;
336
+ * }
337
+ * http.GET().response<HttpErrorMessage>(500, 'Error')
338
+ * ```
339
+ */
340
+ response<T>(statusCode: number, description: string = '', type?: ReceiveType<T>) {
341
+ this.t.responses.push({ statusCode, description, type: type ? resolveReceiveType(type) : undefined });
342
+ }
343
+
344
+ /**
345
+ * Adds a parameter resolver for parameters based on the class type. Use .resolveParameterByName() for name-based resolving.
346
+ *
347
+ * ```typescript
348
+ *
349
+ * class UserResolver {
350
+ * resolve(context: RouteParameterResolverContext): any | Promise<any> {
351
+ * return new User();
352
+ * }
353
+ * }
354
+ *
355
+ * class MyController {
356
+ * @http.GET()
357
+ * @http.resolveParameter(User, UserResolver)
358
+ * myAction(user: User) {
359
+ * }
360
+ * }
361
+ *
362
+ * new App({providers: [UserResolver]}).run();
363
+ * ```
364
+ */
365
+ resolveParameter(classType: ClassType | string | any, resolver: ClassType<RouteParameterResolver>) {
366
+ this.t.resolverForToken.set(classType, resolver);
367
+ }
368
+
369
+ /**
370
+ * Adds a parameter resolver for parameters based on its name. Use .resolveParameter() for class-based resolving.
371
+ *
372
+ * ```typescript
373
+ *
374
+ * class UserResolver {
375
+ * resolve(context: RouteParameterResolverContext): any | Promise<any> {
376
+ * return new User();
377
+ * }
378
+ * }
379
+ *
380
+ * class MyController {
381
+ * @http.GET()
382
+ * @http.resolveParameterByName('user', UserResolver)
383
+ * myAction(user: User) {
384
+ * }
385
+ * }
386
+ *
387
+ * new App({providers: [UserResolver]}).run();
388
+ * ```
389
+ */
390
+ resolveParameterByName(name: string, resolver: ClassType<RouteParameterResolver>) {
391
+ this.t.resolverForParameterName.set(name, resolver);
392
+ }
393
+ }
394
+
395
+ //this workaround is necessary since generic functions (necessary for response<T>) are lost during a mapped type and changed ReturnType
396
+ type HttpActionFluidDecorator<T, D extends Function> = {
397
+ [name in keyof T]: name extends 'response' ? <T2>(statusCode: number, description?: string, type?: ReceiveType<T2>) => D & HttpActionFluidDecorator<T, D>
398
+ : T[name] extends (...args: infer K) => any ? (...args: K) => D & HttpActionFluidDecorator<T, D>
399
+ : D & HttpActionFluidDecorator<T, D> & { _data: ExtractApiDataType<T> };
400
+ };
401
+ type HttpActionPropertyDecoratorResult = HttpActionFluidDecorator<ExtractClass<typeof HttpActionDecorator>, PropertyDecoratorFn> & DecoratorAndFetchSignature<typeof HttpActionDecorator, PropertyDecoratorFn>;
402
+
403
+ export const httpAction: HttpActionPropertyDecoratorResult = createPropertyDecoratorContext(HttpActionDecorator);
404
+
405
+ //this workaround is necessary since generic functions are lost during a mapped type and changed ReturnType
406
+ type HttpMerge<U> = { [K in keyof U]: K extends 'response' ? <T2>(statusCode: number, description?: string, type?: ReceiveType<T2>) => PropertyDecoratorFn & U : U[K] extends ((...a: infer A) => infer R) ? R extends DualDecorator ? (...a: A) => PropertyDecoratorFn & R & U : (...a: A) => R : never };
407
+ type MergedHttp<T extends any[]> = HttpMerge<Omit<UnionToIntersection<T[number]>, '_fetch' | 't'>>
408
+
409
+ export type HttpDecorator = PropertyDecoratorFn & HttpActionFluidDecorator<HttpActionDecorator, PropertyDecoratorFn>;
410
+
411
+ export const http: MergedHttp<[typeof httpClass, typeof httpAction]> = mergeDecorator(httpClass, httpAction) as any as MergedHttp<[typeof httpClass, typeof httpAction]>;
package/src/filter.ts ADDED
@@ -0,0 +1,224 @@
1
+ import { ClassType } from '@deepkit/core';
2
+ import { AppModule } from '@deepkit/app';
3
+ import { RouteConfig, HttpRouter } from './router.js';
4
+
5
+ export interface HttpRouteFilterRoute {
6
+ path?: string;
7
+ pathRegExp?: RegExp;
8
+ httpMethod?: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'PUT' | 'DELETE' | 'OPTIONS' | 'TRACE';
9
+ category?: string;
10
+ group?: string;
11
+ }
12
+
13
+ export class HttpRouteFilterModel {
14
+ controllers: ClassType[] = [];
15
+ excludeControllers: ClassType[] = [];
16
+
17
+ routes: HttpRouteFilterRoute[] = [];
18
+ excludeRoutes: HttpRouteFilterRoute[] = [];
19
+
20
+ routeNames: string[] = [];
21
+ excludeRouteNames: string[] = [];
22
+
23
+ modules: AppModule<any>[] = [];
24
+ excludeModules: AppModule<any>[] = [];
25
+
26
+ moduleClasses: ClassType<AppModule<any>>[] = [];
27
+ excludeModuleClasses: ClassType<AppModule<any>>[] = [];
28
+
29
+ reset() {
30
+ this.controllers = [];
31
+ this.excludeControllers = [];
32
+
33
+ this.routes = [];
34
+ this.excludeRoutes = [];
35
+
36
+ this.routeNames = [];
37
+ this.excludeRouteNames = [];
38
+
39
+ this.modules = [];
40
+ this.moduleClasses = [];
41
+ }
42
+ }
43
+
44
+ function match(routeConfig: RouteConfig, route: HttpRouteFilterRoute): boolean {
45
+ if (route.httpMethod && routeConfig.httpMethods.includes(route.httpMethod)) return true;
46
+
47
+ if (route.category && route.category !== routeConfig.category) return true;
48
+
49
+ if (route.group && routeConfig.groups.includes(route.group)) return true;
50
+
51
+ if (route.path || route.pathRegExp) {
52
+ if (!route.pathRegExp && route.path) route.pathRegExp = new RegExp('^' + route.path.replace(/\*/g, '.*') + '$');
53
+ if (route.pathRegExp && route.pathRegExp.test(routeConfig.getFullPath())) return true;
54
+ }
55
+
56
+ return false;
57
+ }
58
+
59
+ export class HttpRouterFilterResolver {
60
+ constructor(protected router: HttpRouter) {
61
+ }
62
+
63
+ /**
64
+ * Resolves
65
+ */
66
+ public resolve(filter: HttpRouteFilterModel): RouteConfig[] {
67
+ const result: RouteConfig[] = [];
68
+
69
+ outer:
70
+ for (const routeConfig of this.router.getRoutes()) {
71
+ if (filter.controllers.length && routeConfig.action.type === 'controller' && !filter.controllers.includes(routeConfig.action.controller)) continue;
72
+
73
+ if (filter.excludeControllers.length && routeConfig.action.type === 'controller' && filter.excludeControllers.includes(routeConfig.action.controller)) continue;
74
+
75
+ if (filter.modules.length) {
76
+ if (!routeConfig.module) continue;
77
+ let found: boolean = false;
78
+ for (const module of filter.modules) {
79
+ //modules get cloned everywhere, but id is sticky. So we compare that.
80
+ if (routeConfig.module.id === module.id) {
81
+ found = true;
82
+ break;
83
+ }
84
+ }
85
+ if (!found) continue;
86
+ }
87
+
88
+ if (filter.excludeModules.length) {
89
+ if (!routeConfig.module) continue;
90
+ for (const module of filter.excludeModules) {
91
+ //modules get cloned everywhere, but id is sticky. So we compare that.
92
+ if (routeConfig.module.id === module.id) continue outer;
93
+ }
94
+ }
95
+
96
+ if (filter.moduleClasses.length) {
97
+ if (!routeConfig.module) continue;
98
+ let found: boolean = false;
99
+ for (const module of filter.moduleClasses) {
100
+ if (routeConfig.module instanceof module) {
101
+ found = true;
102
+ break;
103
+ }
104
+ }
105
+ if (!found) continue;
106
+ }
107
+
108
+ if (filter.excludeModuleClasses.length) {
109
+ if (!routeConfig.module) continue;
110
+ for (const module of filter.excludeModuleClasses) {
111
+ if (routeConfig.module instanceof module) continue outer;
112
+ }
113
+ }
114
+
115
+ if (filter.routeNames.length) {
116
+ let found: boolean = false;
117
+ for (const name of filter.routeNames) {
118
+ if (name.includes('*')) {
119
+ const regex = new RegExp('^' + name.replace(/\*/g, '.*') + '$');
120
+ if (regex.test(routeConfig.name)) {
121
+ found = true;
122
+ break;
123
+ }
124
+ } else if (name === routeConfig.name) {
125
+ found = true;
126
+ break;
127
+ }
128
+ }
129
+ if (!found) continue;
130
+ }
131
+
132
+ if (filter.excludeRouteNames.length) {
133
+ for (const name of filter.excludeRouteNames) {
134
+ if (name.includes('*')) {
135
+ const regex = new RegExp('^' + name.replace(/\*/g, '.*') + '$');
136
+ if (regex.test(routeConfig.name)) continue outer;
137
+ } else if (name == routeConfig.name) {
138
+ continue outer;
139
+ }
140
+ }
141
+ }
142
+
143
+ if (filter.routes.length) {
144
+ let found: boolean = false;
145
+ for (const route of filter.routes) {
146
+ if (match(routeConfig, route)) {
147
+ found = true;
148
+ }
149
+ }
150
+ if (!found) continue;
151
+ }
152
+
153
+ for (const route of filter.excludeRoutes) {
154
+ if (match(routeConfig, route)) continue outer;
155
+ }
156
+
157
+ result.push(routeConfig);
158
+ }
159
+
160
+ return result;
161
+ }
162
+ }
163
+
164
+ /**
165
+ * A class to describe a filter for routes.
166
+ */
167
+ export class HttpRouteFilter {
168
+ public model = new HttpRouteFilterModel();
169
+
170
+ reset(): this {
171
+ this.model.reset();
172
+ return this;
173
+ }
174
+
175
+ forControllers(...controllers: ClassType[]): this {
176
+ this.model.controllers.push(...controllers);
177
+ return this;
178
+ }
179
+
180
+ excludeControllers(...controllers: ClassType[]): this {
181
+ this.model.excludeControllers.push(...controllers);
182
+ return this;
183
+ }
184
+
185
+ forRoutes(...routes: HttpRouteFilterRoute[]): this {
186
+ this.model.routes.push(...routes);
187
+ return this;
188
+ }
189
+
190
+ excludeRoutes(...routes: HttpRouteFilterRoute[]): this {
191
+ this.model.excludeRoutes.push(...routes);
192
+ return this;
193
+ }
194
+
195
+ forRouteNames(...names: string[]): this {
196
+ this.model.routeNames.push(...names);
197
+ return this
198
+ }
199
+
200
+ excludeRouteNames(...names: string[]): this {
201
+ this.model.excludeRouteNames.push(...names);
202
+ return this;
203
+ }
204
+
205
+ forModules(...modules: AppModule<any>[]): this {
206
+ this.model.modules.push(...modules);
207
+ return this;
208
+ }
209
+
210
+ excludeModules(...modules: AppModule<any>[]): this {
211
+ this.model.excludeModules.push(...modules);
212
+ return this;
213
+ }
214
+
215
+ forModuleClasses(...moduleClasses: ClassType<AppModule<any>>[]): this {
216
+ this.model.moduleClasses.push(...moduleClasses);
217
+ return this;
218
+ }
219
+
220
+ excludeModuleClasses(...moduleClasses: ClassType<AppModule<any>>[]): this {
221
+ this.model.excludeModuleClasses.push(...moduleClasses);
222
+ return this;
223
+ }
224
+ }
@@ -0,0 +1,11 @@
1
+ import { App, RootModuleDefinition } from '@deepkit/app';
2
+ import { HttpRouterRegistry } from './router.js';
3
+
4
+ /**
5
+ * Same as App, but with easily accessible router to make the most common use case easier.
6
+ */
7
+ export class HttpApp<T extends RootModuleDefinition> extends App<T> {
8
+ get router(): HttpRouterRegistry {
9
+ return super.get(HttpRouterRegistry);
10
+ }
11
+ }