fiscalia_bo-nest-helpers 0.1.12 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -20,3 +20,4 @@ export * as MsSkylogs from './ms-skylogs';
20
20
  export * as CustomValidators from './custom-validators';
21
21
  export * as Decorators from './decorators';
22
22
  export * as Dtos from './dto';
23
+ export * as RequestRestrict from './request-restrict';
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Dtos = exports.Decorators = exports.CustomValidators = exports.MsSkylogs = exports.MsFiles = exports.MsPdf = exports.MsPersonas = exports.PermissionsGuard = exports.MsSeguridadHttpError = void 0;
17
+ exports.RequestRestrict = exports.Dtos = exports.Decorators = exports.CustomValidators = exports.MsSkylogs = exports.MsFiles = exports.MsPdf = exports.MsPersonas = exports.PermissionsGuard = exports.MsSeguridadHttpError = void 0;
18
18
  __exportStar(require("./ms-redis/ms-redis.module"), exports);
19
19
  __exportStar(require("./ms-redis/ms-redis.service"), exports);
20
20
  __exportStar(require("./ms-redis/datapass.interceptor"), exports);
@@ -39,4 +39,5 @@ exports.MsSkylogs = require("./ms-skylogs");
39
39
  exports.CustomValidators = require("./custom-validators");
40
40
  exports.Decorators = require("./decorators");
41
41
  exports.Dtos = require("./dto");
42
+ exports.RequestRestrict = require("./request-restrict");
42
43
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,2 @@
1
+ export { BlockModuleOptions, RequestRestrictModule } from './request-block.module';
2
+ export { ThrottleId } from './throttle.decorator';
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ThrottleId = exports.RequestRestrictModule = exports.BlockModuleOptions = void 0;
4
+ var request_block_module_1 = require("./request-block.module");
5
+ Object.defineProperty(exports, "BlockModuleOptions", { enumerable: true, get: function () { return request_block_module_1.BlockModuleOptions; } });
6
+ Object.defineProperty(exports, "RequestRestrictModule", { enumerable: true, get: function () { return request_block_module_1.RequestRestrictModule; } });
7
+ var throttle_decorator_1 = require("./throttle.decorator");
8
+ Object.defineProperty(exports, "ThrottleId", { enumerable: true, get: function () { return throttle_decorator_1.ThrottleId; } });
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/request-restrict/index.ts"],"names":[],"mappings":";;;AAAA,+DAAmF;AAA1E,0HAAA,kBAAkB,OAAA;AAAE,6HAAA,qBAAqB,OAAA;AAClD,2DAAkD;AAAzC,gHAAA,UAAU,OAAA"}
@@ -0,0 +1,11 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ export declare class BlockModuleOptions {
3
+ global?: boolean;
4
+ limit: number;
5
+ limitById: number;
6
+ ttl: number;
7
+ }
8
+ export declare class RequestRestrictModule {
9
+ static register(options: BlockModuleOptions): DynamicModule;
10
+ static registerAsync(options: BlockModuleOptions): Promise<DynamicModule>;
11
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var RequestRestrictModule_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.RequestRestrictModule = exports.BlockModuleOptions = void 0;
11
+ const common_1 = require("@nestjs/common");
12
+ const core_1 = require("@nestjs/core");
13
+ const throttler_1 = require("@nestjs/throttler");
14
+ class BlockModuleOptions {
15
+ }
16
+ exports.BlockModuleOptions = BlockModuleOptions;
17
+ let RequestRestrictModule = RequestRestrictModule_1 = class RequestRestrictModule {
18
+ static register(options) {
19
+ const ttl = (options === null || options === void 0 ? void 0 : options.ttl) || 60;
20
+ const limit = (options === null || options === void 0 ? void 0 : options.limit) || 10;
21
+ const limitById = (options === null || options === void 0 ? void 0 : options.limitById) || 15;
22
+ return {
23
+ global: options.global,
24
+ module: RequestRestrictModule_1,
25
+ imports: [throttler_1.ThrottlerModule.forRoot({ limit, ttl })],
26
+ providers: [
27
+ throttler_1.ThrottlerStorageService,
28
+ { provide: core_1.APP_GUARD, useClass: throttler_1.ThrottlerGuard },
29
+ { provide: 'REQUEST_BLOCK_OPTIONS', useValue: { limit, ttl, limitById } },
30
+ ],
31
+ exports: [throttler_1.ThrottlerStorageService, 'REQUEST_BLOCK_OPTIONS'],
32
+ };
33
+ }
34
+ static async registerAsync(options) {
35
+ const ttl = (options === null || options === void 0 ? void 0 : options.ttl) || 60;
36
+ const limit = (options === null || options === void 0 ? void 0 : options.limit) || 10;
37
+ const limitById = (options === null || options === void 0 ? void 0 : options.limitById) || 15;
38
+ return {
39
+ global: options.global,
40
+ module: RequestRestrictModule_1,
41
+ imports: [throttler_1.ThrottlerModule.forRoot({ limit, ttl })],
42
+ providers: [
43
+ throttler_1.ThrottlerStorageService,
44
+ { provide: core_1.APP_GUARD, useClass: throttler_1.ThrottlerGuard },
45
+ { provide: 'REQUEST_BLOCK_OPTIONS', useValue: { limit, ttl, limitById } },
46
+ ],
47
+ exports: [throttler_1.ThrottlerStorageService, 'REQUEST_BLOCK_OPTIONS'],
48
+ };
49
+ }
50
+ };
51
+ RequestRestrictModule = RequestRestrictModule_1 = __decorate([
52
+ (0, common_1.Module)({})
53
+ ], RequestRestrictModule);
54
+ exports.RequestRestrictModule = RequestRestrictModule;
55
+ //# sourceMappingURL=request-block.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-block.module.js","sourceRoot":"","sources":["../../src/request-restrict/request-block.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAuD;AACvD,uCAAyC;AACzC,iDAA6F;AAE7F,MAAa,kBAAkB;CAK9B;AALD,gDAKC;AAGM,IAAM,qBAAqB,6BAA3B,MAAM,qBAAqB;IAIhC,MAAM,CAAC,QAAQ,CAAC,OAA2B;QACzC,MAAM,GAAG,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,KAAI,EAAE,CAAC;QAC/B,MAAM,KAAK,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,KAAI,EAAE,CAAC;QACnC,MAAM,SAAS,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS,KAAI,EAAE,CAAC;QAC3C,OAAO;YACL,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,MAAM,EAAE,uBAAqB;YAC7B,OAAO,EAAE,CAAC,2BAAe,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;YAClD,SAAS,EAAE;gBACT,mCAAuB;gBACvB,EAAE,OAAO,EAAE,gBAAS,EAAE,QAAQ,EAAE,0BAAc,EAAE;gBAChD,EAAE,OAAO,EAAE,uBAAuB,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,EAAE;aAC1E;YACD,OAAO,EAAE,CAAC,mCAAuB,EAAE,uBAAuB,CAAC;SAC5D,CAAC;IACJ,CAAC;IAKD,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,OAA2B;QACpD,MAAM,GAAG,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,KAAI,EAAE,CAAC;QAC/B,MAAM,KAAK,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,KAAI,EAAE,CAAC;QACnC,MAAM,SAAS,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS,KAAI,EAAE,CAAC;QAC3C,OAAO;YACL,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,MAAM,EAAE,uBAAqB;YAC7B,OAAO,EAAE,CAAC,2BAAe,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;YAClD,SAAS,EAAE;gBACT,mCAAuB;gBACvB,EAAE,OAAO,EAAE,gBAAS,EAAE,QAAQ,EAAE,0BAAc,EAAE;gBAChD,EAAE,OAAO,EAAE,uBAAuB,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,EAAE;aAC1E;YACD,OAAO,EAAE,CAAC,mCAAuB,EAAE,uBAAuB,CAAC;SAC5D,CAAC;IACJ,CAAC;CACF,CAAA;AAxCY,qBAAqB;IADjC,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,qBAAqB,CAwCjC;AAxCY,sDAAqB"}
@@ -0,0 +1,10 @@
1
+ import { CanActivate, ExecutionContext } from '@nestjs/common';
2
+ import { ThrottlerStorageService } from '@nestjs/throttler';
3
+ import { BlockModuleOptions } from './request-block.module';
4
+ export declare class ThrottleIdGuard implements CanActivate {
5
+ private blockOptions;
6
+ private throttlerStorageService;
7
+ constructor(blockOptions: BlockModuleOptions, throttlerStorageService: ThrottlerStorageService);
8
+ canActivate(context: ExecutionContext): Promise<boolean>;
9
+ }
10
+ export declare function ThrottleId(): MethodDecorator;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.ThrottleId = exports.ThrottleIdGuard = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const throttler_1 = require("@nestjs/throttler");
18
+ const request_block_module_1 = require("./request-block.module");
19
+ let ThrottleIdGuard = class ThrottleIdGuard {
20
+ constructor(blockOptions, throttlerStorageService) {
21
+ this.blockOptions = blockOptions;
22
+ this.throttlerStorageService = throttlerStorageService;
23
+ }
24
+ async canActivate(context) {
25
+ const ttl = this.blockOptions.ttl;
26
+ const limitById = this.blockOptions.limitById;
27
+ const req = context.switchToHttp().getRequest();
28
+ const key = req.method + '-' + req.ip + '-' + context.getHandler().name;
29
+ const storage = this.throttlerStorageService.storage;
30
+ const record = storage[key];
31
+ let totalHits = 0;
32
+ if (record && Date.now() < record.expiresAt) {
33
+ totalHits = record.totalHits;
34
+ }
35
+ const remainingPoints = Math.max(limitById - totalHits, 0);
36
+ if (remainingPoints <= 0) {
37
+ throw new throttler_1.ThrottlerException('You are being rate limited');
38
+ }
39
+ storage[key] = {
40
+ totalHits: totalHits + 1,
41
+ expiresAt: Date.now() + ttl * 1000,
42
+ };
43
+ return true;
44
+ }
45
+ };
46
+ ThrottleIdGuard = __decorate([
47
+ (0, common_1.Injectable)(),
48
+ __param(0, (0, common_1.Inject)('REQUEST_BLOCK_OPTIONS')),
49
+ __metadata("design:paramtypes", [request_block_module_1.BlockModuleOptions,
50
+ throttler_1.ThrottlerStorageService])
51
+ ], ThrottleIdGuard);
52
+ exports.ThrottleIdGuard = ThrottleIdGuard;
53
+ function ThrottleId() {
54
+ return (0, common_1.applyDecorators)((0, throttler_1.SkipThrottle)(), (0, common_1.UseGuards)(ThrottleIdGuard));
55
+ }
56
+ exports.ThrottleId = ThrottleId;
57
+ //# sourceMappingURL=throttle.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttle.decorator.js","sourceRoot":"","sources":["../../src/request-restrict/throttle.decorator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAOwB;AACxB,iDAA8F;AAC9F,iEAA4D;AAIrD,IAAM,eAAe,GAArB,MAAM,eAAe;IAC1B,YAC2C,YAAgC,EACjE,uBAAgD;QADf,iBAAY,GAAZ,YAAY,CAAoB;QACjE,4BAAuB,GAAvB,uBAAuB,CAAyB;IACvD,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,OAAyB;QACzC,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC;QAElC,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC;QAE9C,MAAM,GAAG,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QAChD,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,GAAG,GAAG,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,IAAI,CAAC;QAExE,MAAM,OAAO,GACX,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC;QACvC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;QAE5B,IAAI,SAAS,GAAG,CAAC,CAAC;QAClB,IAAI,MAAM,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,SAAS,EAAE;YAC3C,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;SAC9B;QAED,MAAM,eAAe,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,SAAS,EAAE,CAAC,CAAC,CAAC;QAC3D,IAAI,eAAe,IAAI,CAAC,EAAE;YACxB,MAAM,IAAI,8BAAkB,CAAC,4BAA4B,CAAC,CAAC;SAC5D;QAED,OAAO,CAAC,GAAG,CAAC,GAAG;YACb,SAAS,EAAE,SAAS,GAAG,CAAC;YACxB,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,IAAI;SACnC,CAAC;QAEF,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AAnCY,eAAe;IAD3B,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,uBAAuB,CAAC,CAAA;qCAAuB,yCAAkB;QACxC,mCAAuB;GAH/C,eAAe,CAmC3B;AAnCY,0CAAe;AAqC5B,SAAgB,UAAU;IACxB,OAAO,IAAA,wBAAe,EAAC,IAAA,wBAAY,GAAE,EAAE,IAAA,kBAAS,EAAC,eAAe,CAAC,CAAC,CAAC;AACrE,CAAC;AAFD,gCAEC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiscalia_bo-nest-helpers",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "author": "UTIC",
5
5
  "contact": {
6
6
  "name": "Unidad de Tecnologías de la Información y Comunicación - Ministerio Público",
@@ -21,6 +21,7 @@
21
21
  "dist/services/*",
22
22
  "dist/ms-geojson/*",
23
23
  "dist/helpers/*",
24
+ "dist/request-restrict/*",
24
25
  "dist/types/*",
25
26
  "dist/index.d.ts",
26
27
  "*.md"
@@ -64,6 +65,7 @@
64
65
  },
65
66
  "dependencies": {
66
67
  "@nestjs/common": "^9.0.0",
68
+ "@nestjs/throttler": "^4.0.0",
67
69
  "@turf/turf": "^6.5.0",
68
70
  "dotenv": "^16.0.3",
69
71
  "ua-parser-js": "^1.0.35"