@bool-ts/core 1.5.7 → 1.5.9

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 (48) hide show
  1. package/.prettierrc +11 -11
  2. package/LICENSE +21 -21
  3. package/__test/afterDispatcher.ts +9 -9
  4. package/__test/beforeDispatcher.ts +9 -9
  5. package/__test/controller.ts +68 -68
  6. package/__test/firstGuard.ts +10 -10
  7. package/__test/firstMiddleware.ts +9 -9
  8. package/__test/index.ts +8 -8
  9. package/__test/interfaces.ts +7 -7
  10. package/__test/module.ts +22 -22
  11. package/__test/repository.ts +16 -16
  12. package/__test/secondGuard.ts +10 -10
  13. package/__test/secondMiddleware.ts +9 -9
  14. package/__test/service.ts +20 -20
  15. package/bun.lockb +0 -0
  16. package/dist/entities/route.js +0 -1
  17. package/jsconfig.json +27 -27
  18. package/package.json +1 -1
  19. package/src/decorators/arguments.ts +214 -214
  20. package/src/decorators/controller.ts +22 -22
  21. package/src/decorators/dispatcher.ts +19 -19
  22. package/src/decorators/guard.ts +19 -19
  23. package/src/decorators/http.ts +81 -81
  24. package/src/decorators/index.ts +28 -28
  25. package/src/decorators/inject.ts +13 -13
  26. package/src/decorators/injectable.ts +8 -8
  27. package/src/decorators/middleware.ts +19 -19
  28. package/src/decorators/module.ts +92 -92
  29. package/src/decorators/zodSchema.ts +20 -20
  30. package/src/entities/index.ts +3 -3
  31. package/src/entities/route.ts +327 -328
  32. package/src/entities/router.ts +35 -35
  33. package/src/entities/routerGroup.ts +34 -34
  34. package/src/hooks/factory.ts +616 -616
  35. package/src/hooks/index.ts +2 -2
  36. package/src/hooks/injector.ts +43 -43
  37. package/src/http/clientError.ts +45 -45
  38. package/src/http/index.ts +61 -61
  39. package/src/http/serverError.ts +38 -38
  40. package/src/index.ts +6 -6
  41. package/src/interfaces/controller.ts +1 -1
  42. package/src/interfaces/dispatcher.ts +3 -3
  43. package/src/interfaces/guard.ts +3 -3
  44. package/src/interfaces/index.ts +5 -5
  45. package/src/interfaces/middleware.ts +3 -3
  46. package/src/interfaces/module.ts +1 -1
  47. package/test.http +31 -31
  48. package/tsconfig.json +108 -108
@@ -1,328 +1,327 @@
1
- "use strict";
2
-
3
- import type { THttpMethods } from "../http";
4
-
5
- type THandler<T = unknown> = Required<{
6
- class: new (...args: Array<any>) => T;
7
- funcName: string | symbol;
8
- func: (...args: Array<any>) => unknown;
9
- }>;
10
-
11
- export class Route {
12
- public static rootPattern = ":([a-z0-9A-Z_.-]{1,})";
13
-
14
- public readonly alias: string;
15
-
16
- private _map = new Map<keyof THttpMethods, Array<THandler>>();
17
-
18
- constructor(alias: string) {
19
- this.alias = this._thinAlias(alias);
20
- }
21
-
22
- public test(
23
- pathname: string,
24
- method: keyof THttpMethods
25
- ): Readonly<{ params: Record<string, string>; handlers: Array<THandler> }> | false | undefined {
26
- try {
27
- const handlers = this._map.get(method);
28
- const aliasSplitted = this.alias.split("/");
29
- const currentPathNameSplitted = this._thinAlias(pathname).split("/");
30
-
31
- if (!handlers) {
32
- return undefined;
33
- }
34
-
35
- // Compare splitted length
36
- if (aliasSplitted.length !== currentPathNameSplitted.length) {
37
- return undefined;
38
- }
39
-
40
- const parameters: Record<string, string> = Object();
41
-
42
- for (let index = 0; index < aliasSplitted.length; index++) {
43
- const aliasPart = aliasSplitted[index];
44
- const pathnamePart = currentPathNameSplitted[index];
45
-
46
- // Check pathmane path match a dynamic syntax, if no match => start compare equal or not
47
- if (!new RegExp(Route.rootPattern, "g").test(aliasPart)) {
48
- if (aliasPart !== pathnamePart) return undefined;
49
- } else {
50
- let isFailed = false;
51
-
52
- aliasPart.replace(new RegExp(Route.rootPattern, "g"), (match, key, offset) => {
53
- if (offset === 0) {
54
- Object.assign(parameters, {
55
- [key]: pathnamePart
56
- });
57
- }
58
-
59
- return match;
60
- });
61
-
62
- if (isFailed) {
63
- console.log(3, "isFailed", isFailed);
64
- return undefined;
65
- }
66
- }
67
-
68
- continue;
69
- }
70
-
71
- return Object.freeze({
72
- params: parameters,
73
- handlers: handlers
74
- });
75
- } catch (err) {
76
- console.error(err);
77
- return false;
78
- }
79
- }
80
-
81
- /**
82
- *
83
- */
84
- public isMatch(pathname: string, method: keyof THttpMethods) {
85
- try {
86
- const handlers = this._map.get(method);
87
-
88
- if (!handlers) {
89
- return undefined;
90
- }
91
-
92
- const aliasSplitted = this.alias.split("/");
93
- const currentPathNameSplitted = this._thinAlias(pathname).split("/");
94
-
95
- // Compare splitted length
96
- if (aliasSplitted.length !== currentPathNameSplitted.length) {
97
- return false;
98
- }
99
-
100
- const parameters = Object();
101
-
102
- for (let index = 0; index < aliasSplitted.length; index++) {
103
- const aliasPart = aliasSplitted[index];
104
- const pathnamePart = currentPathNameSplitted[index];
105
-
106
- // Check pathmane path match a dynamic syntax, if no match => start compare equal or not
107
- if (!new RegExp(Route.rootPattern, "g").test(aliasPart)) {
108
- if (aliasPart !== pathnamePart) {
109
- return false;
110
- }
111
- } else {
112
- let isFailed = false;
113
-
114
- aliasPart.replace(new RegExp(Route.rootPattern, "g"), (subString, key, value) => {
115
- if (!new RegExp(value, "g").test(pathnamePart)) {
116
- isFailed = true;
117
- } else {
118
- Object.assign(parameters, {
119
- [key]: pathnamePart
120
- });
121
- }
122
- return "";
123
- });
124
-
125
- if (isFailed) {
126
- return false;
127
- }
128
- }
129
-
130
- continue;
131
- }
132
-
133
- return true;
134
- } catch (err) {
135
- console.error(err);
136
- return undefined;
137
- }
138
- }
139
-
140
- /**
141
- *
142
- * @param handlers
143
- * @returns
144
- */
145
- public get(...handlers: Array<THandler>) {
146
- const currentHandlers = this._map.get("GET");
147
-
148
- if (!currentHandlers) {
149
- this._map.set("GET", handlers);
150
- }
151
-
152
- return this;
153
- }
154
-
155
- /**
156
- *
157
- * @param handlers
158
- * @returns
159
- */
160
- public post(...handlers: Array<THandler>) {
161
- const currentHandlers = this._map.get("POST");
162
-
163
- if (!currentHandlers) {
164
- this._map.set("POST", handlers);
165
- }
166
-
167
- return this;
168
- }
169
-
170
- /**
171
- *
172
- * @param handlers
173
- * @returns
174
- */
175
- public put(...handlers: Array<THandler>) {
176
- const currentHandlers = this._map.get("PUT");
177
-
178
- if (!currentHandlers) {
179
- this._map.set("PUT", handlers);
180
- }
181
-
182
- return this;
183
- }
184
-
185
- /**
186
- *
187
- * @param handlers
188
- * @returns
189
- */
190
- public delete(...handlers: Array<THandler>) {
191
- const currentHandlers = this._map.get("DELETE");
192
-
193
- if (!currentHandlers) {
194
- this._map.set("DELETE", handlers);
195
- }
196
-
197
- return this;
198
- }
199
-
200
- /**
201
- *
202
- * @param handlers
203
- * @returns
204
- */
205
- public connect(...handlers: Array<THandler>) {
206
- const currentHandlers = this._map.get("CONNECT");
207
-
208
- if (!currentHandlers) {
209
- return this._map.set("CONNECT", handlers);
210
- }
211
-
212
- return this;
213
- }
214
-
215
- /**
216
- *
217
- * @param handlers
218
- * @returns
219
- */
220
- public options(...handlers: Array<THandler>) {
221
- const currentHandlers = this._map.get("OPTIONS");
222
-
223
- if (!currentHandlers) {
224
- return this._map.set("OPTIONS", handlers);
225
- }
226
-
227
- return this;
228
- }
229
-
230
- /**
231
- *
232
- * @param handlers
233
- * @returns
234
- */
235
- public trace(...handlers: Array<THandler>) {
236
- const currentHandlers = this._map.get("TRACE");
237
-
238
- if (!currentHandlers) {
239
- return this._map.set("TRACE", handlers);
240
- }
241
-
242
- return this;
243
- }
244
-
245
- /**
246
- *
247
- * @param handlers
248
- * @returns
249
- */
250
- public patch(...handlers: Array<THandler>) {
251
- const currentHandlers = this._map.get("PATCH");
252
-
253
- if (!currentHandlers) {
254
- return this._map.set("PATCH", handlers);
255
- }
256
-
257
- return this;
258
- }
259
-
260
- /**
261
- *
262
- * @param handlers
263
- * @returns
264
- */
265
- private _thinAlias(alias: string) {
266
- return alias.replace(new RegExp("[/]{2,}", "g"), "/").replace(new RegExp("^[/]|[/]$", "g"), "");
267
- }
268
-
269
- /**
270
- * Internal get fullpath after check regular expression
271
- * @returns
272
- */
273
- get _fullPath(): string {
274
- // Split path to start filter
275
- const pathSplited = this.alias.split("/");
276
-
277
- const blockFiltered = pathSplited.map((value, index) => {
278
- // Initialize full parameter regex to validate
279
- let validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})(\\(.*?\\))", "g");
280
-
281
- if (!validateReg.test(value)) {
282
- // Initialize key parameter regex to validate
283
- validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})", "g");
284
-
285
- if (!validateReg.test(value)) {
286
- return value;
287
- }
288
-
289
- return value.replace(validateReg, (value, index) => `${value}(.*?)`);
290
- }
291
-
292
- return value;
293
- });
294
-
295
- return blockFiltered.join("/");
296
- }
297
-
298
- /**
299
- * Internal get filterd path after check regular expression
300
- * @returns
301
- */
302
- get _filteredPath(): string {
303
- // Split path to start filter
304
- const pathSplited = this.alias.split("/");
305
-
306
- //
307
- const blockFiltered = pathSplited.map((value, index) => {
308
- let validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})((.*?))", "g");
309
-
310
- if (!validateReg.test(value)) {
311
- // Initialize key parameter regex to validate
312
- validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})", "g");
313
-
314
- if (!validateReg.test(value)) {
315
- return value;
316
- }
317
-
318
- return value.replace(validateReg, (value, index) => "(.*?)");
319
- }
320
-
321
- return value.replace(validateReg, (subString, arg_01, arg_02) => arg_02);
322
- });
323
-
324
- return blockFiltered.join("/");
325
- }
326
- }
327
-
328
- export default Route;
1
+ "use strict";
2
+
3
+ import type { THttpMethods } from "../http";
4
+
5
+ type THandler<T = unknown> = Required<{
6
+ class: new (...args: Array<any>) => T;
7
+ funcName: string | symbol;
8
+ func: (...args: Array<any>) => unknown;
9
+ }>;
10
+
11
+ export class Route {
12
+ public static rootPattern = ":([a-z0-9A-Z_.-]{1,})";
13
+
14
+ public readonly alias: string;
15
+
16
+ private _map = new Map<keyof THttpMethods, Array<THandler>>();
17
+
18
+ constructor(alias: string) {
19
+ this.alias = this._thinAlias(alias);
20
+ }
21
+
22
+ public test(
23
+ pathname: string,
24
+ method: keyof THttpMethods
25
+ ): Readonly<{ params: Record<string, string>; handlers: Array<THandler> }> | false | undefined {
26
+ try {
27
+ const handlers = this._map.get(method);
28
+ const aliasSplitted = this.alias.split("/");
29
+ const currentPathNameSplitted = this._thinAlias(pathname).split("/");
30
+
31
+ if (!handlers) {
32
+ return undefined;
33
+ }
34
+
35
+ // Compare splitted length
36
+ if (aliasSplitted.length !== currentPathNameSplitted.length) {
37
+ return undefined;
38
+ }
39
+
40
+ const parameters: Record<string, string> = Object();
41
+
42
+ for (let index = 0; index < aliasSplitted.length; index++) {
43
+ const aliasPart = aliasSplitted[index];
44
+ const pathnamePart = currentPathNameSplitted[index];
45
+
46
+ // Check pathmane path match a dynamic syntax, if no match => start compare equal or not
47
+ if (!new RegExp(Route.rootPattern, "g").test(aliasPart)) {
48
+ if (aliasPart !== pathnamePart) return undefined;
49
+ } else {
50
+ let isFailed = false;
51
+
52
+ aliasPart.replace(new RegExp(Route.rootPattern, "g"), (match, key, offset) => {
53
+ if (offset === 0) {
54
+ Object.assign(parameters, {
55
+ [key]: pathnamePart
56
+ });
57
+ }
58
+
59
+ return match;
60
+ });
61
+
62
+ if (isFailed) {
63
+ return undefined;
64
+ }
65
+ }
66
+
67
+ continue;
68
+ }
69
+
70
+ return Object.freeze({
71
+ params: parameters,
72
+ handlers: handlers
73
+ });
74
+ } catch (err) {
75
+ console.error(err);
76
+ return false;
77
+ }
78
+ }
79
+
80
+ /**
81
+ *
82
+ */
83
+ public isMatch(pathname: string, method: keyof THttpMethods) {
84
+ try {
85
+ const handlers = this._map.get(method);
86
+
87
+ if (!handlers) {
88
+ return undefined;
89
+ }
90
+
91
+ const aliasSplitted = this.alias.split("/");
92
+ const currentPathNameSplitted = this._thinAlias(pathname).split("/");
93
+
94
+ // Compare splitted length
95
+ if (aliasSplitted.length !== currentPathNameSplitted.length) {
96
+ return false;
97
+ }
98
+
99
+ const parameters = Object();
100
+
101
+ for (let index = 0; index < aliasSplitted.length; index++) {
102
+ const aliasPart = aliasSplitted[index];
103
+ const pathnamePart = currentPathNameSplitted[index];
104
+
105
+ // Check pathmane path match a dynamic syntax, if no match => start compare equal or not
106
+ if (!new RegExp(Route.rootPattern, "g").test(aliasPart)) {
107
+ if (aliasPart !== pathnamePart) {
108
+ return false;
109
+ }
110
+ } else {
111
+ let isFailed = false;
112
+
113
+ aliasPart.replace(new RegExp(Route.rootPattern, "g"), (subString, key, value) => {
114
+ if (!new RegExp(value, "g").test(pathnamePart)) {
115
+ isFailed = true;
116
+ } else {
117
+ Object.assign(parameters, {
118
+ [key]: pathnamePart
119
+ });
120
+ }
121
+ return "";
122
+ });
123
+
124
+ if (isFailed) {
125
+ return false;
126
+ }
127
+ }
128
+
129
+ continue;
130
+ }
131
+
132
+ return true;
133
+ } catch (err) {
134
+ console.error(err);
135
+ return undefined;
136
+ }
137
+ }
138
+
139
+ /**
140
+ *
141
+ * @param handlers
142
+ * @returns
143
+ */
144
+ public get(...handlers: Array<THandler>) {
145
+ const currentHandlers = this._map.get("GET");
146
+
147
+ if (!currentHandlers) {
148
+ this._map.set("GET", handlers);
149
+ }
150
+
151
+ return this;
152
+ }
153
+
154
+ /**
155
+ *
156
+ * @param handlers
157
+ * @returns
158
+ */
159
+ public post(...handlers: Array<THandler>) {
160
+ const currentHandlers = this._map.get("POST");
161
+
162
+ if (!currentHandlers) {
163
+ this._map.set("POST", handlers);
164
+ }
165
+
166
+ return this;
167
+ }
168
+
169
+ /**
170
+ *
171
+ * @param handlers
172
+ * @returns
173
+ */
174
+ public put(...handlers: Array<THandler>) {
175
+ const currentHandlers = this._map.get("PUT");
176
+
177
+ if (!currentHandlers) {
178
+ this._map.set("PUT", handlers);
179
+ }
180
+
181
+ return this;
182
+ }
183
+
184
+ /**
185
+ *
186
+ * @param handlers
187
+ * @returns
188
+ */
189
+ public delete(...handlers: Array<THandler>) {
190
+ const currentHandlers = this._map.get("DELETE");
191
+
192
+ if (!currentHandlers) {
193
+ this._map.set("DELETE", handlers);
194
+ }
195
+
196
+ return this;
197
+ }
198
+
199
+ /**
200
+ *
201
+ * @param handlers
202
+ * @returns
203
+ */
204
+ public connect(...handlers: Array<THandler>) {
205
+ const currentHandlers = this._map.get("CONNECT");
206
+
207
+ if (!currentHandlers) {
208
+ return this._map.set("CONNECT", handlers);
209
+ }
210
+
211
+ return this;
212
+ }
213
+
214
+ /**
215
+ *
216
+ * @param handlers
217
+ * @returns
218
+ */
219
+ public options(...handlers: Array<THandler>) {
220
+ const currentHandlers = this._map.get("OPTIONS");
221
+
222
+ if (!currentHandlers) {
223
+ return this._map.set("OPTIONS", handlers);
224
+ }
225
+
226
+ return this;
227
+ }
228
+
229
+ /**
230
+ *
231
+ * @param handlers
232
+ * @returns
233
+ */
234
+ public trace(...handlers: Array<THandler>) {
235
+ const currentHandlers = this._map.get("TRACE");
236
+
237
+ if (!currentHandlers) {
238
+ return this._map.set("TRACE", handlers);
239
+ }
240
+
241
+ return this;
242
+ }
243
+
244
+ /**
245
+ *
246
+ * @param handlers
247
+ * @returns
248
+ */
249
+ public patch(...handlers: Array<THandler>) {
250
+ const currentHandlers = this._map.get("PATCH");
251
+
252
+ if (!currentHandlers) {
253
+ return this._map.set("PATCH", handlers);
254
+ }
255
+
256
+ return this;
257
+ }
258
+
259
+ /**
260
+ *
261
+ * @param handlers
262
+ * @returns
263
+ */
264
+ private _thinAlias(alias: string) {
265
+ return alias.replace(new RegExp("[/]{2,}", "g"), "/").replace(new RegExp("^[/]|[/]$", "g"), "");
266
+ }
267
+
268
+ /**
269
+ * Internal get fullpath after check regular expression
270
+ * @returns
271
+ */
272
+ get _fullPath(): string {
273
+ // Split path to start filter
274
+ const pathSplited = this.alias.split("/");
275
+
276
+ const blockFiltered = pathSplited.map((value, index) => {
277
+ // Initialize full parameter regex to validate
278
+ let validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})(\\(.*?\\))", "g");
279
+
280
+ if (!validateReg.test(value)) {
281
+ // Initialize key parameter regex to validate
282
+ validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})", "g");
283
+
284
+ if (!validateReg.test(value)) {
285
+ return value;
286
+ }
287
+
288
+ return value.replace(validateReg, (value, index) => `${value}(.*?)`);
289
+ }
290
+
291
+ return value;
292
+ });
293
+
294
+ return blockFiltered.join("/");
295
+ }
296
+
297
+ /**
298
+ * Internal get filterd path after check regular expression
299
+ * @returns
300
+ */
301
+ get _filteredPath(): string {
302
+ // Split path to start filter
303
+ const pathSplited = this.alias.split("/");
304
+
305
+ //
306
+ const blockFiltered = pathSplited.map((value, index) => {
307
+ let validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})((.*?))", "g");
308
+
309
+ if (!validateReg.test(value)) {
310
+ // Initialize key parameter regex to validate
311
+ validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})", "g");
312
+
313
+ if (!validateReg.test(value)) {
314
+ return value;
315
+ }
316
+
317
+ return value.replace(validateReg, (value, index) => "(.*?)");
318
+ }
319
+
320
+ return value.replace(validateReg, (subString, arg_01, arg_02) => arg_02);
321
+ });
322
+
323
+ return blockFiltered.join("/");
324
+ }
325
+ }
326
+
327
+ export default Route;