@podkopaev-tech/nest-infra-modules 0.0.13 → 0.0.15

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.
@@ -14,7 +14,7 @@ const Cache = (options) => {
14
14
  cacheManagerInjection(target, exports.CACHE_INJECTION_PROP);
15
15
  descriptor.value = async function (...args) {
16
16
  const { [exports.CACHE_INJECTION_PROP]: cache } = this;
17
- const cacheKey = `${cacheKeyBase}[${args
17
+ const cacheKey = `${this.constructor.name}:${cacheKeyBase}[${args
18
18
  .map((res) => JSON.stringify(res))
19
19
  .join(',')}]`;
20
20
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"cache.decorator.js","sourceRoot":"","sources":["../../../../../src/modules/cache/decorators/cache.decorator.ts"],"names":[],"mappings":";;;AAAA,yDAAsD;AACtD,2CAAwC;AAG3B,QAAA,gBAAgB,GAAG,gBAAgB,CAAC;AACpC,QAAA,oBAAoB,GAAG,QAAQ,CAAC;AAOtC,MAAM,KAAK,GAAG,CAAC,OAA8B,EAAmB,EAAE;IACvE,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAExB,OAAO,CACL,MAA+B,EAC/B,SAA0B,EAC1B,UAA8B,EAC9B,EAAE;QACF,MAAM,qBAAqB,GAAG,IAAA,eAAM,EAAC,6BAAa,CAAC,CAAC;QACpD,MAAM,YAAY,GAAG,UAAU,CAAC,KAAK,CAAC;QACtC,MAAM,YAAY,GAAG,GAAG,wBAAgB,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QAEzD,qBAAqB,CAAC,MAAM,EAAE,4BAAoB,CAAC,CAAC;QAEpD,UAAU,CAAC,KAAK,GAAG,KAAK,WAAW,GAAG,IAAe;YACnD,MAAM,EAAE,CAAC,4BAAoB,CAAC,EAAE,KAAK,EAAE,GAAG,IAEzC,CAAC;YAEF,MAAM,QAAQ,GAAG,GAAG,YAAY,IAAI,IAAI;iBACrC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;iBACjC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;YAEhB,IAAI,CAAC;gBACH,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,IAAI,CAC1B,QAAQ,EACR,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,EACtC,EAAE,GAAG,EAAE,CACR,CAAC;gBAEF,OAAO,GAAG,CAAC;YACb,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;YAC1C,CAAC;QACH,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AApCW,QAAA,KAAK,SAoChB"}
1
+ {"version":3,"file":"cache.decorator.js","sourceRoot":"","sources":["../../../../../src/modules/cache/decorators/cache.decorator.ts"],"names":[],"mappings":";;;AAAA,yDAAsD;AACtD,2CAAwC;AAG3B,QAAA,gBAAgB,GAAG,gBAAgB,CAAC;AACpC,QAAA,oBAAoB,GAAG,QAAQ,CAAC;AAOtC,MAAM,KAAK,GAAG,CAAC,OAA8B,EAAmB,EAAE;IACvE,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAExB,OAAO,CACL,MAA+B,EAC/B,SAA0B,EAC1B,UAA8B,EAC9B,EAAE;QACF,MAAM,qBAAqB,GAAG,IAAA,eAAM,EAAC,6BAAa,CAAC,CAAC;QACpD,MAAM,YAAY,GAAG,UAAU,CAAC,KAAK,CAAC;QACtC,MAAM,YAAY,GAAG,GAAG,wBAAgB,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QAEzD,qBAAqB,CAAC,MAAM,EAAE,4BAAoB,CAAC,CAAC;QAEpD,UAAU,CAAC,KAAK,GAAG,KAAK,WAAW,GAAG,IAAe;YACnD,MAAM,EAAE,CAAC,4BAAoB,CAAC,EAAE,KAAK,EAAE,GAAG,IAEzC,CAAC;YAEF,MAAM,QAAQ,GAAG,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,IAAI,YAAY,IAAI,IAAI;iBAC9D,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;iBACjC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;YAEhB,IAAI,CAAC;gBACH,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,IAAI,CAC1B,QAAQ,EACR,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,EACtC,EAAE,GAAG,EAAE,CACR,CAAC;gBAEF,OAAO,GAAG,CAAC;YACb,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;YAC1C,CAAC;QACH,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AApCW,QAAA,KAAK,SAoChB"}
@@ -0,0 +1 @@
1
+ export * from './rate-limiter.decorator';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./rate-limiter.decorator"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/modules/redis/decorators/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2DAAyC"}
@@ -0,0 +1,8 @@
1
+ import 'reflect-metadata';
2
+ import { IRateLimiterRedisOptions, RateLimiterRes } from 'rate-limiter-flexible';
3
+ export interface RateLimiterDecoratorOptions extends Omit<IRateLimiterRedisOptions, 'storeClient'> {
4
+ redisToken: string | symbol;
5
+ keyGenerator?: (target: any, propertyKey: string | symbol, args: any[]) => string;
6
+ onLimitReached?: (res: RateLimiterRes) => Error;
7
+ }
8
+ export declare const RateLimiter: (options: RateLimiterDecoratorOptions) => MethodDecorator;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RateLimiter = void 0;
4
+ require("reflect-metadata");
5
+ const common_1 = require("@nestjs/common");
6
+ const rate_limiter_flexible_1 = require("rate-limiter-flexible");
7
+ const RateLimiter = (options) => {
8
+ return (target, propertyKey, descriptor) => {
9
+ if (!descriptor.value) {
10
+ throw new Error('@RateLimiter can only be applied to methods.');
11
+ }
12
+ const originalMethod = descriptor.value;
13
+ const redisToken = options.redisToken;
14
+ if (!(redisToken in target)) {
15
+ (0, common_1.Inject)(redisToken)(target, redisToken);
16
+ }
17
+ const { redisToken: _, keyGenerator, onLimitReached, ...rateLimiterOptions } = options;
18
+ const rateLimiterKey = Symbol(`rateLimiter_${String(propertyKey)}`);
19
+ descriptor.value = async function (...args) {
20
+ const redisClient = this[redisToken];
21
+ if (!redisClient) {
22
+ throw new Error(`Redis client not injected. Make sure to provide the redisToken or inject Redis client with token: ${String(redisToken)}`);
23
+ }
24
+ if (!this[rateLimiterKey]) {
25
+ this[rateLimiterKey] = new rate_limiter_flexible_1.RateLimiterRedis({
26
+ ...rateLimiterOptions,
27
+ storeClient: redisClient,
28
+ });
29
+ }
30
+ const rateLimiter = this[rateLimiterKey];
31
+ let key;
32
+ if (keyGenerator) {
33
+ key = keyGenerator(this, propertyKey, args);
34
+ }
35
+ else {
36
+ key = `${this.constructor.name}:${String(propertyKey)}`;
37
+ }
38
+ try {
39
+ const rateLimiterRes = await rateLimiter.consume(key);
40
+ return originalMethod.apply(this, args);
41
+ }
42
+ catch (rejRes) {
43
+ if (rejRes instanceof Error) {
44
+ throw rejRes;
45
+ }
46
+ const rateLimiterRes = rejRes;
47
+ if (onLimitReached) {
48
+ throw onLimitReached(rateLimiterRes);
49
+ }
50
+ throw new Error(`Rate limit exceeded. Try again in ${Math.round(rateLimiterRes.msBeforeNext / 1000)} seconds.`);
51
+ }
52
+ };
53
+ };
54
+ };
55
+ exports.RateLimiter = RateLimiter;
56
+ //# sourceMappingURL=rate-limiter.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limiter.decorator.js","sourceRoot":"","sources":["../../../../../src/modules/redis/decorators/rate-limiter.decorator.ts"],"names":[],"mappings":";;;AAAA,4BAA0B;AAC1B,2CAAwC;AACxC,iEAAmG;AAsC5F,MAAM,WAAW,GAAG,CAAC,OAAoC,EAAmB,EAAE;IACnF,OAAO,CAAC,MAAc,EAAE,WAA4B,EAAE,UAA8B,EAAE,EAAE;QACtF,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;QAClE,CAAC;QAED,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QACxC,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QAGtC,IAAI,CAAC,CAAC,UAAU,IAAI,MAAM,CAAC,EAAE,CAAC;YAC5B,IAAA,eAAM,EAAC,UAAU,CAAC,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;QACzC,CAAC;QAGD,MAAM,EAAE,UAAU,EAAE,CAAC,EAAE,YAAY,EAAE,cAAc,EAAE,GAAG,kBAAkB,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cAAc,CAAC,cAAc,CAAC,CAAC;gBACvC,CAAC;gBAGD,MAAM,IAAI,KAAK,CACb,qCAAqC,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,YAAY,GAAG,IAAI,CAAC,WAAW,CAC/F,CAAC;YACJ,CAAC;QACH,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AA1EW,QAAA,WAAW,eA0EtB"}
@@ -1,2 +1,3 @@
1
1
  export * from './redis.module';
2
2
  export * from './types';
3
+ export * from './decorators';
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./redis.module"), exports);
18
18
  __exportStar(require("./types"), exports);
19
+ __exportStar(require("./decorators"), exports);
19
20
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/modules/redis/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,0CAAwB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/modules/redis/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,0CAAwB;AACxB,+CAA6B"}