@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
package/.prettierrc ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "semi": true,
3
+ "tabWidth": 4,
4
+ "printWidth": 128,
5
+ "singleQuote": false,
6
+ "singleAttributePerLine": false,
7
+ "trailingComma": "none",
8
+ "bracketSameLine": true,
9
+ "bracketSpacing": true,
10
+ "htmlWhitespaceSensitivity": "ignore"
11
+ }
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 BoolTS
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 BoolTS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,79 +1,66 @@
1
- import type { IService } from "./interfaces";
2
-
3
- import * as Zod from "zod";
4
-
5
- import { Controller, Delete, Get, Inject, Options, Patch, Post, Put, ZodSchema } from "../src";
6
- import { TestService } from "./service";
7
- import { Request, Response } from "express";
8
-
9
-
10
- const getAbcSchema = Zod.object({
11
- headers: Zod.object({
12
- authorization: Zod.string().min(10)
13
- })
14
- });
15
-
16
- const stringSchema = Zod.object({}).refine(async (val) => {
17
- const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
18
- await delay(10000);
19
-
20
- return val;
21
- });
22
-
23
- @Controller("test")
24
- export class TestController {
25
- constructor(
26
- @Inject(TestService) private readonly testService: IService
27
- ) { }
28
-
29
- @Get("abc")
30
- private _get(
31
- @ZodSchema(getAbcSchema) req: Request,
32
- res: Response
33
- ) {
34
- console.log("this.testService", this.testService.exec())
35
- res.json({ test: "success" }).send();
36
- }
37
-
38
- @Post("abc")
39
- private _post(
40
- @ZodSchema(getAbcSchema) req: Request,
41
- res: Response
42
- ) {
43
- console.log("req.body", req.body);
44
- res.json({ test: "success" }).send();
45
- }
46
-
47
- @Put()
48
- private _put(
49
- @ZodSchema(getAbcSchema) req: Request,
50
- res: Response
51
- ) {
52
- res.json({ test: "success" }).send();
53
- }
54
-
55
- @Patch("abc/:id")
56
- private _patch(
57
- @ZodSchema(getAbcSchema) req: Request,
58
- res: Response
59
- ) {
60
- console.log(req.params)
61
- res.json({ test: "success" }).send();
62
- }
63
-
64
- @Delete()
65
- private _delete(
66
- req: Request,
67
- @ZodSchema(stringSchema) res: Response
68
- ) {
69
- res.json({ test: "success" }).send();
70
- }
71
-
72
- @Options()
73
- private _options(
74
- @ZodSchema(getAbcSchema) req: Request,
75
- res: Response
76
- ) {
77
- res.json({ test: "success" }).send();
78
- }
79
- }
1
+ import type { IService } from "./interfaces";
2
+
3
+ import * as Zod from "zod";
4
+
5
+ import { Body, Controller, Delete, Get, Inject, Options, Params, Patch, Post, Put, ZodSchema } from "../src";
6
+ import { TestService } from "./service";
7
+
8
+ const postParamsSchema = Zod.object({
9
+ id: Zod.string(),
10
+ providerId: Zod.string().uuid()
11
+ });
12
+
13
+ const bodySchema = Zod.object({
14
+ data: Zod.object({
15
+ name: Zod.string(),
16
+ age: Zod.number()
17
+ })
18
+ });
19
+
20
+ const requestSchema = Zod.object({
21
+ headers: Zod.object({
22
+ authorization: Zod.string().min(10)
23
+ }),
24
+ body: bodySchema
25
+ });
26
+
27
+ const stringSchema = Zod.object({}).refine(async (val) => {
28
+ const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
29
+ await delay(10000);
30
+
31
+ return val;
32
+ });
33
+
34
+ @Controller("test")
35
+ export class TestController {
36
+ constructor(@Inject(TestService) private readonly testService: IService) {}
37
+
38
+ @Get("abc")
39
+ public get(req: Request) {
40
+ console.log("HEHE");
41
+ console.log("this.testService", this.testService.exec());
42
+ }
43
+
44
+ @Post("abc/:id/provider/:providerId")
45
+ public async post(
46
+ @Params(undefined, postParamsSchema)
47
+ params: any,
48
+ @Body(bodySchema)
49
+ req: Zod.infer<typeof bodySchema>
50
+ ) {
51
+ console.log("req.headers", req);
52
+ console.log("===========================");
53
+ }
54
+
55
+ @Put()
56
+ public put(req: any) {}
57
+
58
+ @Patch("abc/:testId")
59
+ public patch(req: Request) {}
60
+
61
+ @Delete()
62
+ public delete(req: Request) {}
63
+
64
+ @Options()
65
+ public options(req: Request) {}
66
+ }
package/__test/index.ts CHANGED
@@ -1,11 +1,8 @@
1
- import "reflect-metadata";
2
-
3
- import { BoolFactory } from "../src";
4
- import { TestModule } from "./module";
5
-
6
-
7
- const app = BoolFactory(TestModule);
8
-
9
- app.listen(3000, () => {
10
- console.log(`Listening on port ${3000}...`);
11
- });
1
+ import "reflect-metadata";
2
+
3
+ import { BoolFactory } from "../src";
4
+ import { TestModule } from "./module";
5
+
6
+ BoolFactory(TestModule, {
7
+ port: 3000
8
+ });
@@ -1,7 +1,7 @@
1
- export interface IRepository {
2
- exec(): void;
3
- }
4
-
5
- export interface IService {
6
- exec(): void;
7
- }
1
+ export interface IRepository {
2
+ exec(): void;
3
+ }
4
+
5
+ export interface IService {
6
+ exec(): void;
7
+ }
package/__test/module.ts CHANGED
@@ -1,17 +1,16 @@
1
- import { Module } from "../src";
2
- import { TestController } from "./controller";
3
- // import { TestRepository } from "./repository";
4
- import { TestService } from "./service";
5
-
6
- @Module({
7
- controllers: [
8
- TestController
9
- ],
10
- dependencies: [
11
- // TestService,
12
- // TestRepository
13
- ]
14
- })
15
- export class TestModule { }
16
-
17
- export default TestModule;
1
+ import { Module } from "../src";
2
+ import { TestController } from "./controller";
3
+ // import { TestRepository } from "./repository";
4
+ import { TestService } from "./service";
5
+
6
+ @Module({
7
+ allowOrigins: ["http://localhost:7000"],
8
+ controllers: [TestController],
9
+ dependencies: [
10
+ // TestService,
11
+ // TestRepository
12
+ ]
13
+ })
14
+ export class TestModule {}
15
+
16
+ export default TestModule;
@@ -1,16 +1,16 @@
1
- import type { IRepository } from "./interfaces";
2
-
3
- import { Injectable } from "../src";
4
-
5
-
6
- @Injectable()
7
- export class TestRepository implements IRepository {
8
- /**
9
- *
10
- */
11
- exec() {
12
- console.log("This is test repository.");
13
- }
14
- }
15
-
16
- export default TestRepository;
1
+ import type { IRepository } from "./interfaces";
2
+
3
+ import { Injectable } from "../src";
4
+
5
+
6
+ @Injectable()
7
+ export class TestRepository implements IRepository {
8
+ /**
9
+ *
10
+ */
11
+ exec() {
12
+ console.log("This is test repository.");
13
+ }
14
+ }
15
+
16
+ export default TestRepository;
package/__test/service.ts CHANGED
@@ -1,20 +1,20 @@
1
- import type { IService, IRepository } from "./interfaces";
2
-
3
- import { Inject, Injectable } from "../src";
4
- import { TestRepository } from "./repository";
5
-
6
-
7
- @Injectable()
8
- export class TestService implements IService {
9
- constructor(
10
- @Inject(TestRepository) private readonly testRepository: IRepository
11
- ) { }
12
-
13
- /**
14
- *
15
- */
16
- exec() {
17
- console.log("This is test service.", this.testRepository);
18
- this.testRepository.exec();
19
- }
20
- }
1
+ import type { IService, IRepository } from "./interfaces";
2
+
3
+ import { Inject, Injectable } from "../src";
4
+ import { TestRepository } from "./repository";
5
+
6
+
7
+ @Injectable()
8
+ export class TestService implements IService {
9
+ constructor(
10
+ @Inject(TestRepository) private readonly testRepository: IRepository
11
+ ) { }
12
+
13
+ /**
14
+ *
15
+ */
16
+ exec() {
17
+ console.log("This is test service.", this.testRepository);
18
+ this.testRepository.exec();
19
+ }
20
+ }
package/bun.lockb CHANGED
Binary file
@@ -0,0 +1,43 @@
1
+ import * as Zod from "zod";
2
+ export declare enum EArgumentTypes {
3
+ headers = "HEADERS",
4
+ body = "BODY",
5
+ params = "PARAMS",
6
+ param = "PARAM",
7
+ query = "QUERY",
8
+ request = "REQUEST"
9
+ }
10
+ export type TMetadata = {
11
+ index: number;
12
+ type: EArgumentTypes.headers;
13
+ zodSchema?: Zod.Schema;
14
+ } | {
15
+ index: number;
16
+ type: EArgumentTypes.body;
17
+ zodSchema?: Zod.Schema;
18
+ parser?: "arrayBuffer" | "blob" | "formData" | "json" | "text";
19
+ } | {
20
+ index: number;
21
+ type: EArgumentTypes.params;
22
+ zodSchema?: Zod.Schema;
23
+ } | {
24
+ index: number;
25
+ type: EArgumentTypes.param;
26
+ key: string;
27
+ zodSchema?: Zod.Schema;
28
+ } | {
29
+ index: number;
30
+ type: EArgumentTypes.query;
31
+ zodSchema?: Zod.Schema;
32
+ } | {
33
+ index: number;
34
+ type: EArgumentTypes.request;
35
+ zodSchema?: Zod.Schema;
36
+ };
37
+ export declare const controllerActionArgumentsKey: unique symbol;
38
+ export declare const Headers: (zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
39
+ export declare const Body: (zodSchema?: Zod.Schema, parser?: "arrayBuffer" | "blob" | "formData" | "json" | "text") => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
40
+ export declare const Params: (zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
41
+ export declare const Param: (key: string, zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
42
+ export declare const Query: (zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
43
+ export declare const Request: (zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
@@ -0,0 +1,97 @@
1
+ import * as Zod from "zod";
2
+ export var EArgumentTypes;
3
+ (function (EArgumentTypes) {
4
+ EArgumentTypes["headers"] = "HEADERS";
5
+ EArgumentTypes["body"] = "BODY";
6
+ EArgumentTypes["params"] = "PARAMS";
7
+ EArgumentTypes["param"] = "PARAM";
8
+ EArgumentTypes["query"] = "QUERY";
9
+ EArgumentTypes["request"] = "REQUEST";
10
+ })(EArgumentTypes || (EArgumentTypes = {}));
11
+ export const controllerActionArgumentsKey = Symbol.for("__bool:controller.action::arguments__");
12
+ export const Headers = (zodSchema) => {
13
+ return (target, methodName, parameterIndex) => {
14
+ if (!methodName) {
15
+ return;
16
+ }
17
+ const headersMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
18
+ headersMetadata[`argumentIndexes.${parameterIndex}`] = {
19
+ index: parameterIndex,
20
+ type: EArgumentTypes.headers,
21
+ zodSchema: zodSchema
22
+ };
23
+ Reflect.defineMetadata(controllerActionArgumentsKey, headersMetadata, target.constructor, methodName);
24
+ };
25
+ };
26
+ export const Body = (zodSchema, parser) => {
27
+ return (target, methodName, parameterIndex) => {
28
+ if (!methodName) {
29
+ return;
30
+ }
31
+ const bodyMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
32
+ bodyMetadata[`argumentIndexes.${parameterIndex}`] = {
33
+ index: parameterIndex,
34
+ type: EArgumentTypes.body,
35
+ zodSchema: zodSchema,
36
+ parser: parser
37
+ };
38
+ Reflect.defineMetadata(controllerActionArgumentsKey, bodyMetadata, target.constructor, methodName);
39
+ };
40
+ };
41
+ export const Params = (zodSchema) => {
42
+ return (target, methodName, parameterIndex) => {
43
+ if (!methodName) {
44
+ return;
45
+ }
46
+ const paramsMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
47
+ paramsMetadata[`argumentIndexes.${parameterIndex}`] = {
48
+ index: parameterIndex,
49
+ type: EArgumentTypes.params,
50
+ zodSchema: zodSchema
51
+ };
52
+ Reflect.defineMetadata(controllerActionArgumentsKey, paramsMetadata, target.constructor, methodName);
53
+ };
54
+ };
55
+ export const Param = (key, zodSchema) => {
56
+ return (target, methodName, parameterIndex) => {
57
+ if (!methodName) {
58
+ return;
59
+ }
60
+ const paramsMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
61
+ paramsMetadata[`argumentIndexes.${parameterIndex}`] = {
62
+ index: parameterIndex,
63
+ type: EArgumentTypes.param,
64
+ key: key,
65
+ zodSchema: zodSchema
66
+ };
67
+ Reflect.defineMetadata(controllerActionArgumentsKey, paramsMetadata, target.constructor, methodName);
68
+ };
69
+ };
70
+ export const Query = (zodSchema) => {
71
+ return (target, methodName, parameterIndex) => {
72
+ if (!methodName) {
73
+ return;
74
+ }
75
+ const queryMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
76
+ queryMetadata[`argumentIndexes.${parameterIndex}`] = {
77
+ index: parameterIndex,
78
+ type: EArgumentTypes.params,
79
+ zodSchema: zodSchema
80
+ };
81
+ Reflect.defineMetadata(controllerActionArgumentsKey, queryMetadata, target.constructor, methodName);
82
+ };
83
+ };
84
+ export const Request = (zodSchema) => {
85
+ return (target, methodName, parameterIndex) => {
86
+ if (!methodName) {
87
+ return;
88
+ }
89
+ const queryMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
90
+ queryMetadata[`argumentIndexes.${parameterIndex}`] = {
91
+ index: parameterIndex,
92
+ type: EArgumentTypes.request,
93
+ zodSchema: zodSchema
94
+ };
95
+ Reflect.defineMetadata(controllerActionArgumentsKey, queryMetadata, target.constructor, methodName);
96
+ };
97
+ };
@@ -1,3 +1,3 @@
1
- export declare const controllerKey = "__bool:controller__";
2
- export declare const Controller: (prefix: string) => <T extends Object>(target: T, context?: ClassDecoratorContext) => T;
1
+ export declare const controllerKey: unique symbol;
2
+ export declare const Controller: (prefix: string) => <T extends Object>(target: T) => T;
3
3
  export default Controller;
@@ -1,12 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Controller = exports.controllerKey = void 0;
4
- const injectable_1 = require("./injectable");
5
- exports.controllerKey = "__bool:controller__";
6
- const Controller = (prefix) => (target, context) => {
7
- Reflect.defineMetadata(exports.controllerKey, !prefix.startsWith("/") ? `/${prefix}` : prefix, target);
8
- Reflect.defineMetadata(injectable_1.injectableKey, undefined, target);
1
+ import { injectableKey } from "./injectable";
2
+ export const controllerKey = Symbol.for("__bool:controller__");
3
+ export const Controller = (prefix) => (target) => {
4
+ Reflect.defineMetadata(controllerKey, !prefix.startsWith("/") ? `/${prefix}` : prefix, target);
5
+ Reflect.defineMetadata(injectableKey, undefined, target);
9
6
  return target;
10
7
  };
11
- exports.Controller = Controller;
12
- exports.default = exports.Controller;
8
+ export default Controller;
@@ -4,7 +4,7 @@ export interface IControllerRoute {
4
4
  methodName: string;
5
5
  descriptor: PropertyDescriptor;
6
6
  }
7
- export declare const controllerRoutesKey = "__bool:controller.routes__";
7
+ export declare const controllerRoutesKey: unique symbol;
8
8
  /**
9
9
  *
10
10
  * @param path