@venok/http 1.0.1-canary.0

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 (254) hide show
  1. package/adapter/adapter.d.ts +57 -0
  2. package/adapter/adapter.js +58 -0
  3. package/adapter/host.d.ts +27 -0
  4. package/adapter/host.js +33 -0
  5. package/application/application.d.ts +18 -0
  6. package/application/application.js +51 -0
  7. package/application/config.d.ts +16 -0
  8. package/application/config.js +47 -0
  9. package/application/http.module-defenition.d.ts +8 -0
  10. package/application/http.module-defenition.js +6 -0
  11. package/application/http.module.d.ts +3 -0
  12. package/application/http.module.js +21 -0
  13. package/application/http.service.d.ts +11 -0
  14. package/application/http.service.js +43 -0
  15. package/application/starter.d.ts +41 -0
  16. package/application/starter.js +140 -0
  17. package/constants.d.ts +12 -0
  18. package/constants.js +15 -0
  19. package/context/context.d.ts +56 -0
  20. package/context/context.js +102 -0
  21. package/context/response.controller.d.ts +30 -0
  22. package/context/response.controller.js +86 -0
  23. package/decorators/controller.decorator.d.ts +82 -0
  24. package/decorators/controller.decorator.js +53 -0
  25. package/decorators/header.decorator.d.ts +12 -0
  26. package/decorators/header.decorator.js +23 -0
  27. package/decorators/http-code.decorator.d.ts +9 -0
  28. package/decorators/http-code.decorator.js +19 -0
  29. package/decorators/index.d.ts +9 -0
  30. package/decorators/index.js +25 -0
  31. package/decorators/redirect.decorator.d.ts +6 -0
  32. package/decorators/redirect.decorator.js +16 -0
  33. package/decorators/render.decorator.d.ts +10 -0
  34. package/decorators/render.decorator.js +20 -0
  35. package/decorators/request-mapping.decorator.d.ts +78 -0
  36. package/decorators/request-mapping.decorator.js +98 -0
  37. package/decorators/route-params.decorator.d.ts +378 -0
  38. package/decorators/route-params.decorator.js +227 -0
  39. package/decorators/sse.decorator.d.ts +6 -0
  40. package/decorators/sse.decorator.js +20 -0
  41. package/decorators/version.decorator.d.ts +7 -0
  42. package/decorators/version.decorator.js +20 -0
  43. package/enums/http-status.enum.d.ts +53 -0
  44. package/enums/http-status.enum.js +57 -0
  45. package/enums/index.d.ts +4 -0
  46. package/enums/index.js +20 -0
  47. package/enums/request-method.enum.d.ts +11 -0
  48. package/enums/request-method.enum.js +15 -0
  49. package/enums/route-paramtypes.enum.d.ts +14 -0
  50. package/enums/route-paramtypes.enum.js +18 -0
  51. package/enums/version-type.enum.d.ts +9 -0
  52. package/enums/version-type.enum.js +13 -0
  53. package/errors/bad-gateway.exception.d.ts +33 -0
  54. package/errors/bad-gateway.exception.js +41 -0
  55. package/errors/bad-request.exception.d.ts +33 -0
  56. package/errors/bad-request.exception.js +41 -0
  57. package/errors/conflict.exception.d.ts +33 -0
  58. package/errors/conflict.exception.js +41 -0
  59. package/errors/forbidden.exception.d.ts +33 -0
  60. package/errors/forbidden.exception.js +41 -0
  61. package/errors/gateway-timeout.exception.d.ts +33 -0
  62. package/errors/gateway-timeout.exception.js +41 -0
  63. package/errors/gone.exception.d.ts +33 -0
  64. package/errors/gone.exception.js +41 -0
  65. package/errors/http-version-not-supported.exception.d.ts +33 -0
  66. package/errors/http-version-not-supported.exception.js +41 -0
  67. package/errors/http.exception.d.ts +79 -0
  68. package/errors/http.exception.js +123 -0
  69. package/errors/im-a-teapot.exception.d.ts +36 -0
  70. package/errors/im-a-teapot.exception.js +44 -0
  71. package/errors/index.d.ts +11 -0
  72. package/errors/index.js +27 -0
  73. package/errors/internal-server-error.exception.d.ts +33 -0
  74. package/errors/internal-server-error.exception.js +41 -0
  75. package/errors/invalid-middleware-configuration.exception.d.ts +4 -0
  76. package/errors/invalid-middleware-configuration.exception.js +11 -0
  77. package/errors/invalid-middleware.exception.d.ts +4 -0
  78. package/errors/invalid-middleware.exception.js +11 -0
  79. package/errors/not-found.exception.d.ts +33 -0
  80. package/errors/not-found.exception.js +41 -0
  81. package/errors/unknown-request-mapping.exception.d.ts +5 -0
  82. package/errors/unknown-request-mapping.exception.js +11 -0
  83. package/exceptions/messages.d.ts +4 -0
  84. package/exceptions/messages.js +13 -0
  85. package/explorers/path.explorer.d.ts +8 -0
  86. package/explorers/path.explorer.js +38 -0
  87. package/explorers/router.explorer.d.ts +51 -0
  88. package/explorers/router.explorer.js +195 -0
  89. package/factory/context-id.factory.d.ts +41 -0
  90. package/factory/context-id.factory.js +52 -0
  91. package/factory/index.d.ts +4 -0
  92. package/factory/index.js +20 -0
  93. package/factory/method.factory.d.ts +5 -0
  94. package/factory/method.factory.js +30 -0
  95. package/factory/params.factory.d.ts +5 -0
  96. package/factory/params.factory.js +39 -0
  97. package/factory/path.factory.d.ts +13 -0
  98. package/factory/path.factory.js +95 -0
  99. package/filters/context.d.ts +9 -0
  100. package/filters/context.js +15 -0
  101. package/filters/filter.d.ts +22 -0
  102. package/filters/filter.js +78 -0
  103. package/helpers/adapter.helper.d.ts +27 -0
  104. package/helpers/adapter.helper.js +37 -0
  105. package/helpers/exclude-route.helper.d.ts +4 -0
  106. package/helpers/exclude-route.helper.js +18 -0
  107. package/helpers/flatten-routes.helper.d.ts +5 -0
  108. package/helpers/flatten-routes.helper.js +27 -0
  109. package/helpers/index.d.ts +6 -0
  110. package/helpers/index.js +22 -0
  111. package/helpers/messages.helper.d.ts +6 -0
  112. package/helpers/messages.helper.js +25 -0
  113. package/helpers/path.helper.d.ts +3 -0
  114. package/helpers/path.helper.js +13 -0
  115. package/helpers/sse.helper.d.ts +45 -0
  116. package/helpers/sse.helper.js +83 -0
  117. package/index.d.ts +1 -0
  118. package/index.js +3 -0
  119. package/interfaces/http/cors.interface.d.ts +58 -0
  120. package/interfaces/http/cors.interface.js +2 -0
  121. package/interfaces/http/exception.interface.d.ts +6 -0
  122. package/interfaces/http/exception.interface.js +2 -0
  123. package/interfaces/http/exclude-route.interface.d.ts +15 -0
  124. package/interfaces/http/exclude-route.interface.js +2 -0
  125. package/interfaces/http/index.d.ts +10 -0
  126. package/interfaces/http/index.js +26 -0
  127. package/interfaces/http/message-event.interface.d.ts +6 -0
  128. package/interfaces/http/message-event.interface.js +2 -0
  129. package/interfaces/http/module.inteface.d.ts +5 -0
  130. package/interfaces/http/module.inteface.js +2 -0
  131. package/interfaces/http/options.interface.d.ts +127 -0
  132. package/interfaces/http/options.interface.js +2 -0
  133. package/interfaces/http/path-metadata.interface.d.ts +31 -0
  134. package/interfaces/http/path-metadata.interface.js +2 -0
  135. package/interfaces/http/prefix-options.interface.d.ts +7 -0
  136. package/interfaces/http/prefix-options.interface.js +2 -0
  137. package/interfaces/http/raw-request.d.ts +4 -0
  138. package/interfaces/http/raw-request.js +2 -0
  139. package/interfaces/http/routes.interface.d.ts +7 -0
  140. package/interfaces/http/routes.interface.js +2 -0
  141. package/interfaces/http/server.interface.d.ts +54 -0
  142. package/interfaces/http/server.interface.js +2 -0
  143. package/interfaces/index.d.ts +3 -0
  144. package/interfaces/index.js +19 -0
  145. package/interfaces/middleware/config-proxy.interface.d.ts +23 -0
  146. package/interfaces/middleware/config-proxy.interface.js +2 -0
  147. package/interfaces/middleware/configuration.interface.d.ts +12 -0
  148. package/interfaces/middleware/configuration.interface.js +2 -0
  149. package/interfaces/middleware/consumer.interface.d.ts +16 -0
  150. package/interfaces/middleware/consumer.interface.js +2 -0
  151. package/interfaces/middleware/index.d.ts +4 -0
  152. package/interfaces/middleware/index.js +20 -0
  153. package/interfaces/middleware/middleware.interface.d.ts +7 -0
  154. package/interfaces/middleware/middleware.interface.js +2 -0
  155. package/interfaces/router/callback-paramtypes.interface.d.ts +1 -0
  156. package/interfaces/router/callback-paramtypes.interface.js +2 -0
  157. package/interfaces/router/definition.interface.d.ts +10 -0
  158. package/interfaces/router/definition.interface.js +2 -0
  159. package/interfaces/router/index.d.ts +3 -0
  160. package/interfaces/router/index.js +19 -0
  161. package/interfaces/router/version-options.interface.d.ts +92 -0
  162. package/interfaces/router/version-options.interface.js +9 -0
  163. package/middleware/builder.d.ts +15 -0
  164. package/middleware/builder.js +78 -0
  165. package/middleware/container.d.ts +13 -0
  166. package/middleware/container.js +50 -0
  167. package/middleware/extractor.d.ts +15 -0
  168. package/middleware/extractor.js +50 -0
  169. package/middleware/index.d.ts +1 -0
  170. package/middleware/index.js +17 -0
  171. package/middleware/module.d.ts +33 -0
  172. package/middleware/module.js +180 -0
  173. package/middleware/resolver.d.ts +10 -0
  174. package/middleware/resolver.js +27 -0
  175. package/middleware/routes-mapper.d.ts +19 -0
  176. package/middleware/routes-mapper.js +111 -0
  177. package/middleware/utils.d.ts +8 -0
  178. package/middleware/utils.js +86 -0
  179. package/package.json +41 -0
  180. package/router/module.d.ts +18 -0
  181. package/router/module.js +88 -0
  182. package/router/resolver.d.ts +31 -0
  183. package/router/resolver.js +128 -0
  184. package/storage/http-instance.storage.d.ts +9 -0
  185. package/storage/http-instance.storage.js +19 -0
  186. package/stream/index.d.ts +1 -0
  187. package/stream/index.js +17 -0
  188. package/stream/interfaces/index.d.ts +2 -0
  189. package/stream/interfaces/index.js +18 -0
  190. package/stream/interfaces/streamable-handler-response.interface.d.ts +12 -0
  191. package/stream/interfaces/streamable-handler-response.interface.js +2 -0
  192. package/stream/interfaces/streamable-options.interface.d.ts +10 -0
  193. package/stream/interfaces/streamable-options.interface.js +2 -0
  194. package/stream/streamable-file.d.ts +22 -0
  195. package/stream/streamable-file.js +55 -0
  196. package/test/context/response.controller.spec.d.ts +1 -0
  197. package/test/context/response.controller.spec.js +328 -0
  198. package/test/decorators/controller.decorator.spec.d.ts +1 -0
  199. package/test/decorators/controller.decorator.spec.js +113 -0
  200. package/test/decorators/header.decorator.spec.d.ts +1 -0
  201. package/test/decorators/header.decorator.spec.js +33 -0
  202. package/test/decorators/http-code.decorator.spec.d.ts +1 -0
  203. package/test/decorators/http-code.decorator.spec.js +30 -0
  204. package/test/decorators/redirect.decorator.spec.d.ts +1 -0
  205. package/test/decorators/redirect.decorator.spec.js +36 -0
  206. package/test/decorators/render.decorator.spec.d.ts +1 -0
  207. package/test/decorators/render.decorator.spec.js +30 -0
  208. package/test/decorators/request-mapping.decorator.spec.d.ts +1 -0
  209. package/test/decorators/request-mapping.decorator.spec.js +85 -0
  210. package/test/decorators/route-params.decorator.spec.d.ts +1 -0
  211. package/test/decorators/route-params.decorator.spec.js +493 -0
  212. package/test/decorators/sse.decorator.spec.d.ts +1 -0
  213. package/test/decorators/sse.decorator.spec.js +35 -0
  214. package/test/decorators/version.decorator.spec.d.ts +1 -0
  215. package/test/decorators/version.decorator.spec.js +43 -0
  216. package/test/exceptions/handler.spec.d.ts +1 -0
  217. package/test/exceptions/handler.spec.js +145 -0
  218. package/test/exceptions/proxy.spec.d.ts +1 -0
  219. package/test/exceptions/proxy.spec.js +74 -0
  220. package/test/explorers/path.explorer.spec.d.ts +1 -0
  221. package/test/explorers/path.explorer.spec.js +190 -0
  222. package/test/explorers/router.explorer.spec.d.ts +1 -0
  223. package/test/explorers/router.explorer.spec.js +206 -0
  224. package/test/factory/method.factory.spec.d.ts +1 -0
  225. package/test/factory/method.factory.spec.js +33 -0
  226. package/test/factory/params.factory.spec.d.ts +1 -0
  227. package/test/factory/params.factory.spec.js +104 -0
  228. package/test/factory/path.factory.spec.d.ts +1 -0
  229. package/test/factory/path.factory.spec.js +260 -0
  230. package/test/filters/context.spec.d.ts +1 -0
  231. package/test/filters/context.spec.js +96 -0
  232. package/test/filters/filter.spec.d.ts +1 -0
  233. package/test/filters/filter.spec.js +96 -0
  234. package/test/helpers/flatten-routes.helper.spec.d.ts +1 -0
  235. package/test/helpers/flatten-routes.helper.spec.js +131 -0
  236. package/test/helpers/sse.helper.spec.d.ts +1 -0
  237. package/test/helpers/sse.helper.spec.js +132 -0
  238. package/test/middleware/builder.spec.d.ts +1 -0
  239. package/test/middleware/builder.spec.js +235 -0
  240. package/test/middleware/container.spec.d.ts +1 -0
  241. package/test/middleware/container.spec.js +81 -0
  242. package/test/middleware/module.spec.d.ts +1 -0
  243. package/test/middleware/module.spec.js +206 -0
  244. package/test/middleware/route-info-path-extractor.spec.d.ts +1 -0
  245. package/test/middleware/route-info-path-extractor.spec.js +123 -0
  246. package/test/middleware/routes-mapper.spec.d.ts +1 -0
  247. package/test/middleware/routes-mapper.spec.js +162 -0
  248. package/test/middleware/utils.spec.d.ts +1 -0
  249. package/test/middleware/utils.spec.js +132 -0
  250. package/test/router/module.spec.d.ts +1 -0
  251. package/test/router/module.spec.js +40 -0
  252. package/test/router/resolver.spec.d.ts +1 -0
  253. package/test/router/resolver.spec.js +326 -0
  254. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,33 @@
1
+ import { HttpException, HttpExceptionOptions } from "./http.exception";
2
+ /**
3
+ * Defines an HTTP exception for *Not Found* type errors.
4
+ *
5
+ * @publicApi
6
+ */
7
+ export declare class NotFoundException extends HttpException {
8
+ /**
9
+ * Instantiate a `NotFoundException` Exception.
10
+ *
11
+ * @example
12
+ * `throw new NotFoundException()`
13
+ *
14
+ * @usageNotes
15
+ * The HTTP response status code will be 404.
16
+ * - The `objectOrError` argument defines the JSON response body or the message string.
17
+ * - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
18
+ *
19
+ * By default, the JSON response body contains two properties:
20
+ * - `statusCode`: this will be the value 404.
21
+ * - `message`: the string `'Not Found'` by default; override this by supplying
22
+ * a string in the `objectOrError` parameter.
23
+ *
24
+ * If the parameter `objectOrError` is a string, the response body will contain an
25
+ * additional property, `error`, with a short description of the HTTP error. To override the
26
+ * entire JSON response body, pass an object instead. Venok will serialize the object
27
+ * and return it as the JSON response body.
28
+ *
29
+ * @param objectOrError string or object describing the error condition.
30
+ * @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
31
+ */
32
+ constructor(objectOrError?: string | object | any, descriptionOrOptions?: string | HttpExceptionOptions);
33
+ }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotFoundException = void 0;
4
+ const enums_1 = require("../enums");
5
+ const http_exception_1 = require("./http.exception");
6
+ /**
7
+ * Defines an HTTP exception for *Not Found* type errors.
8
+ *
9
+ * @publicApi
10
+ */
11
+ class NotFoundException extends http_exception_1.HttpException {
12
+ /**
13
+ * Instantiate a `NotFoundException` Exception.
14
+ *
15
+ * @example
16
+ * `throw new NotFoundException()`
17
+ *
18
+ * @usageNotes
19
+ * The HTTP response status code will be 404.
20
+ * - The `objectOrError` argument defines the JSON response body or the message string.
21
+ * - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
22
+ *
23
+ * By default, the JSON response body contains two properties:
24
+ * - `statusCode`: this will be the value 404.
25
+ * - `message`: the string `'Not Found'` by default; override this by supplying
26
+ * a string in the `objectOrError` parameter.
27
+ *
28
+ * If the parameter `objectOrError` is a string, the response body will contain an
29
+ * additional property, `error`, with a short description of the HTTP error. To override the
30
+ * entire JSON response body, pass an object instead. Venok will serialize the object
31
+ * and return it as the JSON response body.
32
+ *
33
+ * @param objectOrError string or object describing the error condition.
34
+ * @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
35
+ */
36
+ constructor(objectOrError, descriptionOrOptions = "Not Found") {
37
+ const { description, httpExceptionOptions } = http_exception_1.HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
38
+ super(http_exception_1.HttpException.createBody(objectOrError, description, enums_1.HttpStatus.NOT_FOUND), enums_1.HttpStatus.NOT_FOUND, httpExceptionOptions);
39
+ }
40
+ }
41
+ exports.NotFoundException = NotFoundException;
@@ -0,0 +1,5 @@
1
+ import { Type } from "@venok/core";
2
+ import { RuntimeException } from "@venok/core/errors/exceptions";
3
+ export declare class UnknownRequestMappingException extends RuntimeException {
4
+ constructor(metatype: Type);
5
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UnknownRequestMappingException = void 0;
4
+ const exceptions_1 = require("../../core/errors/exceptions");
5
+ const messages_1 = require("../exceptions/messages");
6
+ class UnknownRequestMappingException extends exceptions_1.RuntimeException {
7
+ constructor(metatype) {
8
+ super((0, messages_1.UNKNOWN_REQUEST_MAPPING)(metatype));
9
+ }
10
+ }
11
+ exports.UnknownRequestMappingException = UnknownRequestMappingException;
@@ -0,0 +1,4 @@
1
+ import { Type } from "@venok/core";
2
+ export declare const UNKNOWN_REQUEST_MAPPING: (metatype: Type) => string;
3
+ export declare const INVALID_MIDDLEWARE_CONFIGURATION = "An invalid middleware configuration has been passed inside the module 'configure()' method.";
4
+ export declare const INVALID_MIDDLEWARE_MESSAGE: (text: TemplateStringsArray, name: string) => string;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.INVALID_MIDDLEWARE_MESSAGE = exports.INVALID_MIDDLEWARE_CONFIGURATION = exports.UNKNOWN_REQUEST_MAPPING = void 0;
4
+ const UNKNOWN_REQUEST_MAPPING = (metatype) => {
5
+ const className = metatype.name;
6
+ return className
7
+ ? `An invalid controller has been detected. "${className}" does not have the @Controller() decorator but it is being listed in the "controllers" array of some module.`
8
+ : `An invalid controller has been detected. Perhaps, one of your controllers is missing the @Controller() decorator.`;
9
+ };
10
+ exports.UNKNOWN_REQUEST_MAPPING = UNKNOWN_REQUEST_MAPPING;
11
+ exports.INVALID_MIDDLEWARE_CONFIGURATION = `An invalid middleware configuration has been passed inside the module 'configure()' method.`;
12
+ const INVALID_MIDDLEWARE_MESSAGE = (text, name) => `The middleware doesn't provide the 'use' method (${name})`;
13
+ exports.INVALID_MIDDLEWARE_MESSAGE = INVALID_MIDDLEWARE_MESSAGE;
@@ -0,0 +1,8 @@
1
+ import { MetadataScanner } from "@venok/core";
2
+ import { RouteDefinition } from "../interfaces";
3
+ export declare class PathsExplorer {
4
+ private readonly metadataScanner;
5
+ constructor(metadataScanner: MetadataScanner);
6
+ scanForPaths(instance: Object, prototype?: object): RouteDefinition[];
7
+ exploreMethodMetadata<T extends Record<string | symbol, any>>(instance: T, prototype: T, methodName: string): RouteDefinition | null;
8
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PathsExplorer = void 0;
4
+ const shared_helper_1 = require("../../core/helpers/shared.helper");
5
+ const constants_1 = require("../constants");
6
+ const helpers_1 = require("../helpers");
7
+ class PathsExplorer {
8
+ constructor(metadataScanner) {
9
+ this.metadataScanner = metadataScanner;
10
+ }
11
+ scanForPaths(instance, prototype) {
12
+ const instancePrototype = (0, shared_helper_1.isUndefined)(prototype) ? Object.getPrototypeOf(instance) : prototype;
13
+ return this.metadataScanner.getAllMethodNames(instancePrototype).reduce((acc, method) => {
14
+ const route = this.exploreMethodMetadata(instance, instancePrototype, method);
15
+ if (route)
16
+ acc.push(route);
17
+ return acc;
18
+ }, []);
19
+ }
20
+ exploreMethodMetadata(instance, prototype, methodName) {
21
+ const instanceCallback = instance[methodName];
22
+ const prototypeCallback = prototype[methodName];
23
+ const routePath = Reflect.getMetadata(constants_1.PATH_METADATA, prototypeCallback);
24
+ if ((0, shared_helper_1.isUndefined)(routePath))
25
+ return null;
26
+ const requestMethod = Reflect.getMetadata(constants_1.METHOD_METADATA, prototypeCallback);
27
+ const version = Reflect.getMetadata(constants_1.VERSION_METADATA, prototypeCallback);
28
+ const path = (0, shared_helper_1.isString)(routePath) ? [(0, helpers_1.addLeadingSlash)(routePath)] : routePath.map((p) => (0, helpers_1.addLeadingSlash)(p));
29
+ return {
30
+ path,
31
+ requestMethod,
32
+ targetCallback: instanceCallback,
33
+ methodName,
34
+ version,
35
+ };
36
+ }
37
+ }
38
+ exports.PathsExplorer = PathsExplorer;
@@ -0,0 +1,51 @@
1
+ import { ApplicationConfig, ContextId, MetadataScanner, Type, VenokContainer } from "@venok/core";
2
+ import { RequestMethod } from "../enums";
3
+ import { HttpServer, RouteDefinition, RoutePathMetadata, VersionValue } from "../interfaces";
4
+ import { RoutePathFactory } from "../factory";
5
+ import { Injector } from "@venok/core/injector/injector";
6
+ import { GraphInspector } from "@venok/core/inspector/graph-inspector";
7
+ import { AbstractHttpAdapter } from "../adapter/adapter";
8
+ import { InstanceWrapper } from "@venok/core/injector/instance/wrapper";
9
+ import { Module } from "@venok/core/injector/module/module";
10
+ import { RouterExceptionFiltersContext } from "../filters/context";
11
+ import { VenokProxy } from "@venok/core/context";
12
+ import { VenokExceptionsHandler } from "@venok/core/exceptions/handler";
13
+ export interface ExceptionsFilter {
14
+ create(instance: Object, callback: Function, module: string, contextId?: ContextId, inquirerId?: string): VenokExceptionsHandler;
15
+ }
16
+ export type HttpEntrypointMetadata = {
17
+ path: string;
18
+ requestMethod: keyof typeof RequestMethod;
19
+ methodVersion?: VersionValue;
20
+ controllerVersion?: VersionValue;
21
+ };
22
+ export type MiddlewareEntrypointMetadata = {
23
+ path: string;
24
+ requestMethod: keyof typeof RequestMethod;
25
+ version?: VersionValue;
26
+ };
27
+ export declare class RouterExplorer {
28
+ private readonly container;
29
+ private readonly injector;
30
+ private readonly routerProxy;
31
+ private readonly exceptionsFilter;
32
+ private readonly routePathFactory;
33
+ private readonly graphInspector;
34
+ private readonly applicationRef;
35
+ private readonly httpContextCreator;
36
+ private readonly pathsExplorer;
37
+ private readonly routerMethodFactory;
38
+ private readonly logger;
39
+ private readonly exceptionFiltersCache;
40
+ constructor(metadataScanner: MetadataScanner, container: VenokContainer, injector: Injector, routerProxy: VenokProxy, exceptionsFilter: RouterExceptionFiltersContext, config: ApplicationConfig, routePathFactory: RoutePathFactory, graphInspector: GraphInspector, applicationRef: AbstractHttpAdapter);
41
+ explore<T extends HttpServer = any>(instanceWrapper: InstanceWrapper, moduleKey: string, applicationRef: T, host: string | RegExp | Array<string | RegExp>, routePathMetadata: RoutePathMetadata): void;
42
+ extractRouterPath(metatype: Type<Object>): string[];
43
+ applyPathsToRouterProxy<T extends HttpServer>(router: T, routeDefinitions: RouteDefinition[], instanceWrapper: InstanceWrapper, moduleKey: string, routePathMetadata: RoutePathMetadata, host: string | RegExp | Array<string | RegExp>): void;
44
+ private applyCallbackToRouter;
45
+ private applyHostFilter;
46
+ private applyVersionFilter;
47
+ private createCallbackProxy;
48
+ createRequestScopedHandler(instanceWrapper: InstanceWrapper, requestMethod: RequestMethod, moduleRef: Module, moduleKey: string, methodName: string): <TRequest extends Record<any, any>, TResponse>(req: TRequest, res: TResponse, next: () => void) => Promise<void>;
49
+ private getContextId;
50
+ private copyMetadataToCallback;
51
+ }
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RouterExplorer = void 0;
4
+ const enums_1 = require("../enums");
5
+ const path_explorer_1 = require("./path.explorer");
6
+ const factory_1 = require("../factory");
7
+ const logger_service_1 = require("../../core/services/logger.service");
8
+ const pipes_1 = require("../../core/pipes");
9
+ const guards_1 = require("../../core/guards");
10
+ const interceptors_1 = require("../../core/interceptors");
11
+ const constants_1 = require("../constants");
12
+ const shared_helper_1 = require("../../core/helpers/shared.helper");
13
+ const helpers_1 = require("../helpers");
14
+ const path_to_regexp_1 = require("path-to-regexp");
15
+ const errors_1 = require("../errors");
16
+ const constants_2 = require("../../core/injector/constants");
17
+ const execution_host_1 = require("../../core/context/execution-host");
18
+ const context_1 = require("../../core/context");
19
+ const context_2 = require("../context/context");
20
+ class RouterExplorer {
21
+ constructor(metadataScanner, container, injector, routerProxy, exceptionsFilter, config, routePathFactory, graphInspector, applicationRef) {
22
+ this.container = container;
23
+ this.injector = injector;
24
+ this.routerProxy = routerProxy;
25
+ this.exceptionsFilter = exceptionsFilter;
26
+ this.routePathFactory = routePathFactory;
27
+ this.graphInspector = graphInspector;
28
+ this.applicationRef = applicationRef;
29
+ this.routerMethodFactory = new factory_1.RouterMethodFactory();
30
+ this.logger = new logger_service_1.Logger(RouterExplorer.name, {
31
+ timestamp: true,
32
+ });
33
+ this.exceptionFiltersCache = new WeakMap();
34
+ this.pathsExplorer = new path_explorer_1.PathsExplorer(metadataScanner);
35
+ this.httpContextCreator = new context_2.HttpContextCreator(context_1.VenokContextCreator.fromContainer(container), this.exceptionsFilter, this.applicationRef);
36
+ const routeParamsFactory = new factory_1.RouteParamsFactory();
37
+ const pipesContextCreator = new pipes_1.PipesContextCreator(container, config);
38
+ const pipesConsumer = new pipes_1.PipesConsumer();
39
+ const guardsContextCreator = new guards_1.GuardsContextCreator(container, config);
40
+ const guardsConsumer = new guards_1.GuardsConsumer();
41
+ const interceptorsContextCreator = new interceptors_1.InterceptorsContextCreator(container, config);
42
+ const interceptorsConsumer = new interceptors_1.InterceptorsConsumer();
43
+ }
44
+ explore(instanceWrapper, moduleKey, applicationRef, host, routePathMetadata) {
45
+ const { instance } = instanceWrapper;
46
+ const routerPaths = this.pathsExplorer.scanForPaths(instance);
47
+ this.applyPathsToRouterProxy(applicationRef, routerPaths, instanceWrapper, moduleKey, routePathMetadata, host);
48
+ }
49
+ extractRouterPath(metatype) {
50
+ const path = Reflect.getMetadata(constants_1.PATH_METADATA, metatype);
51
+ if ((0, shared_helper_1.isUndefined)(path))
52
+ return [];
53
+ if (Array.isArray(path))
54
+ return path.map((p) => (0, helpers_1.addLeadingSlash)(p));
55
+ return [(0, helpers_1.addLeadingSlash)(path)];
56
+ }
57
+ applyPathsToRouterProxy(router, routeDefinitions, instanceWrapper, moduleKey, routePathMetadata, host) {
58
+ (routeDefinitions || []).forEach((routeDefinition) => {
59
+ const { version: methodVersion } = routeDefinition;
60
+ routePathMetadata.methodVersion = methodVersion;
61
+ this.applyCallbackToRouter(router, routeDefinition, instanceWrapper, moduleKey, routePathMetadata, host);
62
+ });
63
+ }
64
+ applyCallbackToRouter(router, routeDefinition, instanceWrapper, moduleKey, routePathMetadata, host) {
65
+ const { path: paths, requestMethod, targetCallback, methodName } = routeDefinition;
66
+ const { instance } = instanceWrapper;
67
+ const routerMethodRef = this.routerMethodFactory.get(router, requestMethod).bind(router);
68
+ const isRequestScoped = !instanceWrapper.isDependencyTreeStatic();
69
+ const proxy = isRequestScoped
70
+ ? this.createRequestScopedHandler(instanceWrapper, requestMethod, this.container.getModuleByKey(moduleKey), moduleKey, methodName)
71
+ : this.createCallbackProxy(instance, targetCallback, methodName, moduleKey, requestMethod);
72
+ const isVersioned = (routePathMetadata.methodVersion || routePathMetadata.controllerVersion) && routePathMetadata.versioningOptions;
73
+ let routeHandler = this.applyHostFilter(host, proxy);
74
+ paths.forEach((path) => {
75
+ if (isVersioned && routePathMetadata.versioningOptions.type !== enums_1.VersioningType.URI) {
76
+ // All versioning (except for URI Versioning) is done via the "Version Filter"
77
+ routeHandler = this.applyVersionFilter(router, routePathMetadata, routeHandler);
78
+ }
79
+ routePathMetadata.methodPath = path;
80
+ const pathsToRegister = this.routePathFactory.create(routePathMetadata, requestMethod);
81
+ pathsToRegister.forEach((path) => {
82
+ const entrypointDefinition = {
83
+ type: "http-endpoint",
84
+ methodName,
85
+ className: instanceWrapper.name,
86
+ classNodeId: instanceWrapper.id,
87
+ metadata: {
88
+ key: path,
89
+ path,
90
+ requestMethod: enums_1.RequestMethod[requestMethod],
91
+ methodVersion: routePathMetadata.methodVersion,
92
+ controllerVersion: routePathMetadata.controllerVersion,
93
+ },
94
+ };
95
+ this.copyMetadataToCallback(targetCallback, routeHandler);
96
+ routerMethodRef(path, routeHandler);
97
+ this.graphInspector.insertEntrypointDefinition(entrypointDefinition, instanceWrapper.id);
98
+ });
99
+ const pathsToLog = this.routePathFactory.create({
100
+ ...routePathMetadata,
101
+ versioningOptions: undefined,
102
+ }, requestMethod);
103
+ pathsToLog.forEach((path) => {
104
+ if (isVersioned) {
105
+ const version = this.routePathFactory.getVersion(routePathMetadata);
106
+ this.logger.log((0, helpers_1.VERSIONED_ROUTE_MAPPED_MESSAGE)(path, requestMethod, version));
107
+ }
108
+ else {
109
+ this.logger.log((0, helpers_1.ROUTE_MAPPED_MESSAGE)(path, requestMethod));
110
+ }
111
+ });
112
+ });
113
+ }
114
+ applyHostFilter(host, handler) {
115
+ if (!host)
116
+ return handler;
117
+ const hosts = Array.isArray(host) ? host : [host];
118
+ const hostRegExps = hosts.map((host) => {
119
+ const keys = [];
120
+ const regexp = (0, path_to_regexp_1.pathToRegexp)(host, keys);
121
+ return { regexp, keys };
122
+ });
123
+ const unsupportedFilteringErrorMessage = Array.isArray(host)
124
+ ? `HTTP adapter does not support filtering on hosts: ["${host.join('", "')}"]`
125
+ : `HTTP adapter does not support filtering on host: "${host}"`;
126
+ return (req, res, next) => {
127
+ req.hosts = {};
128
+ const hostname = this.applicationRef.getRequestHostname(req) || "";
129
+ for (const exp of hostRegExps) {
130
+ const match = hostname.match(exp.regexp);
131
+ if (match) {
132
+ if (exp.keys.length > 0) {
133
+ exp.keys.forEach((key, i) => (req.hosts[key.name] = match[i + 1]));
134
+ }
135
+ else if (exp.regexp && match.groups) {
136
+ for (const groupName in match.groups)
137
+ req.hosts[groupName] = match.groups[groupName];
138
+ }
139
+ return handler(req, res, next);
140
+ }
141
+ }
142
+ if (!next)
143
+ throw new errors_1.InternalServerErrorException(unsupportedFilteringErrorMessage);
144
+ return next();
145
+ };
146
+ }
147
+ applyVersionFilter(router, routePathMetadata, handler) {
148
+ const version = this.routePathFactory.getVersion(routePathMetadata);
149
+ return router.applyVersionFilter(handler, version, routePathMetadata.versioningOptions);
150
+ }
151
+ createCallbackProxy(instance, callback, methodName, moduleRef, requestMethod, contextId = constants_2.STATIC_CONTEXT, inquirerId) {
152
+ return this.httpContextCreator.create(instance, callback, methodName, moduleRef, requestMethod, contextId, inquirerId);
153
+ }
154
+ createRequestScopedHandler(instanceWrapper, requestMethod, moduleRef, moduleKey, methodName) {
155
+ const { instance } = instanceWrapper;
156
+ const collection = moduleRef.injectables;
157
+ const isTreeDurable = instanceWrapper.isDependencyTreeDurable();
158
+ return async (req, res, next) => {
159
+ try {
160
+ const contextId = this.getContextId(req, isTreeDurable);
161
+ const contextInstance = await this.injector.loadPerContext(instance, moduleRef, collection, contextId);
162
+ await this.createCallbackProxy(contextInstance, contextInstance[methodName], methodName, moduleKey, requestMethod, contextId, instanceWrapper.id)(req, res, next);
163
+ }
164
+ catch (err) {
165
+ let exceptionFilter = this.exceptionFiltersCache.get(instance[methodName]);
166
+ if (!exceptionFilter) {
167
+ exceptionFilter = this.exceptionsFilter.create(instance, instance[methodName], moduleKey);
168
+ this.exceptionFiltersCache.set(instance[methodName], exceptionFilter);
169
+ }
170
+ const host = new execution_host_1.ExecutionContextHost([req, res, next]);
171
+ exceptionFilter.next(err, host);
172
+ }
173
+ };
174
+ }
175
+ getContextId(request, isTreeDurable) {
176
+ const contextId = factory_1.ContextIdFactory.getByRequest(request);
177
+ if (!request[constants_1.REQUEST_CONTEXT_ID]) {
178
+ Object.defineProperty(request, constants_1.REQUEST_CONTEXT_ID, {
179
+ value: contextId,
180
+ enumerable: false,
181
+ writable: false,
182
+ configurable: false,
183
+ });
184
+ const requestProviderValue = isTreeDurable ? contextId.payload : request;
185
+ this.container.registerRequestProvider(requestProviderValue, contextId);
186
+ }
187
+ return contextId;
188
+ }
189
+ copyMetadataToCallback(originalCallback, targetCallback) {
190
+ for (const key of Reflect.getMetadataKeys(originalCallback)) {
191
+ Reflect.defineMetadata(key, Reflect.getMetadata(key, originalCallback), targetCallback);
192
+ }
193
+ }
194
+ }
195
+ exports.RouterExplorer = RouterExplorer;
@@ -0,0 +1,41 @@
1
+ import { ContextId, HostComponentInfo } from "@venok/core";
2
+ export type ContextIdResolverFn = (info: HostComponentInfo) => ContextId;
3
+ export interface ContextIdResolver {
4
+ /**
5
+ * Payload associated with the custom context id
6
+ */
7
+ payload: unknown;
8
+ /**
9
+ * A context id resolver function
10
+ */
11
+ resolve: ContextIdResolverFn;
12
+ }
13
+ export interface ContextIdStrategy<T = any> {
14
+ /**
15
+ * Allows to attach a parent context id to the existing child context id.
16
+ * This lets you construct durable DI subtrees that can be shared between contexts.
17
+ * @param contextId auto-generated child context id
18
+ * @param request request object
19
+ */
20
+ attach(contextId: ContextId, request: T): ContextIdResolverFn | ContextIdResolver | undefined;
21
+ }
22
+ export declare class ContextIdFactory {
23
+ private static strategy?;
24
+ /**
25
+ * Generates a context identifier based on the request object.
26
+ */
27
+ static create(): ContextId;
28
+ /**
29
+ * Generates a random identifier to track asynchronous execution context.
30
+ * @param request request object
31
+ * @param propsToInspect
32
+ */
33
+ static getByRequest<T extends Record<any, any> = any>(request: T, propsToInspect?: string[]): ContextId;
34
+ /**
35
+ * Registers a custom context id strategy that lets you attach
36
+ * a parent context id to the existing context id object.
37
+ * @param strategy strategy instance
38
+ */
39
+ static apply(strategy: ContextIdStrategy): void;
40
+ private static isContextIdResolverWithPayload;
41
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ContextIdFactory = void 0;
4
+ const constants_1 = require("@venok/http/constants");
5
+ const context_id_factory_helper_1 = require("../../core/helpers/context-id-factory.helper");
6
+ const shared_helper_1 = require("../../core/helpers/shared.helper");
7
+ class ContextIdFactory {
8
+ /**
9
+ * Generates a context identifier based on the request object.
10
+ */
11
+ static create() {
12
+ return (0, context_id_factory_helper_1.createContextId)();
13
+ }
14
+ /**
15
+ * Generates a random identifier to track asynchronous execution context.
16
+ * @param request request object
17
+ * @param propsToInspect
18
+ */
19
+ static getByRequest(request, propsToInspect = ["raw"]) {
20
+ if (!request)
21
+ return ContextIdFactory.create();
22
+ if (request[constants_1.REQUEST_CONTEXT_ID])
23
+ return request[constants_1.REQUEST_CONTEXT_ID];
24
+ for (const key of propsToInspect)
25
+ if (request[key]?.[constants_1.REQUEST_CONTEXT_ID])
26
+ return request[key][constants_1.REQUEST_CONTEXT_ID];
27
+ if (!this.strategy)
28
+ return ContextIdFactory.create();
29
+ const contextId = (0, context_id_factory_helper_1.createContextId)();
30
+ const resolverObjectOrFunction = this.strategy.attach(contextId, request);
31
+ if (this.isContextIdResolverWithPayload(resolverObjectOrFunction)) {
32
+ contextId.getParent = resolverObjectOrFunction.resolve;
33
+ contextId.payload = resolverObjectOrFunction.payload;
34
+ }
35
+ else {
36
+ contextId.getParent = resolverObjectOrFunction;
37
+ }
38
+ return contextId;
39
+ }
40
+ /**
41
+ * Registers a custom context id strategy that lets you attach
42
+ * a parent context id to the existing context id object.
43
+ * @param strategy strategy instance
44
+ */
45
+ static apply(strategy) {
46
+ this.strategy = strategy;
47
+ }
48
+ static isContextIdResolverWithPayload(resolverOrResolverFn) {
49
+ return (0, shared_helper_1.isObject)(resolverOrResolverFn);
50
+ }
51
+ }
52
+ exports.ContextIdFactory = ContextIdFactory;
@@ -0,0 +1,4 @@
1
+ export * from "./context-id.factory";
2
+ export * from "./method.factory";
3
+ export * from "./params.factory";
4
+ export * from "./path.factory";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./context-id.factory"), exports);
18
+ __exportStar(require("./method.factory"), exports);
19
+ __exportStar(require("./params.factory"), exports);
20
+ __exportStar(require("./path.factory"), exports);
@@ -0,0 +1,5 @@
1
+ import { HttpServer } from "../interfaces";
2
+ import { RequestMethod } from "../enums";
3
+ export declare class RouterMethodFactory {
4
+ get(target: HttpServer, requestMethod: RequestMethod): Function;
5
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RouterMethodFactory = void 0;
4
+ const enums_1 = require("../enums");
5
+ class RouterMethodFactory {
6
+ get(target, requestMethod) {
7
+ switch (requestMethod) {
8
+ case enums_1.RequestMethod.POST:
9
+ return target.post;
10
+ case enums_1.RequestMethod.ALL:
11
+ return target.all;
12
+ case enums_1.RequestMethod.DELETE:
13
+ return target.delete;
14
+ case enums_1.RequestMethod.PUT:
15
+ return target.put;
16
+ case enums_1.RequestMethod.PATCH:
17
+ return target.patch;
18
+ case enums_1.RequestMethod.OPTIONS:
19
+ return target.options;
20
+ case enums_1.RequestMethod.HEAD:
21
+ return target.head;
22
+ case enums_1.RequestMethod.GET:
23
+ return target.get;
24
+ default: {
25
+ return target.use;
26
+ }
27
+ }
28
+ }
29
+ }
30
+ exports.RouterMethodFactory = RouterMethodFactory;
@@ -0,0 +1,5 @@
1
+ import { RouteParamtypes } from "../enums";
2
+ import { IRouteParamsFactory } from "../context/context";
3
+ export declare class RouteParamsFactory implements IRouteParamsFactory {
4
+ exchangeKeyForValue<TRequest extends Record<string, any> = any, TResponse = any, TResult = any>(key: RouteParamtypes | string, data: string | object | any, args: [TRequest, TResponse, Function]): TResult;
5
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RouteParamsFactory = void 0;
4
+ const enums_1 = require("../enums");
5
+ class RouteParamsFactory {
6
+ exchangeKeyForValue(key, data, args) {
7
+ const [req, res, next] = args;
8
+ switch (key) {
9
+ case enums_1.RouteParamtypes.NEXT:
10
+ return next;
11
+ case enums_1.RouteParamtypes.REQUEST:
12
+ return req;
13
+ case enums_1.RouteParamtypes.RESPONSE:
14
+ return res;
15
+ case enums_1.RouteParamtypes.BODY:
16
+ return data && req.body ? req.body[data] : req.body;
17
+ case enums_1.RouteParamtypes.PARAM:
18
+ return data ? req.params[data] : req.params;
19
+ case enums_1.RouteParamtypes.HOST:
20
+ const hosts = req.hosts || {};
21
+ return data ? hosts[data] : hosts;
22
+ case enums_1.RouteParamtypes.QUERY:
23
+ return data ? req.query[data] : req.query;
24
+ case enums_1.RouteParamtypes.HEADERS:
25
+ return data ? req.headers[data.toLowerCase()] : req.headers;
26
+ case enums_1.RouteParamtypes.SESSION:
27
+ return req.session;
28
+ case enums_1.RouteParamtypes.FILE:
29
+ return req[data || "file"];
30
+ case enums_1.RouteParamtypes.FILES:
31
+ return req.files;
32
+ case enums_1.RouteParamtypes.IP:
33
+ return req.ip;
34
+ default:
35
+ return null;
36
+ }
37
+ }
38
+ }
39
+ exports.RouteParamsFactory = RouteParamsFactory;
@@ -0,0 +1,13 @@
1
+ import { HttpConfig } from "../application/config";
2
+ import { RoutePathMetadata, VersioningOptions, VersionValue } from "../interfaces";
3
+ import { RequestMethod } from "../enums";
4
+ export declare class RoutePathFactory {
5
+ private readonly config;
6
+ constructor(config: HttpConfig);
7
+ create(metadata: RoutePathMetadata, requestMethod?: RequestMethod): string[];
8
+ getVersion(metadata: RoutePathMetadata): VersionValue | undefined;
9
+ getVersionPrefix(versioningOptions: VersioningOptions): string;
10
+ appendToAllIfDefined(paths: string[], fragmentToAppend: string | string[] | undefined): string[];
11
+ isExcludedFromGlobalPrefix(path: string, requestMethod?: RequestMethod, versionOrVersions?: VersionValue, versioningOptions?: VersioningOptions): boolean;
12
+ private truncateVersionPrefixFromPath;
13
+ }