@heliosjs/middlewares 2.0.2 → 4.0.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/dist/catch.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ErrorCB } from './types/core';
1
+ import { ErorrHandler } from './types/core';
2
2
  /**
3
3
  * Decorator to register a global or controller-level error handler.
4
4
  *
@@ -7,7 +7,7 @@ import { ErrorCB } from './types/core';
7
7
  * It attaches the handler as metadata on the target class, allowing the framework to
8
8
  * retrieve and execute the error handler appropriately during runtime.
9
9
  *
10
- * @param {ErrorCB} handler - The error callback function to handle errors.
10
+ * @param {ErorrHandler} handler - The error callback function to handle errors.
11
11
  *
12
12
  * @returns {Function} A class decorator function that defines the error handler metadata.
13
13
  *
@@ -26,4 +26,4 @@ import { ErrorCB } from './types/core';
26
26
  * This metadata is accessible via Reflect API and used internally by the framework
27
27
  * to invoke the registered error handler when errors occur.
28
28
  */
29
- export declare function Catch(handler: ErrorCB): (target: any) => any;
29
+ export declare function Catch(handler: ErorrHandler): (target: any, propertyKey?: string, descriptor?: PropertyDescriptor) => void;
package/dist/catch.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Catch = Catch;
4
- const constants_1 = require("./constants");
4
+ const shared_1 = require("./utils/shared");
5
5
  /**
6
6
  * Decorator to register a global or controller-level error handler.
7
7
  *
@@ -10,7 +10,7 @@ const constants_1 = require("./constants");
10
10
  * It attaches the handler as metadata on the target class, allowing the framework to
11
11
  * retrieve and execute the error handler appropriately during runtime.
12
12
  *
13
- * @param {ErrorCB} handler - The error callback function to handle errors.
13
+ * @param {ErorrHandler} handler - The error callback function to handle errors.
14
14
  *
15
15
  * @returns {Function} A class decorator function that defines the error handler metadata.
16
16
  *
@@ -30,9 +30,14 @@ const constants_1 = require("./constants");
30
30
  * to invoke the registered error handler when errors occur.
31
31
  */
32
32
  function Catch(handler) {
33
- return function (target) {
34
- Reflect.defineMetadata(constants_1.CATCH, handler, target);
35
- return target;
33
+ return function (target, propertyKey, descriptor) {
34
+ const data = { errors: [handler] };
35
+ if (descriptor) {
36
+ (0, shared_1.defineMiddlewaresMeta)(data, target, propertyKey);
37
+ }
38
+ else {
39
+ (0, shared_1.defineMiddlewaresMeta)(data, target);
40
+ }
36
41
  };
37
42
  }
38
43
  //# sourceMappingURL=catch.js.map
package/dist/catch.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"catch.js","sourceRoot":"","sources":["../src/catch.ts"],"names":[],"mappings":";;AA8BA,sBAMC;AApCD,2CAAoC;AAGpC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,SAAgB,KAAK,CAAC,OAAgB;IACpC,OAAO,UAAU,MAAW;QAC1B,OAAO,CAAC,cAAc,CAAC,iBAAK,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QAE/C,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"catch.js","sourceRoot":"","sources":["../src/catch.ts"],"names":[],"mappings":";;AA6BA,sBAUC;AAtCD,2CAAuD;AACvD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,SAAgB,KAAK,CAAC,OAAqB;IACzC,OAAO,UAAU,MAAW,EAAE,WAAoB,EAAE,UAA+B;QACjF,MAAM,IAAI,GAAG,EAAE,MAAM,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC;QAEnC,IAAI,UAAU,EAAE,CAAC;YACf,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACN,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
@@ -23,3 +23,8 @@ export declare const SSE_METADATA_KEY = "sse:handlers";
23
23
  export declare const SSE_TOPIC_KEY = "sse:topics";
24
24
  export declare const SSE_SERVICE_KEY = "sse:service";
25
25
  export declare const STATIC_METADATA_KEY = "static:config";
26
+ export declare enum DECORATOR {
27
+ controller = "controller:config",
28
+ middlewares = "controller:middlewares",
29
+ route = "controller:route"
30
+ }
package/dist/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.STATIC_METADATA_KEY = exports.SSE_SERVICE_KEY = exports.SSE_TOPIC_KEY = exports.SSE_METADATA_KEY = exports.CORS_METADATA = exports.TO_VALIDATE = exports.OK_STATUSES = exports.STOPPED = exports.SANITIZE = exports.CATCH = exports.INTERCEPT = exports.WS_SERVICE_KEY = exports.WS_TOPIC_KEY = exports.WS_HANDLER = exports.USE_MIDDLEWARE = exports.SERVER_CONFIG_KEY = exports.OK_METADATA_KEY = exports.ENDPOINT = exports.INTERCEPTOR = exports.CONTROLLERS = exports.MIDDLEWARES = exports.ROUTE_MIDDLEWARES = exports.ROUTE_PREFIX = exports.APP_METADATA_KEY = exports.PARAM_METADATA_KEY = void 0;
3
+ exports.DECORATOR = exports.STATIC_METADATA_KEY = exports.SSE_SERVICE_KEY = exports.SSE_TOPIC_KEY = exports.SSE_METADATA_KEY = exports.CORS_METADATA = exports.TO_VALIDATE = exports.OK_STATUSES = exports.STOPPED = exports.SANITIZE = exports.CATCH = exports.INTERCEPT = exports.WS_SERVICE_KEY = exports.WS_TOPIC_KEY = exports.WS_HANDLER = exports.USE_MIDDLEWARE = exports.SERVER_CONFIG_KEY = exports.OK_METADATA_KEY = exports.ENDPOINT = exports.INTERCEPTOR = exports.CONTROLLERS = exports.MIDDLEWARES = exports.ROUTE_MIDDLEWARES = exports.ROUTE_PREFIX = exports.APP_METADATA_KEY = exports.PARAM_METADATA_KEY = void 0;
4
4
  exports.PARAM_METADATA_KEY = 'design:parameters';
5
5
  exports.APP_METADATA_KEY = 'app:configuration';
6
6
  exports.ROUTE_PREFIX = 'route:prefix';
@@ -31,4 +31,10 @@ exports.SSE_METADATA_KEY = 'sse:handlers';
31
31
  exports.SSE_TOPIC_KEY = 'sse:topics';
32
32
  exports.SSE_SERVICE_KEY = 'sse:service';
33
33
  exports.STATIC_METADATA_KEY = 'static:config';
34
+ var DECORATOR;
35
+ (function (DECORATOR) {
36
+ DECORATOR["controller"] = "controller:config";
37
+ DECORATOR["middlewares"] = "controller:middlewares";
38
+ DECORATOR["route"] = "controller:route";
39
+ })(DECORATOR || (exports.DECORATOR = DECORATOR = {}));
34
40
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,kBAAkB,GAAG,mBAAmB,CAAC;AACzC,QAAA,gBAAgB,GAAG,mBAAmB,CAAC;AACvC,QAAA,YAAY,GAAG,cAAc,CAAC;AAC9B,QAAA,iBAAiB,GAAG,mBAAmB,CAAC;AACxC,QAAA,WAAW,GAAG,wBAAwB,CAAC;AACvC,QAAA,WAAW,GAAG,iBAAiB,CAAC;AAChC,QAAA,WAAW,GAAG,kBAAkB,CAAC;AACjC,QAAA,QAAQ,GAAG,iBAAiB,CAAC;AAC7B,QAAA,eAAe,GAAG,WAAW,CAAC;AAE9B,QAAA,iBAAiB,GAAG,eAAe,CAAC;AACpC,QAAA,cAAc,GAAG,0BAA0B,CAAC;AAE5C,QAAA,UAAU,GAAG,mBAAmB,CAAC;AACjC,QAAA,YAAY,GAAG,iBAAiB,CAAC;AACjC,QAAA,cAAc,GAAG,mBAAmB,CAAC;AACrC,QAAA,SAAS,GAAG,kBAAkB,CAAC;AAC/B,QAAA,KAAK,GAAG,cAAc,CAAC;AACvB,QAAA,QAAQ,GAAG,iBAAiB,CAAC;AAE7B,QAAA,OAAO,GAAG;;;;;WAKZ,CAAC;AAEC,QAAA,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GA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EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AACjE,QAAA,WAAW,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;AAElE,QAAA,aAAa,GAAG,aAAa,CAAC;AAC9B,QAAA,gBAAgB,GAAG,cAAc,CAAC;AAClC,QAAA,aAAa,GAAG,YAAY,CAAC;AAC7B,QAAA,eAAe,GAAG,aAAa,CAAC;AAChC,QAAA,mBAAmB,GAAG,eAAe,CAAC;AAEnD,IAAY,SAIX;AAJD,WAAY,SAAS;IACnB,6CAAgC,CAAA;IAChC,mDAAsC,CAAA;IACtC,uCAA0B,CAAA;AAC5B,CAAC,EAJW,SAAS,yBAAT,SAAS,QAIpB"}
package/dist/cors.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Cors = Cors;
4
- const constants_1 = require("./constants");
5
4
  const core_1 = require("./types/core");
5
+ const shared_1 = require("./utils/shared");
6
6
  /**
7
7
  * Decorator to configure Cross-Origin Resource Sharing (CORS) settings for HTTP controllers or methods.
8
8
  *
@@ -46,11 +46,12 @@ function Cors(config = {}) {
46
46
  methods: Object.keys(core_1.HTTP_METHODS),
47
47
  };
48
48
  const finalConfig = { ...defaultConfig, ...config };
49
- if (propertyKey && descriptor) {
50
- Reflect.defineMetadata(constants_1.CORS_METADATA, finalConfig, target, propertyKey);
49
+ const data = { cors: [finalConfig] };
50
+ if (descriptor) {
51
+ (0, shared_1.defineMiddlewaresMeta)(data, target, propertyKey);
51
52
  }
52
53
  else {
53
- Reflect.defineMetadata(constants_1.CORS_METADATA, finalConfig, target.prototype || target);
54
+ (0, shared_1.defineMiddlewaresMeta)(data, target);
54
55
  }
55
56
  };
56
57
  }
package/dist/cors.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cors.js","sourceRoot":"","sources":["../src/cors.ts"],"names":[],"mappings":";;AAsCA,oBAgBC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,UAAU,EAAE,CAAC;YACf,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACN,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,44 @@
1
+ import { type Guard, GuardFn } from './types/core';
2
+ /**
3
+ * Decorator to register a guard.
4
+ *
5
+ * Guards are used to control access to a controller by validating
6
+ * incoming requests before they reach route handlers. If a guard
7
+ * returns `false`, the request is blocked.
8
+ *
9
+ * @param guard - A guard class or function used to determine whether
10
+ * the request is allowed to proceed.
11
+ *
12
+ * @returns A class decorator that attaches guard metadata to the target.
13
+ *
14
+ * @example
15
+ * // Using function guard
16
+ * @Guard((req, res) => {
17
+ * return !!req.headers.authorization;
18
+ * })
19
+ * class MyController {}
20
+ *
21
+ * @example
22
+ * // Using class-based guard
23
+ * class AuthGuard {
24
+ * canActivate(req: Request, res: Response) {
25
+ * return !!req.headers.authorization;
26
+ * }
27
+ * }
28
+ *
29
+ * @Guard(AuthGuard)
30
+ * class MyController {}
31
+ *
32
+ * @remarks
33
+ * Guards are executed before controller methods.
34
+ * They can be used for:
35
+ * - authentication
36
+ * - authorization
37
+ * - request validation
38
+ *
39
+ * If any guard returns `false`, the request handling is stopped.
40
+ *
41
+ * Metadata is stored under the CONTROLLER_CONFIGUARTION key and used
42
+ * internally by the framework during request processing.
43
+ */
44
+ export declare function Guard(guard: Guard | GuardFn): (target: any, propertyKey?: string, descriptor?: PropertyDescriptor) => void;
package/dist/guard.js ADDED
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Guard = Guard;
4
+ const shared_1 = require("./utils/shared");
5
+ /**
6
+ * Decorator to register a guard.
7
+ *
8
+ * Guards are used to control access to a controller by validating
9
+ * incoming requests before they reach route handlers. If a guard
10
+ * returns `false`, the request is blocked.
11
+ *
12
+ * @param guard - A guard class or function used to determine whether
13
+ * the request is allowed to proceed.
14
+ *
15
+ * @returns A class decorator that attaches guard metadata to the target.
16
+ *
17
+ * @example
18
+ * // Using function guard
19
+ * @Guard((req, res) => {
20
+ * return !!req.headers.authorization;
21
+ * })
22
+ * class MyController {}
23
+ *
24
+ * @example
25
+ * // Using class-based guard
26
+ * class AuthGuard {
27
+ * canActivate(req: Request, res: Response) {
28
+ * return !!req.headers.authorization;
29
+ * }
30
+ * }
31
+ *
32
+ * @Guard(AuthGuard)
33
+ * class MyController {}
34
+ *
35
+ * @remarks
36
+ * Guards are executed before controller methods.
37
+ * They can be used for:
38
+ * - authentication
39
+ * - authorization
40
+ * - request validation
41
+ *
42
+ * If any guard returns `false`, the request handling is stopped.
43
+ *
44
+ * Metadata is stored under the CONTROLLER_CONFIGUARTION key and used
45
+ * internally by the framework during request processing.
46
+ */
47
+ function Guard(guard) {
48
+ return function (target, propertyKey, descriptor) {
49
+ const data = { guards: [guard] };
50
+ if (descriptor) {
51
+ (0, shared_1.defineMiddlewaresMeta)(data, target, propertyKey);
52
+ }
53
+ else {
54
+ (0, shared_1.defineMiddlewaresMeta)(data, target);
55
+ }
56
+ };
57
+ }
58
+ //# sourceMappingURL=guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"guard.js","sourceRoot":"","sources":["../src/guard.ts"],"names":[],"mappings":";;AA4CA,sBAUC;AArDD,2CAAuD;AACvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,SAAgB,KAAK,CAAC,KAAsB;IAC1C,OAAO,UAAU,MAAW,EAAE,WAAoB,EAAE,UAA+B;QACjF,MAAM,IAAI,GAAG,EAAE,MAAM,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC;QAEjC,IAAI,UAAU,EAAE,CAAC;YACf,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACN,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import 'reflect-metadata';
2
2
  export * from './catch';
3
3
  export * from './cors';
4
+ export * from './guard';
5
+ export * from './interceptor';
6
+ export * from './pipe';
4
7
  export * from './sanitize';
5
8
  export * from './status';
6
9
  export * from './use';
package/dist/index.js CHANGED
@@ -17,6 +17,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  require("reflect-metadata");
18
18
  __exportStar(require("./catch"), exports);
19
19
  __exportStar(require("./cors"), exports);
20
+ __exportStar(require("./guard"), exports);
21
+ __exportStar(require("./interceptor"), exports);
22
+ __exportStar(require("./pipe"), exports);
20
23
  __exportStar(require("./sanitize"), exports);
21
24
  __exportStar(require("./status"), exports);
22
25
  __exportStar(require("./use"), exports);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4BAA0B;AAC1B,0CAAwB;AACxB,yCAAuB;AACvB,6CAA2B;AAC3B,2CAAyB;AACzB,wCAAsB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4BAA0B;AAC1B,0CAAwB;AACxB,yCAAuB;AACvB,0CAAwB;AACxB,gDAA8B;AAC9B,yCAAuB;AACvB,6CAA2B;AAC3B,2CAAyB;AACzB,wCAAsB"}
@@ -0,0 +1,45 @@
1
+ import { InterceptorCB } from '@heliosjs/core';
2
+ /**
3
+ * Decorator to register an interceptor at the controller or method level.
4
+ *
5
+ * Interceptors are functions that wrap the execution of a route handler.
6
+ * They can run logic before after the handler is executed, allowing
7
+ * you to modify responses, or handle cross-cutting concerns.
8
+ *
9
+ * @param handler - Interceptor callback function.
10
+ *
11
+ * @returns A decorator that attaches interceptor metadata to the target
12
+ * (either a class or a method).
13
+ *
14
+ * @example
15
+ * // Logging interceptor
16
+ * @Intercept(async (ctx, next) => {
17
+ * console.log('Before');
18
+ * const result = await next();
19
+ * console.log('After');
20
+ * return result;
21
+ * })
22
+ * class MyController {}
23
+ *
24
+ * @example
25
+ * // Method-level interceptor
26
+ * class MyController {
27
+ * @Intercept(async (ctx, next) => {
28
+ * return next();
29
+ * })
30
+ * getData() {}
31
+ * }
32
+ *
33
+ * @remarks
34
+ * - Interceptors can modify the result returned by the handler.
35
+ * - They are executed in the order they are applied.
36
+ * - Can be used for:
37
+ * - logging
38
+ * - response transformation
39
+ * - caching
40
+ * - error handling (wrapping)
41
+ *
42
+ * Metadata is stored using the MIDDLEWARES_CONFIG key and
43
+ * used internally by the framework during request processing.
44
+ */
45
+ export declare function Intercept(handler: InterceptorCB): (target: any, propertyKey?: string, descriptor?: PropertyDescriptor) => void;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Intercept = Intercept;
4
+ const shared_1 = require("./utils/shared");
5
+ /**
6
+ * Decorator to register an interceptor at the controller or method level.
7
+ *
8
+ * Interceptors are functions that wrap the execution of a route handler.
9
+ * They can run logic before after the handler is executed, allowing
10
+ * you to modify responses, or handle cross-cutting concerns.
11
+ *
12
+ * @param handler - Interceptor callback function.
13
+ *
14
+ * @returns A decorator that attaches interceptor metadata to the target
15
+ * (either a class or a method).
16
+ *
17
+ * @example
18
+ * // Logging interceptor
19
+ * @Intercept(async (ctx, next) => {
20
+ * console.log('Before');
21
+ * const result = await next();
22
+ * console.log('After');
23
+ * return result;
24
+ * })
25
+ * class MyController {}
26
+ *
27
+ * @example
28
+ * // Method-level interceptor
29
+ * class MyController {
30
+ * @Intercept(async (ctx, next) => {
31
+ * return next();
32
+ * })
33
+ * getData() {}
34
+ * }
35
+ *
36
+ * @remarks
37
+ * - Interceptors can modify the result returned by the handler.
38
+ * - They are executed in the order they are applied.
39
+ * - Can be used for:
40
+ * - logging
41
+ * - response transformation
42
+ * - caching
43
+ * - error handling (wrapping)
44
+ *
45
+ * Metadata is stored using the MIDDLEWARES_CONFIG key and
46
+ * used internally by the framework during request processing.
47
+ */
48
+ function Intercept(handler) {
49
+ return function (target, propertyKey, descriptor) {
50
+ const data = { interceptors: [handler] };
51
+ if (descriptor) {
52
+ (0, shared_1.defineMiddlewaresMeta)(data, target, propertyKey);
53
+ }
54
+ else {
55
+ (0, shared_1.defineMiddlewaresMeta)(data, target);
56
+ }
57
+ };
58
+ }
59
+ //# sourceMappingURL=interceptor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interceptor.js","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":";;AA6CA,8BAUC;AAtDD,2CAAuD;AACvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,SAAgB,SAAS,CAAC,OAAsB;IAC9C,OAAO,UAAU,MAAW,EAAE,WAAoB,EAAE,UAA+B;QACjF,MAAM,IAAI,GAAG,EAAE,YAAY,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC;QAEzC,IAAI,UAAU,EAAE,CAAC;YACf,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACN,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
package/dist/pipe.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { type Pipe } from './types/core';
2
+ /**
3
+ * Decorator to register a data transformation pipe.
4
+ *
5
+ * Pipes are used to transform incoming request data before it reaches
6
+ * the route handler. They can be applied at the controller level and
7
+ * modify parts of the request such as body, query, params, or headers.
8
+ *
9
+ * @param config - Configuration object containing transformation functions
10
+ * for different parts of the request.
11
+ *
12
+ * @returns A class decorator that attaches pipe metadata to the target.
13
+ *
14
+ * @example
15
+ * @Pipe({
16
+ * body: (body) => ({ ...body, name: body.name.trim() }),
17
+ * query: (query) => ({ ...query, page: Number(query.page) }),
18
+ * })
19
+ * class MyController {}
20
+ *
21
+ * @remarks
22
+ * Each function in the pipe receives raw data and must return the transformed value.
23
+ * Pipes are executed before controller methods and can be used for:
24
+ * - data normalization
25
+ * - type casting
26
+ * - sanitization
27
+ *
28
+ * Metadata is stored under the CONTROLLER_CONFIGUARTION key and later used
29
+ * by the framework during request processing.
30
+ */
31
+ export declare function Pipe(pipe: Pipe): (target: any, propertyKey?: string, descriptor?: PropertyDescriptor) => void;
package/dist/pipe.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Pipe = Pipe;
4
+ const shared_1 = require("./utils/shared");
5
+ /**
6
+ * Decorator to register a data transformation pipe.
7
+ *
8
+ * Pipes are used to transform incoming request data before it reaches
9
+ * the route handler. They can be applied at the controller level and
10
+ * modify parts of the request such as body, query, params, or headers.
11
+ *
12
+ * @param config - Configuration object containing transformation functions
13
+ * for different parts of the request.
14
+ *
15
+ * @returns A class decorator that attaches pipe metadata to the target.
16
+ *
17
+ * @example
18
+ * @Pipe({
19
+ * body: (body) => ({ ...body, name: body.name.trim() }),
20
+ * query: (query) => ({ ...query, page: Number(query.page) }),
21
+ * })
22
+ * class MyController {}
23
+ *
24
+ * @remarks
25
+ * Each function in the pipe receives raw data and must return the transformed value.
26
+ * Pipes are executed before controller methods and can be used for:
27
+ * - data normalization
28
+ * - type casting
29
+ * - sanitization
30
+ *
31
+ * Metadata is stored under the CONTROLLER_CONFIGUARTION key and later used
32
+ * by the framework during request processing.
33
+ */
34
+ function Pipe(pipe) {
35
+ return function (target, propertyKey, descriptor) {
36
+ const data = { pipes: [pipe] };
37
+ if (descriptor) {
38
+ (0, shared_1.defineMiddlewaresMeta)(data, target, propertyKey);
39
+ }
40
+ else {
41
+ (0, shared_1.defineMiddlewaresMeta)(data, target);
42
+ }
43
+ };
44
+ }
45
+ //# sourceMappingURL=pipe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pipe.js","sourceRoot":"","sources":["../src/pipe.ts"],"names":[],"mappings":";;AA+BA,oBAUC;AAxCD,2CAAuD;AACvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,SAAgB,IAAI,CAAC,IAAU;IAC7B,OAAO,UAAU,MAAW,EAAE,WAAoB,EAAE,UAA+B;QACjF,MAAM,IAAI,GAAG,EAAE,KAAK,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QAE/B,IAAI,UAAU,EAAE,CAAC;YACf,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACN,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
package/dist/sanitize.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Sanitize = Sanitize;
4
- const constants_1 = require("./constants");
4
+ const shared_1 = require("./utils/shared");
5
5
  /**
6
6
  * Decorator to apply sanitization configurations to a controller or method.
7
7
  *
@@ -42,12 +42,12 @@ const constants_1 = require("./constants");
42
42
  */
43
43
  function Sanitize(sanitizeConfig) {
44
44
  return function (target, propertyKey, descriptor) {
45
- const configs = Array.isArray(sanitizeConfig) ? sanitizeConfig : [sanitizeConfig];
46
- if (propertyKey && descriptor) {
47
- Reflect.defineMetadata(constants_1.SANITIZE, configs, target, propertyKey);
45
+ const data = { sanitizers: Array.isArray(sanitizeConfig) ? sanitizeConfig : [sanitizeConfig] };
46
+ if (descriptor) {
47
+ (0, shared_1.defineMiddlewaresMeta)(data, target, propertyKey);
48
48
  }
49
49
  else {
50
- Reflect.defineMetadata(constants_1.SANITIZE, configs, target.prototype || target);
50
+ (0, shared_1.defineMiddlewaresMeta)(data, target);
51
51
  }
52
52
  };
53
53
  }
@@ -1 +1 @@
1
- {"version":3,"file":"sanitize.js","sourceRoot":"","sources":["../src/sanitize.ts"],"names":[],"mappings":";;AAyCA,4BAUC;AAnDD,2CAAuC;AAGvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,SAAgB,QAAQ,CAAC,cAAmD;IAC1E,OAAO,UAAU,MAAW,EAAE,WAAoB,EAAE,UAA+B;QACjF,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC;QAElF,IAAI,WAAW,IAAI,UAAU,EAAE,CAAC;YAC9B,OAAO,CAAC,cAAc,CAAC,oBAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACjE,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,cAAc,CAAC,oBAAQ,EAAE,OAAO,EAAE,MAAM,CAAC,SAAS,IAAI,MAAM,CAAC,CAAC;QACxE,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"sanitize.js","sourceRoot":"","sources":["../src/sanitize.ts"],"names":[],"mappings":";;AAwCA,4BAUC;AAjDD,2CAAuD;AACvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,SAAgB,QAAQ,CAAC,cAAmD;IAC1E,OAAO,UAAU,MAAW,EAAE,WAAoB,EAAE,UAA+B;QACjF,MAAM,IAAI,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,EAAE,CAAC;QAE/F,IAAI,UAAU,EAAE,CAAC;YACf,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACN,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
package/dist/status.d.ts CHANGED
@@ -31,7 +31,7 @@
31
31
  * This metadata is accessible via Reflect API and used internally by the framework
32
32
  * to set the HTTP response status.
33
33
  */
34
- export declare function Status(status: number): (target: any, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<any>) => void;
34
+ export declare function Status(status: number): (target: any, propertyKey?: string) => void;
35
35
  /**
36
36
  * Shortcut decorator to set HTTP 200 OK status.
37
37
  *
@@ -40,7 +40,7 @@ export declare function Status(status: number): (target: any, propertyKey?: stri
40
40
  *
41
41
  * @returns {Function} A decorator function that sets the HTTP status to 200.
42
42
  */
43
- export declare const Ok200: () => (target: any, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<any>) => void;
43
+ export declare const Ok200: () => (target: any, propertyKey?: string) => void;
44
44
  /**
45
45
  * Shortcut decorator to set HTTP 201 Created status.
46
46
  *
@@ -49,7 +49,7 @@ export declare const Ok200: () => (target: any, propertyKey?: string | symbol, d
49
49
  *
50
50
  * @returns {Function} A decorator function that sets the HTTP status to 201.
51
51
  */
52
- export declare const Ok201: () => (target: any, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<any>) => void;
52
+ export declare const Ok201: () => (target: any, propertyKey?: string) => void;
53
53
  /**
54
54
  * Shortcut decorator to set HTTP 204 No Content status.
55
55
  *
@@ -59,4 +59,4 @@ export declare const Ok201: () => (target: any, propertyKey?: string | symbol, d
59
59
  *
60
60
  * @returns {Function} A decorator function that sets the HTTP status to 204.
61
61
  */
62
- export declare const Ok204: () => (target: any, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<any>) => void;
62
+ export declare const Ok204: () => (target: any, propertyKey?: string) => void;
package/dist/status.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Ok204 = exports.Ok201 = exports.Ok200 = void 0;
4
4
  exports.Status = Status;
5
- const constants_1 = require("./constants");
5
+ const shared_1 = require("./utils/shared");
6
6
  /**
7
7
  * Decorator to set the HTTP status code for the response.
8
8
  *
@@ -37,13 +37,11 @@ const constants_1 = require("./constants");
37
37
  * to set the HTTP response status.
38
38
  */
39
39
  function Status(status) {
40
- return function (target, propertyKey, descriptor) {
41
- if (!propertyKey) {
42
- Reflect.defineMetadata(constants_1.OK_METADATA_KEY, status, target.prototype || target);
43
- return;
44
- }
45
- if (descriptor) {
46
- Reflect.defineMetadata(constants_1.OK_METADATA_KEY, status, target, propertyKey);
40
+ return function (target, propertyKey) {
41
+ if (propertyKey) {
42
+ const meta = (0, shared_1.reflectMiddlewaresMetadata)(target);
43
+ meta.status = status;
44
+ (0, shared_1.defineMiddlewaresMeta)(meta, target, propertyKey);
47
45
  }
48
46
  };
49
47
  }
@@ -1 +1 @@
1
- {"version":3,"file":"status.js","sourceRoot":"","sources":["../src/status.ts"],"names":[],"mappings":";;;AAmCA,wBAeC;AAlDD,2CAA8C;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,SAAgB,MAAM,CAAC,MAAc;IACnC,OAAO,UACL,MAAW,EACX,WAA6B,EAC7B,UAAyC;QAEzC,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,OAAO,CAAC,cAAc,CAAC,2BAAe,EAAE,MAAM,EAAE,MAAM,CAAC,SAAS,IAAI,MAAM,CAAC,CAAC;YAC5E,OAAO;QACT,CAAC;QAED,IAAI,UAAU,EAAE,CAAC;YACf,OAAO,CAAC,cAAc,CAAC,2BAAe,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACvE,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACI,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAA1B,QAAA,KAAK,SAAqB;AAEvC;;;;;;;GAOG;AACI,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAA1B,QAAA,KAAK,SAAqB;AAEvC;;;;;;;;GAQG;AACI,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAA1B,QAAA,KAAK,SAAqB"}
1
+ {"version":3,"file":"status.js","sourceRoot":"","sources":["../src/status.ts"],"names":[],"mappings":";;;AAkCA,wBAQC;AA1CD,2CAAmF;AACnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,SAAgB,MAAM,CAAC,MAAc;IACnC,OAAO,UAAU,MAAW,EAAE,WAAoB;QAChD,IAAI,WAAW,EAAE,CAAC;YAChB,MAAM,IAAI,GAAG,IAAA,mCAA0B,EAAC,MAAM,CAAC,CAAC;YAChD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;YACrB,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACI,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAA1B,QAAA,KAAK,SAAqB;AAEvC;;;;;;;GAOG;AACI,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAA1B,QAAA,KAAK,SAAqB;AAEvC;;;;;;;;GAQG;AACI,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAA1B,QAAA,KAAK,SAAqB"}
@@ -20,33 +20,11 @@ export type ControllerType = {
20
20
  new (...args: any[]): any;
21
21
  };
22
22
  export type ControllerInstance = InstanceType<ControllerType>;
23
- export type ControllerMetadata = {
24
- routePrefix: string;
25
- middlewares: MiddlewareCB[];
26
- interceptor?: InterceptorCB;
27
- subControllers: ControllerInstance[];
28
- errorHandler?: ErorrHandler;
29
- cors?: CORSConfig;
30
- sanitizers: SanitizerConfig[];
31
- };
32
23
  export interface ControllerConfig {
33
24
  prefix: string;
34
25
  middlewares?: Array<MiddlewareCB>;
35
26
  controllers?: ControllerInstance[];
36
- interceptor?: InterceptorCB;
37
27
  }
38
- export type RouteContext = {
39
- controllerInstance: any;
40
- controllerMeta: ControllerMetadata;
41
- path: string;
42
- method: string;
43
- middlewareChain: MiddlewareCB[];
44
- interceptorChain: InterceptorCB[];
45
- corsChain: CORSConfig[];
46
- errorHandlerChain: ErorrHandler[];
47
- subPath: string;
48
- sanitizersChain: SanitizerConfig[];
49
- };
50
28
  export type SSE_HANDLER_META = {
51
29
  type: string;
52
30
  method: string;
@@ -76,31 +54,67 @@ export type SeeControllerHandlers = {
76
54
  error: HandlerMeta[];
77
55
  };
78
56
  };
57
+ export type FunctionsMeta = {
58
+ middlewares: MiddlewareCB[];
59
+ errors: ErorrHandler[];
60
+ cors: CORSConfig[];
61
+ sanitizers: SanitizerConfig[];
62
+ pipes: Pipe[];
63
+ guards: (Guard | GuardFn)[];
64
+ interceptors: InterceptorCB[];
65
+ status?: number;
66
+ };
79
67
  export type Route = {
80
68
  name: string;
81
69
  route: string;
82
70
  method: HTTP_METHODS;
83
71
  cors?: CORSConfig[];
84
72
  ok: number;
85
- paramMetadata: ParamMetadata[];
86
- interceptors: InterceptorCB[];
87
- functions: {
88
- sanitizers: SanitizerConfig[];
89
- errors: ErorrHandler[];
90
- middlewares: MiddlewareCB[];
91
- }[];
73
+ parameters: ParamMetadata[];
74
+ interceptor?: InterceptorCB;
75
+ functions: FunctionsMeta[];
92
76
  fn: (...args: any[]) => any;
93
77
  };
94
78
  export type NextFunction = (error?: unknown) => void;
95
79
  export type ControllerMeta = {
96
80
  prefix: string;
81
+ name: string;
97
82
  routes: Route[];
98
- interceptors: InterceptorCB[];
99
- cors: CORSConfig[];
100
- functions: {
101
- sanitizers: SanitizerConfig[];
102
- errors: ErorrHandler[];
103
- middlewares: MiddlewareCB[];
104
- }[];
105
83
  children?: ControllerMeta[];
84
+ functions: MiddlewaresMetadata[];
85
+ controllers: ControllerClass[];
86
+ };
87
+ export type ControllerMetadata = {
88
+ prefix: string;
89
+ name: string;
90
+ middlewares: MiddlewareCB[];
91
+ controllers: ControllerInstance[];
92
+ };
93
+ export type RouteMetadata = {
94
+ route: string;
95
+ method: HTTP_METHODS;
96
+ middlewares: MiddlewareCB[];
97
+ parameters: ParamMetadata[];
98
+ };
99
+ export type PipeKey = 'body' | 'query' | 'params' | 'headers';
100
+ export type Pipe = {
101
+ body?: (body: unknown, request: Request) => unknown;
102
+ query?: (query: Record<string, string | string[]>, request: Request) => Record<string, string | string[]>;
103
+ params?: (params: Record<string, string>, request: Request) => Record<string, string>;
104
+ headers?: (headers: Record<string, string | string[]>, request: Request) => Record<string, string | string[]>;
105
+ };
106
+ export interface Guard {
107
+ new (...any: any[]): any;
108
+ canActivate(request: Request, response: Response): boolean;
109
+ }
110
+ export type GuardFn = (request: Request, response: Response) => boolean;
111
+ export type MiddlewaresMetadata = {
112
+ middlewares: MiddlewareCB[];
113
+ errors: ErorrHandler[];
114
+ cors: CORSConfig[];
115
+ sanitizers: SanitizerConfig[];
116
+ pipes: Pipe[];
117
+ guards: (Guard | GuardFn)[];
118
+ interceptors: InterceptorCB[];
119
+ status?: number;
106
120
  };
@@ -7,7 +7,6 @@ export declare enum ErrorCode {
7
7
  NOT_FOUND = "NOT_FOUND",
8
8
  VALIDATION_FAILED = "VALIDATION_FAILED",
9
9
  RATE_LIMIT_EXCEEDED = "RATE_LIMIT_EXCEEDED",
10
- INTERNAL_ERROR = "INTERNAL_ERROR",
11
10
  SERVICE_UNAVAILABLE = "SERVICE_UNAVAILABLE",
12
11
  DATABASE_ERROR = "DATABASE_ERROR",
13
12
  DUPLICATE_ENTRY = "DUPLICATE_ENTRY",
@@ -9,7 +9,6 @@ var ErrorCode;
9
9
  ErrorCode["NOT_FOUND"] = "NOT_FOUND";
10
10
  ErrorCode["VALIDATION_FAILED"] = "VALIDATION_FAILED";
11
11
  ErrorCode["RATE_LIMIT_EXCEEDED"] = "RATE_LIMIT_EXCEEDED";
12
- ErrorCode["INTERNAL_ERROR"] = "INTERNAL_ERROR";
13
12
  ErrorCode["SERVICE_UNAVAILABLE"] = "SERVICE_UNAVAILABLE";
14
13
  ErrorCode["DATABASE_ERROR"] = "DATABASE_ERROR";
15
14
  ErrorCode["DUPLICATE_ENTRY"] = "DUPLICATE_ENTRY";
@@ -1 +1 @@
1
- {"version":3,"file":"error.js","sourceRoot":"","sources":["../../../src/types/core/error.ts"],"names":[],"mappings":";;;AAGA,IAAY,SAcX;AAdD,WAAY,SAAS;IACnB,wCAA2B,CAAA;IAC3B,0CAA6B,CAAA;IAC7B,oCAAuB,CAAA;IACvB,oCAAuB,CAAA;IACvB,oDAAuC,CAAA;IACvC,wDAA2C,CAAA;IAC3C,8CAAiC,CAAA;IACjC,wDAA2C,CAAA;IAC3C,8CAAiC,CAAA;IACjC,gDAAmC,CAAA;IACnC,4CAA+B,CAAA;IAC/B,oDAAuC,CAAA;IACvC,4DAA+C,CAAA;AACjD,CAAC,EAdW,SAAS,yBAAT,SAAS,QAcpB"}
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../../../src/types/core/error.ts"],"names":[],"mappings":";;;AAGA,IAAY,SAaX;AAbD,WAAY,SAAS;IACnB,wCAA2B,CAAA;IAC3B,0CAA6B,CAAA;IAC7B,oCAAuB,CAAA;IACvB,oCAAuB,CAAA;IACvB,oDAAuC,CAAA;IACvC,wDAA2C,CAAA;IAC3C,wDAA2C,CAAA;IAC3C,8CAAiC,CAAA;IACjC,gDAAmC,CAAA;IACnC,4CAA+B,CAAA;IAC/B,oDAAuC,CAAA;IACvC,4DAA+C,CAAA;AACjD,CAAC,EAbW,SAAS,yBAAT,SAAS,QAapB"}
package/dist/use.d.ts CHANGED
@@ -1,2 +1,46 @@
1
1
  import { MiddlewareCB } from './types/core';
2
- export declare function Use(middleware: MiddlewareCB | MiddlewareCB[]): (target: any) => any;
2
+ /**
3
+ * Decorator to register middleware(s) at the controller or method level.
4
+ *
5
+ * Middlewares are functions executed before the route handler.
6
+ * They can be used to:
7
+ * - modify request/response
8
+ * - perform logging
9
+ * - handle authentication/authorization
10
+ * - short-circuit request handling
11
+ *
12
+ * @param middleware - A single middleware function or an array of middleware functions.
13
+ *
14
+ * @returns A decorator that attaches middleware metadata to the target
15
+ * (either a class or a method).
16
+ *
17
+ * @example
18
+ * // Single middleware
19
+ * @Use((req, res, next) => {
20
+ * console.log('Request received');
21
+ * next();
22
+ * })
23
+ * class MyController {}
24
+ *
25
+ * @example
26
+ * // Multiple middlewares
27
+ * @Use([
28
+ * authMiddleware,
29
+ * loggingMiddleware,
30
+ * ])
31
+ * class MyController {}
32
+ *
33
+ * @example
34
+ * // Method-level middleware
35
+ * class MyController {
36
+ * @Use(authMiddleware)
37
+ * getData() {}
38
+ * }
39
+ *
40
+ * @remarks
41
+ * - Middlewares are executed in the order they are defined.
42
+ * - Can be applied at both class and method levels.
43
+ * - Metadata is stored using the MIDDLEWARES_CONFIG key and used
44
+ * internally by the framework during request handling.
45
+ */
46
+ export declare function Use(middleware: MiddlewareCB | MiddlewareCB[]): (target: any, propertyKey?: string, descriptor?: PropertyDescriptor) => any;
package/dist/use.js CHANGED
@@ -1,11 +1,61 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Use = Use;
4
- const constants_1 = require("./constants");
4
+ const shared_1 = require("./utils/shared");
5
+ /**
6
+ * Decorator to register middleware(s) at the controller or method level.
7
+ *
8
+ * Middlewares are functions executed before the route handler.
9
+ * They can be used to:
10
+ * - modify request/response
11
+ * - perform logging
12
+ * - handle authentication/authorization
13
+ * - short-circuit request handling
14
+ *
15
+ * @param middleware - A single middleware function or an array of middleware functions.
16
+ *
17
+ * @returns A decorator that attaches middleware metadata to the target
18
+ * (either a class or a method).
19
+ *
20
+ * @example
21
+ * // Single middleware
22
+ * @Use((req, res, next) => {
23
+ * console.log('Request received');
24
+ * next();
25
+ * })
26
+ * class MyController {}
27
+ *
28
+ * @example
29
+ * // Multiple middlewares
30
+ * @Use([
31
+ * authMiddleware,
32
+ * loggingMiddleware,
33
+ * ])
34
+ * class MyController {}
35
+ *
36
+ * @example
37
+ * // Method-level middleware
38
+ * class MyController {
39
+ * @Use(authMiddleware)
40
+ * getData() {}
41
+ * }
42
+ *
43
+ * @remarks
44
+ * - Middlewares are executed in the order they are defined.
45
+ * - Can be applied at both class and method levels.
46
+ * - Metadata is stored using the MIDDLEWARES_CONFIG key and used
47
+ * internally by the framework during request handling.
48
+ */
5
49
  function Use(middleware) {
6
- return function (target) {
7
- const existed = Reflect.getMetadata(constants_1.USE_MIDDLEWARE, target) || [];
8
- Reflect.defineMetadata(constants_1.USE_MIDDLEWARE, existed.concat(middleware).reverse(), target);
50
+ return function (target, propertyKey, descriptor) {
51
+ const middlewares = Array.isArray(middleware) ? middleware : [middleware];
52
+ const data = { middlewares };
53
+ if (descriptor) {
54
+ (0, shared_1.defineMiddlewaresMeta)(data, target, propertyKey);
55
+ }
56
+ else {
57
+ (0, shared_1.defineMiddlewaresMeta)(data, target);
58
+ }
9
59
  return target;
10
60
  };
11
61
  }
package/dist/use.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"use.js","sourceRoot":"","sources":["../src/use.ts"],"names":[],"mappings":";;AAGA,kBAQC;AAXD,2CAA6C;AAG7C,SAAgB,GAAG,CAAC,UAAyC;IAC3D,OAAO,UAAU,MAAW;QAC1B,MAAM,OAAO,GAAG,OAAO,CAAC,WAAW,CAAC,0BAAc,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;QAElE,OAAO,CAAC,cAAc,CAAC,0BAAc,EAAE,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE,EAAE,MAAM,CAAC,CAAC;QAErF,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"use.js","sourceRoot":"","sources":["../src/use.ts"],"names":[],"mappings":";;AA+CA,kBAcC;AA5DD,2CAAuD;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,SAAgB,GAAG,CAAC,UAAyC;IAC3D,OAAO,UAAU,MAAW,EAAE,WAAoB,EAAE,UAA+B;QACjF,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;QAE1E,MAAM,IAAI,GAAG,EAAE,WAAW,EAAE,CAAC;QAE7B,IAAI,UAAU,EAAE,CAAC;YACf,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACN,IAAA,8BAAqB,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
@@ -1 +1,10 @@
1
+ import 'reflect-metadata';
2
+ import { ControllerMeta, MiddlewaresMetadata, RouteMetadata } from '../../types/core/controller';
1
3
  export declare const generateUniqueId: () => string;
4
+ export declare function reflectControllerMeta(target: object): ControllerMeta;
5
+ export declare function defineControllerMeta(meta: Partial<ControllerMeta>, target: object): void;
6
+ export declare function reflectMiddlewaresMetadata(target: any, propertie?: string): MiddlewaresMetadata;
7
+ export declare function reflectRouteMetadata(target: object, popertie: string): RouteMetadata;
8
+ export declare function defineMiddlewaresMeta(meta: Partial<MiddlewaresMetadata>, target: object): void;
9
+ export declare function defineMiddlewaresMeta(meta: Partial<MiddlewaresMetadata>, target: object, propertie?: string): void;
10
+ export declare function defineRouteMeta(meta: Partial<RouteMetadata>, target: object, propertie: string): void;
@@ -1,8 +1,91 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateUniqueId = void 0;
4
+ exports.reflectControllerMeta = reflectControllerMeta;
5
+ exports.defineControllerMeta = defineControllerMeta;
6
+ exports.reflectMiddlewaresMetadata = reflectMiddlewaresMetadata;
7
+ exports.reflectRouteMetadata = reflectRouteMetadata;
8
+ exports.defineMiddlewaresMeta = defineMiddlewaresMeta;
9
+ exports.defineRouteMeta = defineRouteMeta;
10
+ require("reflect-metadata");
11
+ const constants_1 = require("../../constants");
4
12
  const generateUniqueId = () => {
5
13
  return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
6
14
  };
7
15
  exports.generateUniqueId = generateUniqueId;
16
+ function reflectControllerMeta(target) {
17
+ const data = Reflect.getMetadata(constants_1.DECORATOR.controller, target) ?? {};
18
+ if (!data['controllers']) {
19
+ data['controllers'] = [];
20
+ }
21
+ return data;
22
+ }
23
+ function defineControllerMeta(meta, target) {
24
+ const existed = reflectControllerMeta(target);
25
+ const merged = Object.entries(meta).reduce((acc, [key, value]) => {
26
+ if (Array.isArray(acc[key])) {
27
+ acc[key] = acc[key].concat(value);
28
+ }
29
+ else {
30
+ acc[key] = value;
31
+ }
32
+ return acc;
33
+ }, existed);
34
+ Reflect.defineMetadata(constants_1.DECORATOR.controller, merged, target);
35
+ }
36
+ function reflectMiddlewaresMetadata(target, propertie) {
37
+ let data;
38
+ if (propertie) {
39
+ data = Reflect.getMetadata(constants_1.DECORATOR.middlewares, target, propertie) ?? {};
40
+ }
41
+ else {
42
+ data = Reflect.getMetadata(constants_1.DECORATOR.middlewares, target) ?? {};
43
+ }
44
+ ['middlewares', 'errors', 'guards', 'pipes', 'cors', 'sanitizers', 'interceptors'].forEach((prop) => {
45
+ if (!data[prop]?.length) {
46
+ data[prop] = [];
47
+ }
48
+ });
49
+ return data;
50
+ }
51
+ function reflectRouteMetadata(target, popertie) {
52
+ const data = Reflect.getMetadata(constants_1.DECORATOR.route, target, popertie) ?? {};
53
+ ['parameters', 'middlewares'].forEach((prop) => {
54
+ if (!data[prop]?.length) {
55
+ data[prop] = [];
56
+ }
57
+ });
58
+ return data;
59
+ }
60
+ function defineMiddlewaresMeta(meta, target, propertie) {
61
+ const existed = reflectMiddlewaresMetadata(target, propertie);
62
+ const merged = Object.entries(meta).reduce((acc, [key, value]) => {
63
+ if (Array.isArray(acc[key])) {
64
+ acc[key] = acc[key].concat(value);
65
+ }
66
+ else {
67
+ acc[key] = value;
68
+ }
69
+ return acc;
70
+ }, existed);
71
+ if (propertie) {
72
+ Reflect.defineMetadata(constants_1.DECORATOR.middlewares, merged, target, propertie);
73
+ }
74
+ else {
75
+ Reflect.defineMetadata(constants_1.DECORATOR.middlewares, merged, target);
76
+ }
77
+ }
78
+ function defineRouteMeta(meta, target, propertie) {
79
+ const existed = reflectRouteMetadata(target, propertie);
80
+ const merged = Object.entries(meta).reduce((acc, [key, value]) => {
81
+ if (Array.isArray(acc[key])) {
82
+ acc[key] = acc[key].concat(value);
83
+ }
84
+ else {
85
+ acc[key] = value;
86
+ }
87
+ return acc;
88
+ }, existed);
89
+ Reflect.defineMetadata(constants_1.DECORATOR.route, merged, target, propertie);
90
+ }
8
91
  //# sourceMappingURL=helpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/utils/shared/helpers.ts"],"names":[],"mappings":";;;AAAO,MAAM,gBAAgB,GAAG,GAAG,EAAE;IACnC,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;AACvE,CAAC,CAAC;AAFW,QAAA,gBAAgB,oBAE3B"}
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/utils/shared/helpers.ts"],"names":[],"mappings":";;;AAQA,sDAOC;AACD,oDAaC;AAED,gEAiBC;AACD,oDAUC;AAQD,sDAqBC;AAMD,0CAaC;AA3GD,4BAA0B;AAC1B,+CAA4C;AAGrC,MAAM,gBAAg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O,CAAC,cAAc,CAAC,qBAAS,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;AACrE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heliosjs/middlewares",
3
- "version": "2.0.2",
3
+ "version": "4.0.0",
4
4
  "description": "Core functionality for Quantum Flow",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -30,7 +30,7 @@
30
30
  "reflect-metadata": "^0.2.2"
31
31
  },
32
32
  "peerDependencies": {
33
- "@heliosjs/core": "^1.4.2",
33
+ "@heliosjs/core": "^2.1.0",
34
34
  "typescript": ">=4.0.0"
35
35
  }
36
36
  }