@bool-ts/core 1.4.1 → 1.5.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 -11
- package/LICENSE +21 -21
- package/__test/afterDispatcher.ts +9 -0
- package/__test/beforeDispatcher.ts +9 -0
- package/__test/controller.ts +66 -66
- package/__test/firstGuard.ts +10 -0
- package/__test/firstMiddleware.ts +9 -0
- package/__test/index.ts +8 -8
- package/__test/interfaces.ts +7 -7
- package/__test/module.ts +25 -16
- package/__test/repository.ts +16 -16
- package/__test/secondGuard.ts +10 -0
- package/__test/secondMiddleware.ts +9 -0
- package/__test/service.ts +20 -20
- package/bun.lockb +0 -0
- package/dist/decorators/arguments.d.ts +12 -6
- package/dist/decorators/arguments.js +39 -26
- package/dist/decorators/controller.d.ts +9 -1
- package/dist/decorators/controller.js +6 -4
- package/dist/decorators/dispatcher.d.ts +7 -0
- package/dist/decorators/dispatcher.js +9 -0
- package/dist/decorators/guard.d.ts +7 -0
- package/dist/decorators/guard.js +9 -0
- package/dist/decorators/http.d.ts +4 -3
- package/dist/decorators/http.js +6 -5
- package/dist/decorators/index.d.ts +13 -3
- package/dist/decorators/index.js +5 -2
- package/dist/decorators/injectable.d.ts +1 -1
- package/dist/decorators/injectable.js +1 -4
- package/dist/decorators/middleware.d.ts +7 -0
- package/dist/decorators/middleware.js +9 -0
- package/dist/decorators/module.d.ts +28 -7
- package/dist/decorators/module.js +48 -1
- package/dist/entities/route.d.ts +5 -5
- package/dist/entities/routerGroup.d.ts +1 -1
- package/dist/hooks/factory.d.ts +3 -3
- package/dist/hooks/factory.js +282 -60
- package/dist/hooks/injector.js +6 -5
- package/dist/interfaces/index.d.ts +5 -2
- package/dist/interfaces/index.js +1 -1
- package/package.json +1 -1
- package/src/decorators/arguments.ts +214 -186
- package/src/decorators/controller.ts +22 -14
- package/src/decorators/dispatcher.ts +19 -0
- package/src/decorators/guard.ts +19 -0
- package/src/decorators/http.ts +81 -81
- package/src/decorators/index.ts +28 -7
- package/src/decorators/inject.ts +13 -13
- package/src/decorators/injectable.ts +8 -11
- package/src/decorators/middleware.ts +19 -0
- package/src/decorators/module.ts +100 -21
- package/src/decorators/zodSchema.ts +20 -20
- package/src/entities/index.ts +3 -3
- package/src/entities/route.ts +328 -328
- package/src/entities/router.ts +35 -35
- package/src/entities/routerGroup.ts +34 -34
- package/src/hooks/factory.ts +653 -319
- package/src/hooks/index.ts +2 -2
- package/src/hooks/injector.ts +43 -43
- package/src/http/clientError.ts +45 -45
- package/src/http/index.ts +63 -63
- package/src/http/serverError.ts +38 -38
- package/src/index.ts +6 -6
- package/src/interfaces/controller.d.ts +1 -0
- package/src/interfaces/dispatcher.d.ts +3 -0
- package/src/interfaces/guard.d.ts +3 -0
- package/src/interfaces/index.ts +5 -3
- package/src/interfaces/middleware.d.ts +3 -0
- package/src/interfaces/module.d.ts +1 -0
- package/test.http +31 -30
- package/tsconfig.json +107 -107
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const guardKey = Symbol.for("__bool:guard__");
|
|
2
|
+
export const Guard = () => (target) => {
|
|
3
|
+
if (!("enforce" in target.prototype) || typeof target.prototype.enforce !== "function") {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
const metadata = undefined;
|
|
7
|
+
Reflect.defineMetadata(guardKey, metadata, target);
|
|
8
|
+
};
|
|
9
|
+
export default Guard;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type TRoute = {
|
|
2
2
|
path: string;
|
|
3
3
|
httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
|
|
4
4
|
methodName: string;
|
|
5
5
|
descriptor: PropertyDescriptor;
|
|
6
|
-
}
|
|
7
|
-
export
|
|
6
|
+
};
|
|
7
|
+
export type THttpMetadata = Array<TRoute>;
|
|
8
|
+
export declare const controllerHttpKey: unique symbol;
|
|
8
9
|
/**
|
|
9
10
|
*
|
|
10
11
|
* @param path
|
package/dist/decorators/http.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
export const
|
|
1
|
+
export const controllerHttpKey = Symbol.for("__bool:controller.http__");
|
|
2
2
|
const defaultDecorator = (path, method) => (target, methodName, descriptor) => {
|
|
3
3
|
if (!(descriptor.value instanceof Function)) {
|
|
4
4
|
throw Error(`${method} decorator only use for class method.`);
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
...(Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || []),
|
|
6
|
+
const metadata = [
|
|
7
|
+
...(Reflect.getOwnMetadata(controllerHttpKey, target.constructor) || []),
|
|
9
8
|
{
|
|
10
9
|
path: !path.startsWith("/") ? `/${path}` : path,
|
|
11
10
|
httpMethod: method.toUpperCase(),
|
|
12
11
|
methodName: methodName,
|
|
13
12
|
descriptor: descriptor
|
|
14
13
|
}
|
|
15
|
-
]
|
|
14
|
+
];
|
|
15
|
+
// Define controller metadata
|
|
16
|
+
Reflect.defineMetadata(controllerHttpKey, metadata, target.constructor);
|
|
16
17
|
};
|
|
17
18
|
/**
|
|
18
19
|
*
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { RequestHeaders, Body, Params, Param, Query, Request, ResponseHeaders, EArgumentTypes, argumentsKey } from "./arguments";
|
|
2
2
|
export { Controller, controllerKey } from "./controller";
|
|
3
|
+
export { Guard, guardKey } from "./guard";
|
|
3
4
|
export { Inject, injectKey } from "./inject";
|
|
4
5
|
export { Injectable, injectableKey } from "./injectable";
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
6
|
+
export { Middleware, middlewareKey } from "./middleware";
|
|
7
|
+
export { Module, moduleKey } from "./module";
|
|
8
|
+
export { Get, Post, Put, Patch, Delete, Options, controllerHttpKey } from "./http";
|
|
7
9
|
export { ZodSchema, controllerRouteZodSchemaKey } from "./zodSchema";
|
|
10
|
+
export { Dispatcher, dispatcherKey } from "./dispatcher";
|
|
11
|
+
export type { TControllerMetadata } from "./controller";
|
|
12
|
+
export type { TModuleMetadata, TModuleOptions } from "./module";
|
|
13
|
+
export type { THttpMetadata } from "./http";
|
|
14
|
+
export type { TArgumentsMetadata } from "./arguments";
|
|
15
|
+
export type { TMiddlewareMetadata } from "./middleware";
|
|
16
|
+
export type { TGuardMetadata } from "./guard";
|
|
17
|
+
export type { TDispatcherMetadata } from "./dispatcher";
|
package/dist/decorators/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { RequestHeaders, Body, Params, Param, Query, Request, ResponseHeaders, EArgumentTypes, argumentsKey } from "./arguments";
|
|
2
2
|
export { Controller, controllerKey } from "./controller";
|
|
3
|
+
export { Guard, guardKey } from "./guard";
|
|
3
4
|
export { Inject, injectKey } from "./inject";
|
|
4
5
|
export { Injectable, injectableKey } from "./injectable";
|
|
6
|
+
export { Middleware, middlewareKey } from "./middleware";
|
|
5
7
|
export { Module, moduleKey } from "./module";
|
|
6
|
-
export { Get, Post, Put, Patch, Delete, Options,
|
|
8
|
+
export { Get, Post, Put, Patch, Delete, Options, controllerHttpKey } from "./http";
|
|
7
9
|
export { ZodSchema, controllerRouteZodSchemaKey } from "./zodSchema";
|
|
10
|
+
export { Dispatcher, dispatcherKey } from "./dispatcher";
|
|
@@ -1,6 +1,3 @@
|
|
|
1
1
|
export const injectableKey = Symbol.for("__bool:injectable__");
|
|
2
|
-
export const Injectable = () => (target) =>
|
|
3
|
-
Reflect.defineMetadata(injectableKey, undefined, target);
|
|
4
|
-
return target;
|
|
5
|
-
};
|
|
2
|
+
export const Injectable = () => (target) => Reflect.defineMetadata(injectableKey, undefined, target);
|
|
6
3
|
export default Injectable;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { IMiddleware } from "../interfaces";
|
|
2
|
+
export type TMiddlewareMetadata = undefined;
|
|
3
|
+
export declare const middlewareKey: unique symbol;
|
|
4
|
+
export declare const Middleware: () => <T extends {
|
|
5
|
+
new (...args: any[]): IMiddleware;
|
|
6
|
+
}>(target: T) => void;
|
|
7
|
+
export default Middleware;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const middlewareKey = Symbol.for("__bool:middleware__");
|
|
2
|
+
export const Middleware = () => (target) => {
|
|
3
|
+
if (!("enforce" in target.prototype) || typeof target.prototype.enforce !== "function") {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
const metadata = undefined;
|
|
7
|
+
Reflect.defineMetadata(middlewareKey, metadata, target);
|
|
8
|
+
};
|
|
9
|
+
export default Middleware;
|
|
@@ -1,12 +1,33 @@
|
|
|
1
|
+
import type { IModule } from "../interfaces/module";
|
|
2
|
+
type TInstances = Array<new (...args: any[]) => any>;
|
|
1
3
|
export type TModuleOptions = Partial<{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
options: Partial<{
|
|
5
|
+
prefix: string;
|
|
6
|
+
allowOrigins: string | Array<string>;
|
|
7
|
+
allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
|
|
8
|
+
}>;
|
|
9
|
+
dependencies: TInstances;
|
|
10
|
+
controllers: TInstances;
|
|
11
|
+
middlewares: TInstances;
|
|
12
|
+
guards: TInstances;
|
|
13
|
+
beforeDispatchers: TInstances;
|
|
14
|
+
afterDispatchers: TInstances;
|
|
15
|
+
}> | undefined;
|
|
16
|
+
export type TModuleMetadata = Partial<{
|
|
17
|
+
options: Partial<{
|
|
18
|
+
prefix: string;
|
|
19
|
+
allowOrigins: string | Array<string>;
|
|
20
|
+
allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
|
|
21
|
+
}>;
|
|
22
|
+
controllers: TInstances;
|
|
23
|
+
dependencies: TInstances;
|
|
24
|
+
middlewares: TInstances;
|
|
25
|
+
guards: TInstances;
|
|
26
|
+
beforeDispatchers: TInstances;
|
|
27
|
+
afterDispatchers: TInstances;
|
|
7
28
|
}> | undefined;
|
|
8
29
|
export declare const moduleKey: unique symbol;
|
|
9
30
|
export declare const Module: (args?: TModuleOptions) => <T extends {
|
|
10
|
-
new (...args: any[]):
|
|
11
|
-
}>(target: T) =>
|
|
31
|
+
new (...args: any[]): IModule;
|
|
32
|
+
}>(target: T) => void;
|
|
12
33
|
export default Module;
|
|
@@ -1,6 +1,53 @@
|
|
|
1
|
+
import { controllerKey } from "./controller";
|
|
2
|
+
import { dispatcherKey } from "./dispatcher";
|
|
3
|
+
import { guardKey } from "./guard";
|
|
4
|
+
import { injectableKey } from "./injectable";
|
|
5
|
+
import { middlewareKey } from "./middleware";
|
|
1
6
|
export const moduleKey = Symbol.for("__bool:module__");
|
|
2
7
|
export const Module = (args) => (target) => {
|
|
8
|
+
const { middlewares, guards, beforeDispatchers, controllers, afterDispatchers, dependencies } = args || {};
|
|
9
|
+
if (middlewares) {
|
|
10
|
+
for (let i = 0; i < middlewares.length; i++) {
|
|
11
|
+
if (!Reflect.getOwnMetadataKeys(middlewares[i]).includes(middlewareKey)) {
|
|
12
|
+
throw Error(`${middlewares[i].name} is not a middleware.`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (guards) {
|
|
17
|
+
for (let i = 0; i < guards.length; i++) {
|
|
18
|
+
if (!Reflect.getOwnMetadataKeys(guards[i]).includes(guardKey)) {
|
|
19
|
+
throw Error(`${guards[i].name} is not a guard.`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (beforeDispatchers) {
|
|
24
|
+
for (let i = 0; i < beforeDispatchers.length; i++) {
|
|
25
|
+
if (!Reflect.getOwnMetadataKeys(beforeDispatchers[i]).includes(dispatcherKey)) {
|
|
26
|
+
throw Error(`${beforeDispatchers[i].name} is not a dispatcher.`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (controllers) {
|
|
31
|
+
for (let i = 0; i < controllers.length; i++) {
|
|
32
|
+
if (!Reflect.getOwnMetadataKeys(controllers[i]).includes(controllerKey)) {
|
|
33
|
+
throw Error(`${controllers[i].name} is not a controller.`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (afterDispatchers) {
|
|
38
|
+
for (let i = 0; i < afterDispatchers.length; i++) {
|
|
39
|
+
if (!Reflect.getOwnMetadataKeys(afterDispatchers[i]).includes(dispatcherKey)) {
|
|
40
|
+
throw Error(`${afterDispatchers[i].name} is not a dispatcher.`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (dependencies) {
|
|
45
|
+
for (let i = 0; i < dependencies.length; i++) {
|
|
46
|
+
if (!Reflect.getOwnMetadataKeys(dependencies[i]).includes(injectableKey)) {
|
|
47
|
+
throw Error(`${dependencies[i].name} is not an injectable.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
3
51
|
Reflect.defineMetadata(moduleKey, args, target);
|
|
4
|
-
return target;
|
|
5
52
|
};
|
|
6
53
|
export default Module;
|
package/dist/entities/route.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { THttpMethods } from "../http";
|
|
2
2
|
type THandler<T = unknown> = Required<{
|
|
3
|
-
|
|
3
|
+
class: new (...args: Array<any>) => T;
|
|
4
4
|
funcName: string | symbol;
|
|
5
5
|
func: (...args: Array<any>) => unknown;
|
|
6
6
|
}>;
|
|
@@ -47,7 +47,7 @@ export declare class Route {
|
|
|
47
47
|
* @returns
|
|
48
48
|
*/
|
|
49
49
|
connect(...handlers: Array<THandler>): this | Map<keyof THttpMethods, Required<{
|
|
50
|
-
|
|
50
|
+
class: new (...args: Array<any>) => unknown;
|
|
51
51
|
funcName: string | symbol;
|
|
52
52
|
func: (...args: Array<any>) => unknown;
|
|
53
53
|
}>[]>;
|
|
@@ -57,7 +57,7 @@ export declare class Route {
|
|
|
57
57
|
* @returns
|
|
58
58
|
*/
|
|
59
59
|
options(...handlers: Array<THandler>): this | Map<keyof THttpMethods, Required<{
|
|
60
|
-
|
|
60
|
+
class: new (...args: Array<any>) => unknown;
|
|
61
61
|
funcName: string | symbol;
|
|
62
62
|
func: (...args: Array<any>) => unknown;
|
|
63
63
|
}>[]>;
|
|
@@ -67,7 +67,7 @@ export declare class Route {
|
|
|
67
67
|
* @returns
|
|
68
68
|
*/
|
|
69
69
|
trace(...handlers: Array<THandler>): this | Map<keyof THttpMethods, Required<{
|
|
70
|
-
|
|
70
|
+
class: new (...args: Array<any>) => unknown;
|
|
71
71
|
funcName: string | symbol;
|
|
72
72
|
func: (...args: Array<any>) => unknown;
|
|
73
73
|
}>[]>;
|
|
@@ -77,7 +77,7 @@ export declare class Route {
|
|
|
77
77
|
* @returns
|
|
78
78
|
*/
|
|
79
79
|
patch(...handlers: Array<THandler>): this | Map<keyof THttpMethods, Required<{
|
|
80
|
-
|
|
80
|
+
class: new (...args: Array<any>) => unknown;
|
|
81
81
|
funcName: string | symbol;
|
|
82
82
|
func: (...args: Array<any>) => unknown;
|
|
83
83
|
}>[]>;
|
|
@@ -6,7 +6,7 @@ export declare class RouterGroup {
|
|
|
6
6
|
find(pathame: string, method: keyof THttpMethods): Readonly<{
|
|
7
7
|
params: Record<string, string>;
|
|
8
8
|
handlers: Array<Required<{
|
|
9
|
-
|
|
9
|
+
class: new (...args: Array<any>) => unknown;
|
|
10
10
|
funcName: string | symbol;
|
|
11
11
|
func: (...args: Array<any>) => unknown;
|
|
12
12
|
}>>;
|
package/dist/hooks/factory.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export type TBoolFactoryOptions = Required<{
|
|
|
13
13
|
}>;
|
|
14
14
|
queryParser: Parameters<typeof Qs.parse>[1];
|
|
15
15
|
}>;
|
|
16
|
-
export declare const controllerCreator: (controllerConstructor: new (...args: any[]) => unknown, group: RouterGroup) => RouterGroup;
|
|
17
|
-
export declare const
|
|
18
|
-
export declare const BoolFactory: (target: new (...args: any[]) => unknown, options: TBoolFactoryOptions) =>
|
|
16
|
+
export declare const controllerCreator: (controllerConstructor: new (...args: any[]) => unknown, group: RouterGroup, prefix?: string) => RouterGroup;
|
|
17
|
+
export declare const argumentsResolution: (data: unknown, zodSchema: Zod.Schema, argumentIndex: number, funcName: string | symbol) => Promise<any>;
|
|
18
|
+
export declare const BoolFactory: (target: new (...args: any[]) => unknown, options: TBoolFactoryOptions) => import("bun").Server | undefined;
|
|
19
19
|
export default BoolFactory;
|