@bool-ts/core 1.3.1 → 1.4.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.
- package/.prettierrc +11 -0
- package/LICENSE +21 -21
- package/__test/controller.ts +66 -79
- package/__test/index.ts +8 -11
- package/__test/interfaces.ts +7 -7
- package/__test/module.ts +16 -17
- package/__test/repository.ts +16 -16
- package/__test/service.ts +20 -20
- package/bun.lockb +0 -0
- package/dist/decorators/arguments.d.ts +31 -0
- package/dist/decorators/arguments.js +67 -0
- package/dist/decorators/controller.d.ts +2 -2
- package/dist/decorators/controller.js +6 -10
- package/dist/decorators/http.d.ts +1 -1
- package/dist/decorators/http.js +16 -101
- package/dist/decorators/index.d.ts +1 -0
- package/dist/decorators/index.js +7 -26
- package/dist/decorators/inject.js +5 -9
- package/dist/decorators/injectable.d.ts +2 -2
- package/dist/decorators/injectable.js +4 -8
- package/dist/decorators/module.d.ts +4 -2
- package/dist/decorators/module.js +4 -8
- package/dist/decorators/zodSchema.d.ts +1 -1
- package/dist/decorators/zodSchema.js +5 -8
- package/dist/entities/index.d.ts +3 -0
- package/dist/entities/index.js +3 -0
- package/dist/entities/route.d.ts +101 -0
- package/dist/entities/route.js +257 -0
- package/dist/entities/router.d.ts +10 -0
- package/dist/entities/router.js +25 -0
- package/dist/entities/routerGroup.d.ts +14 -0
- package/dist/entities/routerGroup.js +24 -0
- package/dist/hooks/factory.d.ts +12 -12
- package/dist/hooks/factory.js +180 -151
- package/dist/hooks/index.js +2 -7
- package/dist/hooks/injector.js +7 -10
- package/dist/http/clientError.d.ts +1 -1
- package/dist/http/clientError.js +3 -7
- package/dist/http/index.d.ts +12 -2
- package/dist/http/index.js +34 -73
- package/dist/http/serverError.js +3 -7
- package/dist/index.js +5 -21
- package/dist/interfaces/index.js +1 -3
- package/dist/ultils/asyncFunction.js +1 -4
- package/dist/ultils/index.js +1 -17
- package/package.json +30 -33
- package/src/decorators/arguments.ts +128 -0
- package/src/decorators/controller.ts +14 -18
- package/src/decorators/http.ts +81 -191
- package/src/decorators/index.ts +7 -6
- package/src/decorators/inject.ts +13 -19
- package/src/decorators/injectable.ts +11 -12
- package/src/decorators/module.ts +21 -22
- package/src/decorators/zodSchema.ts +20 -27
- package/src/entities/index.ts +3 -0
- package/src/entities/route.ts +328 -0
- package/src/entities/router.ts +35 -0
- package/src/entities/routerGroup.ts +34 -0
- package/src/hooks/factory.ts +285 -205
- package/src/hooks/index.ts +2 -2
- package/src/hooks/injector.ts +43 -43
- package/src/http/clientError.ts +45 -56
- package/src/http/index.ts +63 -72
- package/src/http/serverError.ts +38 -38
- package/src/index.ts +6 -6
- package/src/interfaces/index.ts +3 -3
- package/test.http +30 -28
- package/tsconfig.json +107 -109
package/dist/decorators/index.js
CHANGED
|
@@ -1,26 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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, 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
|
-
|
|
2
|
-
|
|
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(
|
|
4
|
+
const designParameterTypes = Reflect.getMetadata(injectKey, target) || [];
|
|
8
5
|
designParameterTypes[parameterIndex] = classDefinition;
|
|
9
|
-
Reflect.defineMetadata(
|
|
6
|
+
Reflect.defineMetadata(injectKey, designParameterTypes, target);
|
|
10
7
|
};
|
|
11
8
|
};
|
|
12
|
-
|
|
13
|
-
exports.default = exports.Inject;
|
|
9
|
+
export default Inject;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const injectableKey
|
|
2
|
-
export declare const Injectable: () => <T extends Object>(target: 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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
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
|
|
9
|
-
export declare const Module: (args?: TModuleOptions) => <T extends
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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(
|
|
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(
|
|
13
|
+
Reflect.defineMetadata(controllerRouteZodSchemaKey, zodSchemasMetadata, target.constructor, methodName);
|
|
16
14
|
};
|
|
17
15
|
};
|
|
18
|
-
exports.ZodSchema = ZodSchema;
|
|
@@ -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;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import Route from "./route";
|
|
3
|
+
export class Router {
|
|
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 Route(`${this.alias}/${alias}`) : route;
|
|
13
|
+
if (!route) {
|
|
14
|
+
this._routes.set(thinAlias, newRoute);
|
|
15
|
+
}
|
|
16
|
+
return newRoute;
|
|
17
|
+
}
|
|
18
|
+
_thinAlias(alias) {
|
|
19
|
+
return alias.replace(new RegExp("[/]{2,}", "g"), "/").replace(new RegExp("^[/]|[/]$", "g"), "");
|
|
20
|
+
}
|
|
21
|
+
get routes() {
|
|
22
|
+
return this._routes;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export default Router;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { THttpMethods } from "../http";
|
|
2
|
+
import type { Router } from "./router";
|
|
3
|
+
export declare class RouterGroup {
|
|
4
|
+
private _routers;
|
|
5
|
+
add(...routers: Array<Router>): this;
|
|
6
|
+
find(pathame: string, method: keyof THttpMethods): Readonly<{
|
|
7
|
+
params: Record<string, string>;
|
|
8
|
+
handlers: Array<Required<{
|
|
9
|
+
constructor: new (...args: Array<any>) => unknown;
|
|
10
|
+
funcName: string | symbol;
|
|
11
|
+
func: (...args: Array<any>) => unknown;
|
|
12
|
+
}>>;
|
|
13
|
+
}> | undefined;
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export class RouterGroup {
|
|
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
|
+
}
|
package/dist/hooks/factory.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import "reflect-metadata";
|
|
2
1
|
import "colors";
|
|
3
|
-
|
|
2
|
+
import "reflect-metadata";
|
|
3
|
+
import Qs from "qs";
|
|
4
|
+
import * as Zod from "zod";
|
|
5
|
+
import { RouterGroup } from "../entities";
|
|
6
|
+
export type TBoolFactoryOptions = Required<{
|
|
7
|
+
port: number;
|
|
8
|
+
}> & Partial<{
|
|
9
|
+
prefix: string;
|
|
4
10
|
debug: boolean;
|
|
5
11
|
log: Partial<{
|
|
6
12
|
methods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
|
|
7
13
|
}>;
|
|
8
|
-
queryParser:
|
|
9
|
-
depth: 10;
|
|
10
|
-
arrayLimit: 50;
|
|
11
|
-
}>;
|
|
12
|
-
prefix: string;
|
|
14
|
+
queryParser: Parameters<typeof Qs.parse>[1];
|
|
13
15
|
}>;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*/
|
|
18
|
-
export declare const BoolFactory: (target: new (...args: any[]) => unknown, options?: TBoolFactoryOptions) => import("express-serve-static-core").Express;
|
|
16
|
+
export declare const controllerCreator: (controllerConstructor: new (...args: any[]) => unknown, group: RouterGroup) => RouterGroup;
|
|
17
|
+
export declare const controllerActionArgumentsResolution: (data: unknown, zodSchema: Zod.Schema, argumentIndex: number, funcName: string | symbol) => Promise<any>;
|
|
18
|
+
export declare const BoolFactory: (target: new (...args: any[]) => unknown, options: TBoolFactoryOptions) => void;
|
|
19
19
|
export default BoolFactory;
|