@bool-ts/core 1.0.11 → 1.1.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/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,63 +1,63 @@
1
- import type { IService } from "./interfaces";
2
-
3
- import { Controller, Delete, Get, Inject, Options, Patch, Post, Put } from "../src";
4
- import { TestService } from "./service";
5
- import { Request, Response } from "express";
6
-
7
-
8
- @Controller("test")
9
- export class TestController {
10
- constructor(
11
- @Inject(TestService) private readonly testService: IService
12
- ) { }
13
-
14
- @Get()
15
- private _get(
16
- req: Request,
17
- res: Response
18
- ) {
19
- console.log("this.testService", this.testService.exec())
20
- res.json({ test: "success" }).send();
21
- }
22
-
23
- @Post()
24
- private _post(
25
- req: Request,
26
- res: Response
27
- ) {
28
- console.log("req.body", req.body);
29
- res.json({ test: "success" }).send();
30
- }
31
-
32
- @Put()
33
- private _put(
34
- req: Request,
35
- res: Response
36
- ) {
37
- res.json({ test: "success" }).send();
38
- }
39
-
40
- @Patch()
41
- private _patch(
42
- req: Request,
43
- res: Response
44
- ) {
45
- res.json({ test: "success" }).send();
46
- }
47
-
48
- @Delete()
49
- private _delete(
50
- req: Request,
51
- res: Response
52
- ) {
53
- res.json({ test: "success" }).send();
54
- }
55
-
56
- @Options()
57
- private _options(
58
- req: Request,
59
- res: Response
60
- ) {
61
- res.json({ test: "success" }).send();
62
- }
63
- }
1
+ import type { IService } from "./interfaces";
2
+
3
+ import { Controller, Delete, Get, Inject, Options, Patch, Post, Put } from "../src";
4
+ import { TestService } from "./service";
5
+ import { Request, Response } from "express";
6
+
7
+
8
+ @Controller("test")
9
+ export class TestController {
10
+ constructor(
11
+ @Inject(TestService) private readonly testService: IService
12
+ ) { }
13
+
14
+ @Get()
15
+ private _get(
16
+ req: Request,
17
+ res: Response
18
+ ) {
19
+ console.log("this.testService", this.testService.exec())
20
+ res.json({ test: "success" }).send();
21
+ }
22
+
23
+ @Post()
24
+ private _post(
25
+ req: Request,
26
+ res: Response
27
+ ) {
28
+ console.log("req.body", req.body);
29
+ res.json({ test: "success" }).send();
30
+ }
31
+
32
+ @Put()
33
+ private _put(
34
+ req: Request,
35
+ res: Response
36
+ ) {
37
+ res.json({ test: "success" }).send();
38
+ }
39
+
40
+ @Patch()
41
+ private _patch(
42
+ req: Request,
43
+ res: Response
44
+ ) {
45
+ res.json({ test: "success" }).send();
46
+ }
47
+
48
+ @Delete()
49
+ private _delete(
50
+ req: Request,
51
+ res: Response
52
+ ) {
53
+ res.json({ test: "success" }).send();
54
+ }
55
+
56
+ @Options()
57
+ private _options(
58
+ req: Request,
59
+ res: Response
60
+ ) {
61
+ res.json({ test: "success" }).send();
62
+ }
63
+ }
package/__test/index.ts CHANGED
@@ -1,11 +1,11 @@
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
+
7
+ const app = BoolFactory(TestModule);
8
+
9
+ app.listen(3000, () => {
10
+ console.log(`Listening on port ${3000}...`);
11
+ });
@@ -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,17 @@
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
+ controllers: [
8
+ TestController
9
+ ],
10
+ dependencies: [
11
+ // TestService,
12
+ // TestRepository
13
+ ]
14
+ })
15
+ export class TestModule { }
16
+
17
+ 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
File without changes
@@ -1,8 +1,18 @@
1
1
  import "reflect-metadata";
2
2
  import "colors";
3
+ export type TBoolFactoryOptions = Partial<{
4
+ debug: boolean;
5
+ log: Partial<{
6
+ methods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
7
+ }>;
8
+ queryParser: Partial<{
9
+ depth: 10;
10
+ arrayLimit: 50;
11
+ }>;
12
+ }>;
3
13
  /**
4
14
  *
5
15
  * @param target
6
16
  */
7
- export declare const BoolFactory: (target: new (...args: any[]) => unknown) => import("express-serve-static-core").Express;
17
+ export declare const BoolFactory: (target: new (...args: any[]) => unknown, options?: TBoolFactoryOptions) => import("express-serve-static-core").Express;
8
18
  export default BoolFactory;
@@ -5,6 +5,7 @@ import * as ResponseTime from "response-time";
5
5
  import { controllerKey, controllerRoutesKey, moduleKey } from "../decorators";
6
6
  import { default as ExpressApp, Router, json, urlencoded } from "express";
7
7
  import { Injector } from "./injector";
8
+ import { errorInfer } from "../http";
8
9
  /**
9
10
  *
10
11
  * @param target
@@ -46,7 +47,7 @@ const controllerCreator = (target, router = Router()) => {
46
47
  *
47
48
  * @param target
48
49
  */
49
- export const BoolFactory = (target) => {
50
+ export const BoolFactory = (target, options) => {
50
51
  if (!Reflect.getOwnMetadataKeys(target).includes(moduleKey)) {
51
52
  throw Error(`${target.name} is not a module.`);
52
53
  }
@@ -58,19 +59,13 @@ export const BoolFactory = (target) => {
58
59
  const allowMethods = !metadata?.allowMethods ?
59
60
  ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] : metadata.allowMethods;
60
61
  const app = ExpressApp();
61
- const configs = Object.freeze({
62
- allowLogsMethods: [
63
- "GET",
64
- "POST",
65
- "PUT",
66
- "PATCH",
67
- "DELETE"
68
- ]
62
+ const factoryOptions = Object.freeze({
63
+ allowLogsMethods: !options?.log?.methods ? ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] : options.log.methods
69
64
  });
70
65
  app.set("etag", "strong");
71
66
  app.set("query parser", (query) => Qs.parse(query, {
72
- depth: 10,
73
- arrayLimit: 50
67
+ depth: !options?.queryParser?.depth || options.queryParser.depth < 0 ? 10 : options.queryParser.depth,
68
+ arrayLimit: !options?.queryParser?.arrayLimit || options.queryParser.arrayLimit < 0 ? 50 : options.queryParser.arrayLimit
74
69
  }));
75
70
  app.use(urlencoded({
76
71
  extended: true,
@@ -103,13 +98,16 @@ export const BoolFactory = (target) => {
103
98
  },
104
99
  // Error catcher
105
100
  (err, req, res, next) => {
106
- console.error(err);
107
- next();
101
+ errorInfer(res, err);
102
+ if (!options?.debug) {
103
+ return;
104
+ }
105
+ console.error("Headers:", JSON.stringify(req.headers), "\nBody:", JSON.stringify(req.body), "\nError:", JSON.stringify(err));
108
106
  },
109
107
  // Response time log
110
108
  ResponseTime.default((req, res, time) => {
111
109
  const requestMethod = req.method.toUpperCase();
112
- if (!configs.allowLogsMethods.includes(requestMethod)) {
110
+ if (!factoryOptions.allowLogsMethods.includes(requestMethod)) {
113
111
  return;
114
112
  }
115
113
  const convertedMethod = `${requestMethod.yellow}`.bgBlue;
@@ -0,0 +1,40 @@
1
+ export declare const httpClientErrors: Readonly<{
2
+ 400: "BAD_REQUEST";
3
+ 401: "UNAUTHORIZED";
4
+ 402: "PAYMENT_REQUIRED";
5
+ 403: "FORBIDDEN";
6
+ 404: "NOT_FOUND";
7
+ 405: "METHOD_NOT_ALLOWED";
8
+ 406: "NOT_ACCEPTABLE";
9
+ 407: "PROXY_AUTHENCATION_REQUIRED";
10
+ 408: "REQUEST_TIMEOUT";
11
+ 409: "CONFLICT";
12
+ 410: "GONE";
13
+ 411: "LENGTH_REQUIRED";
14
+ 412: "PRECONDITION_FAILED";
15
+ 413: "PAYLOAD_TOO_LARGE";
16
+ 414: "URI_TOO_LONG";
17
+ 415: "UNSUPPORTED_MEDIA_TYPE";
18
+ 416: "RANGE_NOT_SATISFIABLE";
19
+ 417: "EXPECTATION_FAILED";
20
+ 418: "IM_A_TEAPOT";
21
+ 421: "MISDIRECTED_REQUEST";
22
+ 422: "UNPROCESSABLE_ENTITY";
23
+ 423: "LOCKED";
24
+ 424: "FAILED_DEPENDENCY";
25
+ 425: "TOO_EARLY_";
26
+ 426: "UPGRAGE_REQUIRED";
27
+ 428: "PRECONDITION_REQUIRED";
28
+ 429: "TOO_MANY_REQUESTS";
29
+ 431: "REQUEST_HEADER_FIELDS_TOO_LARGE";
30
+ 451: "UNAVAILABLE_FOR_LEGAL_REASONS";
31
+ }>;
32
+ export declare class HttpClientError<T extends keyof typeof httpClientErrors = keyof typeof httpClientErrors, K = any> extends Error {
33
+ readonly httpCode: T;
34
+ readonly message: typeof httpClientErrors[T];
35
+ readonly data: K;
36
+ constructor({ httpCode, data }: {
37
+ ["httpCode"]: T;
38
+ ["data"]: K;
39
+ });
40
+ }
@@ -0,0 +1,42 @@
1
+ export const httpClientErrors = Object.freeze({
2
+ 400: "BAD_REQUEST",
3
+ 401: "UNAUTHORIZED",
4
+ 402: "PAYMENT_REQUIRED",
5
+ 403: "FORBIDDEN",
6
+ 404: "NOT_FOUND",
7
+ 405: "METHOD_NOT_ALLOWED",
8
+ 406: "NOT_ACCEPTABLE",
9
+ 407: "PROXY_AUTHENCATION_REQUIRED",
10
+ 408: "REQUEST_TIMEOUT",
11
+ 409: "CONFLICT",
12
+ 410: "GONE",
13
+ 411: "LENGTH_REQUIRED",
14
+ 412: "PRECONDITION_FAILED",
15
+ 413: "PAYLOAD_TOO_LARGE",
16
+ 414: "URI_TOO_LONG",
17
+ 415: "UNSUPPORTED_MEDIA_TYPE",
18
+ 416: "RANGE_NOT_SATISFIABLE",
19
+ 417: "EXPECTATION_FAILED",
20
+ 418: "IM_A_TEAPOT",
21
+ 421: "MISDIRECTED_REQUEST",
22
+ 422: "UNPROCESSABLE_ENTITY",
23
+ 423: "LOCKED",
24
+ 424: "FAILED_DEPENDENCY",
25
+ 425: "TOO_EARLY_",
26
+ 426: "UPGRAGE_REQUIRED",
27
+ 428: "PRECONDITION_REQUIRED",
28
+ 429: "TOO_MANY_REQUESTS",
29
+ 431: "REQUEST_HEADER_FIELDS_TOO_LARGE",
30
+ 451: "UNAVAILABLE_FOR_LEGAL_REASONS"
31
+ });
32
+ export class HttpClientError extends Error {
33
+ httpCode;
34
+ message;
35
+ data;
36
+ constructor({ httpCode, data }) {
37
+ super();
38
+ this.httpCode = httpCode;
39
+ this.message = httpClientErrors[httpCode];
40
+ this.data = data;
41
+ }
42
+ }
@@ -0,0 +1,4 @@
1
+ import { Response } from "express";
2
+ export declare const errorInfer: (res: Response, data: any) => void;
3
+ export * from "./clientError";
4
+ export * from "./serverError";
@@ -0,0 +1,58 @@
1
+ import { HttpClientError } from "./clientError";
2
+ import { HttpServerError } from "./serverError";
3
+ export const errorInfer = (res, data) => {
4
+ if (res.headersSent) {
5
+ return;
6
+ }
7
+ try {
8
+ if (data instanceof HttpClientError) {
9
+ res.status(data.httpCode).json(data);
10
+ return;
11
+ }
12
+ if (data instanceof HttpServerError) {
13
+ res.status(data.httpCode).json(data);
14
+ return;
15
+ }
16
+ if (typeof data === "object") {
17
+ res.status(500).json({
18
+ httpCode: 500,
19
+ message: "INTERNAL SERVER ERROR",
20
+ data: data
21
+ });
22
+ return;
23
+ }
24
+ switch (typeof data) {
25
+ case "string":
26
+ res.status(500).json({
27
+ httpCode: 500,
28
+ message: "INTERNAL SERVER ERROR",
29
+ data: {
30
+ message: data
31
+ }
32
+ });
33
+ return;
34
+ case "number":
35
+ res.status(500).json({
36
+ httpCode: 500,
37
+ message: "INTERNAL SERVER ERROR",
38
+ data: {
39
+ code: data
40
+ }
41
+ });
42
+ return;
43
+ default:
44
+ res.status(500).json({
45
+ httpCode: 500,
46
+ message: "INTERNAL SERVER ERROR",
47
+ data: undefined
48
+ });
49
+ return;
50
+ }
51
+ }
52
+ catch (error) {
53
+ console.error(error);
54
+ res.end();
55
+ }
56
+ };
57
+ export * from "./clientError";
58
+ export * from "./serverError";
@@ -0,0 +1,22 @@
1
+ export declare const httpServerErrors: Readonly<{
2
+ 500: "INTERNAL_SERVER_ERROR";
3
+ 501: "NOT_IMPLEMENTED";
4
+ 502: "BAD_GATEWAY";
5
+ 503: "SERVICE_UNAVAILABLE";
6
+ 504: "GATEWAY_TIMEOUT";
7
+ 505: "HTTP_VERSION_NOT_SUPPORTED";
8
+ 506: "VARIANT_ALSO_NEGOTIATES";
9
+ 507: "INSUFFICIENT_STORAGE";
10
+ 508: "LOOP_DETECTED";
11
+ 510: "NOT_EXTENDED";
12
+ 511: "NETWORK_AUTHENTICATION_REQUIRED";
13
+ }>;
14
+ export declare class HttpServerError<T extends keyof typeof httpServerErrors = keyof typeof httpServerErrors, K = any> extends Error {
15
+ readonly httpCode: T;
16
+ readonly message: typeof httpServerErrors[T];
17
+ readonly data: K;
18
+ constructor({ httpCode, data }: {
19
+ ["httpCode"]: T;
20
+ ["data"]: K;
21
+ });
22
+ }
@@ -0,0 +1,24 @@
1
+ export const httpServerErrors = Object.freeze({
2
+ 500: "INTERNAL_SERVER_ERROR",
3
+ 501: "NOT_IMPLEMENTED",
4
+ 502: "BAD_GATEWAY",
5
+ 503: "SERVICE_UNAVAILABLE",
6
+ 504: "GATEWAY_TIMEOUT",
7
+ 505: "HTTP_VERSION_NOT_SUPPORTED",
8
+ 506: "VARIANT_ALSO_NEGOTIATES",
9
+ 507: "INSUFFICIENT_STORAGE",
10
+ 508: "LOOP_DETECTED",
11
+ 510: "NOT_EXTENDED",
12
+ 511: "NETWORK_AUTHENTICATION_REQUIRED"
13
+ });
14
+ export class HttpServerError extends Error {
15
+ httpCode;
16
+ message;
17
+ data;
18
+ constructor({ httpCode, data }) {
19
+ super();
20
+ this.httpCode = httpCode;
21
+ this.message = httpServerErrors[httpCode];
22
+ this.data = data;
23
+ }
24
+ }
package/dist/index.d.ts CHANGED
@@ -2,3 +2,4 @@ import "reflect-metadata";
2
2
  export * from "./interfaces";
3
3
  export * from "./hooks";
4
4
  export * from "./decorators";
5
+ export * from "./http";
package/dist/index.js CHANGED
@@ -2,3 +2,4 @@ import "reflect-metadata";
2
2
  export * from "./interfaces";
3
3
  export * from "./hooks";
4
4
  export * from "./decorators";
5
+ export * from "./http";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bool-ts/core",
3
- "version": "1.0.11",
3
+ "version": "1.1.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -19,7 +19,7 @@
19
19
  "homepage": "https://github.com/BoolTS/core#readme",
20
20
  "dependencies": {
21
21
  "colors": "^1.4.0",
22
- "express": ">=5.0.0-beta.1",
22
+ "express": "^5.0.0-beta.3",
23
23
  "qs": "^6.12.1",
24
24
  "reflect-metadata": "^0.2.2",
25
25
  "response-time": "^2.3.2"
@@ -1,18 +1,18 @@
1
- import { injectableKey } from "./injectable";
2
-
3
-
4
- export const controllerKey = "__bool:controller__";
5
-
6
- export const Controller = (
7
- prefix: string
8
- ) => <T extends Object>(
9
- target: T,
10
- context?: ClassDecoratorContext
11
- ) => {
12
- Reflect.defineMetadata(controllerKey, !prefix.startsWith("/") ? `/${prefix}` : prefix, target);
13
- Reflect.defineMetadata(injectableKey, undefined, target);
14
-
15
- return target;
16
- }
17
-
18
- export default Controller;
1
+ import { injectableKey } from "./injectable";
2
+
3
+
4
+ export const controllerKey = "__bool:controller__";
5
+
6
+ export const Controller = (
7
+ prefix: string
8
+ ) => <T extends Object>(
9
+ target: T,
10
+ context?: ClassDecoratorContext
11
+ ) => {
12
+ Reflect.defineMetadata(controllerKey, !prefix.startsWith("/") ? `/${prefix}` : prefix, target);
13
+ Reflect.defineMetadata(injectableKey, undefined, target);
14
+
15
+ return target;
16
+ }
17
+
18
+ export default Controller;