@avleon/core 0.0.45 → 0.0.48

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 (131) hide show
  1. package/README.md +355 -369
  2. package/dist/chunk-9hOWP6kD.cjs +64 -0
  3. package/dist/chunk-DORXReHP.js +37 -0
  4. package/dist/index-BxIMWhgy.d.ts +1284 -0
  5. package/dist/index-DPn7qtzq.d.cts +1283 -0
  6. package/dist/index.cjs +3194 -0
  7. package/dist/index.cjs.map +1 -0
  8. package/dist/index.js +3022 -83
  9. package/dist/index.js.map +1 -0
  10. package/dist/lib-Bk8hUm06.cjs +7847 -0
  11. package/dist/lib-Bk8hUm06.cjs.map +1 -0
  12. package/dist/lib-CvDxBMkR.js +7843 -0
  13. package/dist/lib-CvDxBMkR.js.map +1 -0
  14. package/package.json +67 -116
  15. package/dist/application.d.ts +0 -47
  16. package/dist/application.js +0 -50
  17. package/dist/authentication.d.ts +0 -13
  18. package/dist/authentication.js +0 -16
  19. package/dist/cache.d.ts +0 -12
  20. package/dist/cache.js +0 -78
  21. package/dist/cache.test.d.ts +0 -1
  22. package/dist/cache.test.js +0 -36
  23. package/dist/collection.d.ts +0 -43
  24. package/dist/collection.js +0 -231
  25. package/dist/collection.test.d.ts +0 -1
  26. package/dist/collection.test.js +0 -59
  27. package/dist/config.d.ts +0 -18
  28. package/dist/config.js +0 -58
  29. package/dist/config.test.d.ts +0 -1
  30. package/dist/config.test.js +0 -40
  31. package/dist/constants.d.ts +0 -1
  32. package/dist/constants.js +0 -4
  33. package/dist/container.d.ts +0 -30
  34. package/dist/container.js +0 -55
  35. package/dist/controller.d.ts +0 -50
  36. package/dist/controller.js +0 -71
  37. package/dist/controller.test.d.ts +0 -1
  38. package/dist/controller.test.js +0 -111
  39. package/dist/decorators.d.ts +0 -15
  40. package/dist/decorators.js +0 -41
  41. package/dist/environment-variables.d.ts +0 -49
  42. package/dist/environment-variables.js +0 -130
  43. package/dist/environment-variables.test.d.ts +0 -1
  44. package/dist/environment-variables.test.js +0 -70
  45. package/dist/event-dispatcher.d.ts +0 -23
  46. package/dist/event-dispatcher.js +0 -100
  47. package/dist/event-subscriber.d.ts +0 -14
  48. package/dist/event-subscriber.js +0 -87
  49. package/dist/exceptions/http-exceptions.d.ts +0 -50
  50. package/dist/exceptions/http-exceptions.js +0 -85
  51. package/dist/exceptions/index.d.ts +0 -1
  52. package/dist/exceptions/index.js +0 -17
  53. package/dist/exceptions/system-exception.d.ts +0 -22
  54. package/dist/exceptions/system-exception.js +0 -26
  55. package/dist/file-storage.d.ts +0 -69
  56. package/dist/file-storage.js +0 -323
  57. package/dist/file-storage.test.d.ts +0 -1
  58. package/dist/file-storage.test.js +0 -104
  59. package/dist/helpers.d.ts +0 -44
  60. package/dist/helpers.js +0 -419
  61. package/dist/helpers.test.d.ts +0 -1
  62. package/dist/helpers.test.js +0 -95
  63. package/dist/icore.d.ts +0 -226
  64. package/dist/icore.js +0 -968
  65. package/dist/icore.test.d.ts +0 -1
  66. package/dist/icore.test.js +0 -14
  67. package/dist/index.d.ts +0 -55
  68. package/dist/interfaces/avleon-application.d.ts +0 -27
  69. package/dist/interfaces/avleon-application.js +0 -1
  70. package/dist/kenx-provider.d.ts +0 -7
  71. package/dist/kenx-provider.js +0 -44
  72. package/dist/kenx-provider.test.d.ts +0 -1
  73. package/dist/kenx-provider.test.js +0 -36
  74. package/dist/logger.d.ts +0 -12
  75. package/dist/logger.js +0 -87
  76. package/dist/logger.test.d.ts +0 -1
  77. package/dist/logger.test.js +0 -42
  78. package/dist/map-types.d.ts +0 -17
  79. package/dist/map-types.js +0 -89
  80. package/dist/middleware.d.ts +0 -27
  81. package/dist/middleware.js +0 -64
  82. package/dist/middleware.test.d.ts +0 -1
  83. package/dist/middleware.test.js +0 -121
  84. package/dist/multipart.d.ts +0 -17
  85. package/dist/multipart.js +0 -70
  86. package/dist/multipart.test.d.ts +0 -1
  87. package/dist/multipart.test.js +0 -87
  88. package/dist/openapi.d.ts +0 -343
  89. package/dist/openapi.js +0 -27
  90. package/dist/openapi.test.d.ts +0 -1
  91. package/dist/openapi.test.js +0 -111
  92. package/dist/params.d.ts +0 -17
  93. package/dist/params.js +0 -64
  94. package/dist/params.test.d.ts +0 -1
  95. package/dist/params.test.js +0 -83
  96. package/dist/queue.d.ts +0 -29
  97. package/dist/queue.js +0 -84
  98. package/dist/response.d.ts +0 -16
  99. package/dist/response.js +0 -56
  100. package/dist/results.d.ts +0 -20
  101. package/dist/results.js +0 -32
  102. package/dist/route-methods.d.ts +0 -25
  103. package/dist/route-methods.js +0 -49
  104. package/dist/route-methods.test.d.ts +0 -1
  105. package/dist/route-methods.test.js +0 -129
  106. package/dist/swagger-schema.d.ts +0 -43
  107. package/dist/swagger-schema.js +0 -452
  108. package/dist/swagger-schema.test.d.ts +0 -1
  109. package/dist/swagger-schema.test.js +0 -105
  110. package/dist/testing.d.ts +0 -55
  111. package/dist/testing.js +0 -196
  112. package/dist/types/app-builder.interface.d.ts +0 -15
  113. package/dist/types/app-builder.interface.js +0 -8
  114. package/dist/types/application.interface.d.ts +0 -8
  115. package/dist/types/application.interface.js +0 -2
  116. package/dist/utils/hash.d.ts +0 -4
  117. package/dist/utils/hash.js +0 -15
  118. package/dist/utils/index.d.ts +0 -2
  119. package/dist/utils/index.js +0 -18
  120. package/dist/utils/optional-require.d.ts +0 -8
  121. package/dist/utils/optional-require.js +0 -70
  122. package/dist/validation.d.ts +0 -39
  123. package/dist/validation.js +0 -111
  124. package/dist/validation.test.d.ts +0 -1
  125. package/dist/validation.test.js +0 -61
  126. package/dist/validator-extend.d.ts +0 -7
  127. package/dist/validator-extend.js +0 -28
  128. package/dist/websocket.d.ts +0 -7
  129. package/dist/websocket.js +0 -20
  130. package/dist/websocket.test.d.ts +0 -1
  131. package/dist/websocket.test.js +0 -27
@@ -1 +0,0 @@
1
- export {};
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const icore_1 = require("./icore");
4
- describe("Avleon.createApplication", () => {
5
- it("should return an instance of AvleonApplication", () => {
6
- const app = icore_1.Avleon.createApplication();
7
- expect(app).toBeInstanceOf(icore_1.AvleonApplication);
8
- });
9
- it("should always return the same instance (singleton)", () => {
10
- const app1 = icore_1.Avleon.createApplication();
11
- const app2 = icore_1.Avleon.createApplication();
12
- expect(app1).toBe(app2);
13
- });
14
- });
package/dist/index.d.ts DELETED
@@ -1,55 +0,0 @@
1
- /**
2
- * @copyright 2024
3
- * @author Tareq Hossain
4
- * @email xtrinsic96@gmail.com
5
- * @url https://github.com/xtareq
6
- */
7
- import * as sw from "./swagger-schema";
8
- export * from "./icore";
9
- export * from "./testing";
10
- export { inject, validateRequestBody, pick, exclude } from "./helpers";
11
- export * from "./decorators";
12
- export * from "./middleware";
13
- export * from "./config";
14
- export * from "./openapi";
15
- export * from "./map-types";
16
- export * from "./response";
17
- export * from "./exceptions";
18
- export * from "./validator-extend";
19
- export * from "./validation";
20
- export * from "./environment-variables";
21
- export * from "./collection";
22
- export * from "./queue";
23
- export * from "./utils/hash";
24
- export * from "./multipart";
25
- export * from "./file-storage";
26
- export * from "./logger";
27
- export * from "./event-dispatcher";
28
- export * from "./kenx-provider";
29
- export { Subscribe, Private } from "./event-subscriber";
30
- export declare const GetSchema: typeof sw.generateSwaggerSchema;
31
- export declare const GetObjectSchema: typeof sw.CreateSwaggerObjectSchema;
32
- export declare const OpenApiOk: (args1: any) => {
33
- description: string;
34
- content: {
35
- "application/json": {
36
- schema: {
37
- type: string;
38
- properties: {
39
- code: {
40
- type: string;
41
- example: number;
42
- };
43
- status: {
44
- type: string;
45
- example: string;
46
- };
47
- data: any;
48
- };
49
- };
50
- };
51
- };
52
- };
53
- export declare const OpenApiResponse: typeof sw.OpenApiResponse;
54
- export declare const OpenApiProperty: typeof sw.OpenApiProperty;
55
- export { default as AvleonContainer } from "./container";
@@ -1,27 +0,0 @@
1
- interface AvleonApplication {
2
- useCors: () => void;
3
- /**
4
- * function for register database
5
- * @param options datasource options. options can be plain object or avleon config class
6
- * */
7
- useDatasource: () => void;
8
- useMultipart: () => void;
9
- useOpenApi: () => void;
10
- useMiddlewares: () => void;
11
- useAuthorization: () => void;
12
- useSerialization: () => void;
13
- useControllers: () => void;
14
- useStaticFiles: () => void;
15
- /**
16
- * @experimental
17
- * use https as defalut http protocol
18
- * */
19
- useHttps: () => void;
20
- mapGet: () => void;
21
- mapPost: () => void;
22
- mapPut: () => void;
23
- mapPatch: () => void;
24
- mapOptions: () => void;
25
- mapGroup: () => void;
26
- run: () => void;
27
- }
@@ -1 +0,0 @@
1
- "use strict";
@@ -1,7 +0,0 @@
1
- import type { Knex } from "knex";
2
- export declare class DB {
3
- private connection;
4
- constructor();
5
- init(config: Knex.Config): Knex<any, any[]>;
6
- get client(): Knex;
7
- }
@@ -1,44 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DB = void 0;
13
- const typedi_1 = require("typedi");
14
- const typedi_2 = require("typedi");
15
- let DB = class DB {
16
- constructor() {
17
- const existing = typedi_2.Container.has("KnexConnection")
18
- ? typedi_2.Container.get("KnexConnection")
19
- : null;
20
- if (existing) {
21
- this.connection = existing;
22
- }
23
- }
24
- // Initialize manually (call this in main if you want)
25
- init(config) {
26
- if (!this.connection) {
27
- const knex = require("knex");
28
- this.connection = knex(config);
29
- typedi_2.Container.set("KnexConnection", this.connection);
30
- }
31
- return this.connection;
32
- }
33
- get client() {
34
- if (!this.connection) {
35
- throw new Error("Knex is not initialized. Call DB.init(config) first.");
36
- }
37
- return this.connection;
38
- }
39
- };
40
- exports.DB = DB;
41
- exports.DB = DB = __decorate([
42
- (0, typedi_1.Service)(),
43
- __metadata("design:paramtypes", [])
44
- ], DB);
@@ -1 +0,0 @@
1
- import "reflect-metadata";
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- require("reflect-metadata");
4
- const typedi_1 = require("typedi");
5
- const kenx_provider_1 = require("./kenx-provider");
6
- jest.mock("knex", () => jest.fn(() => ({ mock: "knexInstance" })));
7
- describe("DB", () => {
8
- beforeEach(() => {
9
- typedi_1.Container.reset();
10
- });
11
- it("should throw error if client is accessed before init", () => {
12
- const db = new kenx_provider_1.DB();
13
- expect(() => db.client).toThrow("Knex is not initialized. Call DB.init(config) first.");
14
- });
15
- it("should initialize knex and set connection", () => {
16
- const db = new kenx_provider_1.DB();
17
- const config = { client: "sqlite3", connection: {} };
18
- const conn = db.init(config);
19
- expect(conn).toEqual({ mock: "knexInstance" });
20
- expect(db.client).toEqual({ mock: "knexInstance" });
21
- expect(typedi_1.Container.get("KnexConnection")).toEqual({ mock: "knexInstance" });
22
- });
23
- it("should use existing connection from Container", () => {
24
- const existingConn = { mock: "existingKnex" };
25
- typedi_1.Container.set("KnexConnection", existingConn);
26
- const db = new kenx_provider_1.DB();
27
- expect(db.client).toBe(existingConn);
28
- });
29
- // it("should not reinitialize if connection exists", () => {
30
- // const db = new DB();
31
- // const config = { client: "sqlite3", connection: {} };
32
- // db.init(config as Knex.Config);
33
- // const conn2 = db.init(config as Knex.Config);
34
- // expect(conn2).toEqual({ mock: "knexInstance" });
35
- // });
36
- });
package/dist/logger.d.ts DELETED
@@ -1,12 +0,0 @@
1
- import pino from "pino";
2
- export declare class LoggerService {
3
- private logger;
4
- constructor();
5
- getLogger(): pino.Logger;
6
- info(message: string, obj?: any): void;
7
- error(message: string, obj?: any): void;
8
- warn(message: string, obj?: any): void;
9
- debug(message: string, obj?: any): void;
10
- fatal(message: string, obj?: any): void;
11
- trace(message: string, obj?: any): void;
12
- }
package/dist/logger.js DELETED
@@ -1,87 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.LoggerService = void 0;
16
- const pino_1 = __importDefault(require("pino"));
17
- const decorators_1 = require("./decorators");
18
- let LoggerService = class LoggerService {
19
- constructor() {
20
- this.logger = (0, pino_1.default)({
21
- level: process.env.LOG_LEVEL || "info",
22
- transport: {
23
- target: "pino-pretty",
24
- options: {
25
- translateTime: "SYS:standard",
26
- ignore: "pid,hostname",
27
- },
28
- },
29
- });
30
- }
31
- getLogger() {
32
- return this.logger;
33
- }
34
- info(message, obj) {
35
- if (obj) {
36
- this.logger.info(obj, message);
37
- }
38
- else {
39
- this.logger.info(message);
40
- }
41
- }
42
- error(message, obj) {
43
- if (obj) {
44
- this.logger.error(obj, message);
45
- }
46
- else {
47
- this.logger.error(message);
48
- }
49
- }
50
- warn(message, obj) {
51
- if (obj) {
52
- this.logger.warn(obj, message);
53
- }
54
- else {
55
- this.logger.warn(message);
56
- }
57
- }
58
- debug(message, obj) {
59
- if (obj) {
60
- this.logger.debug(obj, message);
61
- }
62
- else {
63
- this.logger.debug(message);
64
- }
65
- }
66
- fatal(message, obj) {
67
- if (obj) {
68
- this.logger.fatal(obj, message);
69
- }
70
- else {
71
- this.logger.fatal(message);
72
- }
73
- }
74
- trace(message, obj) {
75
- if (obj) {
76
- this.logger.trace(obj, message);
77
- }
78
- else {
79
- this.logger.trace(message);
80
- }
81
- }
82
- };
83
- exports.LoggerService = LoggerService;
84
- exports.LoggerService = LoggerService = __decorate([
85
- decorators_1.AppService,
86
- __metadata("design:paramtypes", [])
87
- ], LoggerService);
@@ -1 +0,0 @@
1
- export {};
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const logger_1 = require("./logger");
7
- const pino_1 = __importDefault(require("pino"));
8
- jest.mock("pino");
9
- describe("LoggerService constructor", () => {
10
- const mockPino = pino_1.default;
11
- beforeEach(() => {
12
- mockPino.mockClear();
13
- });
14
- it("should initialize logger with default level 'info' when LOG_LEVEL is not set", () => {
15
- delete process.env.LOG_LEVEL;
16
- new logger_1.LoggerService();
17
- expect(mockPino).toHaveBeenCalledWith({
18
- level: "info",
19
- transport: {
20
- target: "pino-pretty",
21
- options: {
22
- translateTime: "SYS:standard",
23
- ignore: "pid,hostname",
24
- },
25
- },
26
- });
27
- });
28
- it("should initialize logger with LOG_LEVEL from environment", () => {
29
- process.env.LOG_LEVEL = "debug";
30
- new logger_1.LoggerService();
31
- expect(mockPino).toHaveBeenCalledWith({
32
- level: "debug",
33
- transport: {
34
- target: "pino-pretty",
35
- options: {
36
- translateTime: "SYS:standard",
37
- ignore: "pid,hostname",
38
- },
39
- },
40
- });
41
- });
42
- });
@@ -1,17 +0,0 @@
1
- /**
2
- * @copyright 2024
3
- * @author Tareq Hossain
4
- * @email xtrinsic96@gmail.com
5
- * @url https://github.com/xtareq
6
- */
7
- type Constructor<T = any> = new (...args: any[]) => T;
8
- export declare function PartialType<T>(BaseClass: Constructor<T>): Constructor<Partial<T>>;
9
- /**
10
- * Utility to pick specific properties from a class.
11
- */
12
- export declare function PickType<T, K extends keyof T>(BaseClass: Constructor<T>, keys: K[]): Constructor<Pick<T, K>>;
13
- /**
14
- * Utility to omit specific properties from a class.
15
- */
16
- export declare function OmitType<T, K extends keyof T>(BaseClass: Constructor<T>, keys: K[]): Constructor<Omit<T, K>>;
17
- export {};
package/dist/map-types.js DELETED
@@ -1,89 +0,0 @@
1
- "use strict";
2
- /**
3
- * @copyright 2024
4
- * @author Tareq Hossain
5
- * @email xtrinsic96@gmail.com
6
- * @url https://github.com/xtareq
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.PartialType = PartialType;
10
- exports.PickType = PickType;
11
- exports.OmitType = OmitType;
12
- function PartialType(BaseClass) {
13
- const baseProperties = [];
14
- let currentPrototype = BaseClass.prototype;
15
- // Collect properties from the base class (including inherited ones)
16
- while (currentPrototype && currentPrototype !== Object.prototype) {
17
- const properties = Object.getOwnPropertyNames(currentPrototype).filter((prop) => prop !== "constructor");
18
- // Retrieve metadata for each property
19
- properties.forEach((key) => {
20
- // Check if the property has type metadata (design:type)
21
- const designType = Reflect.getMetadata("design:type", currentPrototype, key);
22
- if (designType) {
23
- baseProperties.push(key);
24
- }
25
- // Retrieve validation metadata (class-validator)
26
- const validationMetadata = Reflect.getMetadataKeys(currentPrototype, key);
27
- validationMetadata.forEach((metadataKey) => { });
28
- });
29
- currentPrototype = Object.getPrototypeOf(currentPrototype); // Move up the prototype chain
30
- }
31
- class PartialClass {
32
- }
33
- // Define properties as optional and copy metadata
34
- baseProperties.forEach((key) => {
35
- const propertyType = Reflect.getMetadata("design:type", BaseClass.prototype, key);
36
- Reflect.defineMetadata("design:type", propertyType, PartialClass.prototype, key);
37
- // Propagate class-validator metadata to the new class
38
- const validationMetadataKeys = Reflect.getMetadataKeys(BaseClass.prototype, key) || [];
39
- validationMetadataKeys.forEach((metadataKey) => {
40
- const metadataValue = Reflect.getMetadata(metadataKey, BaseClass.prototype, key);
41
- Reflect.defineMetadata(metadataKey, metadataValue, PartialClass.prototype, key);
42
- });
43
- });
44
- // Copy other metadata from the base class (non-property metadata)
45
- Reflect.getMetadataKeys(BaseClass.prototype).forEach((key) => {
46
- const metadataValue = Reflect.getMetadata(key, BaseClass.prototype);
47
- Reflect.defineMetadata(key, metadataValue, PartialClass.prototype);
48
- });
49
- return PartialClass;
50
- }
51
- /**
52
- * Utility to pick specific properties from a class.
53
- */
54
- function PickType(BaseClass, keys) {
55
- class PickClass {
56
- constructor() {
57
- keys.forEach((key) => {
58
- Reflect.defineMetadata("design:type", Reflect.getMetadata("design:type", BaseClass.prototype, key), this, key);
59
- });
60
- }
61
- }
62
- Reflect.decorate([Reflect.metadata("design:properties", keys)], PickClass);
63
- // Copy metadata from BaseClass to PickClass
64
- Reflect.getMetadataKeys(BaseClass.prototype).forEach((key) => {
65
- Reflect.defineMetadata(key, Reflect.getMetadata(key, BaseClass.prototype), PickClass.prototype);
66
- });
67
- return PickClass;
68
- }
69
- /**
70
- * Utility to omit specific properties from a class.
71
- */
72
- function OmitType(BaseClass, keys) {
73
- const allKeys = Reflect.getMetadata("design:properties", BaseClass) || [];
74
- const omitKeys = new Set(keys);
75
- const finalKeys = allKeys.filter((key) => !omitKeys.has(key));
76
- class OmitClass {
77
- constructor() {
78
- finalKeys.forEach((key) => {
79
- Reflect.defineMetadata("design:type", Reflect.getMetadata("design:type", BaseClass.prototype, key), this, key);
80
- });
81
- }
82
- }
83
- Reflect.decorate([Reflect.metadata("design:properties", finalKeys)], OmitClass);
84
- // Copy metadata from BaseClass to OmitClass
85
- Reflect.getMetadataKeys(BaseClass.prototype).forEach((key) => {
86
- Reflect.defineMetadata(key, Reflect.getMetadata(key, BaseClass.prototype), OmitClass.prototype);
87
- });
88
- return OmitClass;
89
- }
@@ -1,27 +0,0 @@
1
- import { IRequest, IResponse } from "./icore";
2
- import { HttpExceptionTypes as HttpException } from "./exceptions";
3
- export declare abstract class AvleonMiddleware {
4
- abstract invoke(req: IRequest, res?: IResponse): Promise<IRequest | HttpException>;
5
- }
6
- export type AuthHandler = (req: IRequest, roles?: string[]) => Promise<IRequest | HttpException>;
7
- export type Constructor<T> = {
8
- new (...args: any[]): T;
9
- };
10
- export declare abstract class AuthorizeMiddleware {
11
- abstract authorize(roles: string[]): (req: IRequest, res?: IResponse) => IRequest | Promise<IRequest>;
12
- }
13
- export type AuthReturnTypes = IRequest | Promise<IRequest>;
14
- interface AuthorizeClass {
15
- authorize(req: IRequest, options?: any): AuthReturnTypes;
16
- }
17
- export declare function CanAuthorize(target: {
18
- new (...args: any[]): AuthorizeClass;
19
- }): void;
20
- export declare function AppAuthorization(target: {
21
- new (...args: any[]): AuthorizeClass;
22
- }): void;
23
- export declare function Authorized(): ClassDecorator & MethodDecorator;
24
- export declare function Authorized(options?: any): ClassDecorator & MethodDecorator;
25
- export declare function AppMiddleware(target: Constructor<AvleonMiddleware>): void;
26
- export declare function UseMiddleware<T extends AvleonMiddleware | (new (...args: any[]) => AvleonMiddleware)>(options: T | T[]): MethodDecorator & ClassDecorator;
27
- export {};
@@ -1,64 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AuthorizeMiddleware = exports.AvleonMiddleware = void 0;
4
- exports.CanAuthorize = CanAuthorize;
5
- exports.AppAuthorization = AppAuthorization;
6
- exports.Authorized = Authorized;
7
- exports.AppMiddleware = AppMiddleware;
8
- exports.UseMiddleware = UseMiddleware;
9
- /**
10
- * @copyright 2024
11
- * @author Tareq Hossain
12
- * @email xtrinsic96@gmail.com
13
- * @url https://github.com/xtareq
14
- */
15
- const typedi_1 = require("typedi");
16
- const container_1 = require("./container");
17
- class AvleonMiddleware {
18
- }
19
- exports.AvleonMiddleware = AvleonMiddleware;
20
- class AuthorizeMiddleware {
21
- }
22
- exports.AuthorizeMiddleware = AuthorizeMiddleware;
23
- function CanAuthorize(target) {
24
- if (typeof target.prototype.authorize !== "function") {
25
- throw new Error(`Class "${target.name}" must implement an "authorize" method.`);
26
- }
27
- (0, typedi_1.Service)()(target);
28
- }
29
- function AppAuthorization(target) {
30
- if (typeof target.prototype.authorize !== "function") {
31
- throw new Error(`Class "${target.name}" must implement an "authorize" method.`);
32
- }
33
- (0, typedi_1.Service)()(target);
34
- }
35
- function Authorized(options = {}) {
36
- return function (target, propertyKey, descriptor) {
37
- if (propertyKey && descriptor) {
38
- Reflect.defineMetadata(container_1.AUTHORIZATION_META_KEY, { authorize: true, options }, target.constructor, propertyKey);
39
- }
40
- else {
41
- Reflect.defineMetadata(container_1.AUTHORIZATION_META_KEY, { authorize: true, options }, target);
42
- }
43
- };
44
- }
45
- function AppMiddleware(target) {
46
- if (typeof target.prototype.invoke !== "function") {
47
- throw new Error(`Class "${target.name}" must implement an "invoke" method.`);
48
- }
49
- (0, typedi_1.Service)()(target);
50
- }
51
- function UseMiddleware(options) {
52
- return function (target, propertyKey, descriptor) {
53
- const normalizeMiddleware = (middleware) => typeof middleware === "function" ? new middleware() : middleware;
54
- const middlewareList = (Array.isArray(options) ? options : [options]).map(normalizeMiddleware);
55
- if (typeof target === "function" && !propertyKey) {
56
- const existingMiddlewares = Reflect.getMetadata("controller:middleware", target) || [];
57
- Reflect.defineMetadata("controller:middleware", [...existingMiddlewares, ...middlewareList], target);
58
- }
59
- else if (descriptor) {
60
- const existingMiddlewares = Reflect.getMetadata("route:middleware", target, propertyKey) || [];
61
- Reflect.defineMetadata("route:middleware", [...existingMiddlewares, ...middlewareList], target, propertyKey);
62
- }
63
- };
64
- }
@@ -1 +0,0 @@
1
- import "reflect-metadata";
@@ -1,121 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- require("reflect-metadata");
13
- const middleware_1 = require("./middleware");
14
- describe("AvleonMiddleware", () => {
15
- class TestMiddleware extends middleware_1.AvleonMiddleware {
16
- async invoke(req, res) {
17
- return req;
18
- }
19
- }
20
- it("should allow AppMiddleware decorator on valid class", () => {
21
- expect(() => (0, middleware_1.AppMiddleware)(TestMiddleware)).not.toThrow();
22
- });
23
- it("should throw error if AppMiddleware is used on class without invoke", () => {
24
- class InvalidMiddleware {
25
- }
26
- expect(() => (0, middleware_1.AppMiddleware)(InvalidMiddleware)).toThrow(/must implement an "invoke" method/);
27
- });
28
- });
29
- describe("UseMiddleware", () => {
30
- class MW1 extends middleware_1.AvleonMiddleware {
31
- async invoke(req) {
32
- return req;
33
- }
34
- }
35
- class MW2 extends middleware_1.AvleonMiddleware {
36
- async invoke(req) {
37
- return req;
38
- }
39
- }
40
- it("should attach middleware to class", () => {
41
- let TestController = class TestController {
42
- };
43
- TestController = __decorate([
44
- (0, middleware_1.UseMiddleware)([MW1, MW2])
45
- ], TestController);
46
- const middlewares = Reflect.getMetadata("controller:middleware", TestController);
47
- expect(middlewares).toHaveLength(2);
48
- expect(middlewares[0]).toBeInstanceOf(MW1);
49
- expect(middlewares[1]).toBeInstanceOf(MW2);
50
- });
51
- it("should attach middleware to method", () => {
52
- class TestController {
53
- testMethod() { }
54
- }
55
- __decorate([
56
- (0, middleware_1.UseMiddleware)(MW1),
57
- __metadata("design:type", Function),
58
- __metadata("design:paramtypes", []),
59
- __metadata("design:returntype", void 0)
60
- ], TestController.prototype, "testMethod", null);
61
- const middlewares = Reflect.getMetadata("route:middleware", TestController.prototype, "testMethod");
62
- expect(middlewares).toHaveLength(1);
63
- expect(middlewares[0]).toBeInstanceOf(MW1);
64
- });
65
- });
66
- // describe("Authorized decorator", () => {
67
- // it("should define metadata on class", () => {
68
- // @Authorized({ roles: ["admin"] })
69
- // class TestClass {}
70
- // const meta = Reflect.getMetadata("AUTHORIZATION_META_KEY", TestClass);
71
- // expect(meta).toEqual({ authorize: true, options: { roles: ["admin"] } });
72
- // });
73
- // it("should define metadata on method", () => {
74
- // class TestClass {
75
- // @Authorized({ roles: ["user"] })
76
- // testMethod() {}
77
- // }
78
- // const meta = Reflect.getMetadata(
79
- // "AUTHORIZATION_META_KEY",
80
- // TestClass.constructor,
81
- // "testMethod",
82
- // );
83
- // expect(meta).toEqual({ authorize: true, options: { roles: ["user"] } });
84
- // });
85
- describe("CanAuthorize and AppAuthorization", () => {
86
- class ValidAuthorize {
87
- authorize(req, options) {
88
- return req;
89
- }
90
- }
91
- it("should not throw for valid CanAuthorize", () => {
92
- expect(() => (0, middleware_1.CanAuthorize)(ValidAuthorize)).not.toThrow();
93
- });
94
- it("should throw for invalid CanAuthorize", () => {
95
- class InvalidAuthorize {
96
- }
97
- expect(() => (0, middleware_1.CanAuthorize)(InvalidAuthorize)).toThrow(/must implement an "authorize" method/);
98
- });
99
- it("should not throw for valid AppAuthorization", () => {
100
- expect(() => (0, middleware_1.AppAuthorization)(ValidAuthorize)).not.toThrow();
101
- });
102
- it("should throw for invalid AppAuthorization", () => {
103
- class InvalidAuthorize {
104
- }
105
- expect(() => (0, middleware_1.AppAuthorization)(InvalidAuthorize)).toThrow(/must implement an "authorize" method/);
106
- });
107
- });
108
- describe("AuthorizeMiddleware", () => {
109
- class TestAuthorizeMiddleware extends middleware_1.AuthorizeMiddleware {
110
- authorize(roles) {
111
- return (req) => req;
112
- }
113
- }
114
- it("should implement authorize method", () => {
115
- const mw = new TestAuthorizeMiddleware();
116
- const handler = mw.authorize(["admin"]);
117
- expect(typeof handler).toBe("function");
118
- const req = {};
119
- expect(handler(req)).toBe(req);
120
- });
121
- });