@catbee/utils 2.0.0-next.0 → 2.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -16
- package/array/index.cjs +180 -71
- package/array/index.d.ts +293 -1
- package/array/index.mjs +171 -72
- package/async/index.cjs +92 -36
- package/async/index.d.ts +275 -1
- package/async/index.mjs +92 -36
- package/cache/index.cjs +1 -1
- package/cache/index.d.ts +155 -1
- package/cache/index.mjs +2 -2
- package/config/index.cjs +78 -64
- package/config/index.d.ts +64 -2
- package/config/index.mjs +76 -64
- package/context-store/index.d.ts +192 -1
- package/crypto/index.d.ts +163 -1
- package/date/index.cjs +46 -1
- package/date/index.d.ts +190 -1
- package/date/index.mjs +45 -2
- package/decorators/index.cjs +1156 -18
- package/decorators/index.d.ts +684 -1
- package/decorators/index.mjs +1156 -18
- package/dir/index.cjs +4 -3
- package/dir/index.d.ts +195 -1
- package/dir/index.mjs +4 -3
- package/env/index.cjs +10 -26
- package/env/index.d.ts +379 -1
- package/env/index.mjs +10 -26
- package/exception/index.d.ts +232 -1
- package/fs/index.cjs +70 -36
- package/fs/index.d.ts +205 -1
- package/fs/index.mjs +64 -34
- package/http-status-codes/index.d.ts +267 -1
- package/id/index.d.ts +37 -1
- package/index.cjs +3 -3
- package/index.d.ts +1 -1
- package/index.mjs +1 -1
- package/logger/index.cjs +11 -11
- package/logger/index.d.ts +189 -1
- package/logger/index.mjs +12 -12
- package/middleware/index.d.ts +103 -1
- package/obj/index.cjs +150 -162
- package/obj/index.d.ts +136 -1
- package/obj/index.mjs +150 -162
- package/package.json +11 -11
- package/performance/index.cjs +2 -2
- package/performance/index.d.ts +138 -1
- package/performance/index.mjs +2 -2
- package/request/index.cjs +1 -1
- package/request/index.d.ts +241 -2
- package/request/index.mjs +1 -1
- package/response/index.d.ts +318 -2
- package/server/index.cjs +27 -23
- package/server/index.d.ts +785 -4
- package/server/index.mjs +28 -23
- package/stream/index.d.ts +90 -1
- package/string/index.d.ts +102 -1
- package/type/index.cjs +1 -1
- package/type/index.d.ts +107 -1
- package/type/index.mjs +1 -1
- package/types/index.d.ts +774 -4
- package/url/index.cjs +2 -4
- package/url/index.d.ts +142 -1
- package/url/index.mjs +2 -4
- package/{validate → validation}/index.cjs +89 -42
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +32 -23
- package/{validate → validation}/index.mjs +85 -42
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/decorators.utils.d.ts +0 -705
- package/dir/dir.utils.d.ts +0 -216
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/obj.utils.d.ts +0 -156
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/decorators/index.d.ts
CHANGED
|
@@ -22,4 +22,687 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
import * as express_rate_limit from 'express-rate-limit';
|
|
26
|
+
import { RequestHandler, Router } from 'express';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Supported HTTP methods for route decorators
|
|
30
|
+
*/
|
|
31
|
+
type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'options' | 'head' | 'trace' | 'connect';
|
|
32
|
+
/**
|
|
33
|
+
* Represents a route definition for controller methods
|
|
34
|
+
*/
|
|
35
|
+
interface RouteDefinition {
|
|
36
|
+
/** The URL path for this route */
|
|
37
|
+
path: string;
|
|
38
|
+
/** HTTP method for this route */
|
|
39
|
+
method: HttpMethod;
|
|
40
|
+
/** Name of the handler method in the controller class */
|
|
41
|
+
handlerName: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Parameter decoration definition for method parameters
|
|
45
|
+
*/
|
|
46
|
+
interface ParamDefinition {
|
|
47
|
+
/** Parameter position in method signature */
|
|
48
|
+
index: number;
|
|
49
|
+
/** Type of parameter (query, body, etc.) */
|
|
50
|
+
type: 'query' | 'param' | 'body' | 'req' | 'res' | 'logger' | 'reqHeader' | 'reqId' | 'cookie';
|
|
51
|
+
/** Optional key for extracting specific property */
|
|
52
|
+
key?: string;
|
|
53
|
+
/** Optional ParamOptions for advanced extraction */
|
|
54
|
+
options?: ParamOptions;
|
|
55
|
+
}
|
|
56
|
+
type CachedRateLimiter = {
|
|
57
|
+
limiter: ReturnType<typeof express_rate_limit.rateLimit>;
|
|
58
|
+
config: string;
|
|
59
|
+
};
|
|
60
|
+
type Constructor<T = any> = new (...args: any[]) => T;
|
|
61
|
+
declare class DIContainer {
|
|
62
|
+
private readonly instances;
|
|
63
|
+
private readonly constructing;
|
|
64
|
+
private readonly propertyInjections;
|
|
65
|
+
register<T>(target: Constructor<T>): void;
|
|
66
|
+
/**
|
|
67
|
+
* Register a property injection to be resolved when the target class is instantiated
|
|
68
|
+
*/
|
|
69
|
+
registerPropertyInjection(target: object, propertyKey: string | symbol, injectClass: Constructor): void;
|
|
70
|
+
get<T>(target: Constructor<T>): T;
|
|
71
|
+
private applyPropertyInjections;
|
|
72
|
+
clear(): void;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Injectable decorator for marking classes as injectable.
|
|
76
|
+
*
|
|
77
|
+
* @returns Class decorator that marks a class as injectable and registers it with the DI container.
|
|
78
|
+
*/
|
|
79
|
+
declare function Injectable(): ClassDecorator;
|
|
80
|
+
/**
|
|
81
|
+
* Inject decorator for injecting dependencies into class properties.
|
|
82
|
+
*
|
|
83
|
+
* @param targetClass - The class to inject
|
|
84
|
+
* @returns Property decorator that injects the specified class into the property
|
|
85
|
+
*/
|
|
86
|
+
declare function Inject<T>(targetClass: new (...args: any[]) => T): PropertyDecorator;
|
|
87
|
+
/**
|
|
88
|
+
* Inject function for retrieving instances from the DI container.
|
|
89
|
+
* @param targetClass - The class to inject
|
|
90
|
+
*
|
|
91
|
+
* @returns The instance of the requested class
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* const a = inject(TestClass);
|
|
95
|
+
*/
|
|
96
|
+
declare function inject<T>(targetClass: new (...args: any[]) => T): T;
|
|
97
|
+
/**
|
|
98
|
+
* Decorator for GET HTTP method routes.
|
|
99
|
+
*
|
|
100
|
+
* @param path - URL path for the route
|
|
101
|
+
* @returns Method decorator
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* @Get('/users')
|
|
106
|
+
* getUsers() {
|
|
107
|
+
* return this.userService.findAll();
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare const Get: (path: string) => MethodDecorator;
|
|
112
|
+
/**
|
|
113
|
+
* Decorator for POST HTTP method routes.
|
|
114
|
+
*
|
|
115
|
+
* @param path - URL path for the route
|
|
116
|
+
* @returns Method decorator
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* @Post('/users')
|
|
121
|
+
* createUser(@Body() userData: any) {
|
|
122
|
+
* return this.userService.create(userData);
|
|
123
|
+
* }
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
declare const Post: (path: string) => MethodDecorator;
|
|
127
|
+
/**
|
|
128
|
+
* Decorator for PUT HTTP method routes.
|
|
129
|
+
*
|
|
130
|
+
* @param path - URL path for the route
|
|
131
|
+
* @returns Method decorator
|
|
132
|
+
*/
|
|
133
|
+
declare const Put: (path: string) => MethodDecorator;
|
|
134
|
+
/**
|
|
135
|
+
* Decorator for PATCH HTTP method routes.
|
|
136
|
+
*
|
|
137
|
+
* @param path - URL path for the route
|
|
138
|
+
* @returns Method decorator
|
|
139
|
+
*/
|
|
140
|
+
declare const Patch: (path: string) => MethodDecorator;
|
|
141
|
+
/**
|
|
142
|
+
* Decorator for DELETE HTTP method routes.
|
|
143
|
+
*
|
|
144
|
+
* @param path - URL path for the route
|
|
145
|
+
* @returns Method decorator
|
|
146
|
+
*/
|
|
147
|
+
declare const Delete: (path: string) => MethodDecorator;
|
|
148
|
+
/**
|
|
149
|
+
* Decorator for OPTIONS HTTP method routes.
|
|
150
|
+
*
|
|
151
|
+
* @param path - URL path for the route
|
|
152
|
+
* @returns Method decorator
|
|
153
|
+
*/
|
|
154
|
+
declare const Options: (path: string) => MethodDecorator;
|
|
155
|
+
/**
|
|
156
|
+
* Decorator for HEAD HTTP method routes.
|
|
157
|
+
*
|
|
158
|
+
* @param path - URL path for the route
|
|
159
|
+
* @returns Method decorator
|
|
160
|
+
*/
|
|
161
|
+
declare const Head: (path: string) => MethodDecorator;
|
|
162
|
+
/**
|
|
163
|
+
* Decorator for TRACE HTTP method routes.
|
|
164
|
+
*
|
|
165
|
+
* @param path - URL path for the route
|
|
166
|
+
* @returns Method decorator
|
|
167
|
+
*/
|
|
168
|
+
declare const Trace: (path: string) => MethodDecorator;
|
|
169
|
+
/**
|
|
170
|
+
* Decorator for CONNECT HTTP method routes.
|
|
171
|
+
*
|
|
172
|
+
* @param path - URL path for the route
|
|
173
|
+
* @returns Method decorator
|
|
174
|
+
*/
|
|
175
|
+
declare const Connect: (path: string) => MethodDecorator;
|
|
176
|
+
/**
|
|
177
|
+
* Decorator that marks a class as a controller with a base path.
|
|
178
|
+
* Used as the entry point for routing configuration.
|
|
179
|
+
*
|
|
180
|
+
* @param basePath - Base URL path for all routes in this controller
|
|
181
|
+
* @returns Class decorator
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```ts
|
|
185
|
+
* @Controller('/api/users')
|
|
186
|
+
* class UserController {
|
|
187
|
+
* // Controller methods...
|
|
188
|
+
* }
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
declare function Controller(basePath: string): ClassDecorator;
|
|
192
|
+
/**
|
|
193
|
+
* Decorator that applies middleware to a controller method or an entire controller.
|
|
194
|
+
* Multiple middlewares can be applied and will execute in order.
|
|
195
|
+
*
|
|
196
|
+
* @param middlewares - Express middleware functions to apply
|
|
197
|
+
* @returns Method decorator or Class decorator
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* @Get('/protected')
|
|
202
|
+
* @Use(authMiddleware, loggingMiddleware)
|
|
203
|
+
* getProtectedResource() {
|
|
204
|
+
* // This route is protected by auth middleware
|
|
205
|
+
* }
|
|
206
|
+
*
|
|
207
|
+
* @Controller('/api')
|
|
208
|
+
* @Use(commonMiddleware)
|
|
209
|
+
* class ApiController {
|
|
210
|
+
* // All routes in this controller use the middleware
|
|
211
|
+
* }
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
declare function Use(...middlewares: RequestHandler[]): MethodDecorator & ClassDecorator;
|
|
215
|
+
/**
|
|
216
|
+
* Options for parameter decorators
|
|
217
|
+
* @template T - Type of the parameter value after transformation
|
|
218
|
+
*
|
|
219
|
+
* @property type - Base type of the parameter (default: 'string')'
|
|
220
|
+
* @property dataType - Data structure type (single, array, object)
|
|
221
|
+
* @property delimiter - Delimiter for array types
|
|
222
|
+
* @property default - Default value if parameter is missing
|
|
223
|
+
* @property required - Whether the parameter is required
|
|
224
|
+
* @property throwError - Throw error on validation failure
|
|
225
|
+
* @property validate - Custom validation function
|
|
226
|
+
* @property transform - Custom transformation function
|
|
227
|
+
*/
|
|
228
|
+
interface ParamOptions<T = any> {
|
|
229
|
+
/** Base type of the parameter (default: 'string') */
|
|
230
|
+
type?: 'string' | 'number' | 'boolean';
|
|
231
|
+
/** Data structure type (default: 'single') */
|
|
232
|
+
dataType?: 'single' | 'array' | 'object';
|
|
233
|
+
/** Delimiter for array types (default: ',') */
|
|
234
|
+
delimiter?: string;
|
|
235
|
+
/** Default value if parameter is missing */
|
|
236
|
+
default?: T;
|
|
237
|
+
/** Whether the parameter is required (default: false) */
|
|
238
|
+
required?: boolean;
|
|
239
|
+
/** Throw error on validation failure (default: true) */
|
|
240
|
+
throwError?: boolean;
|
|
241
|
+
/** Minimum value for number type */
|
|
242
|
+
min?: number;
|
|
243
|
+
/** Maximum value for number type */
|
|
244
|
+
max?: number;
|
|
245
|
+
/** Regex pattern the value must match */
|
|
246
|
+
pattern?: RegExp;
|
|
247
|
+
/** Name of the pattern for error messages */
|
|
248
|
+
patternName?: string;
|
|
249
|
+
/** Custom validation function */
|
|
250
|
+
validate?: (value: any) => boolean;
|
|
251
|
+
/** Custom transformation function */
|
|
252
|
+
transform?: (value: any) => any;
|
|
253
|
+
}
|
|
254
|
+
declare function createParamDecorator(type: ParamDefinition['type'], key?: string): (paramKey?: string) => ParameterDecorator;
|
|
255
|
+
declare function createParamDecoratorWithoutParam(type: ParamDefinition['type']): () => ParameterDecorator;
|
|
256
|
+
/**
|
|
257
|
+
* Decorator that extracts query parameters from request.
|
|
258
|
+
*
|
|
259
|
+
* @param paramKey - Optional key to extract specific query parameter
|
|
260
|
+
* @param options - Optional ParamOptions
|
|
261
|
+
* @returns Parameter decorator
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```ts
|
|
265
|
+
* @Get('/search')
|
|
266
|
+
* search(@Query('term') term: string, @Query('page', { type: 'number', default: 1 }) page: number) {
|
|
267
|
+
* // term will contain the value of req.query.term
|
|
268
|
+
* // page will contain the numeric value of req.query.page or default to 1
|
|
269
|
+
* }
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
declare const Query: (paramKey?: string, options?: ParamOptions) => ParameterDecorator;
|
|
273
|
+
/**
|
|
274
|
+
* Decorator that extracts route parameters from request.
|
|
275
|
+
*
|
|
276
|
+
* @param paramKey - Optional key to extract specific route parameter
|
|
277
|
+
* @param options - Optional ParamOptions
|
|
278
|
+
* @returns Parameter decorator
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```ts
|
|
282
|
+
* @Get('/users/:id')
|
|
283
|
+
* getUser(@Param('id') id: string) {
|
|
284
|
+
* // id will contain the value of req.params.id
|
|
285
|
+
* }
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
declare const Param: (paramKey?: string, options?: ParamOptions) => ParameterDecorator;
|
|
289
|
+
/**
|
|
290
|
+
* Decorator that extracts body or body property from request.
|
|
291
|
+
*
|
|
292
|
+
* @param paramKey - Optional key to extract specific body property
|
|
293
|
+
* @returns Parameter decorator
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```ts
|
|
297
|
+
* @Post('/users')
|
|
298
|
+
* createUser(@Body() userData: any) {
|
|
299
|
+
* // userData will contain the entire req.body
|
|
300
|
+
* }
|
|
301
|
+
*
|
|
302
|
+
* @Post('/update')
|
|
303
|
+
* updateName(@Body('name') name: string) {
|
|
304
|
+
* // name will contain the value of req.body.name
|
|
305
|
+
* }
|
|
306
|
+
* ```
|
|
307
|
+
*/
|
|
308
|
+
declare const Body: (paramKey?: string) => ParameterDecorator;
|
|
309
|
+
/**
|
|
310
|
+
* Decorator that injects a logger instance.
|
|
311
|
+
* @returns Parameter decorator
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```ts
|
|
315
|
+
* @Get('/log')
|
|
316
|
+
* log(@ReqLogger() logger: Logger) {
|
|
317
|
+
* logger.info('Logging request...');
|
|
318
|
+
* }
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
declare const ReqLogger: () => ParameterDecorator;
|
|
322
|
+
/**
|
|
323
|
+
* Decorator that extracts request ID from headers.
|
|
324
|
+
* @returns Parameter decorator
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* @Get('/data')
|
|
329
|
+
* getData(@ReqId() reqId: string) {
|
|
330
|
+
* // reqId will contain the value of req.headers['x-request-id'] or req.id
|
|
331
|
+
* }
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
declare const ReqId: () => ParameterDecorator;
|
|
335
|
+
/**
|
|
336
|
+
* Decorator that extracts request headers.
|
|
337
|
+
* @param key - Optional key to extract specific header
|
|
338
|
+
* @returns Parameter decorator
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```ts
|
|
342
|
+
* @Get('/data')
|
|
343
|
+
* getData(@ReqHeader('Authorization') authHeader: string) {
|
|
344
|
+
* // authHeader will contain the value of req.headers['authorization']
|
|
345
|
+
* }
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
348
|
+
declare const ReqHeader: (paramKey?: string) => ParameterDecorator;
|
|
349
|
+
/**
|
|
350
|
+
* Decorator that extracts cookies from request.
|
|
351
|
+
* @param key - Optional key to extract specific cookie
|
|
352
|
+
* @returns Parameter decorator
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```ts
|
|
356
|
+
* @Get('/data')
|
|
357
|
+
* getData(@ReqCookie('session_id') sessionId: string) {
|
|
358
|
+
* // sessionId will contain the value of req.cookies['session_id']
|
|
359
|
+
* }
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
declare const ReqCookie: (paramKey?: string) => ParameterDecorator;
|
|
363
|
+
/**
|
|
364
|
+
* Decorator that injects the entire request object.
|
|
365
|
+
*
|
|
366
|
+
* @returns Parameter decorator
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* ```ts
|
|
370
|
+
* @Get('/complex')
|
|
371
|
+
* complex(@Req() req: Request) {
|
|
372
|
+
* // Access the full request object
|
|
373
|
+
* console.log(req.headers);
|
|
374
|
+
* }
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
declare const Req: () => ParameterDecorator;
|
|
378
|
+
/**
|
|
379
|
+
* Decorator that injects the response object.
|
|
380
|
+
*
|
|
381
|
+
* @returns Parameter decorator
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* ```ts
|
|
385
|
+
* @Get('/custom')
|
|
386
|
+
* custom(@Res() res: Response) {
|
|
387
|
+
* // Direct access to response object
|
|
388
|
+
* return res.status(201).send('Created');
|
|
389
|
+
* }
|
|
390
|
+
* ```
|
|
391
|
+
*/
|
|
392
|
+
declare const Res: () => ParameterDecorator;
|
|
393
|
+
/**
|
|
394
|
+
* Decorator that sets a custom HTTP status code for a response.
|
|
395
|
+
*
|
|
396
|
+
* @param status - HTTP status code to use
|
|
397
|
+
* @returns Method decorator
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* @Post('/users')
|
|
402
|
+
* @HttpCode(201)
|
|
403
|
+
* createUser(@Body() userData: any) {
|
|
404
|
+
* // Response will have 201 Created status code
|
|
405
|
+
* return { id: '123', ...userData };
|
|
406
|
+
* }
|
|
407
|
+
* ```
|
|
408
|
+
*/
|
|
409
|
+
declare function HttpCode(status: number): MethodDecorator;
|
|
410
|
+
/**
|
|
411
|
+
* Decorator that adds a custom HTTP header to the response.
|
|
412
|
+
*
|
|
413
|
+
* @param header - Header name-value pairs or a single header name and value
|
|
414
|
+
* @param value - Header value if a single header name is provided
|
|
415
|
+
* @returns Method decorator
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* ```ts
|
|
419
|
+
* @Get('/data')
|
|
420
|
+
* @Header('Cache-Control', 'max-age=60')
|
|
421
|
+
* getData() {
|
|
422
|
+
* // Response will include the Cache-Control header
|
|
423
|
+
* return { data: '...' };
|
|
424
|
+
* }
|
|
425
|
+
*/
|
|
426
|
+
declare function Header(name: string, value: string): MethodDecorator & ClassDecorator;
|
|
427
|
+
/**
|
|
428
|
+
* Decorator that adds a custom HTTP headers to the response.
|
|
429
|
+
*
|
|
430
|
+
* @param headers - Header name-value pairs or a single header name and value
|
|
431
|
+
* @param value - Header value if a single header name is provided
|
|
432
|
+
* @returns Method decorator
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* ```ts
|
|
436
|
+
* @Get('/data')
|
|
437
|
+
* @Headers('Cache-Control', 'max-age=60')
|
|
438
|
+
* getData() {
|
|
439
|
+
* // Response will include the Cache-Control header
|
|
440
|
+
* return { data: '...' };
|
|
441
|
+
* }
|
|
442
|
+
*
|
|
443
|
+
* @Get('/data/:id')
|
|
444
|
+
* @Headers({
|
|
445
|
+
* 'Cache-Control': 'max-age=60',
|
|
446
|
+
* 'X-Custom-Header': 'custom-value',
|
|
447
|
+
* 'Content-Security-Policy': "default-src 'self'"
|
|
448
|
+
* })
|
|
449
|
+
* getData() {
|
|
450
|
+
* // Response will include all specified headers
|
|
451
|
+
* return { data: '...' };
|
|
452
|
+
* }
|
|
453
|
+
*
|
|
454
|
+
* ```
|
|
455
|
+
*/
|
|
456
|
+
declare function Headers(headers: Record<string, string> | string, value?: string): MethodDecorator & ClassDecorator;
|
|
457
|
+
/**
|
|
458
|
+
* Decorator that registers a function to run before route handler execution.
|
|
459
|
+
* Useful for pre-processing or logging.
|
|
460
|
+
*
|
|
461
|
+
* @param fn - Function to execute before the handler
|
|
462
|
+
* @returns Method decorator or Class decorator
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* ```ts
|
|
466
|
+
* @Get('/users/:id')
|
|
467
|
+
* @Before((req, res) => console.log(`Accessing user ${req.params.id}`))
|
|
468
|
+
* getUser(@Param('id') id: string) {
|
|
469
|
+
* // Function will log before this handler runs
|
|
470
|
+
* }
|
|
471
|
+
*
|
|
472
|
+
* @Controller('/api')
|
|
473
|
+
* @Before((req, res) => console.log(`API access: ${req.path}`))
|
|
474
|
+
* class ApiController {
|
|
475
|
+
* // Hook runs before all routes in this controller
|
|
476
|
+
* }
|
|
477
|
+
* ```
|
|
478
|
+
*/
|
|
479
|
+
declare function Before(fn: Function): MethodDecorator & ClassDecorator;
|
|
480
|
+
/**
|
|
481
|
+
* Decorator that registers a function to run after route handler execution.
|
|
482
|
+
* Can access the handler's result.
|
|
483
|
+
*
|
|
484
|
+
* @param fn - Function to execute after the handler
|
|
485
|
+
* @returns Method decorator or Class decorator
|
|
486
|
+
*
|
|
487
|
+
* @example
|
|
488
|
+
* ```ts
|
|
489
|
+
* @Get('/users/:id')
|
|
490
|
+
* @After((req, res, result) => console.log(`User data sent: ${JSON.stringify(result)}`))
|
|
491
|
+
* getUser(@Param('id') id: string) {
|
|
492
|
+
* // After this handler, the function will log the returned data
|
|
493
|
+
* return { id, name: 'Example' };
|
|
494
|
+
* }
|
|
495
|
+
*
|
|
496
|
+
* @Controller('/api')
|
|
497
|
+
* @After((req, res, result) => console.log(`API response: ${JSON.stringify(result)}`))
|
|
498
|
+
* class ApiController {
|
|
499
|
+
* // Hook runs after all routes in this controller
|
|
500
|
+
* }
|
|
501
|
+
* ```
|
|
502
|
+
*/
|
|
503
|
+
declare function After(fn: Function): MethodDecorator & ClassDecorator;
|
|
504
|
+
/**
|
|
505
|
+
* Decorator that requires specific roles for accessing a route.
|
|
506
|
+
* Must be used with authentication middleware.
|
|
507
|
+
*
|
|
508
|
+
* Check req.user.roles for user roles[].
|
|
509
|
+
*
|
|
510
|
+
* @param roles - List of roles that can access this route
|
|
511
|
+
* @returns Method decorator
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```ts
|
|
515
|
+
* @Get('/admin/settings')
|
|
516
|
+
* @Roles('admin', 'superuser')
|
|
517
|
+
* getSettings() {
|
|
518
|
+
* // Only admins and superusers can access
|
|
519
|
+
* return { settings: [...] };
|
|
520
|
+
* }
|
|
521
|
+
* ```
|
|
522
|
+
*/
|
|
523
|
+
declare function Roles(...roles: string[]): MethodDecorator & ClassDecorator;
|
|
524
|
+
/**
|
|
525
|
+
* Decorator that redirects to another URL.
|
|
526
|
+
*
|
|
527
|
+
* @param url - URL to redirect to (can be absolute or relative)
|
|
528
|
+
* @param statusCode - HTTP status code for redirect (default: 302)
|
|
529
|
+
* @returns Method decorator
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* ```ts
|
|
533
|
+
* @Get('/old-path')
|
|
534
|
+
* @Redirect('/new-path', 301)
|
|
535
|
+
* redirectToNewPath() {
|
|
536
|
+
* // This method won't be executed; automatic redirect happens
|
|
537
|
+
* }
|
|
538
|
+
*
|
|
539
|
+
* @Get('/dynamic-redirect')
|
|
540
|
+
* @Redirect()
|
|
541
|
+
* getDynamicRedirect() {
|
|
542
|
+
* // Return an object with url and optionally statusCode
|
|
543
|
+
* return { url: '/calculated-path', statusCode: 307 };
|
|
544
|
+
* }
|
|
545
|
+
* ```
|
|
546
|
+
*/
|
|
547
|
+
declare function Redirect(url?: string, statusCode?: number): MethodDecorator;
|
|
548
|
+
/**
|
|
549
|
+
* Decorator that adds caching to a route response.
|
|
550
|
+
*
|
|
551
|
+
* @param ttlSeconds - Time to live in seconds for the cache
|
|
552
|
+
* @returns Method decorator
|
|
553
|
+
*
|
|
554
|
+
* @example
|
|
555
|
+
* ```ts
|
|
556
|
+
* @Get('/data')
|
|
557
|
+
* @Cache(300) // Cache for 5 minutes
|
|
558
|
+
* getData() {
|
|
559
|
+
* return { data: 'expensive operation result' };
|
|
560
|
+
* }
|
|
561
|
+
* ```
|
|
562
|
+
*/
|
|
563
|
+
declare function Cache(ttlSeconds: number): MethodDecorator & ClassDecorator;
|
|
564
|
+
/**
|
|
565
|
+
* Decorator that applies rate limiting to a route.
|
|
566
|
+
* Note: Requires 'express-rate-limit' package to be installed.
|
|
567
|
+
*
|
|
568
|
+
* @param limit - Maximum number of requests allowed in the window
|
|
569
|
+
* @param windowMs - Time window in milliseconds
|
|
570
|
+
* @returns Method decorator
|
|
571
|
+
*
|
|
572
|
+
* Default Options:
|
|
573
|
+
* - standardHeaders: true
|
|
574
|
+
* - legacyHeaders: false
|
|
575
|
+
*
|
|
576
|
+
* @example
|
|
577
|
+
* ```ts
|
|
578
|
+
* @Post('/login')
|
|
579
|
+
* @RateLimit({ max: 5, windowMs: 60000, standardHeaders: true, legacyHeaders: false }) // 5 requests per minute
|
|
580
|
+
* login(@Body() credentials: LoginDto) {
|
|
581
|
+
* return this.authService.login(credentials);
|
|
582
|
+
* }
|
|
583
|
+
* ```
|
|
584
|
+
*/
|
|
585
|
+
declare function RateLimit(options: {
|
|
586
|
+
max: number;
|
|
587
|
+
windowMs: number;
|
|
588
|
+
standardHeaders?: boolean;
|
|
589
|
+
legacyHeaders?: boolean;
|
|
590
|
+
}): MethodDecorator & ClassDecorator;
|
|
591
|
+
/**
|
|
592
|
+
* Decorator that sets the content type for the response.
|
|
593
|
+
*
|
|
594
|
+
* @param type - MIME type for the response
|
|
595
|
+
* @returns Method decorator or Class decorator
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* ```ts
|
|
599
|
+
* @Get('/download')
|
|
600
|
+
* @ContentType('application/pdf')
|
|
601
|
+
* downloadPdf() {
|
|
602
|
+
* return this.fileService.generatePdf();
|
|
603
|
+
* }
|
|
604
|
+
*
|
|
605
|
+
* @Controller('/api/json')
|
|
606
|
+
* @ContentType('application/json')
|
|
607
|
+
* class JsonApiController {
|
|
608
|
+
* // All routes in this controller use this content type
|
|
609
|
+
* }
|
|
610
|
+
* ```
|
|
611
|
+
*/
|
|
612
|
+
declare function ContentType(type: string): MethodDecorator & ClassDecorator;
|
|
613
|
+
/**
|
|
614
|
+
* Decorator that adds API versioning to a route.
|
|
615
|
+
*
|
|
616
|
+
* @param version - Version string for the API endpoint
|
|
617
|
+
* @param options - Versioning options
|
|
618
|
+
* @returns Method decorator
|
|
619
|
+
*
|
|
620
|
+
* Default Options:
|
|
621
|
+
* - addPrefix: true
|
|
622
|
+
* - addHeader: true
|
|
623
|
+
* - headerName: 'X-API-Version'
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* ```ts
|
|
627
|
+
*
|
|
628
|
+
* @Get('/users')
|
|
629
|
+
* @Version('v2')
|
|
630
|
+
* getUsersV2() {
|
|
631
|
+
* return this.userService.findAllV2();
|
|
632
|
+
* }
|
|
633
|
+
*
|
|
634
|
+
* @Get('/users')
|
|
635
|
+
* @Version('v2', { addPrefix: true, addHeader: true, headerName: 'X-API-Version' })
|
|
636
|
+
* getUsersV2() {
|
|
637
|
+
* // Route becomes /v2/users
|
|
638
|
+
* return this.userService.findAllV2();
|
|
639
|
+
* }
|
|
640
|
+
* ```
|
|
641
|
+
*/
|
|
642
|
+
declare function Version(version: string, options?: {
|
|
643
|
+
addPrefix?: boolean;
|
|
644
|
+
addHeader?: boolean;
|
|
645
|
+
headerName?: string;
|
|
646
|
+
}): MethodDecorator & ClassDecorator;
|
|
647
|
+
/**
|
|
648
|
+
* Decorator that sets a timeout for route execution.
|
|
649
|
+
*
|
|
650
|
+
* @param ms - Timeout in milliseconds
|
|
651
|
+
* @returns Method decorator
|
|
652
|
+
*
|
|
653
|
+
* @example
|
|
654
|
+
* ```ts
|
|
655
|
+
* @Get('/slow-operation')
|
|
656
|
+
* @Timeout(30000) // 30 second timeout
|
|
657
|
+
* slowOperation() {
|
|
658
|
+
* return this.heavyService.processData();
|
|
659
|
+
* }
|
|
660
|
+
* ```
|
|
661
|
+
*/
|
|
662
|
+
declare function Timeout(ms: number): MethodDecorator & ClassDecorator;
|
|
663
|
+
/**
|
|
664
|
+
* Decorator that adds comprehensive logging to a route.
|
|
665
|
+
*
|
|
666
|
+
* @param options - Logging configuration options
|
|
667
|
+
* @returns Method decorator
|
|
668
|
+
*
|
|
669
|
+
* Default Options:
|
|
670
|
+
* - logEntry: true
|
|
671
|
+
* - logExit: true
|
|
672
|
+
* - logBody: false
|
|
673
|
+
* - logParams: false
|
|
674
|
+
* - logResponse: false
|
|
675
|
+
*
|
|
676
|
+
* @example
|
|
677
|
+
* ```ts
|
|
678
|
+
* @Post('/users')
|
|
679
|
+
* @Log({
|
|
680
|
+
* logEntry: true,
|
|
681
|
+
* logExit: true,
|
|
682
|
+
* logBody: true,
|
|
683
|
+
* logParams: true,
|
|
684
|
+
* logResponse: false
|
|
685
|
+
* })
|
|
686
|
+
* createUser(@Body() userData: any) {
|
|
687
|
+
* return this.userService.create(userData);
|
|
688
|
+
* }
|
|
689
|
+
* ```
|
|
690
|
+
*/
|
|
691
|
+
declare function Log(options?: {
|
|
692
|
+
logEntry?: boolean;
|
|
693
|
+
logExit?: boolean;
|
|
694
|
+
logBody?: boolean;
|
|
695
|
+
logParams?: boolean;
|
|
696
|
+
logResponse?: boolean;
|
|
697
|
+
}): MethodDecorator & ClassDecorator;
|
|
698
|
+
/**
|
|
699
|
+
* Registers all controller classes with the provided router.
|
|
700
|
+
* This function processes all decorators and sets up the Express routes.
|
|
701
|
+
*
|
|
702
|
+
* @param router - Express router instance
|
|
703
|
+
* @param controllers - Array of controller classes
|
|
704
|
+
*/
|
|
705
|
+
declare function registerControllers(router: Router, controllers: any[]): void;
|
|
706
|
+
|
|
707
|
+
export { After, Before, Body, Cache, Connect, ContentType, Controller, DIContainer, Delete, Get, Head, Header, Headers, HttpCode, Inject, Injectable, Log, Options, Param, Patch, Post, Put, Query, RateLimit, Redirect, Req, ReqCookie, ReqHeader, ReqId, ReqLogger, Res, Roles, Timeout, Trace, Use, Version, createParamDecorator, createParamDecoratorWithoutParam, inject, registerControllers };
|
|
708
|
+
export type { CachedRateLimiter, HttpMethod, ParamDefinition, ParamOptions, RouteDefinition };
|