@bool-ts/core 1.3.2 → 1.4.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.
Files changed (68) hide show
  1. package/.prettierrc +11 -0
  2. package/LICENSE +21 -21
  3. package/__test/controller.ts +66 -79
  4. package/__test/index.ts +8 -11
  5. package/__test/interfaces.ts +7 -7
  6. package/__test/module.ts +16 -17
  7. package/__test/repository.ts +16 -16
  8. package/__test/service.ts +20 -20
  9. package/bun.lockb +0 -0
  10. package/dist/decorators/arguments.d.ts +43 -0
  11. package/dist/decorators/arguments.js +97 -0
  12. package/dist/decorators/controller.d.ts +2 -2
  13. package/dist/decorators/controller.js +6 -10
  14. package/dist/decorators/http.d.ts +1 -1
  15. package/dist/decorators/http.js +16 -103
  16. package/dist/decorators/index.d.ts +1 -0
  17. package/dist/decorators/index.js +7 -26
  18. package/dist/decorators/inject.js +5 -9
  19. package/dist/decorators/injectable.d.ts +2 -2
  20. package/dist/decorators/injectable.js +4 -8
  21. package/dist/decorators/module.d.ts +4 -2
  22. package/dist/decorators/module.js +4 -8
  23. package/dist/decorators/zodSchema.d.ts +1 -1
  24. package/dist/decorators/zodSchema.js +5 -8
  25. package/dist/entities/index.d.ts +3 -0
  26. package/dist/entities/index.js +3 -0
  27. package/dist/entities/route.d.ts +101 -0
  28. package/dist/entities/route.js +257 -0
  29. package/dist/entities/router.d.ts +10 -0
  30. package/dist/entities/router.js +25 -0
  31. package/dist/entities/routerGroup.d.ts +14 -0
  32. package/dist/entities/routerGroup.js +24 -0
  33. package/dist/hooks/factory.d.ts +12 -12
  34. package/dist/hooks/factory.js +202 -150
  35. package/dist/hooks/index.js +2 -7
  36. package/dist/hooks/injector.js +7 -10
  37. package/dist/http/clientError.d.ts +1 -1
  38. package/dist/http/clientError.js +3 -7
  39. package/dist/http/index.d.ts +12 -2
  40. package/dist/http/index.js +34 -73
  41. package/dist/http/serverError.js +3 -7
  42. package/dist/index.js +5 -21
  43. package/dist/interfaces/index.js +1 -3
  44. package/dist/ultils/asyncFunction.js +1 -4
  45. package/dist/ultils/index.js +1 -17
  46. package/package.json +30 -33
  47. package/src/decorators/arguments.ts +186 -0
  48. package/src/decorators/controller.ts +14 -18
  49. package/src/decorators/http.ts +81 -195
  50. package/src/decorators/index.ts +7 -6
  51. package/src/decorators/inject.ts +13 -19
  52. package/src/decorators/injectable.ts +11 -12
  53. package/src/decorators/module.ts +21 -22
  54. package/src/decorators/zodSchema.ts +20 -27
  55. package/src/entities/index.ts +3 -0
  56. package/src/entities/route.ts +328 -0
  57. package/src/entities/router.ts +35 -0
  58. package/src/entities/routerGroup.ts +34 -0
  59. package/src/hooks/factory.ts +319 -205
  60. package/src/hooks/index.ts +2 -2
  61. package/src/hooks/injector.ts +43 -43
  62. package/src/http/clientError.ts +45 -56
  63. package/src/http/index.ts +63 -72
  64. package/src/http/serverError.ts +38 -38
  65. package/src/index.ts +6 -6
  66. package/src/interfaces/index.ts +3 -3
  67. package/test.http +30 -28
  68. package/tsconfig.json +107 -109
@@ -1,17 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Options = exports.Delete = exports.Patch = exports.Put = exports.Post = exports.Get = exports.controllerRoutesKey = void 0;
4
- const http_1 = require("../http");
5
- const ultils_1 = require("../ultils");
6
- const zodSchema_1 = require("./zodSchema");
7
- exports.controllerRoutesKey = "__bool:controller.routes__";
1
+ export const controllerRoutesKey = Symbol.for("__bool:controller.routes__");
8
2
  const defaultDecorator = (path, method) => (target, methodName, descriptor) => {
9
3
  if (!(descriptor.value instanceof Function)) {
10
- throw Error(`${method} decorator only use for method.`);
4
+ throw Error(`${method} decorator only use for class method.`);
11
5
  }
12
6
  // Define controller metadata
13
- Reflect.defineMetadata(exports.controllerRoutesKey, [
14
- ...Reflect.getOwnMetadata(exports.controllerRoutesKey, target.constructor) || [],
7
+ Reflect.defineMetadata(controllerRoutesKey, [
8
+ ...(Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || []),
15
9
  {
16
10
  path: !path.startsWith("/") ? `/${path}` : path,
17
11
  httpMethod: method.toUpperCase(),
@@ -19,128 +13,47 @@ const defaultDecorator = (path, method) => (target, methodName, descriptor) => {
19
13
  descriptor: descriptor
20
14
  }
21
15
  ], target.constructor);
22
- // Define route parameters zod validation
23
- const currentMethod = descriptor.value;
24
- const isAsync = descriptor.value instanceof ultils_1.AsyncFunction;
25
- if (!isAsync) {
26
- descriptor.value = function () {
27
- const zodSchemaMetadata = Reflect.getOwnMetadata(zodSchema_1.controllerRouteZodSchemaKey, target.constructor, methodName);
28
- if (zodSchemaMetadata) {
29
- for (const zodSchemaProp in zodSchemaMetadata) {
30
- const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
31
- try {
32
- const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
33
- if (!validation.success) {
34
- throw new http_1.HttpClientError({
35
- httpCode: 400,
36
- message: `Validation at the [${methodName}] method fails at positional argument [${tmpZodMetadata.index}].`,
37
- data: validation.error.issues
38
- });
39
- }
40
- arguments[tmpZodMetadata.index] = validation.data;
41
- }
42
- catch (error) {
43
- if (error instanceof http_1.HttpClientError) {
44
- throw error;
45
- }
46
- throw new http_1.HttpServerError({
47
- httpCode: 500,
48
- message: `Validation at the [${methodName}] method error at positional argument [${tmpZodMetadata.index}].`,
49
- data: !(error instanceof Error) ? error : [{
50
- message: error.message,
51
- code: error.name,
52
- cause: error.cause
53
- }]
54
- });
55
- }
56
- }
57
- }
58
- return currentMethod.apply(this, arguments);
59
- };
60
- }
61
- else {
62
- descriptor.value = async function () {
63
- const zodSchemaMetadata = Reflect.getOwnMetadata(zodSchema_1.controllerRouteZodSchemaKey, target.constructor, methodName);
64
- if (zodSchemaMetadata) {
65
- for (const zodSchemaProp in zodSchemaMetadata) {
66
- const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
67
- try {
68
- const validation = await tmpZodMetadata.schema.safeParseAsync(arguments[tmpZodMetadata.index]);
69
- if (!validation.success) {
70
- throw new http_1.HttpClientError({
71
- httpCode: 400,
72
- message: `Validation at the [${methodName}] method fails at positional argument [${tmpZodMetadata.index}].`,
73
- data: validation.error.issues
74
- });
75
- }
76
- arguments[tmpZodMetadata.index] = validation.data;
77
- }
78
- catch (error) {
79
- if (error instanceof http_1.HttpClientError) {
80
- throw error;
81
- }
82
- throw new http_1.HttpServerError({
83
- httpCode: 500,
84
- message: `Validation at the [${methodName}] method error at positional argument [${tmpZodMetadata.index}].`,
85
- data: !(error instanceof Error) ? error : [{
86
- message: error.message,
87
- code: error.name,
88
- cause: error.cause
89
- }]
90
- });
91
- }
92
- }
93
- }
94
- return currentMethod.apply(this, arguments);
95
- };
96
- }
97
16
  };
98
17
  /**
99
18
  *
100
19
  * @param path
101
20
  * @returns
102
21
  */
103
- const Get = (path = "/") => defaultDecorator(path, "Get");
104
- exports.Get = Get;
22
+ export const Get = (path = "/") => defaultDecorator(path, "Get");
105
23
  /**
106
24
  *
107
25
  * @param path
108
26
  * @returns
109
27
  */
110
- const Post = (path = "/") => defaultDecorator(path, "Post");
111
- exports.Post = Post;
28
+ export const Post = (path = "/") => defaultDecorator(path, "Post");
112
29
  /**
113
30
  *
114
31
  * @param path
115
32
  * @returns
116
33
  */
117
- const Put = (path = "/") => defaultDecorator(path, "Put");
118
- exports.Put = Put;
34
+ export const Put = (path = "/") => defaultDecorator(path, "Put");
119
35
  /**
120
36
  *
121
37
  * @param path
122
38
  * @returns
123
39
  */
124
- const Patch = (path = "/") => defaultDecorator(path, "Patch");
125
- exports.Patch = Patch;
40
+ export const Patch = (path = "/") => defaultDecorator(path, "Patch");
126
41
  /**
127
42
  *
128
43
  * @param path
129
44
  * @returns
130
45
  */
131
- const Delete = (path = "/") => defaultDecorator(path, "Delete");
132
- exports.Delete = Delete;
46
+ export const Delete = (path = "/") => defaultDecorator(path, "Delete");
133
47
  /**
134
48
  *
135
49
  * @param path
136
50
  * @returns
137
51
  */
138
- const Options = (path = "/") => defaultDecorator(path, "Options");
139
- exports.Options = Options;
140
- exports.default = {
141
- Get: exports.Get,
142
- Post: exports.Post,
143
- Put: exports.Put,
144
- Patch: exports.Patch,
145
- Delete: exports.Delete
52
+ export const Options = (path = "/") => defaultDecorator(path, "Options");
53
+ export default {
54
+ Get,
55
+ Post,
56
+ Put,
57
+ Patch,
58
+ Delete
146
59
  };
@@ -1,3 +1,4 @@
1
+ export { Headers, Body, Params, Param, Query, EArgumentTypes } from "./arguments";
1
2
  export { Controller, controllerKey } from "./controller";
2
3
  export { Inject, injectKey } from "./inject";
3
4
  export { Injectable, injectableKey } from "./injectable";
@@ -1,26 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.controllerRouteZodSchemaKey = exports.ZodSchema = exports.controllerRoutesKey = exports.Options = exports.Delete = exports.Patch = exports.Put = exports.Post = exports.Get = exports.moduleKey = exports.Module = exports.injectableKey = exports.Injectable = exports.injectKey = exports.Inject = exports.controllerKey = exports.Controller = void 0;
4
- var controller_1 = require("./controller");
5
- Object.defineProperty(exports, "Controller", { enumerable: true, get: function () { return controller_1.Controller; } });
6
- Object.defineProperty(exports, "controllerKey", { enumerable: true, get: function () { return controller_1.controllerKey; } });
7
- var inject_1 = require("./inject");
8
- Object.defineProperty(exports, "Inject", { enumerable: true, get: function () { return inject_1.Inject; } });
9
- Object.defineProperty(exports, "injectKey", { enumerable: true, get: function () { return inject_1.injectKey; } });
10
- var injectable_1 = require("./injectable");
11
- Object.defineProperty(exports, "Injectable", { enumerable: true, get: function () { return injectable_1.Injectable; } });
12
- Object.defineProperty(exports, "injectableKey", { enumerable: true, get: function () { return injectable_1.injectableKey; } });
13
- var module_1 = require("./module");
14
- Object.defineProperty(exports, "Module", { enumerable: true, get: function () { return module_1.Module; } });
15
- Object.defineProperty(exports, "moduleKey", { enumerable: true, get: function () { return module_1.moduleKey; } });
16
- var http_1 = require("./http");
17
- Object.defineProperty(exports, "Get", { enumerable: true, get: function () { return http_1.Get; } });
18
- Object.defineProperty(exports, "Post", { enumerable: true, get: function () { return http_1.Post; } });
19
- Object.defineProperty(exports, "Put", { enumerable: true, get: function () { return http_1.Put; } });
20
- Object.defineProperty(exports, "Patch", { enumerable: true, get: function () { return http_1.Patch; } });
21
- Object.defineProperty(exports, "Delete", { enumerable: true, get: function () { return http_1.Delete; } });
22
- Object.defineProperty(exports, "Options", { enumerable: true, get: function () { return http_1.Options; } });
23
- Object.defineProperty(exports, "controllerRoutesKey", { enumerable: true, get: function () { return http_1.controllerRoutesKey; } });
24
- var zodSchema_1 = require("./zodSchema");
25
- Object.defineProperty(exports, "ZodSchema", { enumerable: true, get: function () { return zodSchema_1.ZodSchema; } });
26
- Object.defineProperty(exports, "controllerRouteZodSchemaKey", { enumerable: true, get: function () { return zodSchema_1.controllerRouteZodSchemaKey; } });
1
+ export { Headers, Body, Params, Param, Query, EArgumentTypes } from "./arguments";
2
+ export { Controller, controllerKey } from "./controller";
3
+ export { Inject, injectKey } from "./inject";
4
+ export { Injectable, injectableKey } from "./injectable";
5
+ export { Module, moduleKey } from "./module";
6
+ export { Get, Post, Put, Patch, Delete, Options, controllerRoutesKey } from "./http";
7
+ export { ZodSchema, controllerRouteZodSchemaKey } from "./zodSchema";
@@ -1,13 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Inject = exports.injectKey = void 0;
4
- exports.injectKey = "design:paramtypes";
5
- const Inject = (classDefinition) => {
1
+ export const injectKey = "design:paramtypes";
2
+ export const Inject = (classDefinition) => {
6
3
  return (target, methodName, parameterIndex) => {
7
- const designParameterTypes = Reflect.getMetadata(exports.injectKey, target) || [];
4
+ const designParameterTypes = Reflect.getMetadata(injectKey, target) || [];
8
5
  designParameterTypes[parameterIndex] = classDefinition;
9
- Reflect.defineMetadata(exports.injectKey, designParameterTypes, target);
6
+ Reflect.defineMetadata(injectKey, designParameterTypes, target);
10
7
  };
11
8
  };
12
- exports.Inject = Inject;
13
- exports.default = exports.Inject;
9
+ export default Inject;
@@ -1,3 +1,3 @@
1
- export declare const injectableKey = "__bool:injectable__";
2
- export declare const Injectable: () => <T extends Object>(target: T, context?: ClassDecoratorContext) => T;
1
+ export declare const injectableKey: unique symbol;
2
+ export declare const Injectable: () => <T extends Object>(target: T) => T;
3
3
  export default Injectable;
@@ -1,10 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Injectable = exports.injectableKey = void 0;
4
- exports.injectableKey = "__bool:injectable__";
5
- const Injectable = () => (target, context) => {
6
- Reflect.defineMetadata(exports.injectableKey, undefined, target);
1
+ export const injectableKey = Symbol.for("__bool:injectable__");
2
+ export const Injectable = () => (target) => {
3
+ Reflect.defineMetadata(injectableKey, undefined, target);
7
4
  return target;
8
5
  };
9
- exports.Injectable = Injectable;
10
- exports.default = exports.Injectable;
6
+ export default Injectable;
@@ -5,6 +5,8 @@ export type TModuleOptions = Partial<{
5
5
  allowOrigins: string | Array<string>;
6
6
  allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
7
7
  }> | undefined;
8
- export declare const moduleKey = "__bool:module__";
9
- export declare const Module: (args?: TModuleOptions) => <T extends new (...args: any[]) => {}>(target: T, context?: ClassDecoratorContext) => T;
8
+ export declare const moduleKey: unique symbol;
9
+ export declare const Module: (args?: TModuleOptions) => <T extends {
10
+ new (...args: any[]): {};
11
+ }>(target: T) => T;
10
12
  export default Module;
@@ -1,10 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Module = exports.moduleKey = void 0;
4
- exports.moduleKey = "__bool:module__";
5
- const Module = (args) => (target, context) => {
6
- Reflect.defineMetadata(exports.moduleKey, args, target);
1
+ export const moduleKey = Symbol.for("__bool:module__");
2
+ export const Module = (args) => (target) => {
3
+ Reflect.defineMetadata(moduleKey, args, target);
7
4
  return target;
8
5
  };
9
- exports.Module = Module;
10
- exports.default = exports.Module;
6
+ export default Module;
@@ -1,3 +1,3 @@
1
1
  import * as Zod from "zod";
2
- export declare const controllerRouteZodSchemaKey = "__bool:controller.route.zodSchema__";
2
+ export declare const controllerRouteZodSchemaKey: unique symbol;
3
3
  export declare const ZodSchema: (schema: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
@@ -1,18 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ZodSchema = exports.controllerRouteZodSchemaKey = void 0;
4
- exports.controllerRouteZodSchemaKey = "__bool:controller.route.zodSchema__";
5
- const ZodSchema = (schema) => {
1
+ import * as Zod from "zod";
2
+ export const controllerRouteZodSchemaKey = Symbol.for("__bool:controller.route.zodSchema__");
3
+ export const ZodSchema = (schema) => {
6
4
  return (target, methodName, parameterIndex) => {
7
5
  if (!methodName) {
8
6
  return;
9
7
  }
10
- const zodSchemasMetadata = Reflect.getOwnMetadata(exports.controllerRouteZodSchemaKey, target.constructor, methodName) || {};
8
+ const zodSchemasMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName) || {};
11
9
  zodSchemasMetadata[`paramterIndexes.${parameterIndex}`] = {
12
10
  index: parameterIndex,
13
11
  schema: schema
14
12
  };
15
- Reflect.defineMetadata(exports.controllerRouteZodSchemaKey, zodSchemasMetadata, target.constructor, methodName);
13
+ Reflect.defineMetadata(controllerRouteZodSchemaKey, zodSchemasMetadata, target.constructor, methodName);
16
14
  };
17
15
  };
18
- exports.ZodSchema = ZodSchema;
@@ -0,0 +1,3 @@
1
+ export { Route } from "./route";
2
+ export { Router } from "./router";
3
+ export { RouterGroup } from "./routerGroup";
@@ -0,0 +1,3 @@
1
+ export { Route } from "./route";
2
+ export { Router } from "./router";
3
+ export { RouterGroup } from "./routerGroup";
@@ -0,0 +1,101 @@
1
+ import type { THttpMethods } from "../http";
2
+ type THandler<T = unknown> = Required<{
3
+ constructor: new (...args: Array<any>) => T;
4
+ funcName: string | symbol;
5
+ func: (...args: Array<any>) => unknown;
6
+ }>;
7
+ export declare class Route {
8
+ static rootPattern: string;
9
+ readonly alias: string;
10
+ private _map;
11
+ constructor(alias: string);
12
+ test(pathname: string, method: keyof THttpMethods): Readonly<{
13
+ params: Record<string, string>;
14
+ handlers: Array<THandler>;
15
+ }> | false | undefined;
16
+ /**
17
+ *
18
+ */
19
+ isMatch(pathname: string, method: keyof THttpMethods): boolean | undefined;
20
+ /**
21
+ *
22
+ * @param handlers
23
+ * @returns
24
+ */
25
+ get(...handlers: Array<THandler>): this;
26
+ /**
27
+ *
28
+ * @param handlers
29
+ * @returns
30
+ */
31
+ post(...handlers: Array<THandler>): this;
32
+ /**
33
+ *
34
+ * @param handlers
35
+ * @returns
36
+ */
37
+ put(...handlers: Array<THandler>): this;
38
+ /**
39
+ *
40
+ * @param handlers
41
+ * @returns
42
+ */
43
+ delete(...handlers: Array<THandler>): this;
44
+ /**
45
+ *
46
+ * @param handlers
47
+ * @returns
48
+ */
49
+ connect(...handlers: Array<THandler>): this | Map<keyof THttpMethods, Required<{
50
+ constructor: new (...args: Array<any>) => unknown;
51
+ funcName: string | symbol;
52
+ func: (...args: Array<any>) => unknown;
53
+ }>[]>;
54
+ /**
55
+ *
56
+ * @param handlers
57
+ * @returns
58
+ */
59
+ options(...handlers: Array<THandler>): this | Map<keyof THttpMethods, Required<{
60
+ constructor: new (...args: Array<any>) => unknown;
61
+ funcName: string | symbol;
62
+ func: (...args: Array<any>) => unknown;
63
+ }>[]>;
64
+ /**
65
+ *
66
+ * @param handlers
67
+ * @returns
68
+ */
69
+ trace(...handlers: Array<THandler>): this | Map<keyof THttpMethods, Required<{
70
+ constructor: new (...args: Array<any>) => unknown;
71
+ funcName: string | symbol;
72
+ func: (...args: Array<any>) => unknown;
73
+ }>[]>;
74
+ /**
75
+ *
76
+ * @param handlers
77
+ * @returns
78
+ */
79
+ patch(...handlers: Array<THandler>): this | Map<keyof THttpMethods, Required<{
80
+ constructor: new (...args: Array<any>) => unknown;
81
+ funcName: string | symbol;
82
+ func: (...args: Array<any>) => unknown;
83
+ }>[]>;
84
+ /**
85
+ *
86
+ * @param handlers
87
+ * @returns
88
+ */
89
+ private _thinAlias;
90
+ /**
91
+ * Internal get fullpath after check regular expression
92
+ * @returns
93
+ */
94
+ get _fullPath(): string;
95
+ /**
96
+ * Internal get filterd path after check regular expression
97
+ * @returns
98
+ */
99
+ get _filteredPath(): string;
100
+ }
101
+ export default Route;
@@ -0,0 +1,257 @@
1
+ "use strict";
2
+ export class Route {
3
+ static rootPattern = ":([a-z0-9A-Z_.-]{1,})";
4
+ alias;
5
+ _map = new Map();
6
+ constructor(alias) {
7
+ this.alias = this._thinAlias(alias);
8
+ }
9
+ test(pathname, method) {
10
+ try {
11
+ const handlers = this._map.get(method);
12
+ const aliasSplitted = this.alias.split("/");
13
+ const currentPathNameSplitted = this._thinAlias(pathname).split("/");
14
+ if (!handlers) {
15
+ return undefined;
16
+ }
17
+ // Compare splitted length
18
+ if (aliasSplitted.length !== currentPathNameSplitted.length) {
19
+ return undefined;
20
+ }
21
+ const parameters = Object();
22
+ for (let index = 0; index < aliasSplitted.length; index++) {
23
+ const aliasPart = aliasSplitted[index];
24
+ const pathnamePart = currentPathNameSplitted[index];
25
+ // Check pathmane path match a dynamic syntax, if no match => start compare equal or not
26
+ if (!new RegExp(Route.rootPattern, "g").test(aliasPart)) {
27
+ if (aliasPart !== pathnamePart)
28
+ return undefined;
29
+ }
30
+ else {
31
+ let isFailed = false;
32
+ aliasPart.replace(new RegExp(Route.rootPattern, "g"), (match, key, offset) => {
33
+ if (offset === 0) {
34
+ Object.assign(parameters, {
35
+ [key]: pathnamePart
36
+ });
37
+ }
38
+ return match;
39
+ });
40
+ if (isFailed) {
41
+ console.log(3, "isFailed", isFailed);
42
+ return undefined;
43
+ }
44
+ }
45
+ continue;
46
+ }
47
+ return Object.freeze({
48
+ params: parameters,
49
+ handlers: handlers
50
+ });
51
+ }
52
+ catch (err) {
53
+ console.error(err);
54
+ return false;
55
+ }
56
+ }
57
+ /**
58
+ *
59
+ */
60
+ isMatch(pathname, method) {
61
+ try {
62
+ const handlers = this._map.get(method);
63
+ if (!handlers) {
64
+ return undefined;
65
+ }
66
+ const aliasSplitted = this.alias.split("/");
67
+ const currentPathNameSplitted = this._thinAlias(pathname).split("/");
68
+ // Compare splitted length
69
+ if (aliasSplitted.length !== currentPathNameSplitted.length) {
70
+ return false;
71
+ }
72
+ const parameters = Object();
73
+ for (let index = 0; index < aliasSplitted.length; index++) {
74
+ const aliasPart = aliasSplitted[index];
75
+ const pathnamePart = currentPathNameSplitted[index];
76
+ // Check pathmane path match a dynamic syntax, if no match => start compare equal or not
77
+ if (!new RegExp(Route.rootPattern, "g").test(aliasPart)) {
78
+ if (aliasPart !== pathnamePart) {
79
+ return false;
80
+ }
81
+ }
82
+ else {
83
+ let isFailed = false;
84
+ aliasPart.replace(new RegExp(Route.rootPattern, "g"), (subString, key, value) => {
85
+ if (!new RegExp(value, "g").test(pathnamePart)) {
86
+ isFailed = true;
87
+ }
88
+ else {
89
+ Object.assign(parameters, {
90
+ [key]: pathnamePart
91
+ });
92
+ }
93
+ return "";
94
+ });
95
+ if (isFailed) {
96
+ return false;
97
+ }
98
+ }
99
+ continue;
100
+ }
101
+ return true;
102
+ }
103
+ catch (err) {
104
+ console.error(err);
105
+ return undefined;
106
+ }
107
+ }
108
+ /**
109
+ *
110
+ * @param handlers
111
+ * @returns
112
+ */
113
+ get(...handlers) {
114
+ const currentHandlers = this._map.get("GET");
115
+ if (!currentHandlers) {
116
+ this._map.set("GET", handlers);
117
+ }
118
+ return this;
119
+ }
120
+ /**
121
+ *
122
+ * @param handlers
123
+ * @returns
124
+ */
125
+ post(...handlers) {
126
+ const currentHandlers = this._map.get("POST");
127
+ if (!currentHandlers) {
128
+ this._map.set("POST", handlers);
129
+ }
130
+ return this;
131
+ }
132
+ /**
133
+ *
134
+ * @param handlers
135
+ * @returns
136
+ */
137
+ put(...handlers) {
138
+ const currentHandlers = this._map.get("PUT");
139
+ if (!currentHandlers) {
140
+ this._map.set("PUT", handlers);
141
+ }
142
+ return this;
143
+ }
144
+ /**
145
+ *
146
+ * @param handlers
147
+ * @returns
148
+ */
149
+ delete(...handlers) {
150
+ const currentHandlers = this._map.get("DELETE");
151
+ if (!currentHandlers) {
152
+ this._map.set("DELETE", handlers);
153
+ }
154
+ return this;
155
+ }
156
+ /**
157
+ *
158
+ * @param handlers
159
+ * @returns
160
+ */
161
+ connect(...handlers) {
162
+ const currentHandlers = this._map.get("CONNECT");
163
+ if (!currentHandlers) {
164
+ return this._map.set("CONNECT", handlers);
165
+ }
166
+ return this;
167
+ }
168
+ /**
169
+ *
170
+ * @param handlers
171
+ * @returns
172
+ */
173
+ options(...handlers) {
174
+ const currentHandlers = this._map.get("OPTIONS");
175
+ if (!currentHandlers) {
176
+ return this._map.set("OPTIONS", handlers);
177
+ }
178
+ return this;
179
+ }
180
+ /**
181
+ *
182
+ * @param handlers
183
+ * @returns
184
+ */
185
+ trace(...handlers) {
186
+ const currentHandlers = this._map.get("TRACE");
187
+ if (!currentHandlers) {
188
+ return this._map.set("TRACE", handlers);
189
+ }
190
+ return this;
191
+ }
192
+ /**
193
+ *
194
+ * @param handlers
195
+ * @returns
196
+ */
197
+ patch(...handlers) {
198
+ const currentHandlers = this._map.get("PATCH");
199
+ if (!currentHandlers) {
200
+ return this._map.set("PATCH", handlers);
201
+ }
202
+ return this;
203
+ }
204
+ /**
205
+ *
206
+ * @param handlers
207
+ * @returns
208
+ */
209
+ _thinAlias(alias) {
210
+ return alias.replace(new RegExp("[/]{2,}", "g"), "/").replace(new RegExp("^[/]|[/]$", "g"), "");
211
+ }
212
+ /**
213
+ * Internal get fullpath after check regular expression
214
+ * @returns
215
+ */
216
+ get _fullPath() {
217
+ // Split path to start filter
218
+ const pathSplited = this.alias.split("/");
219
+ const blockFiltered = pathSplited.map((value, index) => {
220
+ // Initialize full parameter regex to validate
221
+ let validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})(\\(.*?\\))", "g");
222
+ if (!validateReg.test(value)) {
223
+ // Initialize key parameter regex to validate
224
+ validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})", "g");
225
+ if (!validateReg.test(value)) {
226
+ return value;
227
+ }
228
+ return value.replace(validateReg, (value, index) => `${value}(.*?)`);
229
+ }
230
+ return value;
231
+ });
232
+ return blockFiltered.join("/");
233
+ }
234
+ /**
235
+ * Internal get filterd path after check regular expression
236
+ * @returns
237
+ */
238
+ get _filteredPath() {
239
+ // Split path to start filter
240
+ const pathSplited = this.alias.split("/");
241
+ //
242
+ const blockFiltered = pathSplited.map((value, index) => {
243
+ let validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})((.*?))", "g");
244
+ if (!validateReg.test(value)) {
245
+ // Initialize key parameter regex to validate
246
+ validateReg = new RegExp(":([a-z0-9A-Z_.-]{1,})", "g");
247
+ if (!validateReg.test(value)) {
248
+ return value;
249
+ }
250
+ return value.replace(validateReg, (value, index) => "(.*?)");
251
+ }
252
+ return value.replace(validateReg, (subString, arg_01, arg_02) => arg_02);
253
+ });
254
+ return blockFiltered.join("/");
255
+ }
256
+ }
257
+ export default Route;