@bool-ts/core 1.6.14 → 1.7.1
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.
- package/.prettierrc +11 -11
- package/LICENSE +21 -21
- package/__test/controller.ts +93 -79
- package/__test/dispatcher.ts +16 -0
- package/__test/firstGuard.ts +10 -10
- package/__test/firstMiddleware.ts +15 -9
- package/__test/index.ts +8 -8
- package/__test/interfaces.ts +7 -7
- package/__test/module.ts +28 -30
- package/__test/repository.ts +16 -16
- package/__test/secondGuard.ts +10 -10
- package/__test/secondMiddleware.ts +15 -9
- package/__test/service.ts +20 -20
- package/bun.lockb +0 -0
- package/dist/decorators/arguments.d.ts +3 -3
- package/dist/decorators/arguments.js +3 -3
- package/dist/decorators/dispatcher.js +0 -3
- package/dist/decorators/index.d.ts +1 -1
- package/dist/decorators/index.js +1 -1
- package/dist/decorators/middleware.js +0 -3
- package/dist/decorators/module.d.ts +2 -4
- package/dist/decorators/module.js +5 -12
- package/dist/hooks/factory.d.ts +41 -2
- package/dist/hooks/factory.js +496 -404
- package/dist/hooks/injector.d.ts +14 -1
- package/dist/hooks/injector.js +3 -3
- package/dist/http/index.d.ts +1 -1
- package/dist/http/index.js +2 -1
- package/dist/interfaces/dispatcher.d.ts +3 -2
- package/dist/interfaces/middleware.d.ts +3 -2
- package/dist/keys/index.d.ts +2 -1
- package/dist/keys/index.js +2 -1
- package/jsconfig.json +27 -27
- package/package.json +3 -3
- package/src/decorators/arguments.ts +286 -286
- package/src/decorators/controller.ts +21 -21
- package/src/decorators/dispatcher.ts +14 -18
- package/src/decorators/guard.ts +18 -18
- package/src/decorators/http.ts +81 -81
- package/src/decorators/index.ts +29 -29
- package/src/decorators/inject.ts +13 -13
- package/src/decorators/injectable.ts +8 -8
- package/src/decorators/middleware.ts +14 -18
- package/src/decorators/module.ts +84 -94
- package/src/decorators/zodSchema.ts +19 -19
- package/src/entities/index.ts +5 -5
- package/src/entities/route.ts +327 -327
- package/src/entities/router.ts +35 -35
- package/src/entities/routerGroup.ts +34 -34
- package/src/hooks/factory.ts +990 -809
- package/src/hooks/index.ts +2 -2
- package/src/hooks/injector.ts +57 -57
- package/src/http/clientError.ts +45 -45
- package/src/http/index.ts +62 -61
- package/src/http/serverError.ts +27 -27
- package/src/index.ts +9 -9
- package/src/interfaces/context.ts +4 -4
- package/src/interfaces/controller.ts +1 -1
- package/src/interfaces/dispatcher.ts +4 -3
- package/src/interfaces/guard.ts +3 -3
- package/src/interfaces/index.ts +6 -6
- package/src/interfaces/middleware.ts +4 -3
- package/src/interfaces/module.ts +1 -1
- package/src/keys/index.ts +23 -22
- package/test.http +31 -31
- package/tsconfig.json +108 -108
- package/__test/afterDispatcher.ts +0 -9
- package/__test/beforeDispatcher.ts +0 -9
package/src/entities/route.ts
CHANGED
|
@@ -1,327 +1,327 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import type { THttpMethods } from "../http";
|
|
4
|
-
|
|
5
|
-
export type TRouteModel<T = unknown> = Readonly<{
|
|
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, TRouteModel>();
|
|
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<{ parameters: Record<string, string>; model: TRouteModel }> | false | undefined {
|
|
26
|
-
try {
|
|
27
|
-
const model = this._map.get(method);
|
|
28
|
-
const aliasSplitted = this.alias.split("/");
|
|
29
|
-
const currentPathNameSplitted = this._thinAlias(pathname).split("/");
|
|
30
|
-
|
|
31
|
-
if (!model) {
|
|
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
|
-
parameters: parameters,
|
|
72
|
-
model: model
|
|
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(handler: TRouteModel) {
|
|
145
|
-
const currenTRouteModel = this._map.get("GET");
|
|
146
|
-
|
|
147
|
-
if (!currenTRouteModel) {
|
|
148
|
-
this._map.set("GET", handler);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return this;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
*
|
|
156
|
-
* @param handlers
|
|
157
|
-
* @returns
|
|
158
|
-
*/
|
|
159
|
-
public post(handler: TRouteModel) {
|
|
160
|
-
const currenTRouteModel = this._map.get("POST");
|
|
161
|
-
|
|
162
|
-
if (!currenTRouteModel) {
|
|
163
|
-
this._map.set("POST", handler);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return this;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
*
|
|
171
|
-
* @param handlers
|
|
172
|
-
* @returns
|
|
173
|
-
*/
|
|
174
|
-
public put(handler: TRouteModel) {
|
|
175
|
-
const currenTRouteModel = this._map.get("PUT");
|
|
176
|
-
|
|
177
|
-
if (!currenTRouteModel) {
|
|
178
|
-
this._map.set("PUT", handler);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return this;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
*
|
|
186
|
-
* @param handlers
|
|
187
|
-
* @returns
|
|
188
|
-
*/
|
|
189
|
-
public delete(handler: TRouteModel) {
|
|
190
|
-
const currenTRouteModel = this._map.get("DELETE");
|
|
191
|
-
|
|
192
|
-
if (!currenTRouteModel) {
|
|
193
|
-
this._map.set("DELETE", handler);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return this;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
*
|
|
201
|
-
* @param handlers
|
|
202
|
-
* @returns
|
|
203
|
-
*/
|
|
204
|
-
public connect(handler: TRouteModel) {
|
|
205
|
-
const currenTRouteModel = this._map.get("CONNECT");
|
|
206
|
-
|
|
207
|
-
if (!currenTRouteModel) {
|
|
208
|
-
return this._map.set("CONNECT", handler);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return this;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
*
|
|
216
|
-
* @param handlers
|
|
217
|
-
* @returns
|
|
218
|
-
*/
|
|
219
|
-
public options(handler: TRouteModel) {
|
|
220
|
-
const currenTRouteModel = this._map.get("OPTIONS");
|
|
221
|
-
|
|
222
|
-
if (!currenTRouteModel) {
|
|
223
|
-
return this._map.set("OPTIONS", handler);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return this;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
*
|
|
231
|
-
* @param handlers
|
|
232
|
-
* @returns
|
|
233
|
-
*/
|
|
234
|
-
public trace(handler: TRouteModel) {
|
|
235
|
-
const currenTRouteModel = this._map.get("TRACE");
|
|
236
|
-
|
|
237
|
-
if (!currenTRouteModel) {
|
|
238
|
-
return this._map.set("TRACE", handler);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return this;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
*
|
|
246
|
-
* @param handlers
|
|
247
|
-
* @returns
|
|
248
|
-
*/
|
|
249
|
-
public patch(handler: TRouteModel) {
|
|
250
|
-
const currenTRouteModel = this._map.get("PATCH");
|
|
251
|
-
|
|
252
|
-
if (!currenTRouteModel) {
|
|
253
|
-
return this._map.set("PATCH", handler);
|
|
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;
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import type { THttpMethods } from "../http";
|
|
4
|
+
|
|
5
|
+
export type TRouteModel<T = unknown> = Readonly<{
|
|
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, TRouteModel>();
|
|
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<{ parameters: Record<string, string>; model: TRouteModel }> | false | undefined {
|
|
26
|
+
try {
|
|
27
|
+
const model = this._map.get(method);
|
|
28
|
+
const aliasSplitted = this.alias.split("/");
|
|
29
|
+
const currentPathNameSplitted = this._thinAlias(pathname).split("/");
|
|
30
|
+
|
|
31
|
+
if (!model) {
|
|
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
|
+
parameters: parameters,
|
|
72
|
+
model: model
|
|
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(handler: TRouteModel) {
|
|
145
|
+
const currenTRouteModel = this._map.get("GET");
|
|
146
|
+
|
|
147
|
+
if (!currenTRouteModel) {
|
|
148
|
+
this._map.set("GET", handler);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
*
|
|
156
|
+
* @param handlers
|
|
157
|
+
* @returns
|
|
158
|
+
*/
|
|
159
|
+
public post(handler: TRouteModel) {
|
|
160
|
+
const currenTRouteModel = this._map.get("POST");
|
|
161
|
+
|
|
162
|
+
if (!currenTRouteModel) {
|
|
163
|
+
this._map.set("POST", handler);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
*
|
|
171
|
+
* @param handlers
|
|
172
|
+
* @returns
|
|
173
|
+
*/
|
|
174
|
+
public put(handler: TRouteModel) {
|
|
175
|
+
const currenTRouteModel = this._map.get("PUT");
|
|
176
|
+
|
|
177
|
+
if (!currenTRouteModel) {
|
|
178
|
+
this._map.set("PUT", handler);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
*
|
|
186
|
+
* @param handlers
|
|
187
|
+
* @returns
|
|
188
|
+
*/
|
|
189
|
+
public delete(handler: TRouteModel) {
|
|
190
|
+
const currenTRouteModel = this._map.get("DELETE");
|
|
191
|
+
|
|
192
|
+
if (!currenTRouteModel) {
|
|
193
|
+
this._map.set("DELETE", handler);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
*
|
|
201
|
+
* @param handlers
|
|
202
|
+
* @returns
|
|
203
|
+
*/
|
|
204
|
+
public connect(handler: TRouteModel) {
|
|
205
|
+
const currenTRouteModel = this._map.get("CONNECT");
|
|
206
|
+
|
|
207
|
+
if (!currenTRouteModel) {
|
|
208
|
+
return this._map.set("CONNECT", handler);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
*
|
|
216
|
+
* @param handlers
|
|
217
|
+
* @returns
|
|
218
|
+
*/
|
|
219
|
+
public options(handler: TRouteModel) {
|
|
220
|
+
const currenTRouteModel = this._map.get("OPTIONS");
|
|
221
|
+
|
|
222
|
+
if (!currenTRouteModel) {
|
|
223
|
+
return this._map.set("OPTIONS", handler);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
*
|
|
231
|
+
* @param handlers
|
|
232
|
+
* @returns
|
|
233
|
+
*/
|
|
234
|
+
public trace(handler: TRouteModel) {
|
|
235
|
+
const currenTRouteModel = this._map.get("TRACE");
|
|
236
|
+
|
|
237
|
+
if (!currenTRouteModel) {
|
|
238
|
+
return this._map.set("TRACE", handler);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
*
|
|
246
|
+
* @param handlers
|
|
247
|
+
* @returns
|
|
248
|
+
*/
|
|
249
|
+
public patch(handler: TRouteModel) {
|
|
250
|
+
const currenTRouteModel = this._map.get("PATCH");
|
|
251
|
+
|
|
252
|
+
if (!currenTRouteModel) {
|
|
253
|
+
return this._map.set("PATCH", handler);
|
|
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;
|