@nathapp/nestjs-throttler 1.0.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.
@@ -0,0 +1 @@
1
+ export * from './throttle.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("./throttle.decorator"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/decorator/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAqC"}
@@ -0,0 +1 @@
1
+ export { Throttle, SkipThrottle } from '@nestjs/throttler';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SkipThrottle = exports.Throttle = void 0;
4
+ var throttler_1 = require("@nestjs/throttler");
5
+ Object.defineProperty(exports, "Throttle", { enumerable: true, get: function () { return throttler_1.Throttle; } });
6
+ Object.defineProperty(exports, "SkipThrottle", { enumerable: true, get: function () { return throttler_1.SkipThrottle; } });
7
+ //# sourceMappingURL=throttle.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttle.decorator.js","sourceRoot":"","sources":["../../lib/decorator/throttle.decorator.ts"],"names":[],"mappings":";;;AACA,+CAA2D;AAAlD,qGAAA,QAAQ,OAAA;AAAE,yGAAA,YAAY,OAAA"}
@@ -0,0 +1,4 @@
1
+ export * from './module';
2
+ export * from './throttler.guard';
3
+ export * from './interface';
4
+ export * from './decorator';
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
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("./module"), exports);
18
+ __exportStar(require("./throttler.guard"), exports);
19
+ __exportStar(require("./interface"), exports);
20
+ __exportStar(require("./decorator"), exports);
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,oDAAkC;AAClC,8CAA4B;AAC5B,8CAA4B"}
@@ -0,0 +1 @@
1
+ export * from './throttler-options.interface';
@@ -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("./throttler-options.interface"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/interface/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gEAA8C"}
@@ -0,0 +1,15 @@
1
+ import { TrustProxyOptions } from '@nathapp/nestjs-common';
2
+ export interface ThrottlerModuleOptions {
3
+ trustProxy?: TrustProxyOptions;
4
+ throttlers: Array<{
5
+ ttl: number;
6
+ limit: number;
7
+ name?: string;
8
+ }>;
9
+ ignoreUserAgents?: RegExp[];
10
+ skipIf?: (context: any) => boolean;
11
+ storage?: any;
12
+ errorMessage?: string | ((context: any, throttlerLimitDetail: any) => string);
13
+ getTracker?: (req: any) => string | Promise<string>;
14
+ generateKey?: (context: any, tracker: string, throttlerName: string) => string;
15
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=throttler-options.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttler-options.interface.js","sourceRoot":"","sources":["../../lib/interface/throttler-options.interface.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { ThrottlerAsyncOptions } from '@nestjs/throttler';
3
+ import { ThrottlerModuleOptions } from './interface';
4
+ export declare class ThrottlerModule {
5
+ static forRoot(options: ThrottlerModuleOptions): DynamicModule;
6
+ static forRootAsync(options: ThrottlerAsyncOptions & {
7
+ trustProxy?: any;
8
+ }): DynamicModule;
9
+ }
package/dist/module.js ADDED
@@ -0,0 +1,58 @@
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 ThrottlerModule_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.ThrottlerModule = void 0;
11
+ const common_1 = require("@nestjs/common");
12
+ const throttler_1 = require("@nestjs/throttler");
13
+ const nestjs_common_1 = require("@nathapp/nestjs-common");
14
+ const throttler_guard_1 = require("./throttler.guard");
15
+ let ThrottlerModule = ThrottlerModule_1 = class ThrottlerModule {
16
+ static forRoot(options) {
17
+ if (options.trustProxy) {
18
+ (0, nestjs_common_1.configureTrustProxy)(options.trustProxy);
19
+ }
20
+ const { trustProxy, ...throttlerOptions } = options;
21
+ return {
22
+ module: ThrottlerModule_1,
23
+ imports: [throttler_1.ThrottlerModule.forRoot(throttlerOptions)],
24
+ providers: [throttler_guard_1.DefaultThrottlerGuard],
25
+ exports: [throttler_guard_1.DefaultThrottlerGuard, throttler_1.ThrottlerModule],
26
+ };
27
+ }
28
+ static forRootAsync(options) {
29
+ const trustProxyProvider = {
30
+ provide: 'TRUST_PROXY_CONFIG',
31
+ useFactory: async (...args) => {
32
+ if (options.trustProxy) {
33
+ if (typeof options.trustProxy === 'function') {
34
+ const config = await options.trustProxy(...args);
35
+ (0, nestjs_common_1.configureTrustProxy)(config);
36
+ }
37
+ else {
38
+ (0, nestjs_common_1.configureTrustProxy)(options.trustProxy);
39
+ }
40
+ }
41
+ return null;
42
+ },
43
+ inject: options.inject || [],
44
+ };
45
+ const { trustProxy, ...throttlerOptions } = options;
46
+ return {
47
+ module: ThrottlerModule_1,
48
+ imports: [throttler_1.ThrottlerModule.forRootAsync(throttlerOptions)],
49
+ providers: [trustProxyProvider, throttler_guard_1.DefaultThrottlerGuard],
50
+ exports: [throttler_guard_1.DefaultThrottlerGuard, throttler_1.ThrottlerModule],
51
+ };
52
+ }
53
+ };
54
+ exports.ThrottlerModule = ThrottlerModule;
55
+ exports.ThrottlerModule = ThrottlerModule = ThrottlerModule_1 = __decorate([
56
+ (0, common_1.Module)({})
57
+ ], ThrottlerModule);
58
+ //# sourceMappingURL=module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.js","sourceRoot":"","sources":["../lib/module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAiE;AACjE,iDAAkG;AAClG,0DAA6D;AAE7D,uDAA0D;AAGnD,IAAM,eAAe,uBAArB,MAAM,eAAe;IAK1B,MAAM,CAAC,OAAO,CAAC,OAA+B;QAE5C,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACvB,IAAA,mCAAmB,EAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QAC1C,CAAC;QAGD,MAAM,EAAE,UAAU,EAAE,GAAG,gBAAgB,EAAE,GAAG,OAAO,CAAC;QAEpD,OAAO;YACL,MAAM,EAAE,iBAAe;YACvB,OAAO,EAAE,CAAC,2BAAmB,CAAC,OAAO,CAAC,gBAAuB,CAAC,CAAC;YAC/D,SAAS,EAAE,CAAC,uCAAqB,CAAC;YAClC,OAAO,EAAE,CAAC,uCAAqB,EAAE,2BAAmB,CAAC;SACtD,CAAC;IACJ,CAAC;IAMD,MAAM,CAAC,YAAY,CAAC,OAAqD;QACvE,MAAM,kBAAkB,GAAa;YACnC,OAAO,EAAE,oBAAoB;YAC7B,UAAU,EAAE,KAAK,EAAE,GAAG,IAAW,EAAE,EAAE;gBACnC,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;oBAEvB,IAAI,OAAO,OAAO,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC;wBAC7C,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,CAAC;wBACjD,IAAA,mCAAmB,EAAC,MAAM,CAAC,CAAC;oBAC9B,CAAC;yBAAM,CAAC;wBAEN,IAAA,mCAAmB,EAAC,OAAO,CAAC,UAAU,CAAC,CAAC;oBAC1C,CAAC;gBACH,CAAC;gBACD,OAAO,IAAI,CAAC;YACd,CAAC;YACD,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;SAC7B,CAAC;QAGF,MAAM,EAAE,UAAU,EAAE,GAAG,gBAAgB,EAAE,GAAG,OAAO,CAAC;QAEpD,OAAO;YACL,MAAM,EAAE,iBAAe;YACvB,OAAO,EAAE,CAAC,2BAAmB,CAAC,YAAY,CAAC,gBAAuB,CAAC,CAAC;YACpE,SAAS,EAAE,CAAC,kBAAkB,EAAE,uCAAqB,CAAC;YACtD,OAAO,EAAE,CAAC,uCAAqB,EAAE,2BAAmB,CAAC;SACtD,CAAC;IACJ,CAAC;CACF,CAAA;AAvDY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,eAAe,CAuD3B"}
@@ -0,0 +1,9 @@
1
+ import { ExecutionContext } from '@nestjs/common';
2
+ import { ThrottlerGuard } from '@nestjs/throttler';
3
+ export declare class DefaultThrottlerGuard extends ThrottlerGuard {
4
+ getTracker(req: Record<string, any>): Promise<string>;
5
+ protected getRequestResponse(context: ExecutionContext): {
6
+ req: any;
7
+ res: any;
8
+ };
9
+ }
@@ -0,0 +1,27 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.DefaultThrottlerGuard = void 0;
10
+ const common_1 = require("@nestjs/common");
11
+ const throttler_1 = require("@nestjs/throttler");
12
+ const nestjs_common_1 = require("@nathapp/nestjs-common");
13
+ let DefaultThrottlerGuard = class DefaultThrottlerGuard extends throttler_1.ThrottlerGuard {
14
+ async getTracker(req) {
15
+ const ip = (0, nestjs_common_1.getClientIp)(req);
16
+ return ip || 'unknown';
17
+ }
18
+ getRequestResponse(context) {
19
+ const ctx = context.switchToHttp();
20
+ return { req: ctx.getRequest(), res: ctx.getResponse() };
21
+ }
22
+ };
23
+ exports.DefaultThrottlerGuard = DefaultThrottlerGuard;
24
+ exports.DefaultThrottlerGuard = DefaultThrottlerGuard = __decorate([
25
+ (0, common_1.Injectable)()
26
+ ], DefaultThrottlerGuard);
27
+ //# sourceMappingURL=throttler.guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttler.guard.js","sourceRoot":"","sources":["../lib/throttler.guard.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAA8D;AAC9D,iDAAmD;AACnD,0DAAqD;AAG9C,IAAM,qBAAqB,GAA3B,MAAM,qBAAsB,SAAQ,0BAAc;IAKvD,KAAK,CAAC,UAAU,CAAC,GAAwB;QACvC,MAAM,EAAE,GAAG,IAAA,2BAAW,EAAC,GAAG,CAAC,CAAC;QAC5B,OAAO,EAAE,IAAI,SAAS,CAAC;IACzB,CAAC;IAKS,kBAAkB,CAAC,OAAyB;QACpD,MAAM,GAAG,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;QACnC,OAAO,EAAE,GAAG,EAAE,GAAG,CAAC,UAAU,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,WAAW,EAAE,EAAE,CAAC;IAC3D,CAAC;CACF,CAAA;AAjBY,sDAAqB;gCAArB,qBAAqB;IADjC,IAAA,mBAAU,GAAE;GACA,qBAAqB,CAiBjC"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@nathapp/nestjs-throttler",
3
+ "version": "1.0.1",
4
+ "description": "NestJS Throttler with trusted proxy-aware IP resolution",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "clean": "rimraf dist/ .turbo/",
9
+ "build": "rimraf dist && tsc -p tsconfig.json",
10
+ "precommit": "lint-staged",
11
+ "test": "jest --config ./test/jest-test.json",
12
+ "test:cov": "jest --config ./test/jest-test.json --coverage",
13
+ "test:watch": "jest --config ./test/jest-test.json --watch",
14
+ "lint": "eslint -c .eslintrc.js"
15
+ },
16
+ "author": "William Khoo",
17
+ "license": "ISC",
18
+ "engines": {
19
+ "node": ">=16"
20
+ },
21
+ "peerDependencies": {
22
+ "@nathapp/nestjs-common": "^2.1.2",
23
+ "@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0",
24
+ "@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0",
25
+ "@nestjs/throttler": "^6.0.0 || ^7.0.0"
26
+ },
27
+ "devDependencies": {
28
+ "@nathapp/eslint-config": "1.0.1",
29
+ "@nathapp/nestjs-common": "^2.1.2",
30
+ "@nathapp/typescript-config": "1.0.1",
31
+ "@nestjs/common": "^11.0.0",
32
+ "@nestjs/core": "^11.0.0",
33
+ "@nestjs/platform-express": "^11.1.13",
34
+ "@nestjs/platform-fastify": "^11.1.13",
35
+ "@nestjs/testing": "^11.0.0",
36
+ "@nestjs/throttler": "^6.0.1",
37
+ "@types/jest": "^29.5.2",
38
+ "@types/supertest": "^6.0.3",
39
+ "eslint": "^8.57.0",
40
+ "express": "^5.2.1",
41
+ "fastify": "^5.7.4",
42
+ "jest": "^29.5.0",
43
+ "reflect-metadata": "^0.1.13",
44
+ "rxjs": "^7.8.0",
45
+ "supertest": "^7.2.2",
46
+ "ts-jest": "^29.1.0",
47
+ "typescript": "^5"
48
+ },
49
+ "directories": {
50
+ "lib": "lib"
51
+ }
52
+ }