@catbee/utils 0.0.8-rc.0 → 0.0.8-rc.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 CHANGED
@@ -1,10 +1,34 @@
1
1
  # @catbee/utils
2
2
 
3
+ <!-- <div style="width: 100%; display: flex; align-items: center; justify-content: space-between;">
4
+ <b style="font-size: 24px; text-decoration: none;">@catbee/utils</b>
5
+ <a href="https://sonarcloud.io/summary/new_code?id=catbee-technologies_catbee-utils">
6
+ <img src="https://sonarcloud.io/images/project_badges/sonarcloud-dark.svg" alt="SonarQube Cloud" width="200"/>
7
+ </a>
8
+ </div> -->
9
+
3
10
  ## 🧰 Utility Modules for Node.js
4
11
 
5
12
  A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications. All utilities are tree-shakable and can be imported independently.
6
13
 
7
- ![build](https://img.shields.io/badge/build-passing-brightgreen) ![coverage](https://codecov.io/gh/catbee-technologies/catbee-utils/graph/badge.svg?token=XAJHK6R1OQ) ![node](https://img.shields.io/node/v/@catbee/utils) ![npm](https://img.shields.io/npm/v/@catbee/utils) ![downloads](https://img.shields.io/npm/dm/@catbee/utils) ![dependencies](https://img.shields.io/librariesio/release/npm/@catbee%2Futils) ![license](https://img.shields.io/npm/l/@catbee/utils)
14
+ <div style="display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 1rem 0;">
15
+ <img src="https://img.shields.io/badge/build-passing-brightgreen" alt="Build Status" />
16
+ <img src="https://codecov.io/gh/catbee-technologies/catbee-utils/graph/badge.svg?token=XAJHK6R1OQ" alt="Coverage" />
17
+ <img src="https://img.shields.io/node/v/@catbee/utils" alt="Node Version" />
18
+ <img src="https://img.shields.io/npm/v/@catbee/utils" alt="NPM Version" />
19
+ <img src="https://img.shields.io/npm/v/@catbee/utils/rc" alt="NPM RC Version" />
20
+ <img src="https://img.shields.io/npm/dt/@catbee/utils" alt="NPM Downloads" />
21
+ <img src="https://img.shields.io/npm/types/@catbee/utils" alt="TypeScript Types" />
22
+ <img src="https://img.shields.io/librariesio/release/npm/@catbee%2Futils" alt="Dependencies" />
23
+ <img src="https://img.shields.io/maintenance/yes/2025" alt="Maintenance" />
24
+ <img src="https://snyk.io/test/github/<owner>/<repo>/badge.svg" alt="Snyk Vulnerabilities" />
25
+ <!-- <img src="https://sonarcloud.io/api/project_badges/measure?project=catbee-technologies_catbee-utils&metric=alert_status&token=93da835f2d48d37b41fa628cc7fc764c873bd700" alt="Quality Gate Status" /> -->
26
+ <img src="https://sonarcloud.io/api/project_badges/measure?project=catbee-technologies_catbee-utils&metric=ncloc&token=93da835f2d48d37b41fa628cc7fc764c873bd700" alt="Lines of Code" />
27
+ <img src="https://sonarcloud.io/api/project_badges/measure?project=catbee-technologies_catbee-utils&metric=security_rating&token=93da835f2d48d37b41fa628cc7fc764c873bd700" alt="Security Rating" />
28
+ <img src="https://sonarcloud.io/api/project_badges/measure?project=catbee-technologies_catbee-utils&metric=sqale_rating&token=93da835f2d48d37b41fa628cc7fc764c873bd700" alt="Maintainability Rating" />
29
+ <img src="https://sonarcloud.io/api/project_badges/measure?project=catbee-technologies_catbee-utils&metric=vulnerabilities&token=93da835f2d48d37b41fa628cc7fc764c873bd700" alt="Vulnerabilities" />
30
+ <img src="https://img.shields.io/npm/l/@catbee/utils" alt="License" />
31
+ </div>
8
32
 
9
33
  ---
10
34
 
@@ -1,5 +1,24 @@
1
1
  import type { RequestHandler, Router } from 'express';
2
2
  import 'reflect-metadata';
3
+ import { rateLimit } from 'express-rate-limit';
4
+ /**
5
+ * RateLimiter cache that uses TTLCache for automatic TTL and LRU handling.
6
+ */
7
+ export declare class RateLimiterCache {
8
+ private cache;
9
+ constructor(maxSize?: number, ttlMs?: number);
10
+ private generateKey;
11
+ get(options: {
12
+ max: number;
13
+ windowMs: number;
14
+ standardHeaders: boolean;
15
+ legacyHeaders: boolean;
16
+ }): ReturnType<typeof rateLimit>;
17
+ clear(): void;
18
+ size(): number;
19
+ destroy(): void;
20
+ }
21
+ declare const rateLimiterCache: RateLimiterCache;
3
22
  /**
4
23
  * Decorator for GET HTTP method routes.
5
24
  *
@@ -212,8 +231,8 @@ export declare function HttpCode(status: number): MethodDecorator;
212
231
  /**
213
232
  * Decorator that adds a custom HTTP header to the response.
214
233
  *
215
- * @param name - Header name
216
- * @param value - Header value
234
+ * @param header - Header name-value pairs or a single header name and value
235
+ * @param value - Header value if a single header name is provided
217
236
  * @returns Method decorator
218
237
  *
219
238
  * @example
@@ -224,9 +243,38 @@ export declare function HttpCode(status: number): MethodDecorator;
224
243
  * // Response will include the Cache-Control header
225
244
  * return { data: '...' };
226
245
  * }
227
- * ```
228
246
  */
229
247
  export declare function Header(name: string, value: string): MethodDecorator;
248
+ /**
249
+ * Decorator that adds a custom HTTP headers to the response.
250
+ *
251
+ * @param headers - Header name-value pairs or a single header name and value
252
+ * @param value - Header value if a single header name is provided
253
+ * @returns Method decorator
254
+ *
255
+ * @example
256
+ * ```ts
257
+ * @Get('/data')
258
+ * @Headers('Cache-Control', 'max-age=60')
259
+ * getData() {
260
+ * // Response will include the Cache-Control header
261
+ * return { data: '...' };
262
+ * }
263
+ *
264
+ * @Get('/data/:id')
265
+ * @Headers({
266
+ * 'Cache-Control': 'max-age=60',
267
+ * 'X-Custom-Header': 'custom-value',
268
+ * 'Content-Security-Policy': "default-src 'self'"
269
+ * })
270
+ * getData() {
271
+ * // Response will include all specified headers
272
+ * return { data: '...' };
273
+ * }
274
+ *
275
+ * ```
276
+ */
277
+ export declare function Headers(headers: Record<string, string> | string, value?: string): MethodDecorator;
230
278
  /**
231
279
  * Decorator that registers a function to run before route handler execution.
232
280
  * Useful for pre-processing or logging.
@@ -304,6 +352,150 @@ export declare function Roles(...roles: string[]): MethodDecorator;
304
352
  * ```
305
353
  */
306
354
  export declare function Redirect(url?: string, statusCode?: number): MethodDecorator;
355
+ /**
356
+ * Decorator that adds caching to a route response.
357
+ *
358
+ * @param ttlSeconds - Time to live in seconds for the cache
359
+ * @returns Method decorator
360
+ *
361
+ * @example
362
+ * ```ts
363
+ * @Get('/data')
364
+ * @Cache(300) // Cache for 5 minutes
365
+ * getData() {
366
+ * return { data: 'expensive operation result' };
367
+ * }
368
+ * ```
369
+ */
370
+ export declare function Cache(ttlSeconds: number): MethodDecorator;
371
+ /**
372
+ * Decorator that applies rate limiting to a route.
373
+ * Note: Requires 'express-rate-limit' package to be installed.
374
+ *
375
+ * @param limit - Maximum number of requests allowed in the window
376
+ * @param windowMs - Time window in milliseconds
377
+ * @returns Method decorator
378
+ *
379
+ * Default Options:
380
+ * - standardHeaders: true
381
+ * - legacyHeaders: false
382
+ *
383
+ * @example
384
+ * ```ts
385
+ * @Post('/login')
386
+ * @RateLimit({ max: 5, windowMs: 60000, standardHeaders: true, legacyHeaders: false }) // 5 requests per minute
387
+ * login(@Body() credentials: LoginDto) {
388
+ * return this.authService.login(credentials);
389
+ * }
390
+ * ```
391
+ */
392
+ export declare function RateLimit(options: {
393
+ max: number;
394
+ windowMs: number;
395
+ standardHeaders?: boolean;
396
+ legacyHeaders?: boolean;
397
+ }): MethodDecorator;
398
+ /**
399
+ * Decorator that sets the content type for the response.
400
+ *
401
+ * @param type - MIME type for the response
402
+ * @returns Method decorator
403
+ *
404
+ * @example
405
+ * ```ts
406
+ * @Get('/download')
407
+ * @ContentType('application/pdf')
408
+ * downloadPdf() {
409
+ * return this.fileService.generatePdf();
410
+ * }
411
+ * ```
412
+ */
413
+ export declare function ContentType(type: string): MethodDecorator;
414
+ /**
415
+ * Decorator that adds API versioning to a route.
416
+ *
417
+ * @param version - Version string for the API endpoint
418
+ * @param options - Versioning options
419
+ * @returns Method decorator
420
+ *
421
+ * Default Options:
422
+ * - addPrefix: true
423
+ * - addHeader: true
424
+ * - headerName: 'X-API-Version'
425
+ *
426
+ * @example
427
+ * ```ts
428
+ *
429
+ * @Get('/users')
430
+ * @Version('v2')
431
+ * getUsersV2() {
432
+ * return this.userService.findAllV2();
433
+ * }
434
+ *
435
+ * @Get('/users')
436
+ * @Version('v2', { addPrefix: true, addHeader: true, headerName: 'X-API-Version' })
437
+ * getUsersV2() {
438
+ * // Route becomes /v2/users
439
+ * return this.userService.findAllV2();
440
+ * }
441
+ * ```
442
+ */
443
+ export declare function Version(version: string, options?: {
444
+ addPrefix?: boolean;
445
+ addHeader?: boolean;
446
+ headerName?: string;
447
+ }): MethodDecorator;
448
+ /**
449
+ * Decorator that sets a timeout for route execution.
450
+ *
451
+ * @param ms - Timeout in milliseconds
452
+ * @returns Method decorator
453
+ *
454
+ * @example
455
+ * ```ts
456
+ * @Get('/slow-operation')
457
+ * @Timeout(30000) // 30 second timeout
458
+ * slowOperation() {
459
+ * return this.heavyService.processData();
460
+ * }
461
+ * ```
462
+ */
463
+ export declare function Timeout(ms: number): MethodDecorator;
464
+ /**
465
+ * Decorator that adds comprehensive logging to a route.
466
+ *
467
+ * @param options - Logging configuration options
468
+ * @returns Method decorator
469
+ *
470
+ * Default Options:
471
+ * - logEntry: true
472
+ * - logExit: true
473
+ * - logBody: false
474
+ * - logParams: false
475
+ * - logResponse: false
476
+ *
477
+ * @example
478
+ * ```ts
479
+ * @Post('/users')
480
+ * @Log({
481
+ * logEntry: true,
482
+ * logExit: true,
483
+ * logBody: true,
484
+ * logParams: true,
485
+ * logResponse: false
486
+ * })
487
+ * createUser(@Body() userData: any) {
488
+ * return this.userService.create(userData);
489
+ * }
490
+ * ```
491
+ */
492
+ export declare function Log(options?: {
493
+ logEntry?: boolean;
494
+ logExit?: boolean;
495
+ logBody?: boolean;
496
+ logParams?: boolean;
497
+ logResponse?: boolean;
498
+ }): MethodDecorator;
307
499
  /**
308
500
  * Registers all controller classes with the provided router.
309
501
  * This function processes all decorators and sets up the Express routes.
@@ -312,4 +504,5 @@ export declare function Redirect(url?: string, statusCode?: number): MethodDecor
312
504
  * @param controllers - Array of controller classes
313
505
  */
314
506
  export declare function registerControllers(router: Router, controllers: any[]): void;
507
+ export { rateLimiterCache };
315
508
  //# sourceMappingURL=decorators.utils.d.ts.map