@bool-ts/core 1.4.2 → 1.5.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 (71) hide show
  1. package/.prettierrc +11 -11
  2. package/LICENSE +21 -21
  3. package/__test/afterDispatcher.ts +9 -0
  4. package/__test/beforeDispatcher.ts +9 -0
  5. package/__test/controller.ts +66 -66
  6. package/__test/firstGuard.ts +10 -0
  7. package/__test/firstMiddleware.ts +9 -0
  8. package/__test/index.ts +8 -8
  9. package/__test/interfaces.ts +7 -7
  10. package/__test/module.ts +25 -16
  11. package/__test/repository.ts +16 -16
  12. package/__test/secondGuard.ts +10 -0
  13. package/__test/secondMiddleware.ts +9 -0
  14. package/__test/service.ts +20 -20
  15. package/bun.lockb +0 -0
  16. package/dist/decorators/arguments.d.ts +12 -6
  17. package/dist/decorators/arguments.js +39 -26
  18. package/dist/decorators/controller.d.ts +9 -1
  19. package/dist/decorators/controller.js +6 -4
  20. package/dist/decorators/dispatcher.d.ts +7 -0
  21. package/dist/decorators/dispatcher.js +9 -0
  22. package/dist/decorators/guard.d.ts +7 -0
  23. package/dist/decorators/guard.js +9 -0
  24. package/dist/decorators/http.d.ts +4 -3
  25. package/dist/decorators/http.js +6 -5
  26. package/dist/decorators/index.d.ts +13 -3
  27. package/dist/decorators/index.js +5 -2
  28. package/dist/decorators/injectable.d.ts +1 -1
  29. package/dist/decorators/injectable.js +1 -4
  30. package/dist/decorators/middleware.d.ts +7 -0
  31. package/dist/decorators/middleware.js +9 -0
  32. package/dist/decorators/module.d.ts +28 -7
  33. package/dist/decorators/module.js +48 -1
  34. package/dist/entities/route.d.ts +5 -5
  35. package/dist/entities/routerGroup.d.ts +1 -1
  36. package/dist/hooks/factory.d.ts +3 -3
  37. package/dist/hooks/factory.js +283 -60
  38. package/dist/hooks/injector.js +6 -5
  39. package/dist/interfaces/index.d.ts +5 -2
  40. package/dist/interfaces/index.js +1 -1
  41. package/package.json +1 -1
  42. package/src/decorators/arguments.ts +214 -186
  43. package/src/decorators/controller.ts +22 -14
  44. package/src/decorators/dispatcher.ts +19 -0
  45. package/src/decorators/guard.ts +19 -0
  46. package/src/decorators/http.ts +81 -81
  47. package/src/decorators/index.ts +28 -7
  48. package/src/decorators/inject.ts +13 -13
  49. package/src/decorators/injectable.ts +8 -11
  50. package/src/decorators/middleware.ts +19 -0
  51. package/src/decorators/module.ts +100 -21
  52. package/src/decorators/zodSchema.ts +20 -20
  53. package/src/entities/index.ts +3 -3
  54. package/src/entities/route.ts +328 -328
  55. package/src/entities/router.ts +35 -35
  56. package/src/entities/routerGroup.ts +34 -34
  57. package/src/hooks/factory.ts +656 -332
  58. package/src/hooks/index.ts +2 -2
  59. package/src/hooks/injector.ts +43 -43
  60. package/src/http/clientError.ts +45 -45
  61. package/src/http/index.ts +63 -63
  62. package/src/http/serverError.ts +38 -38
  63. package/src/index.ts +6 -6
  64. package/src/interfaces/controller.d.ts +1 -0
  65. package/src/interfaces/dispatcher.d.ts +3 -0
  66. package/src/interfaces/guard.d.ts +3 -0
  67. package/src/interfaces/index.ts +5 -3
  68. package/src/interfaces/middleware.d.ts +3 -0
  69. package/src/interfaces/module.d.ts +1 -0
  70. package/test.http +31 -30
  71. package/tsconfig.json +107 -107
@@ -1,2 +1,2 @@
1
- export { BoolFactory } from "./factory";
2
- export { Injector } from "./injector";
1
+ export { BoolFactory } from "./factory";
2
+ export { Injector } from "./injector";
@@ -1,43 +1,43 @@
1
- import "reflect-metadata";
2
-
3
- import { injectableKey, injectKey } from "../decorators";
4
-
5
-
6
- interface IInjector {
7
- get<T>(classDefinition: { new(...args: any[]): T }): T
8
- }
9
-
10
- export const Injector: IInjector = new class {
11
-
12
- private readonly _mapper: Map<Function, any> = new Map();
13
-
14
- /**
15
- *
16
- * @param constructor
17
- */
18
- get<T>(
19
- classDefinition: { new(...args: any[]): T }
20
- ) {
21
- if (this._mapper.has(classDefinition)) {
22
- return this._mapper.get(classDefinition) as T;
23
- }
24
-
25
- const ownMetadataKeys = Reflect.getMetadataKeys(classDefinition);
26
-
27
- if (!ownMetadataKeys.includes(injectableKey)) {
28
- throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
29
- }
30
-
31
- // Initialize dependencies injection
32
- const dependencies: any[] = Reflect.getOwnMetadata(injectKey, classDefinition) || [];
33
- const injections: any[] = dependencies.map(dependency => Injector.get(dependency));
34
- const instance = new classDefinition(...injections);
35
-
36
- this._mapper.set(classDefinition, instance);
37
-
38
- return instance;
39
- }
40
-
41
- }
42
-
43
- export default Injector;
1
+ import "reflect-metadata";
2
+
3
+ import { controllerKey, dispatcherKey, guardKey, injectableKey, injectKey, middlewareKey } from "../decorators";
4
+
5
+ interface IInjector {
6
+ get<T>(classDefinition: { new (...args: any[]): T }): T;
7
+ }
8
+
9
+ export const Injector: IInjector = new (class {
10
+ private readonly _mapper: Map<Function, any> = new Map();
11
+
12
+ /**
13
+ *
14
+ * @param constructor
15
+ */
16
+ get<T>(classDefinition: { new (...args: any[]): T }) {
17
+ if (this._mapper.has(classDefinition)) {
18
+ return this._mapper.get(classDefinition) as T;
19
+ }
20
+
21
+ const ownMetadataKeys = Reflect.getMetadataKeys(classDefinition);
22
+
23
+ if (
24
+ ![injectableKey, controllerKey, middlewareKey, guardKey, dispatcherKey].some((value) =>
25
+ ownMetadataKeys.includes(value)
26
+ )
27
+ ) {
28
+ console.error(classDefinition);
29
+ throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
30
+ }
31
+
32
+ // Initialize dependencies injection
33
+ const dependencies: any[] = Reflect.getOwnMetadata(injectKey, classDefinition) || [];
34
+ const injections: any[] = dependencies.map((dependency) => Injector.get(dependency));
35
+ const instance = new classDefinition(...injections);
36
+
37
+ this._mapper.set(classDefinition, instance);
38
+
39
+ return instance;
40
+ }
41
+ })();
42
+
43
+ export default Injector;
@@ -1,45 +1,45 @@
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
-
33
- export class HttpClientError<T extends keyof typeof httpClientErrors = keyof typeof httpClientErrors, K = any> extends Error {
34
- public readonly httpCode: T;
35
- public readonly message: (typeof httpClientErrors)[T] | string;
36
- public readonly data: K;
37
-
38
- constructor({ httpCode, data, message }: { httpCode: T; data: K; message?: string }) {
39
- super();
40
-
41
- this.httpCode = httpCode;
42
- this.message = !message?.trim() ? httpClientErrors[httpCode] : message.trim();
43
- this.data = data;
44
- }
45
- }
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
+
33
+ export class HttpClientError<T extends keyof typeof httpClientErrors = keyof typeof httpClientErrors, K = any> extends Error {
34
+ public readonly httpCode: T;
35
+ public readonly message: (typeof httpClientErrors)[T] | string;
36
+ public readonly data: K;
37
+
38
+ constructor({ httpCode, data, message }: { httpCode: T; data: K; message?: string }) {
39
+ super();
40
+
41
+ this.httpCode = httpCode;
42
+ this.message = !message?.trim() ? httpClientErrors[httpCode] : message.trim();
43
+ this.data = data;
44
+ }
45
+ }
package/src/http/index.ts CHANGED
@@ -1,63 +1,63 @@
1
- import { HttpClientError } from "./clientError";
2
- import { HttpServerError } from "./serverError";
3
-
4
- export type THttpMethods = {
5
- GET: "GET";
6
- HEAD: "HEAD";
7
- POST: "POST";
8
- PUT: "PUT";
9
- DELETE: "DELETE";
10
- CONNECT: "CONNECT";
11
- OPTIONS: "OPTIONS";
12
- TRACE: "TRACE";
13
- PATCH: "PATCH";
14
- };
15
-
16
- export const jsonErrorInfer = (data: any) => {
17
- const headers = new Headers();
18
-
19
- headers.append("Content-Type", "application/json");
20
-
21
- if (data instanceof HttpClientError || data instanceof HttpServerError) {
22
- return new Response(JSON.stringify(data), {
23
- status: data.httpCode,
24
- statusText: data.message,
25
- headers: headers
26
- });
27
- }
28
-
29
- return new Response(
30
- JSON.stringify(
31
- (() => {
32
- switch (typeof data) {
33
- case "object":
34
- return !(data instanceof Error)
35
- ? data
36
- : {
37
- message: data.message,
38
- code: data.name,
39
- cause: data.cause
40
- };
41
- case "string":
42
- return {
43
- message: data
44
- };
45
- case "number":
46
- return {
47
- code: data
48
- };
49
- default:
50
- return undefined;
51
- }
52
- })()
53
- ),
54
- {
55
- status: 500,
56
- statusText: "INTERNAL SERVER ERROR",
57
- headers: headers
58
- }
59
- );
60
- };
61
-
62
- export * from "./clientError";
63
- export * from "./serverError";
1
+ import { HttpClientError } from "./clientError";
2
+ import { HttpServerError } from "./serverError";
3
+
4
+ export type THttpMethods = {
5
+ GET: "GET";
6
+ HEAD: "HEAD";
7
+ POST: "POST";
8
+ PUT: "PUT";
9
+ DELETE: "DELETE";
10
+ CONNECT: "CONNECT";
11
+ OPTIONS: "OPTIONS";
12
+ TRACE: "TRACE";
13
+ PATCH: "PATCH";
14
+ };
15
+
16
+ export const jsonErrorInfer = (data: any) => {
17
+ const headers = new Headers();
18
+
19
+ headers.append("Content-Type", "application/json");
20
+
21
+ if (data instanceof HttpClientError || data instanceof HttpServerError) {
22
+ return new Response(JSON.stringify(data), {
23
+ status: data.httpCode,
24
+ statusText: data.message,
25
+ headers: headers
26
+ });
27
+ }
28
+
29
+ return new Response(
30
+ JSON.stringify(
31
+ (() => {
32
+ switch (typeof data) {
33
+ case "object":
34
+ return !(data instanceof Error)
35
+ ? data
36
+ : {
37
+ message: data.message,
38
+ code: data.name,
39
+ cause: data.cause
40
+ };
41
+ case "string":
42
+ return {
43
+ message: data
44
+ };
45
+ case "number":
46
+ return {
47
+ code: data
48
+ };
49
+ default:
50
+ return undefined;
51
+ }
52
+ })()
53
+ ),
54
+ {
55
+ status: 500,
56
+ statusText: "INTERNAL SERVER ERROR",
57
+ headers: headers
58
+ }
59
+ );
60
+ };
61
+
62
+ export * from "./clientError";
63
+ export * from "./serverError";
@@ -1,38 +1,38 @@
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
-
15
- export class HttpServerError<
16
- T extends keyof typeof httpServerErrors = keyof typeof httpServerErrors,
17
- K = any
18
- > extends Error {
19
- public readonly httpCode: T;
20
- public readonly message: typeof httpServerErrors[T] | string;
21
- public readonly data: K;
22
-
23
- constructor({
24
- httpCode,
25
- data,
26
- message
27
- }: {
28
- httpCode: T;
29
- data: K;
30
- message?: string;
31
- }) {
32
- super();
33
-
34
- this.httpCode = httpCode;
35
- this.message = !message?.trim() ? httpServerErrors[httpCode] : message.trim();
36
- this.data = data;
37
- }
38
- }
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
+
15
+ export class HttpServerError<
16
+ T extends keyof typeof httpServerErrors = keyof typeof httpServerErrors,
17
+ K = any
18
+ > extends Error {
19
+ public readonly httpCode: T;
20
+ public readonly message: typeof httpServerErrors[T] | string;
21
+ public readonly data: K;
22
+
23
+ constructor({
24
+ httpCode,
25
+ data,
26
+ message
27
+ }: {
28
+ httpCode: T;
29
+ data: K;
30
+ message?: string;
31
+ }) {
32
+ super();
33
+
34
+ this.httpCode = httpCode;
35
+ this.message = !message?.trim() ? httpServerErrors[httpCode] : message.trim();
36
+ this.data = data;
37
+ }
38
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
- import "reflect-metadata";
2
-
3
- export * from "./interfaces";
4
- export * from "./hooks";
5
- export * from "./decorators";
6
- export * from "./http";
1
+ import "reflect-metadata";
2
+
3
+ export * from "./interfaces";
4
+ export * from "./hooks";
5
+ export * from "./decorators";
6
+ export * from "./http";
@@ -0,0 +1 @@
1
+ export interface IController<T = any> {}
@@ -0,0 +1,3 @@
1
+ export interface IDispatcher<T = any> {
2
+ execute(...args: any[]): T;
3
+ }
@@ -0,0 +1,3 @@
1
+ export interface IGuard {
2
+ enforce(...args: any[]): boolean | Promise<boolean>;
3
+ }
@@ -1,3 +1,5 @@
1
-
2
-
3
- export default {};
1
+ export type { IGuard } from "./guard";
2
+ export type { IMiddleware } from "./middleware";
3
+ export type { IController } from "./controller";
4
+ export type { IDispatcher } from "./dispatcher";
5
+ export type { IModule } from "./module";
@@ -0,0 +1,3 @@
1
+ export interface IMiddleware<T = any> {
2
+ enforce(...args: any[]): T;
3
+ }
@@ -0,0 +1 @@
1
+ export interface IModule<T = any> {}
package/test.http CHANGED
@@ -1,30 +1,31 @@
1
- @baseUrl = localhost:3000
2
-
3
-
4
- ### Send test GET method
5
- GET http://{{baseUrl}}/test/abc/23234
6
-
7
- ### Send test POST method
8
- POST http://{{baseUrl}}/test/abc/23234 HTTP/1.1
9
- content-type: application/json
10
-
11
- {
12
- "data": {
13
- "name": "sample",
14
- "time": "Wed, 21 Oct 2015 18:27:50 GMT"
15
- }
16
- }
17
-
18
- ### Send test PUT method
19
- PUT http://{{baseUrl}}/test
20
- Content-Type: "application/json"
21
-
22
- ### Send test PATCH method
23
- PATCH http://{{baseUrl}}/test/abc/23
24
- Content-Type: "application/json"
25
-
26
- ### Send test DELETE method
27
- DELETE http://{{baseUrl}}/test
28
-
29
- ### Send test OPTIONS method
30
- OPTIONS http://{{baseUrl}}/test
1
+ @baseUrl = localhost:3000
2
+
3
+
4
+ ### Send test GET method
5
+ GET http://{{baseUrl}}/test/abc/23234
6
+
7
+ ### Send test POST method
8
+ POST http://{{baseUrl}}/test/abc/123/provider/23234 HTTP/1.1
9
+ content-type: application/json
10
+
11
+ {
12
+ "data": {
13
+ "age": 2,
14
+ "name": "sample",
15
+ "time": "Wed, 21 Oct 2015 18:27:50 GMT"
16
+ }
17
+ }
18
+
19
+ ### Send test PUT method
20
+ PUT http://{{baseUrl}}/test
21
+ Content-Type: "application/json"
22
+
23
+ ### Send test PATCH method
24
+ PATCH http://{{baseUrl}}/test/abc/23
25
+ Content-Type: "application/json"
26
+
27
+ ### Send test DELETE method
28
+ DELETE http://{{baseUrl}}/test
29
+
30
+ ### Send test OPTIONS method
31
+ OPTIONS http://{{baseUrl}}/test