@bool-ts/core 1.8.1 → 1.9.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 (54) hide show
  1. package/__test/controller.ts +1 -1
  2. package/__test/dispatcher.ts +2 -2
  3. package/__test/firstGuard.ts +3 -1
  4. package/__test/firstMiddleware.ts +3 -2
  5. package/__test/index.ts +1 -1
  6. package/__test/module.ts +1 -1
  7. package/__test/repository.ts +2 -3
  8. package/__test/secondGuard.ts +1 -1
  9. package/__test/secondMiddleware.ts +2 -2
  10. package/__test/service.ts +4 -7
  11. package/__test/tsconfig.json +6 -2
  12. package/__test/webSocket.ts +1 -1
  13. package/dist/index.js +17 -6
  14. package/package.json +5 -5
  15. package/dist/decorators/arguments.js +0 -123
  16. package/dist/decorators/controller.js +0 -9
  17. package/dist/decorators/dispatcher.js +0 -6
  18. package/dist/decorators/guard.js +0 -9
  19. package/dist/decorators/http.js +0 -60
  20. package/dist/decorators/index.js +0 -13
  21. package/dist/decorators/inject.js +0 -9
  22. package/dist/decorators/injectable.js +0 -3
  23. package/dist/decorators/middleware.js +0 -6
  24. package/dist/decorators/module.js +0 -48
  25. package/dist/decorators/webSocket.js +0 -40
  26. package/dist/decorators/webSocketArguments.js +0 -49
  27. package/dist/decorators/webSocketEvent.js +0 -24
  28. package/dist/decorators/zodSchema.js +0 -15
  29. package/dist/entities/httpRoute.js +0 -268
  30. package/dist/entities/httpRouter.js +0 -27
  31. package/dist/entities/httpRouterGroup.js +0 -24
  32. package/dist/entities/index.js +0 -6
  33. package/dist/entities/webSocketRoute.js +0 -22
  34. package/dist/entities/webSocketRouter.js +0 -54
  35. package/dist/entities/webSocketRouterGroup.js +0 -51
  36. package/dist/hooks/factory.js +0 -1072
  37. package/dist/hooks/index.js +0 -2
  38. package/dist/hooks/injector.js +0 -36
  39. package/dist/http/clientError.js +0 -42
  40. package/dist/http/index.js +0 -40
  41. package/dist/http/serverError.js +0 -24
  42. package/dist/interfaces/context.js +0 -1
  43. package/dist/interfaces/controller.js +0 -1
  44. package/dist/interfaces/dispatcher.js +0 -1
  45. package/dist/interfaces/guard.js +0 -1
  46. package/dist/interfaces/index.js +0 -1
  47. package/dist/interfaces/middleware.js +0 -1
  48. package/dist/interfaces/module.js +0 -1
  49. package/dist/interfaces/webSocket.js +0 -1
  50. package/dist/keys/index.js +0 -31
  51. package/dist/ultils/asyncFunction.js +0 -1
  52. package/dist/ultils/colors.js +0 -41
  53. package/dist/ultils/index.js +0 -3
  54. package/dist/ultils/socket.js +0 -7
@@ -1,60 +0,0 @@
1
- import { controllerHttpKey } from "../keys";
2
- const defaultDecorator = (path, method) => (target, methodName, descriptor) => {
3
- if (!(descriptor?.value instanceof Function)) {
4
- throw Error(`${method} decorator only use for class method.`);
5
- }
6
- const metadata = [
7
- ...(Reflect.getOwnMetadata(controllerHttpKey, target.constructor) || []),
8
- {
9
- path: !path.startsWith("/") ? `/${path}` : path,
10
- httpMethod: method.toUpperCase(),
11
- methodName: methodName,
12
- descriptor: descriptor
13
- }
14
- ];
15
- // Define controller metadata
16
- Reflect.defineMetadata(controllerHttpKey, metadata, target.constructor);
17
- };
18
- /**
19
- *
20
- * @param path
21
- * @returns
22
- */
23
- export const Get = (path = "/") => defaultDecorator(path, "Get");
24
- /**
25
- *
26
- * @param path
27
- * @returns
28
- */
29
- export const Post = (path = "/") => defaultDecorator(path, "Post");
30
- /**
31
- *
32
- * @param path
33
- * @returns
34
- */
35
- export const Put = (path = "/") => defaultDecorator(path, "Put");
36
- /**
37
- *
38
- * @param path
39
- * @returns
40
- */
41
- export const Patch = (path = "/") => defaultDecorator(path, "Patch");
42
- /**
43
- *
44
- * @param path
45
- * @returns
46
- */
47
- export const Delete = (path = "/") => defaultDecorator(path, "Delete");
48
- /**
49
- *
50
- * @param path
51
- * @returns
52
- */
53
- export const Options = (path = "/") => defaultDecorator(path, "Options");
54
- export default {
55
- Get,
56
- Post,
57
- Put,
58
- Patch,
59
- Delete
60
- };
@@ -1,13 +0,0 @@
1
- export { Context, Param, Params, Query, Request, RequestBody, RequestHeader, RequestHeaders, ResponseHeaders, RouteModel } from "./arguments";
2
- export { Controller } from "./controller";
3
- export { Dispatcher } from "./dispatcher";
4
- export { Guard } from "./guard";
5
- export { Delete, Get, Options, Patch, Post, Put } from "./http";
6
- export { Inject } from "./inject";
7
- export { Injectable } from "./injectable";
8
- export { Middleware } from "./middleware";
9
- export { Module } from "./module";
10
- export { WebSocket } from "./webSocket";
11
- export { WebSocketCloseCode, WebSocketCloseReason, WebSocketConnection, WebSocketServer } from "./webSocketArguments";
12
- export { WebSocketEvent } from "./webSocketEvent";
13
- export { ZodSchema } from "./zodSchema";
@@ -1,9 +0,0 @@
1
- import { injectKey } from "../keys";
2
- export const Inject = (definition) => {
3
- return (target, methodName, parameterIndex) => {
4
- const designParameterTypes = Reflect.getMetadata(injectKey, target) || [];
5
- designParameterTypes[parameterIndex] = definition;
6
- Reflect.defineMetadata(injectKey, designParameterTypes, target);
7
- };
8
- };
9
- export default Inject;
@@ -1,3 +0,0 @@
1
- import { injectableKey } from "../keys";
2
- export const Injectable = () => (target) => Reflect.defineMetadata(injectableKey, undefined, target);
3
- export default Injectable;
@@ -1,6 +0,0 @@
1
- import { middlewareKey } from "../keys";
2
- export const Middleware = () => (target) => {
3
- const metadata = undefined;
4
- Reflect.defineMetadata(middlewareKey, metadata, target);
5
- };
6
- export default Middleware;
@@ -1,48 +0,0 @@
1
- import { controllerKey, dispatcherKey, guardKey, injectableKey, middlewareKey, moduleKey, webSocketKey } from "../keys";
2
- export const Module = (args) => (target) => {
3
- const { middlewares, guards, dispatchers, controllers, dependencies, webSockets } = args || {};
4
- if (middlewares) {
5
- for (let i = 0; i < middlewares.length; i++) {
6
- if (!Reflect.getOwnMetadataKeys(middlewares[i]).includes(middlewareKey)) {
7
- throw Error(`${middlewares[i].name} is not a middleware.`);
8
- }
9
- }
10
- }
11
- if (guards) {
12
- for (let i = 0; i < guards.length; i++) {
13
- if (!Reflect.getOwnMetadataKeys(guards[i]).includes(guardKey)) {
14
- throw Error(`${guards[i].name} is not a guard.`);
15
- }
16
- }
17
- }
18
- if (dispatchers) {
19
- for (let i = 0; i < dispatchers.length; i++) {
20
- if (!Reflect.getOwnMetadataKeys(dispatchers[i]).includes(dispatcherKey)) {
21
- throw Error(`${dispatchers[i].name} is not a dispatcher.`);
22
- }
23
- }
24
- }
25
- if (controllers) {
26
- for (let i = 0; i < controllers.length; i++) {
27
- if (!Reflect.getOwnMetadataKeys(controllers[i]).includes(controllerKey)) {
28
- throw Error(`${controllers[i].name} is not a controller.`);
29
- }
30
- }
31
- }
32
- if (dependencies) {
33
- for (let i = 0; i < dependencies.length; i++) {
34
- if (!Reflect.getOwnMetadataKeys(dependencies[i]).includes(injectableKey)) {
35
- throw Error(`${dependencies[i].name} is not an injectable.`);
36
- }
37
- }
38
- }
39
- if (webSockets) {
40
- for (let i = 0; i < webSockets.length; i++) {
41
- if (!Reflect.getOwnMetadataKeys(webSockets[i]).includes(webSocketKey)) {
42
- throw Error(`${webSockets[i].name} is not a websocket gateway.`);
43
- }
44
- }
45
- }
46
- Reflect.defineMetadata(moduleKey, args, target);
47
- };
48
- export default Module;
@@ -1,40 +0,0 @@
1
- import { webSocketEventKey, webSocketKey } from "../keys";
2
- const upgradeHandlerSymbol = Symbol("__bool:webSocket.upgrade__");
3
- const upgradeHandler = (server, request, query) => {
4
- const url = new URL(request.url);
5
- return server.upgrade(request, {
6
- data: {
7
- method: request.method.toUpperCase(),
8
- pathname: url.pathname,
9
- query: query
10
- }
11
- });
12
- };
13
- export const WebSocket = (args) => (target) => {
14
- const { prefix } = args || {};
15
- target.prototype[upgradeHandlerSymbol] = upgradeHandler;
16
- const descriptor = Object.getOwnPropertyDescriptor(target.prototype, upgradeHandlerSymbol);
17
- const httpMetadata = !descriptor
18
- ? []
19
- : [
20
- {
21
- path: "/",
22
- httpMethod: "GET",
23
- methodName: upgradeHandlerSymbol,
24
- descriptor: descriptor
25
- },
26
- {
27
- path: "/",
28
- httpMethod: "POST",
29
- methodName: upgradeHandlerSymbol,
30
- descriptor: descriptor
31
- }
32
- ];
33
- const metadata = {
34
- prefix: !prefix?.startsWith("/") ? `/${prefix || ""}` : prefix,
35
- events: Reflect.getOwnMetadata(webSocketEventKey, target) || {},
36
- http: httpMetadata
37
- };
38
- Reflect.defineMetadata(webSocketKey, metadata, target);
39
- };
40
- export default WebSocket;
@@ -1,49 +0,0 @@
1
- import { webSocketCloseCodeArgsKey, webSocketCloseReasonArgsKey, webSocketConnectionArgsKey, webSocketEventArgumentsKey, webSocketMessageArgsKey, webSocketServerArgsKey } from "../keys";
2
- export const WebSocketConnection = () => (target, methodName, parameterIndex) => {
3
- if (!methodName) {
4
- return;
5
- }
6
- const webSocketEventArgumentsMetadata = Reflect.getOwnMetadata(webSocketEventArgumentsKey, target.constructor, methodName) ||
7
- {};
8
- webSocketEventArgumentsMetadata[`argumentIndexes.${parameterIndex}`] = {
9
- index: parameterIndex,
10
- type: webSocketConnectionArgsKey
11
- };
12
- Reflect.defineMetadata(webSocketEventArgumentsKey, webSocketEventArgumentsMetadata, target.constructor, methodName);
13
- };
14
- export const WebSocketServer = () => (target, methodName, parameterIndex) => {
15
- if (!methodName) {
16
- return;
17
- }
18
- const webSocketEventArgumentsMetadata = Reflect.getOwnMetadata(webSocketEventArgumentsKey, target.constructor, methodName) ||
19
- {};
20
- webSocketEventArgumentsMetadata[`argumentIndexes.${parameterIndex}`] = {
21
- index: parameterIndex,
22
- type: webSocketServerArgsKey
23
- };
24
- Reflect.defineMetadata(webSocketEventArgumentsKey, webSocketEventArgumentsMetadata, target.constructor, methodName);
25
- };
26
- export const WebSocketCloseCode = () => (target, methodName, parameterIndex) => {
27
- if (!methodName) {
28
- return;
29
- }
30
- const webSocketEventArgumentsMetadata = Reflect.getOwnMetadata(webSocketEventArgumentsKey, target.constructor, methodName) ||
31
- {};
32
- webSocketEventArgumentsMetadata[`argumentIndexes.${parameterIndex}`] = {
33
- index: parameterIndex,
34
- type: webSocketCloseCodeArgsKey
35
- };
36
- Reflect.defineMetadata(webSocketEventArgumentsKey, webSocketEventArgumentsMetadata, target.constructor, methodName);
37
- };
38
- export const WebSocketCloseReason = () => (target, methodName, parameterIndex) => {
39
- if (!methodName) {
40
- return;
41
- }
42
- const webSocketEventArgumentsMetadata = Reflect.getOwnMetadata(webSocketEventArgumentsKey, target.constructor, methodName) ||
43
- {};
44
- webSocketEventArgumentsMetadata[`argumentIndexes.${parameterIndex}`] = {
45
- index: parameterIndex,
46
- type: webSocketCloseReasonArgsKey
47
- };
48
- Reflect.defineMetadata(webSocketEventArgumentsKey, webSocketEventArgumentsMetadata, target.constructor, methodName);
49
- };
@@ -1,24 +0,0 @@
1
- import { webSocketEventArgumentsKey, webSocketEventKey } from "../keys";
2
- /**
3
- *
4
- * @param path
5
- * @returns
6
- */
7
- export const WebSocketEvent = (eventName) => (target, methodName, descriptor) => {
8
- if (!(descriptor.value instanceof Function)) {
9
- throw Error("WebSocketEvent decorator only use for class's method.");
10
- }
11
- const webSocketEventArgumentsMetadata = Reflect.getOwnMetadata(webSocketEventArgumentsKey, target.constructor, methodName);
12
- const webSocketEventMetadata = Object.freeze({
13
- methodName: methodName,
14
- descriptor: descriptor,
15
- arguments: webSocketEventArgumentsMetadata
16
- });
17
- const webSocketMetadata = {
18
- ...(Reflect.getOwnMetadata(webSocketEventKey, target.constructor) || undefined),
19
- [eventName]: webSocketEventMetadata
20
- };
21
- Reflect.defineMetadata(webSocketEventKey, webSocketEventMetadata, target.constructor, methodName);
22
- Reflect.defineMetadata(webSocketEventKey, webSocketMetadata, target.constructor);
23
- };
24
- export default WebSocket;
@@ -1,15 +0,0 @@
1
- import * as Zod from "zod";
2
- import { zodSchemaKey } from "../keys";
3
- export const ZodSchema = (schema) => {
4
- return (target, methodName, parameterIndex) => {
5
- if (!methodName) {
6
- return;
7
- }
8
- const zodSchemasMetadata = Reflect.getOwnMetadata(zodSchemaKey, target.constructor, methodName) || {};
9
- zodSchemasMetadata[`paramterIndexes.${parameterIndex}`] = {
10
- index: parameterIndex,
11
- schema: schema
12
- };
13
- Reflect.defineMetadata(zodSchemaKey, zodSchemasMetadata, target.constructor, methodName);
14
- };
15
- };
@@ -1,268 +0,0 @@
1
- "use strict";
2
- export class HttpRoute {
3
- static rootPattern = ":([a-z0-9A-Z_-]{1,})";
4
- static innerRootPattern = "([a-z0-9A-Z_-]{1,})";
5
- alias;
6
- _map = new Map();
7
- constructor(alias) {
8
- this.alias = this._thinAlias(alias);
9
- }
10
- test(pathname, method) {
11
- try {
12
- const model = this._map.get(method);
13
- const aliasSplitted = this.alias.split("/");
14
- const currentPathNameSplitted = this._thinAlias(pathname).split("/");
15
- if (!model) {
16
- return undefined;
17
- }
18
- // Compare splitted length
19
- if (aliasSplitted.length !== currentPathNameSplitted.length) {
20
- return undefined;
21
- }
22
- const parameters = Object();
23
- const matchingRegex = this.alias.replace(new RegExp(HttpRoute.rootPattern, "g"), HttpRoute.innerRootPattern);
24
- if (!new RegExp(matchingRegex).test(this._thinAlias(pathname))) {
25
- return undefined;
26
- }
27
- for (let index = 0; index < aliasSplitted.length; index++) {
28
- const aliasPart = aliasSplitted[index];
29
- const pathnamePart = currentPathNameSplitted[index];
30
- // Check pathmane path match a dynamic syntax, if no match => start compare equal or not
31
- if (!new RegExp(HttpRoute.rootPattern, "g").test(aliasPart)) {
32
- if (aliasPart !== pathnamePart)
33
- return undefined;
34
- }
35
- else {
36
- let isFailed = false;
37
- aliasPart.replace(new RegExp(HttpRoute.rootPattern, "g"), (match, key, offset) => {
38
- if (offset === 0) {
39
- pathnamePart.replace(new RegExp(HttpRoute.innerRootPattern, "g"), (innerMatch, innerKey, innerOffset) => {
40
- if (innerOffset === 0) {
41
- Object.assign(parameters, {
42
- [key]: innerMatch
43
- });
44
- }
45
- return innerMatch;
46
- });
47
- }
48
- return match;
49
- });
50
- if (isFailed) {
51
- return undefined;
52
- }
53
- }
54
- continue;
55
- }
56
- return Object.freeze({
57
- parameters: parameters,
58
- model: model
59
- });
60
- }
61
- catch (err) {
62
- console.error(err);
63
- return false;
64
- }
65
- }
66
- /**
67
- *
68
- */
69
- isMatch(pathname, method) {
70
- try {
71
- const handlers = this._map.get(method);
72
- if (!handlers) {
73
- return undefined;
74
- }
75
- const aliasSplitted = this.alias.split("/");
76
- const currentPathNameSplitted = this._thinAlias(pathname).split("/");
77
- // Compare splitted length
78
- if (aliasSplitted.length !== currentPathNameSplitted.length) {
79
- return false;
80
- }
81
- const parameters = Object();
82
- for (let index = 0; index < aliasSplitted.length; index++) {
83
- const aliasPart = aliasSplitted[index];
84
- const pathnamePart = currentPathNameSplitted[index];
85
- // Check pathmane path match a dynamic syntax, if no match => start compare equal or not
86
- if (!new RegExp(HttpRoute.rootPattern, "g").test(aliasPart)) {
87
- if (aliasPart !== pathnamePart) {
88
- return false;
89
- }
90
- }
91
- else {
92
- let isFailed = false;
93
- aliasPart.replace(new RegExp(HttpRoute.rootPattern, "g"), (subString, key, value) => {
94
- if (!new RegExp(value, "g").test(pathnamePart)) {
95
- isFailed = true;
96
- }
97
- else {
98
- Object.assign(parameters, {
99
- [key]: pathnamePart
100
- });
101
- }
102
- return "";
103
- });
104
- if (isFailed) {
105
- return false;
106
- }
107
- }
108
- continue;
109
- }
110
- return true;
111
- }
112
- catch (err) {
113
- console.error(err);
114
- return undefined;
115
- }
116
- }
117
- /**
118
- *
119
- * @param handlers
120
- * @returns
121
- */
122
- get(handler) {
123
- const currenTHttpRouteModel = this._map.get("GET");
124
- if (!currenTHttpRouteModel) {
125
- this._map.set("GET", handler);
126
- }
127
- return this;
128
- }
129
- /**
130
- *
131
- * @param handlers
132
- * @returns
133
- */
134
- post(handler) {
135
- const currenTHttpRouteModel = this._map.get("POST");
136
- if (!currenTHttpRouteModel) {
137
- this._map.set("POST", handler);
138
- }
139
- return this;
140
- }
141
- /**
142
- *
143
- * @param handlers
144
- * @returns
145
- */
146
- put(handler) {
147
- const currenTHttpRouteModel = this._map.get("PUT");
148
- if (!currenTHttpRouteModel) {
149
- this._map.set("PUT", handler);
150
- }
151
- return this;
152
- }
153
- /**
154
- *
155
- * @param handlers
156
- * @returns
157
- */
158
- delete(handler) {
159
- const currenTHttpRouteModel = this._map.get("DELETE");
160
- if (!currenTHttpRouteModel) {
161
- this._map.set("DELETE", handler);
162
- }
163
- return this;
164
- }
165
- /**
166
- *
167
- * @param handlers
168
- * @returns
169
- */
170
- connect(handler) {
171
- const currenTHttpRouteModel = this._map.get("CONNECT");
172
- if (!currenTHttpRouteModel) {
173
- return this._map.set("CONNECT", handler);
174
- }
175
- return this;
176
- }
177
- /**
178
- *
179
- * @param handlers
180
- * @returns
181
- */
182
- options(handler) {
183
- const currenTHttpRouteModel = this._map.get("OPTIONS");
184
- if (!currenTHttpRouteModel) {
185
- return this._map.set("OPTIONS", handler);
186
- }
187
- return this;
188
- }
189
- /**
190
- *
191
- * @param handlers
192
- * @returns
193
- */
194
- trace(handler) {
195
- const currenTHttpRouteModel = this._map.get("TRACE");
196
- if (!currenTHttpRouteModel) {
197
- return this._map.set("TRACE", handler);
198
- }
199
- return this;
200
- }
201
- /**
202
- *
203
- * @param handlers
204
- * @returns
205
- */
206
- patch(handler) {
207
- const currenTHttpRouteModel = this._map.get("PATCH");
208
- if (!currenTHttpRouteModel) {
209
- return this._map.set("PATCH", handler);
210
- }
211
- return this;
212
- }
213
- /**
214
- *
215
- * @param handlers
216
- * @returns
217
- */
218
- _thinAlias(alias) {
219
- return alias
220
- .replace(new RegExp("[/]{2,}", "g"), "/")
221
- .replace(new RegExp("^[/]|[/]$", "g"), "");
222
- }
223
- /**
224
- * Internal get fullpath after check regular expression
225
- * @returns
226
- */
227
- get _fullPath() {
228
- // Split path to start filter
229
- const pathSplited = this.alias.split("/");
230
- const blockFiltered = pathSplited.map((value, index) => {
231
- // Initialize full parameter regex to validate
232
- let validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})(\\(.*?\\))", "g");
233
- if (!validateReg.test(value)) {
234
- // Initialize key parameter regex to validate
235
- validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})", "g");
236
- if (!validateReg.test(value)) {
237
- return value;
238
- }
239
- return value.replace(validateReg, (value, index) => `${value}(.*?)`);
240
- }
241
- return value;
242
- });
243
- return blockFiltered.join("/");
244
- }
245
- /**
246
- * Internal get filterd path after check regular expression
247
- * @returns
248
- */
249
- get _filteredPath() {
250
- // Split path to start filter
251
- const pathSplited = this.alias.split("/");
252
- //
253
- const blockFiltered = pathSplited.map((value, index) => {
254
- let validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})((.*?))", "g");
255
- if (!validateReg.test(value)) {
256
- // Initialize key parameter regex to validate
257
- validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})", "g");
258
- if (!validateReg.test(value)) {
259
- return value;
260
- }
261
- return value.replace(validateReg, (value, index) => "(.*?)");
262
- }
263
- return value.replace(validateReg, (subString, arg_01, arg_02) => arg_02);
264
- });
265
- return blockFiltered.join("/");
266
- }
267
- }
268
- export default HttpRoute;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- import HttpRoute from "./httpRoute";
3
- export class HttpRouter {
4
- alias;
5
- _routes = new Map();
6
- constructor(alias) {
7
- this.alias = this._thinAlias(alias);
8
- }
9
- route(alias) {
10
- const thinAlias = this._thinAlias(`${this.alias}/${alias}`);
11
- const route = this._routes.get(thinAlias);
12
- const newRoute = !route ? new HttpRoute(`${this.alias}/${alias}`) : route;
13
- if (!route) {
14
- this._routes.set(thinAlias, newRoute);
15
- }
16
- return newRoute;
17
- }
18
- _thinAlias(alias) {
19
- return alias
20
- .replace(new RegExp("[/]{2,}", "g"), "/")
21
- .replace(new RegExp("^[/]|[/]$", "g"), "");
22
- }
23
- get routes() {
24
- return this._routes;
25
- }
26
- }
27
- export default HttpRouter;
@@ -1,24 +0,0 @@
1
- export class HttpRouterGroup {
2
- _routers = new Map();
3
- add(...routers) {
4
- for (let i = 0; i < routers.length; i++) {
5
- if (this._routers.has(routers[i].alias)) {
6
- continue;
7
- }
8
- this._routers.set(routers[i].alias, routers[i]);
9
- }
10
- return this;
11
- }
12
- find(pathame, method) {
13
- for (const router of [...this._routers.values()]) {
14
- for (const route of router.routes.values()) {
15
- const result = route.test(pathame, method);
16
- if (!result) {
17
- continue;
18
- }
19
- return result;
20
- }
21
- }
22
- return undefined;
23
- }
24
- }
@@ -1,6 +0,0 @@
1
- export { HttpRoute } from "./httpRoute";
2
- export { HttpRouter } from "./httpRouter";
3
- export { HttpRouterGroup } from "./httpRouterGroup";
4
- export { WebSocketRoute } from "./webSocketRoute";
5
- export { WebSocketRouter } from "./webSocketRouter";
6
- export { WebSocketRouterGroup } from "./webSocketRouterGroup";
@@ -1,22 +0,0 @@
1
- export class WebSocketRoute {
2
- eventName;
3
- metadata;
4
- _context = undefined;
5
- constructor({ eventName, metadata }) {
6
- this.eventName = eventName;
7
- this.metadata = metadata;
8
- }
9
- bind(instance) {
10
- this._context = instance;
11
- return this;
12
- }
13
- execute() {
14
- return Object.freeze({
15
- methodName: this.metadata.methodName,
16
- descriptor: !this._context || typeof this.metadata.descriptor.value !== "function"
17
- ? this.metadata.descriptor
18
- : this.metadata.descriptor.value.bind(this._context),
19
- arguments: this.metadata.arguments
20
- });
21
- }
22
- }
@@ -1,54 +0,0 @@
1
- export class WebSocketRouter {
2
- rawAlias;
3
- alias;
4
- routes = [];
5
- constructor(rawAlias = "/") {
6
- this.rawAlias = rawAlias;
7
- this.alias = WebSocketRouter.thinAlias(rawAlias);
8
- }
9
- /**
10
- * Add websocket routes into router and start analysis
11
- * @param routes
12
- * @returns
13
- */
14
- addRoutes(...routes) {
15
- for (const route of routes) {
16
- if (!this.routes.includes(route)) {
17
- this.routes.push(route);
18
- }
19
- }
20
- return this;
21
- }
22
- /**
23
- * Bind context for descriptor handler in the router
24
- * @param instance
25
- * @returns
26
- */
27
- bind(instance) {
28
- for (const route of this.routes) {
29
- route.bind(instance);
30
- }
31
- return this;
32
- }
33
- /**
34
- * Generate map for websocket handler metadata
35
- * @returns
36
- */
37
- execute() {
38
- const map = new Map();
39
- for (const route of this.routes) {
40
- map.set(`${this.alias}:::${route.eventName}`, route.execute());
41
- }
42
- return map;
43
- }
44
- /**
45
- * Handle alias for router, standardize
46
- * @param alias
47
- * @returns
48
- */
49
- static thinAlias(alias) {
50
- return alias
51
- .replace(new RegExp("[/]{2,}", "g"), "/")
52
- .replace(new RegExp("^[/]|[/]$", "g"), "");
53
- }
54
- }