@catbee/utils 0.0.8-next.0 → 0.0.8-rc.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/README.md +1 -26
- package/build/esm/config.d.ts +122 -0
- package/build/esm/config.js +133 -0
- package/build/esm/config.js.map +1 -0
- package/build/esm/index.d.ts +27 -0
- package/build/esm/index.js +50 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/servers/server.builder.d.ts +508 -0
- package/build/esm/servers/server.builder.js +702 -0
- package/build/esm/servers/server.builder.js.map +1 -0
- package/build/esm/servers/server.d.ts +256 -0
- package/build/esm/servers/server.js +1248 -0
- package/build/esm/servers/server.js.map +1 -0
- package/build/esm/types/api-response.d.ts +152 -0
- package/build/esm/types/api-response.js +34 -0
- package/build/esm/types/api-response.js.map +1 -0
- package/build/esm/types/index.d.ts +125 -0
- package/build/esm/types/index.js +25 -0
- package/build/esm/types/index.js.map +1 -0
- package/build/esm/types/server.d.ts +268 -0
- package/build/esm/types/server.js +25 -0
- package/build/esm/types/server.js.map +1 -0
- package/build/esm/utils/array.utils.d.ts +168 -0
- package/build/esm/utils/array.utils.js +382 -0
- package/build/esm/utils/array.utils.js.map +1 -0
- package/build/esm/utils/async.utils.d.ts +265 -0
- package/build/esm/utils/async.utils.js +896 -0
- package/build/esm/utils/async.utils.js.map +1 -0
- package/build/esm/utils/cache.utils.d.ts +153 -0
- package/build/esm/utils/cache.utils.js +499 -0
- package/build/esm/utils/cache.utils.js.map +1 -0
- package/build/esm/utils/context-store.utils.d.ts +189 -0
- package/build/esm/utils/context-store.utils.js +307 -0
- package/build/esm/utils/context-store.utils.js.map +1 -0
- package/build/esm/utils/crypto.utils.d.ts +160 -0
- package/build/esm/utils/crypto.utils.js +375 -0
- package/build/esm/utils/crypto.utils.js.map +1 -0
- package/build/esm/utils/date.utils.d.ts +159 -0
- package/build/esm/utils/date.utils.js +389 -0
- package/build/esm/utils/date.utils.js.map +1 -0
- package/build/esm/utils/decorators.utils.d.ts +315 -0
- package/build/esm/utils/decorators.utils.js +666 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +196 -0
- package/build/esm/utils/dir.utils.js +998 -0
- package/build/esm/utils/dir.utils.js.map +1 -0
- package/build/esm/utils/env.utils.d.ts +377 -0
- package/build/esm/utils/env.utils.js +873 -0
- package/build/esm/utils/env.utils.js.map +1 -0
- package/build/esm/utils/exception.utils.d.ts +230 -0
- package/build/esm/utils/exception.utils.js +559 -0
- package/build/esm/utils/exception.utils.js.map +1 -0
- package/build/esm/utils/fs.utils.d.ts +164 -0
- package/build/esm/utils/fs.utils.js +580 -0
- package/build/esm/utils/fs.utils.js.map +1 -0
- package/build/esm/utils/http-status-codes.d.ts +266 -0
- package/build/esm/utils/http-status-codes.js +295 -0
- package/build/esm/utils/http-status-codes.js.map +1 -0
- package/build/esm/utils/id.utils.d.ts +36 -0
- package/build/esm/utils/id.utils.js +87 -0
- package/build/esm/utils/id.utils.js.map +1 -0
- package/build/esm/utils/logger.utils.d.ts +160 -0
- package/build/esm/utils/logger.utils.js +341 -0
- package/build/esm/utils/logger.utils.js.map +1 -0
- package/build/esm/utils/middleware.utils.d.ts +100 -0
- package/build/esm/utils/middleware.utils.js +325 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +124 -0
- package/build/esm/utils/obj.utils.js +519 -0
- package/build/esm/utils/obj.utils.js.map +1 -0
- package/build/esm/utils/performance.utils.d.ts +136 -0
- package/build/esm/utils/performance.utils.js +376 -0
- package/build/esm/utils/performance.utils.js.map +1 -0
- package/build/esm/utils/request.utils.d.ts +86 -0
- package/build/esm/utils/request.utils.js +223 -0
- package/build/esm/utils/request.utils.js.map +1 -0
- package/build/esm/utils/response.utils.d.ts +163 -0
- package/build/esm/utils/response.utils.js +284 -0
- package/build/esm/utils/response.utils.js.map +1 -0
- package/build/esm/utils/stream.utils.d.ts +88 -0
- package/build/esm/utils/stream.utils.js +284 -0
- package/build/esm/utils/stream.utils.js.map +1 -0
- package/build/esm/utils/string.utils.d.ts +93 -0
- package/build/esm/utils/string.utils.js +169 -0
- package/build/esm/utils/string.utils.js.map +1 -0
- package/build/esm/utils/type.utils.d.ts +90 -0
- package/build/esm/utils/type.utils.js +190 -0
- package/build/esm/utils/type.utils.js.map +1 -0
- package/build/esm/utils/url.utils.d.ts +141 -0
- package/build/esm/utils/url.utils.js +381 -0
- package/build/esm/utils/url.utils.js.map +1 -0
- package/build/esm/utils/validate.utils.d.ts +177 -0
- package/build/esm/utils/validate.utils.js +336 -0
- package/build/esm/utils/validate.utils.js.map +1 -0
- package/build/esnext/config.d.ts +122 -0
- package/build/esnext/config.js +133 -0
- package/build/esnext/config.js.map +1 -0
- package/build/esnext/index.d.ts +27 -0
- package/build/esnext/index.js +50 -0
- package/build/esnext/index.js.map +1 -0
- package/build/esnext/servers/server.builder.d.ts +508 -0
- package/build/esnext/servers/server.builder.js +657 -0
- package/build/esnext/servers/server.builder.js.map +1 -0
- package/build/esnext/servers/server.d.ts +256 -0
- package/build/esnext/servers/server.js +955 -0
- package/build/esnext/servers/server.js.map +1 -0
- package/build/esnext/types/api-response.d.ts +152 -0
- package/build/esnext/types/api-response.js +34 -0
- package/build/esnext/types/api-response.js.map +1 -0
- package/build/esnext/types/index.d.ts +125 -0
- package/build/esnext/types/index.js +25 -0
- package/build/esnext/types/index.js.map +1 -0
- package/build/esnext/types/server.d.ts +268 -0
- package/build/esnext/types/server.js +25 -0
- package/build/esnext/types/server.js.map +1 -0
- package/build/esnext/utils/array.utils.d.ts +168 -0
- package/build/esnext/utils/array.utils.js +345 -0
- package/build/esnext/utils/array.utils.js.map +1 -0
- package/build/esnext/utils/async.utils.d.ts +265 -0
- package/build/esnext/utils/async.utils.js +620 -0
- package/build/esnext/utils/async.utils.js.map +1 -0
- package/build/esnext/utils/cache.utils.d.ts +153 -0
- package/build/esnext/utils/cache.utils.js +293 -0
- package/build/esnext/utils/cache.utils.js.map +1 -0
- package/build/esnext/utils/context-store.utils.d.ts +189 -0
- package/build/esnext/utils/context-store.utils.js +289 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -0
- package/build/esnext/utils/crypto.utils.d.ts +160 -0
- package/build/esnext/utils/crypto.utils.js +276 -0
- package/build/esnext/utils/crypto.utils.js.map +1 -0
- package/build/esnext/utils/date.utils.d.ts +159 -0
- package/build/esnext/utils/date.utils.js +384 -0
- package/build/esnext/utils/date.utils.js.map +1 -0
- package/build/esnext/utils/decorators.utils.d.ts +315 -0
- package/build/esnext/utils/decorators.utils.js +504 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +196 -0
- package/build/esnext/utils/dir.utils.js +477 -0
- package/build/esnext/utils/dir.utils.js.map +1 -0
- package/build/esnext/utils/env.utils.d.ts +377 -0
- package/build/esnext/utils/env.utils.js +785 -0
- package/build/esnext/utils/env.utils.js.map +1 -0
- package/build/esnext/utils/exception.utils.d.ts +230 -0
- package/build/esnext/utils/exception.utils.js +381 -0
- package/build/esnext/utils/exception.utils.js.map +1 -0
- package/build/esnext/utils/fs.utils.d.ts +164 -0
- package/build/esnext/utils/fs.utils.js +349 -0
- package/build/esnext/utils/fs.utils.js.map +1 -0
- package/build/esnext/utils/http-status-codes.d.ts +266 -0
- package/build/esnext/utils/http-status-codes.js +295 -0
- package/build/esnext/utils/http-status-codes.js.map +1 -0
- package/build/esnext/utils/id.utils.d.ts +36 -0
- package/build/esnext/utils/id.utils.js +84 -0
- package/build/esnext/utils/id.utils.js.map +1 -0
- package/build/esnext/utils/logger.utils.d.ts +160 -0
- package/build/esnext/utils/logger.utils.js +300 -0
- package/build/esnext/utils/logger.utils.js.map +1 -0
- package/build/esnext/utils/middleware.utils.d.ts +100 -0
- package/build/esnext/utils/middleware.utils.js +236 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +124 -0
- package/build/esnext/utils/obj.utils.js +413 -0
- package/build/esnext/utils/obj.utils.js.map +1 -0
- package/build/esnext/utils/performance.utils.d.ts +136 -0
- package/build/esnext/utils/performance.utils.js +271 -0
- package/build/esnext/utils/performance.utils.js.map +1 -0
- package/build/esnext/utils/request.utils.d.ts +86 -0
- package/build/esnext/utils/request.utils.js +186 -0
- package/build/esnext/utils/request.utils.js.map +1 -0
- package/build/esnext/utils/response.utils.d.ts +163 -0
- package/build/esnext/utils/response.utils.js +247 -0
- package/build/esnext/utils/response.utils.js.map +1 -0
- package/build/esnext/utils/stream.utils.d.ts +88 -0
- package/build/esnext/utils/stream.utils.js +210 -0
- package/build/esnext/utils/stream.utils.js.map +1 -0
- package/build/esnext/utils/string.utils.d.ts +93 -0
- package/build/esnext/utils/string.utils.js +165 -0
- package/build/esnext/utils/string.utils.js.map +1 -0
- package/build/esnext/utils/type.utils.d.ts +90 -0
- package/build/esnext/utils/type.utils.js +187 -0
- package/build/esnext/utils/type.utils.js.map +1 -0
- package/build/esnext/utils/url.utils.d.ts +141 -0
- package/build/esnext/utils/url.utils.js +304 -0
- package/build/esnext/utils/url.utils.js.map +1 -0
- package/build/esnext/utils/validate.utils.d.ts +177 -0
- package/build/esnext/utils/validate.utils.js +320 -0
- package/build/esnext/utils/validate.utils.js.map +1 -0
- package/build/src/config.d.ts +122 -0
- package/build/src/config.js +138 -0
- package/build/src/config.js.map +1 -0
- package/build/src/index.d.ts +27 -0
- package/build/src/index.js +69 -0
- package/build/src/index.js.map +1 -0
- package/build/src/servers/server.builder.d.ts +508 -0
- package/build/src/servers/server.builder.js +661 -0
- package/build/src/servers/server.builder.js.map +1 -0
- package/build/src/servers/server.d.ts +256 -0
- package/build/src/servers/server.js +962 -0
- package/build/src/servers/server.js.map +1 -0
- package/build/src/types/api-response.d.ts +152 -0
- package/build/src/types/api-response.js +37 -0
- package/build/src/types/api-response.js.map +1 -0
- package/build/src/types/index.d.ts +125 -0
- package/build/src/types/index.js +26 -0
- package/build/src/types/index.js.map +1 -0
- package/build/src/types/server.d.ts +268 -0
- package/build/src/types/server.js +26 -0
- package/build/src/types/server.js.map +1 -0
- package/build/src/utils/array.utils.d.ts +168 -0
- package/build/src/utils/array.utils.js +364 -0
- package/build/src/utils/array.utils.js.map +1 -0
- package/build/src/utils/async.utils.d.ts +265 -0
- package/build/src/utils/async.utils.js +641 -0
- package/build/src/utils/async.utils.js.map +1 -0
- package/build/src/utils/cache.utils.d.ts +153 -0
- package/build/src/utils/cache.utils.js +297 -0
- package/build/src/utils/cache.utils.js.map +1 -0
- package/build/src/utils/context-store.utils.d.ts +189 -0
- package/build/src/utils/context-store.utils.js +296 -0
- package/build/src/utils/context-store.utils.js.map +1 -0
- package/build/src/utils/crypto.utils.d.ts +160 -0
- package/build/src/utils/crypto.utils.js +293 -0
- package/build/src/utils/crypto.utils.js.map +1 -0
- package/build/src/utils/date.utils.d.ts +159 -0
- package/build/src/utils/date.utils.js +396 -0
- package/build/src/utils/date.utils.js.map +1 -0
- package/build/src/utils/decorators.utils.d.ts +315 -0
- package/build/src/utils/decorators.utils.js +516 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +196 -0
- package/build/src/utils/dir.utils.js +501 -0
- package/build/src/utils/dir.utils.js.map +1 -0
- package/build/src/utils/env.utils.d.ts +377 -0
- package/build/src/utils/env.utils.js +789 -0
- package/build/src/utils/env.utils.js.map +1 -0
- package/build/src/utils/exception.utils.d.ts +230 -0
- package/build/src/utils/exception.utils.js +407 -0
- package/build/src/utils/exception.utils.js.map +1 -0
- package/build/src/utils/fs.utils.d.ts +164 -0
- package/build/src/utils/fs.utils.js +372 -0
- package/build/src/utils/fs.utils.js.map +1 -0
- package/build/src/utils/http-status-codes.d.ts +266 -0
- package/build/src/utils/http-status-codes.js +298 -0
- package/build/src/utils/http-status-codes.js.map +1 -0
- package/build/src/utils/id.utils.d.ts +36 -0
- package/build/src/utils/id.utils.js +91 -0
- package/build/src/utils/id.utils.js.map +1 -0
- package/build/src/utils/logger.utils.d.ts +160 -0
- package/build/src/utils/logger.utils.js +346 -0
- package/build/src/utils/logger.utils.js.map +1 -0
- package/build/src/utils/middleware.utils.d.ts +100 -0
- package/build/src/utils/middleware.utils.js +244 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +124 -0
- package/build/src/utils/obj.utils.js +429 -0
- package/build/src/utils/obj.utils.js.map +1 -0
- package/build/src/utils/performance.utils.d.ts +136 -0
- package/build/src/utils/performance.utils.js +278 -0
- package/build/src/utils/performance.utils.js.map +1 -0
- package/build/src/utils/request.utils.d.ts +86 -0
- package/build/src/utils/request.utils.js +195 -0
- package/build/src/utils/request.utils.js.map +1 -0
- package/build/src/utils/response.utils.d.ts +163 -0
- package/build/src/utils/response.utils.js +260 -0
- package/build/src/utils/response.utils.js.map +1 -0
- package/build/src/utils/stream.utils.d.ts +88 -0
- package/build/src/utils/stream.utils.js +218 -0
- package/build/src/utils/stream.utils.js.map +1 -0
- package/build/src/utils/string.utils.d.ts +93 -0
- package/build/src/utils/string.utils.js +179 -0
- package/build/src/utils/string.utils.js.map +1 -0
- package/build/src/utils/type.utils.d.ts +90 -0
- package/build/src/utils/type.utils.js +196 -0
- package/build/src/utils/type.utils.js.map +1 -0
- package/build/src/utils/url.utils.d.ts +141 -0
- package/build/src/utils/url.utils.js +317 -0
- package/build/src/utils/url.utils.js.map +1 -0
- package/build/src/utils/validate.utils.d.ts +177 -0
- package/build/src/utils/validate.utils.js +345 -0
- package/build/src/utils/validate.utils.js.map +1 -0
- package/package.json +66 -12
- package/build/index.cjs +0 -6888
- package/build/index.d.ts +0 -5242
- package/build/index.mjs +0 -6615
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* The MIT License
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.Res = exports.Req = exports.Body = exports.Param = exports.Query = exports.Connect = exports.Trace = exports.Head = exports.Options = exports.Delete = exports.Patch = exports.Put = exports.Post = exports.Get = void 0;
|
|
27
|
+
exports.Controller = Controller;
|
|
28
|
+
exports.Use = Use;
|
|
29
|
+
exports.HttpCode = HttpCode;
|
|
30
|
+
exports.Header = Header;
|
|
31
|
+
exports.Before = Before;
|
|
32
|
+
exports.After = After;
|
|
33
|
+
exports.Roles = Roles;
|
|
34
|
+
exports.Redirect = Redirect;
|
|
35
|
+
exports.registerControllers = registerControllers;
|
|
36
|
+
require("reflect-metadata");
|
|
37
|
+
const http_status_codes_1 = require("./http-status-codes");
|
|
38
|
+
const exception_utils_1 = require("./exception.utils");
|
|
39
|
+
// Metadata keys
|
|
40
|
+
const ROUTES_KEY = Symbol('routes');
|
|
41
|
+
const MIDDLEWARE_KEY = Symbol('middlewares');
|
|
42
|
+
const PARAMS_KEY = Symbol('params');
|
|
43
|
+
const HTTP_CODE_KEY = Symbol('httpCode');
|
|
44
|
+
const HEADER_KEY = Symbol('headers');
|
|
45
|
+
const BEFORE_KEY = Symbol('before');
|
|
46
|
+
const AFTER_KEY = Symbol('after');
|
|
47
|
+
const ROLES_KEY = Symbol('roles');
|
|
48
|
+
const REDIRECT_KEY = Symbol('redirect');
|
|
49
|
+
/**
|
|
50
|
+
* Factory function that creates HTTP method decorators.
|
|
51
|
+
*
|
|
52
|
+
* @param method - HTTP method to create decorator for
|
|
53
|
+
* @returns A method decorator function
|
|
54
|
+
*/
|
|
55
|
+
function createRouteDecorator(method) {
|
|
56
|
+
return (path) => {
|
|
57
|
+
return (target, propertyKey, descriptor) => {
|
|
58
|
+
const routes = Reflect.getMetadata(ROUTES_KEY, target.constructor) || [];
|
|
59
|
+
routes.push({
|
|
60
|
+
path,
|
|
61
|
+
method,
|
|
62
|
+
handlerName: propertyKey
|
|
63
|
+
});
|
|
64
|
+
Reflect.defineMetadata(ROUTES_KEY, routes, target.constructor);
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Decorator for GET HTTP method routes.
|
|
70
|
+
*
|
|
71
|
+
* @param path - URL path for the route
|
|
72
|
+
* @returns Method decorator
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* @Get('/users')
|
|
77
|
+
* getUsers() {
|
|
78
|
+
* return this.userService.findAll();
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
exports.Get = createRouteDecorator('get');
|
|
83
|
+
/**
|
|
84
|
+
* Decorator for POST HTTP method routes.
|
|
85
|
+
*
|
|
86
|
+
* @param path - URL path for the route
|
|
87
|
+
* @returns Method decorator
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* @Post('/users')
|
|
92
|
+
* createUser(@Body() userData: any) {
|
|
93
|
+
* return this.userService.create(userData);
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
exports.Post = createRouteDecorator('post');
|
|
98
|
+
/**
|
|
99
|
+
* Decorator for PUT HTTP method routes.
|
|
100
|
+
*
|
|
101
|
+
* @param path - URL path for the route
|
|
102
|
+
* @returns Method decorator
|
|
103
|
+
*/
|
|
104
|
+
exports.Put = createRouteDecorator('put');
|
|
105
|
+
/**
|
|
106
|
+
* Decorator for PATCH HTTP method routes.
|
|
107
|
+
*
|
|
108
|
+
* @param path - URL path for the route
|
|
109
|
+
* @returns Method decorator
|
|
110
|
+
*/
|
|
111
|
+
exports.Patch = createRouteDecorator('patch');
|
|
112
|
+
/**
|
|
113
|
+
* Decorator for DELETE HTTP method routes.
|
|
114
|
+
*
|
|
115
|
+
* @param path - URL path for the route
|
|
116
|
+
* @returns Method decorator
|
|
117
|
+
*/
|
|
118
|
+
exports.Delete = createRouteDecorator('delete');
|
|
119
|
+
/**
|
|
120
|
+
* Decorator for OPTIONS HTTP method routes.
|
|
121
|
+
*
|
|
122
|
+
* @param path - URL path for the route
|
|
123
|
+
* @returns Method decorator
|
|
124
|
+
*/
|
|
125
|
+
exports.Options = createRouteDecorator('options');
|
|
126
|
+
/**
|
|
127
|
+
* Decorator for HEAD HTTP method routes.
|
|
128
|
+
*
|
|
129
|
+
* @param path - URL path for the route
|
|
130
|
+
* @returns Method decorator
|
|
131
|
+
*/
|
|
132
|
+
exports.Head = createRouteDecorator('head');
|
|
133
|
+
/**
|
|
134
|
+
* Decorator for TRACE HTTP method routes.
|
|
135
|
+
*
|
|
136
|
+
* @param path - URL path for the route
|
|
137
|
+
* @returns Method decorator
|
|
138
|
+
*/
|
|
139
|
+
exports.Trace = createRouteDecorator('trace');
|
|
140
|
+
/**
|
|
141
|
+
* Decorator for CONNECT HTTP method routes.
|
|
142
|
+
*
|
|
143
|
+
* @param path - URL path for the route
|
|
144
|
+
* @returns Method decorator
|
|
145
|
+
*/
|
|
146
|
+
exports.Connect = createRouteDecorator('connect');
|
|
147
|
+
/**
|
|
148
|
+
* Decorator that marks a class as a controller with a base path.
|
|
149
|
+
* Used as the entry point for routing configuration.
|
|
150
|
+
*
|
|
151
|
+
* @param basePath - Base URL path for all routes in this controller
|
|
152
|
+
* @returns Class decorator
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* @Controller('/api/users')
|
|
157
|
+
* class UserController {
|
|
158
|
+
* // Controller methods...
|
|
159
|
+
* }
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
function Controller(basePath) {
|
|
163
|
+
return target => {
|
|
164
|
+
Reflect.defineMetadata('basePath', basePath, target);
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Decorator that applies middleware to a controller method.
|
|
169
|
+
* Multiple middlewares can be applied and will execute in order.
|
|
170
|
+
*
|
|
171
|
+
* @param middlewares - Express middleware functions to apply
|
|
172
|
+
* @returns Method decorator
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* @Get('/protected')
|
|
177
|
+
* @Use(authMiddleware, loggingMiddleware)
|
|
178
|
+
* getProtectedResource() {
|
|
179
|
+
* // This route is protected by auth middleware
|
|
180
|
+
* }
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
function Use(...middlewares) {
|
|
184
|
+
return (target, propertyKey, _descriptor) => {
|
|
185
|
+
const existing = Reflect.getMetadata(MIDDLEWARE_KEY, target, propertyKey) || [];
|
|
186
|
+
Reflect.defineMetadata(MIDDLEWARE_KEY, [...existing, ...middlewares], target, propertyKey);
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Factory function that creates parameter decorators.
|
|
191
|
+
*
|
|
192
|
+
* @param type - Parameter type to extract from request
|
|
193
|
+
* @param key - Optional fixed key for extraction
|
|
194
|
+
* @returns Parameter decorator function
|
|
195
|
+
*/
|
|
196
|
+
function createParamDecorator(type, key) {
|
|
197
|
+
return (paramKey) => {
|
|
198
|
+
return (target, propertyKey, parameterIndex) => {
|
|
199
|
+
const params = Reflect.getMetadata(PARAMS_KEY, target, propertyKey) || [];
|
|
200
|
+
// Ensure parameters are ordered by index
|
|
201
|
+
params.push({ index: parameterIndex, type, key: paramKey || key });
|
|
202
|
+
params.sort((a, b) => a.index - b.index);
|
|
203
|
+
Reflect.defineMetadata(PARAMS_KEY, params, target, propertyKey);
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Decorator that extracts query parameters from request.
|
|
209
|
+
*
|
|
210
|
+
* @param paramKey - Optional key to extract specific query parameter
|
|
211
|
+
* @returns Parameter decorator
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```ts
|
|
215
|
+
* @Get('/search')
|
|
216
|
+
* search(@Query('term') searchTerm: string) {
|
|
217
|
+
* // searchTerm will contain the value of req.query.term
|
|
218
|
+
* }
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
exports.Query = createParamDecorator('query');
|
|
222
|
+
/**
|
|
223
|
+
* Decorator that extracts route parameters from request.
|
|
224
|
+
*
|
|
225
|
+
* @param paramKey - Optional key to extract specific route parameter
|
|
226
|
+
* @returns Parameter decorator
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```ts
|
|
230
|
+
* @Get('/users/:id')
|
|
231
|
+
* getUser(@Param('id') userId: string) {
|
|
232
|
+
* // userId will contain the value of req.params.id
|
|
233
|
+
* }
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
exports.Param = createParamDecorator('param');
|
|
237
|
+
/**
|
|
238
|
+
* Decorator that extracts body or body property from request.
|
|
239
|
+
*
|
|
240
|
+
* @param paramKey - Optional key to extract specific body property
|
|
241
|
+
* @returns Parameter decorator
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* @Post('/users')
|
|
246
|
+
* createUser(@Body() userData: any) {
|
|
247
|
+
* // userData will contain the entire req.body
|
|
248
|
+
* }
|
|
249
|
+
*
|
|
250
|
+
* @Post('/update')
|
|
251
|
+
* updateName(@Body('name') name: string) {
|
|
252
|
+
* // name will contain the value of req.body.name
|
|
253
|
+
* }
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
exports.Body = createParamDecorator('body');
|
|
257
|
+
/**
|
|
258
|
+
* Decorator that injects the entire request object.
|
|
259
|
+
*
|
|
260
|
+
* @returns Parameter decorator
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```ts
|
|
264
|
+
* @Get('/complex')
|
|
265
|
+
* complex(@Req() req: Request) {
|
|
266
|
+
* // Access the full request object
|
|
267
|
+
* console.log(req.headers);
|
|
268
|
+
* }
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
exports.Req = createParamDecorator('req');
|
|
272
|
+
/**
|
|
273
|
+
* Decorator that injects the response object.
|
|
274
|
+
*
|
|
275
|
+
* @returns Parameter decorator
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* @Get('/custom')
|
|
280
|
+
* custom(@Res() res: Response) {
|
|
281
|
+
* // Direct access to response object
|
|
282
|
+
* return res.status(201).send('Created');
|
|
283
|
+
* }
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
exports.Res = createParamDecorator('res');
|
|
287
|
+
/**
|
|
288
|
+
* Decorator that sets a custom HTTP status code for a response.
|
|
289
|
+
*
|
|
290
|
+
* @param status - HTTP status code to use
|
|
291
|
+
* @returns Method decorator
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* @Post('/users')
|
|
296
|
+
* @HttpCode(201)
|
|
297
|
+
* createUser(@Body() userData: any) {
|
|
298
|
+
* // Response will have 201 Created status code
|
|
299
|
+
* return { id: '123', ...userData };
|
|
300
|
+
* }
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
function HttpCode(status) {
|
|
304
|
+
return (target, propertyKey, _descriptor) => {
|
|
305
|
+
Reflect.defineMetadata(HTTP_CODE_KEY, status, target, propertyKey);
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Decorator that adds a custom HTTP header to the response.
|
|
310
|
+
*
|
|
311
|
+
* @param name - Header name
|
|
312
|
+
* @param value - Header value
|
|
313
|
+
* @returns Method decorator
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```ts
|
|
317
|
+
* @Get('/data')
|
|
318
|
+
* @Header('Cache-Control', 'max-age=60')
|
|
319
|
+
* getData() {
|
|
320
|
+
* // Response will include the Cache-Control header
|
|
321
|
+
* return { data: '...' };
|
|
322
|
+
* }
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
function Header(name, value) {
|
|
326
|
+
return (target, propertyKey, _descriptor) => {
|
|
327
|
+
const headers = Reflect.getMetadata(HEADER_KEY, target, propertyKey) || {};
|
|
328
|
+
headers[name] = value;
|
|
329
|
+
Reflect.defineMetadata(HEADER_KEY, headers, target, propertyKey);
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Decorator that registers a function to run before route handler execution.
|
|
334
|
+
* Useful for pre-processing or logging.
|
|
335
|
+
*
|
|
336
|
+
* @param fn - Function to execute before the handler
|
|
337
|
+
* @returns Method decorator
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```ts
|
|
341
|
+
* @Get('/users/:id')
|
|
342
|
+
* @Before((req, res) => console.log(`Accessing user ${req.params.id}`))
|
|
343
|
+
* getUser(@Param('id') id: string) {
|
|
344
|
+
* // Function will log before this handler runs
|
|
345
|
+
* }
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
348
|
+
function Before(fn) {
|
|
349
|
+
return (target, propertyKey, _descriptor) => {
|
|
350
|
+
const hooks = Reflect.getMetadata(BEFORE_KEY, target, propertyKey) || [];
|
|
351
|
+
hooks.push(fn);
|
|
352
|
+
Reflect.defineMetadata(BEFORE_KEY, hooks, target, propertyKey);
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Decorator that registers a function to run after route handler execution.
|
|
357
|
+
* Can access the handler's result.
|
|
358
|
+
*
|
|
359
|
+
* @param fn - Function to execute after the handler
|
|
360
|
+
* @returns Method decorator
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```ts
|
|
364
|
+
* @Get('/users/:id')
|
|
365
|
+
* @After((req, res, result) => console.log(`User data sent: ${JSON.stringify(result)}`))
|
|
366
|
+
* getUser(@Param('id') id: string) {
|
|
367
|
+
* // After this handler, the function will log the returned data
|
|
368
|
+
* return { id, name: 'Example' };
|
|
369
|
+
* }
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
function After(fn) {
|
|
373
|
+
return (target, propertyKey, _descriptor) => {
|
|
374
|
+
const hooks = Reflect.getMetadata(AFTER_KEY, target, propertyKey) || [];
|
|
375
|
+
hooks.push(fn);
|
|
376
|
+
Reflect.defineMetadata(AFTER_KEY, hooks, target, propertyKey);
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Decorator that requires specific roles for accessing a route.
|
|
381
|
+
* Must be used with authentication middleware.
|
|
382
|
+
*
|
|
383
|
+
* @param roles - List of roles that can access this route
|
|
384
|
+
* @returns Method decorator
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```ts
|
|
388
|
+
* @Get('/admin/settings')
|
|
389
|
+
* @Roles('admin', 'superuser')
|
|
390
|
+
* getSettings() {
|
|
391
|
+
* // Only admins and superusers can access
|
|
392
|
+
* return { settings: [...] };
|
|
393
|
+
* }
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
396
|
+
function Roles(...roles) {
|
|
397
|
+
return (target, propertyKey, _descriptor) => {
|
|
398
|
+
Reflect.defineMetadata(ROLES_KEY, roles, target, propertyKey);
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Decorator that redirects to another URL.
|
|
403
|
+
*
|
|
404
|
+
* @param url - URL to redirect to (can be absolute or relative)
|
|
405
|
+
* @param statusCode - HTTP status code for redirect (default: 302)
|
|
406
|
+
* @returns Method decorator
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* ```ts
|
|
410
|
+
* @Get('/old-path')
|
|
411
|
+
* @Redirect('/new-path', 301)
|
|
412
|
+
* redirectToNewPath() {
|
|
413
|
+
* // This method won't be executed; automatic redirect happens
|
|
414
|
+
* }
|
|
415
|
+
*
|
|
416
|
+
* @Get('/dynamic-redirect')
|
|
417
|
+
* @Redirect()
|
|
418
|
+
* getDynamicRedirect() {
|
|
419
|
+
* // Return an object with url and optionally statusCode
|
|
420
|
+
* return { url: '/calculated-path', statusCode: 307 };
|
|
421
|
+
* }
|
|
422
|
+
* ```
|
|
423
|
+
*/
|
|
424
|
+
function Redirect(url, statusCode = 302) {
|
|
425
|
+
return (target, propertyKey, descriptor) => {
|
|
426
|
+
// Store the redirect information in metadata
|
|
427
|
+
Reflect.defineMetadata(REDIRECT_KEY, { url, statusCode }, target, propertyKey);
|
|
428
|
+
return descriptor;
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Registers all controller classes with the provided router.
|
|
433
|
+
* This function processes all decorators and sets up the Express routes.
|
|
434
|
+
*
|
|
435
|
+
* @param router - Express router instance
|
|
436
|
+
* @param controllers - Array of controller classes
|
|
437
|
+
*/
|
|
438
|
+
function registerControllers(router, controllers) {
|
|
439
|
+
controllers.forEach(ControllerClass => {
|
|
440
|
+
const instance = new ControllerClass();
|
|
441
|
+
const basePath = Reflect.getMetadata('basePath', ControllerClass) || '';
|
|
442
|
+
const routes = Reflect.getMetadata(ROUTES_KEY, ControllerClass) || [];
|
|
443
|
+
routes.forEach(({ path, method, handlerName }) => {
|
|
444
|
+
const middlewares = Reflect.getMetadata(MIDDLEWARE_KEY, instance, handlerName) || [];
|
|
445
|
+
const params = Reflect.getMetadata(PARAMS_KEY, instance, handlerName) || [];
|
|
446
|
+
const httpCode = Reflect.getMetadata(HTTP_CODE_KEY, instance, handlerName);
|
|
447
|
+
const headers = Reflect.getMetadata(HEADER_KEY, instance, handlerName) || {};
|
|
448
|
+
const beforeHooks = Reflect.getMetadata(BEFORE_KEY, instance, handlerName) || [];
|
|
449
|
+
const afterHooks = Reflect.getMetadata(AFTER_KEY, instance, handlerName) || [];
|
|
450
|
+
const roles = Reflect.getMetadata(ROLES_KEY, instance, handlerName) || [];
|
|
451
|
+
const redirect = Reflect.getMetadata(REDIRECT_KEY, instance, handlerName);
|
|
452
|
+
const handler = async (req, res, next) => {
|
|
453
|
+
var _a, _b, _c, _d;
|
|
454
|
+
try {
|
|
455
|
+
// Handle roles-based access control
|
|
456
|
+
if (roles.length && !((_b = (_a = req === null || req === void 0 ? void 0 : req.user) === null || _a === void 0 ? void 0 : _a.roles) === null || _b === void 0 ? void 0 : _b.some((role) => roles.includes(role)))) {
|
|
457
|
+
res.status(http_status_codes_1.HttpStatusCodes.FORBIDDEN).json(new exception_utils_1.ForbiddenException('Forbidden Insufficient Roles'));
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
// Process static redirect if configured
|
|
461
|
+
if (redirect && redirect.url) {
|
|
462
|
+
return res.redirect(redirect.statusCode, redirect.url);
|
|
463
|
+
}
|
|
464
|
+
for (const fn of beforeHooks)
|
|
465
|
+
await fn(req, res);
|
|
466
|
+
const args = [];
|
|
467
|
+
if (params.length) {
|
|
468
|
+
params.forEach(({ index, type, key }) => {
|
|
469
|
+
var _a;
|
|
470
|
+
switch (type) {
|
|
471
|
+
case 'query':
|
|
472
|
+
args[index] = key ? req.query[key] : req.query;
|
|
473
|
+
break;
|
|
474
|
+
case 'param':
|
|
475
|
+
args[index] = key ? req.params[key] : req.params;
|
|
476
|
+
break;
|
|
477
|
+
case 'body':
|
|
478
|
+
args[index] = key ? (_a = req.body) === null || _a === void 0 ? void 0 : _a[key] : req.body;
|
|
479
|
+
break;
|
|
480
|
+
case 'req':
|
|
481
|
+
args[index] = req;
|
|
482
|
+
break;
|
|
483
|
+
case 'res':
|
|
484
|
+
args[index] = res;
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
const result = instance[handlerName](...args);
|
|
490
|
+
// Support both sync and async handlers
|
|
491
|
+
const awaited = result instanceof Promise ? await result : result;
|
|
492
|
+
// Handle dynamic redirects
|
|
493
|
+
if (redirect && awaited && typeof awaited === 'object' && 'url' in awaited) {
|
|
494
|
+
const redirectUrl = awaited.url;
|
|
495
|
+
const redirectStatus = awaited.statusCode || redirect.statusCode;
|
|
496
|
+
return res.redirect(redirectStatus, redirectUrl);
|
|
497
|
+
}
|
|
498
|
+
if (!res.headersSent && typeof awaited !== 'undefined') {
|
|
499
|
+
if (httpCode)
|
|
500
|
+
(_c = res.status) === null || _c === void 0 ? void 0 : _c.call(res, httpCode);
|
|
501
|
+
for (const [k, v] of Object.entries(headers))
|
|
502
|
+
(_d = res.set) === null || _d === void 0 ? void 0 : _d.call(res, k, v);
|
|
503
|
+
res.json(awaited);
|
|
504
|
+
}
|
|
505
|
+
for (const fn of afterHooks)
|
|
506
|
+
await fn(req, res, awaited);
|
|
507
|
+
}
|
|
508
|
+
catch (err) {
|
|
509
|
+
next(err);
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
router[method](basePath + path, ...middlewares, handler);
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
//# sourceMappingURL=decorators.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.utils.js","sourceRoot":"","sources":["../../../src/utils/decorators.utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;AA4KH,gCAIC;AAkBD,kBAKC;AA0HD,4BAIC;AAmBD,wBAMC;AAkBD,wBAMC;AAmBD,sBAMC;AAmBD,sBAIC;AAyBD,4BAMC;AASD,kDAkFC;AA3hBD,4BAA0B;AAC1B,2DAAsD;AACtD,uDAAuD;AAEvD,gBAAgB;AAChB,MAAM,UAAU,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;AACpC,MAAM,cAAc,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AAC7C,MAAM,UAAU,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;AACpC,MAAM,aAAa,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AACzC,MAAM,UAAU,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AACrC,MAAM,UAAU,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;AACpC,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;AAClC,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;AAClC,MAAM,YAAY,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AA+BxC;;;;;GAKG;AACH,SAAS,oBAAoB,CAAC,MAAkB;IAC9C,OAAO,CAAC,IAAY,EAAmB,EAAE;QACvC,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,EAAE;YACzC,MAAM,MAAM,GAAsB,OAAO,CAAC,WAAW,CAAC,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;YAC5F,MAAM,CAAC,IAAI,CAAC;gBACV,IAAI;gBACJ,MAAM;gBACN,WAAW,EAAE,WAAqB;aACnC,CA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O,CAAC,CAAC;QACpE,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/* eslint-disable @typescript-eslint/no-unused-vars */\n\nimport type { RequestHandler, Router } from 'express';\nimport 'reflect-metadata';\nimport { HttpStatusCodes } from './http-status-codes';\nimport { ForbiddenException } from './exception.utils';\n\n// Metadata keys\nconst ROUTES_KEY = Symbol('routes');\nconst MIDDLEWARE_KEY = Symbol('middlewares');\nconst PARAMS_KEY = Symbol('params');\nconst HTTP_CODE_KEY = Symbol('httpCode');\nconst HEADER_KEY = Symbol('headers');\nconst BEFORE_KEY = Symbol('before');\nconst AFTER_KEY = Symbol('after');\nconst ROLES_KEY = Symbol('roles');\nconst REDIRECT_KEY = Symbol('redirect');\n\n/**\n * Supported HTTP methods for route decorators\n */\ntype HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'options' | 'head' | 'trace' | 'connect';\n\n/**\n * Represents a route definition for controller methods\n */\ninterface RouteDefinition {\n /** The URL path for this route */\n path: string;\n /** HTTP method for this route */\n method: HttpMethod;\n /** Name of the handler method in the controller class */\n handlerName: string;\n}\n\n/**\n * Parameter decoration definition for method parameters\n */\ninterface ParamDefinition {\n /** Parameter position in method signature */\n index: number;\n /** Type of parameter (query, body, etc.) */\n type: 'query' | 'param' | 'body' | 'req' | 'res';\n /** Optional key for extracting specific property */\n key?: string;\n}\n\n/**\n * Factory function that creates HTTP method decorators.\n *\n * @param method - HTTP method to create decorator for\n * @returns A method decorator function\n */\nfunction createRouteDecorator(method: HttpMethod) {\n return (path: string): MethodDecorator => {\n return (target, propertyKey, descriptor) => {\n const routes: RouteDefinition[] = Reflect.getMetadata(ROUTES_KEY, target.constructor) || [];\n routes.push({\n path,\n method,\n handlerName: propertyKey as string\n });\n Reflect.defineMetadata(ROUTES_KEY, routes, target.constructor);\n };\n };\n}\n\n/**\n * Decorator for GET HTTP method routes.\n *\n * @param path - URL path for the route\n * @returns Method decorator\n *\n * @example\n * ```ts\n * @Get('/users')\n * getUsers() {\n * return this.userService.findAll();\n * }\n * ```\n */\nexport const Get = createRouteDecorator('get');\n\n/**\n * Decorator for POST HTTP method routes.\n *\n * @param path - URL path for the route\n * @returns Method decorator\n *\n * @example\n * ```ts\n * @Post('/users')\n * createUser(@Body() userData: any) {\n * return this.userService.create(userData);\n * }\n * ```\n */\nexport const Post = createRouteDecorator('post');\n\n/**\n * Decorator for PUT HTTP method routes.\n *\n * @param path - URL path for the route\n * @returns Method decorator\n */\nexport const Put = createRouteDecorator('put');\n\n/**\n * Decorator for PATCH HTTP method routes.\n *\n * @param path - URL path for the route\n * @returns Method decorator\n */\nexport const Patch = createRouteDecorator('patch');\n\n/**\n * Decorator for DELETE HTTP method routes.\n *\n * @param path - URL path for the route\n * @returns Method decorator\n */\nexport const Delete = createRouteDecorator('delete');\n\n/**\n * Decorator for OPTIONS HTTP method routes.\n *\n * @param path - URL path for the route\n * @returns Method decorator\n */\nexport const Options = createRouteDecorator('options');\n\n/**\n * Decorator for HEAD HTTP method routes.\n *\n * @param path - URL path for the route\n * @returns Method decorator\n */\nexport const Head = createRouteDecorator('head');\n\n/**\n * Decorator for TRACE HTTP method routes.\n *\n * @param path - URL path for the route\n * @returns Method decorator\n */\nexport const Trace = createRouteDecorator('trace');\n\n/**\n * Decorator for CONNECT HTTP method routes.\n *\n * @param path - URL path for the route\n * @returns Method decorator\n */\nexport const Connect = createRouteDecorator('connect');\n\n/**\n * Decorator that marks a class as a controller with a base path.\n * Used as the entry point for routing configuration.\n *\n * @param basePath - Base URL path for all routes in this controller\n * @returns Class decorator\n *\n * @example\n * ```ts\n * @Controller('/api/users')\n * class UserController {\n * // Controller methods...\n * }\n * ```\n */\nexport function Controller(basePath: string): ClassDecorator {\n return target => {\n Reflect.defineMetadata('basePath', basePath, target);\n };\n}\n\n/**\n * Decorator that applies middleware to a controller method.\n * Multiple middlewares can be applied and will execute in order.\n *\n * @param middlewares - Express middleware functions to apply\n * @returns Method decorator\n *\n * @example\n * ```ts\n * @Get('/protected')\n * @Use(authMiddleware, loggingMiddleware)\n * getProtectedResource() {\n * // This route is protected by auth middleware\n * }\n * ```\n */\nexport function Use(...middlewares: RequestHandler[]): MethodDecorator {\n return (target, propertyKey, _descriptor) => {\n const existing: RequestHandler[] = Reflect.getMetadata(MIDDLEWARE_KEY, target, propertyKey as string) || [];\n Reflect.defineMetadata(MIDDLEWARE_KEY, [...existing, ...middlewares], target, propertyKey as string);\n };\n}\n\n/**\n * Factory function that creates parameter decorators.\n *\n * @param type - Parameter type to extract from request\n * @param key - Optional fixed key for extraction\n * @returns Parameter decorator function\n */\nfunction createParamDecorator(type: ParamDefinition['type'], key?: string) {\n return (paramKey?: string): ParameterDecorator => {\n return (target, propertyKey, parameterIndex) => {\n const params: ParamDefinition[] = Reflect.getMetadata(PARAMS_KEY, target, propertyKey as string) || [];\n // Ensure parameters are ordered by index\n params.push({ index: parameterIndex, type, key: paramKey || key });\n params.sort((a, b) => a.index - b.index);\n Reflect.defineMetadata(PARAMS_KEY, params, target, propertyKey as string);\n };\n };\n}\n\n/**\n * Decorator that extracts query parameters from request.\n *\n * @param paramKey - Optional key to extract specific query parameter\n * @returns Parameter decorator\n *\n * @example\n * ```ts\n * @Get('/search')\n * search(@Query('term') searchTerm: string) {\n * // searchTerm will contain the value of req.query.term\n * }\n * ```\n */\nexport const Query = createParamDecorator('query');\n\n/**\n * Decorator that extracts route parameters from request.\n *\n * @param paramKey - Optional key to extract specific route parameter\n * @returns Parameter decorator\n *\n * @example\n * ```ts\n * @Get('/users/:id')\n * getUser(@Param('id') userId: string) {\n * // userId will contain the value of req.params.id\n * }\n * ```\n */\nexport const Param = createParamDecorator('param');\n\n/**\n * Decorator that extracts body or body property from request.\n *\n * @param paramKey - Optional key to extract specific body property\n * @returns Parameter decorator\n *\n * @example\n * ```ts\n * @Post('/users')\n * createUser(@Body() userData: any) {\n * // userData will contain the entire req.body\n * }\n *\n * @Post('/update')\n * updateName(@Body('name') name: string) {\n * // name will contain the value of req.body.name\n * }\n * ```\n */\nexport const Body = createParamDecorator('body');\n\n/**\n * Decorator that injects the entire request object.\n *\n * @returns Parameter decorator\n *\n * @example\n * ```ts\n * @Get('/complex')\n * complex(@Req() req: Request) {\n * // Access the full request object\n * console.log(req.headers);\n * }\n * ```\n */\nexport const Req = createParamDecorator('req');\n\n/**\n * Decorator that injects the response object.\n *\n * @returns Parameter decorator\n *\n * @example\n * ```ts\n * @Get('/custom')\n * custom(@Res() res: Response) {\n * // Direct access to response object\n * return res.status(201).send('Created');\n * }\n * ```\n */\nexport const Res = createParamDecorator('res');\n\n/**\n * Decorator that sets a custom HTTP status code for a response.\n *\n * @param status - HTTP status code to use\n * @returns Method decorator\n *\n * @example\n * ```ts\n * @Post('/users')\n * @HttpCode(201)\n * createUser(@Body() userData: any) {\n * // Response will have 201 Created status code\n * return { id: '123', ...userData };\n * }\n * ```\n */\nexport function HttpCode(status: number): MethodDecorator {\n return (target, propertyKey, _descriptor) => {\n Reflect.defineMetadata(HTTP_CODE_KEY, status, target, propertyKey as string);\n };\n}\n\n/**\n * Decorator that adds a custom HTTP header to the response.\n *\n * @param name - Header name\n * @param value - Header value\n * @returns Method decorator\n *\n * @example\n * ```ts\n * @Get('/data')\n * @Header('Cache-Control', 'max-age=60')\n * getData() {\n * // Response will include the Cache-Control header\n * return { data: '...' };\n * }\n * ```\n */\nexport function Header(name: string, value: string): MethodDecorator {\n return (target, propertyKey, _descriptor) => {\n const headers: Record<string, string> = Reflect.getMetadata(HEADER_KEY, target, propertyKey as string) || {};\n headers[name] = value;\n Reflect.defineMetadata(HEADER_KEY, headers, target, propertyKey as string);\n };\n}\n\n/**\n * Decorator that registers a function to run before route handler execution.\n * Useful for pre-processing or logging.\n *\n * @param fn - Function to execute before the handler\n * @returns Method decorator\n *\n * @example\n * ```ts\n * @Get('/users/:id')\n * @Before((req, res) => console.log(`Accessing user ${req.params.id}`))\n * getUser(@Param('id') id: string) {\n * // Function will log before this handler runs\n * }\n * ```\n */\nexport function Before(fn: Function): MethodDecorator {\n return (target, propertyKey, _descriptor) => {\n const hooks: Function[] = Reflect.getMetadata(BEFORE_KEY, target, propertyKey as string) || [];\n hooks.push(fn);\n Reflect.defineMetadata(BEFORE_KEY, hooks, target, propertyKey as string);\n };\n}\n\n/**\n * Decorator that registers a function to run after route handler execution.\n * Can access the handler's result.\n *\n * @param fn - Function to execute after the handler\n * @returns Method decorator\n *\n * @example\n * ```ts\n * @Get('/users/:id')\n * @After((req, res, result) => console.log(`User data sent: ${JSON.stringify(result)}`))\n * getUser(@Param('id') id: string) {\n * // After this handler, the function will log the returned data\n * return { id, name: 'Example' };\n * }\n * ```\n */\nexport function After(fn: Function): MethodDecorator {\n return (target, propertyKey, _descriptor) => {\n const hooks: Function[] = Reflect.getMetadata(AFTER_KEY, target, propertyKey as string) || [];\n hooks.push(fn);\n Reflect.defineMetadata(AFTER_KEY, hooks, target, propertyKey as string);\n };\n}\n\n/**\n * Decorator that requires specific roles for accessing a route.\n * Must be used with authentication middleware.\n *\n * @param roles - List of roles that can access this route\n * @returns Method decorator\n *\n * @example\n * ```ts\n * @Get('/admin/settings')\n * @Roles('admin', 'superuser')\n * getSettings() {\n * // Only admins and superusers can access\n * return { settings: [...] };\n * }\n * ```\n */\nexport function Roles(...roles: string[]): MethodDecorator {\n return (target, propertyKey, _descriptor) => {\n Reflect.defineMetadata(ROLES_KEY, roles, target, propertyKey as string);\n };\n}\n\n/**\n * Decorator that redirects to another URL.\n *\n * @param url - URL to redirect to (can be absolute or relative)\n * @param statusCode - HTTP status code for redirect (default: 302)\n * @returns Method decorator\n *\n * @example\n * ```ts\n * @Get('/old-path')\n * @Redirect('/new-path', 301)\n * redirectToNewPath() {\n * // This method won't be executed; automatic redirect happens\n * }\n *\n * @Get('/dynamic-redirect')\n * @Redirect()\n * getDynamicRedirect() {\n * // Return an object with url and optionally statusCode\n * return { url: '/calculated-path', statusCode: 307 };\n * }\n * ```\n */\nexport function Redirect(url?: string, statusCode: number = 302): MethodDecorator {\n return (target, propertyKey, descriptor) => {\n // Store the redirect information in metadata\n Reflect.defineMetadata(REDIRECT_KEY, { url, statusCode }, target, propertyKey as string);\n return descriptor;\n };\n}\n\n/**\n * Registers all controller classes with the provided router.\n * This function processes all decorators and sets up the Express routes.\n *\n * @param router - Express router instance\n * @param controllers - Array of controller classes\n */\nexport function registerControllers(router: Router, controllers: any[]) {\n controllers.forEach(ControllerClass => {\n const instance = new ControllerClass();\n const basePath: string = Reflect.getMetadata('basePath', ControllerClass) || '';\n const routes: RouteDefinition[] = Reflect.getMetadata(ROUTES_KEY, ControllerClass) || [];\n\n routes.forEach(({ path, method, handlerName }) => {\n const middlewares: RequestHandler[] = Reflect.getMetadata(MIDDLEWARE_KEY, instance, handlerName) || [];\n const params: ParamDefinition[] = Reflect.getMetadata(PARAMS_KEY, instance, handlerName) || [];\n\n const httpCode: number | undefined = Reflect.getMetadata(HTTP_CODE_KEY, instance, handlerName);\n const headers: Record<string, string> = Reflect.getMetadata(HEADER_KEY, instance, handlerName) || {};\n const beforeHooks: Function[] = Reflect.getMetadata(BEFORE_KEY, instance, handlerName) || [];\n const afterHooks: Function[] = Reflect.getMetadata(AFTER_KEY, instance, handlerName) || [];\n const roles: string[] = Reflect.getMetadata(ROLES_KEY, instance, handlerName) || [];\n const redirect: { url?: string; statusCode: number } | undefined = Reflect.getMetadata(\n REDIRECT_KEY,\n instance,\n handlerName\n );\n\n const handler: RequestHandler = async (req, res, next) => {\n try {\n // Handle roles-based access control\n if (roles.length && !(req as any)?.user?.roles?.some((role: string) => roles.includes(role))) {\n res.status(HttpStatusCodes.FORBIDDEN).json(new ForbiddenException('Forbidden Insufficient Roles'));\n return;\n }\n\n // Process static redirect if configured\n if (redirect && redirect.url) {\n return res.redirect(redirect.statusCode, redirect.url);\n }\n\n for (const fn of beforeHooks) await fn(req, res);\n const args: any[] = [];\n if (params.length) {\n params.forEach(({ index, type, key }) => {\n switch (type) {\n case 'query':\n args[index] = key ? req.query[key] : req.query;\n break;\n case 'param':\n args[index] = key ? req.params[key] : req.params;\n break;\n case 'body':\n args[index] = key ? req.body?.[key] : req.body;\n break;\n case 'req':\n args[index] = req;\n break;\n case 'res':\n args[index] = res;\n break;\n }\n });\n }\n const result = (instance as any)[handlerName](...args);\n // Support both sync and async handlers\n const awaited = result instanceof Promise ? await result : result;\n\n // Handle dynamic redirects\n if (redirect && awaited && typeof awaited === 'object' && 'url' in awaited) {\n const redirectUrl = awaited.url as string;\n const redirectStatus = (awaited.statusCode as number) || redirect.statusCode;\n return res.redirect(redirectStatus, redirectUrl);\n }\n\n if (!res.headersSent && typeof awaited !== 'undefined') {\n if (httpCode) res.status?.(httpCode);\n for (const [k, v] of Object.entries(headers)) res.set?.(k, v);\n res.json(awaited);\n }\n for (const fn of afterHooks) await fn(req, res, awaited);\n } catch (err) {\n next(err);\n }\n };\n\n (router as any)[method](basePath + path, ...middlewares, handler);\n });\n });\n}\n"]}
|