@edirect/rate-limit-module 11.0.62 → 11.0.64
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/README.md +584 -0
- package/dist/package.json +41 -0
- package/dist/src/decorators/rate-limiting-rule.decorator.d.ts +10 -0
- package/dist/src/decorators/rate-limiting-rule.decorator.d.ts.map +1 -0
- package/dist/src/decorators/rate-limiting-rule.decorator.js +7 -0
- package/dist/src/guards/rate-limiting.guard.d.ts +21 -0
- package/dist/src/guards/rate-limiting.guard.d.ts.map +1 -0
- package/dist/src/guards/rate-limiting.guard.js +248 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +14 -0
- package/dist/src/main.d.ts +2 -0
- package/dist/src/main.d.ts.map +1 -0
- package/dist/src/main.js +9 -0
- package/dist/src/object.utils.d.ts +3 -0
- package/dist/src/object.utils.d.ts.map +1 -0
- package/dist/src/object.utils.js +38 -0
- package/dist/src/rate-limiting.module.d.ts +6 -0
- package/dist/src/rate-limiting.module.d.ts.map +1 -0
- package/dist/src/rate-limiting.module.js +26 -0
- package/dist/src/rules/dto/pagination.dto.d.ts +6 -0
- package/dist/src/rules/dto/pagination.dto.d.ts.map +1 -0
- package/dist/src/rules/dto/pagination.dto.js +42 -0
- package/dist/src/rules/dto/rule.dto.d.ts +24 -0
- package/dist/src/rules/dto/rule.dto.d.ts.map +1 -0
- package/dist/src/rules/dto/rule.dto.js +198 -0
- package/dist/src/rules/interface/pagination.interface.d.ts +7 -0
- package/dist/src/rules/interface/pagination.interface.d.ts.map +1 -0
- package/dist/src/rules/interface/pagination.interface.js +2 -0
- package/dist/src/rules/interface/rule.interface.d.ts +29 -0
- package/dist/src/rules/interface/rule.interface.d.ts.map +1 -0
- package/dist/src/rules/interface/rule.interface.js +2 -0
- package/dist/src/rules/rule.controller.d.ts +14 -0
- package/dist/src/rules/rule.controller.d.ts.map +1 -0
- package/dist/src/rules/rule.controller.js +87 -0
- package/dist/src/rules/rule.module.d.ts +5 -0
- package/dist/src/rules/rule.module.d.ts.map +1 -0
- package/dist/src/rules/rule.module.js +36 -0
- package/dist/src/rules/rule.repository.d.ts +15 -0
- package/dist/src/rules/rule.repository.d.ts.map +1 -0
- package/dist/src/rules/rule.repository.js +79 -0
- package/dist/src/rules/rule.schema.d.ts +166 -0
- package/dist/src/rules/rule.schema.d.ts.map +1 -0
- package/dist/src/rules/rule.schema.js +150 -0
- package/dist/src/rules/rule.service.d.ts +26 -0
- package/dist/src/rules/rule.service.d.ts.map +1 -0
- package/dist/src/rules/rule.service.js +204 -0
- package/dist/src/shared/cache/cache.service.d.ts +10 -0
- package/dist/src/shared/cache/cache.service.d.ts.map +1 -0
- package/dist/src/shared/cache/cache.service.js +29 -0
- package/dist/src/shared/dsl/dsl.interface.d.ts +43 -0
- package/dist/src/shared/dsl/dsl.interface.d.ts.map +1 -0
- package/dist/src/shared/dsl/dsl.interface.js +9 -0
- package/dist/src/shared/dsl/dsl.service.d.ts +8 -0
- package/dist/src/shared/dsl/dsl.service.d.ts.map +1 -0
- package/dist/src/shared/dsl/dsl.service.js +87 -0
- package/dist/src/shared/redis/redis.module.d.ts +3 -0
- package/dist/src/shared/redis/redis.module.d.ts.map +1 -0
- package/dist/src/shared/redis/redis.module.js +32 -0
- package/dist/src/shared/redis/redis.service.d.ts +13 -0
- package/dist/src/shared/redis/redis.service.d.ts.map +1 -0
- package/dist/src/shared/redis/redis.service.js +53 -0
- package/dist/src/shared/rule-engine/interface/rule-engine.interface.d.ts +14 -0
- package/dist/src/shared/rule-engine/interface/rule-engine.interface.d.ts.map +1 -0
- package/dist/src/shared/rule-engine/interface/rule-engine.interface.js +2 -0
- package/dist/src/shared/rule-engine/rule-engine.module.d.ts +11 -0
- package/dist/src/shared/rule-engine/rule-engine.module.d.ts.map +1 -0
- package/dist/src/shared/rule-engine/rule-engine.module.js +51 -0
- package/dist/src/shared/rule-engine/rule-engine.service.d.ts +18 -0
- package/dist/src/shared/rule-engine/rule-engine.service.d.ts.map +1 -0
- package/dist/src/shared/rule-engine/rule-engine.service.js +134 -0
- package/dist/src/shared/shared.module.d.ts +3 -0
- package/dist/src/shared/shared.module.d.ts.map +1 -0
- package/dist/src/shared/shared.module.js +29 -0
- package/dist/src/validators/rule.constraint.d.ts +10 -0
- package/dist/src/validators/rule.constraint.d.ts.map +1 -0
- package/dist/src/validators/rule.constraint.js +49 -0
- package/dist/src/validators/rule.validator.d.ts +3 -0
- package/dist/src/validators/rule.validator.d.ts.map +1 -0
- package/dist/src/validators/rule.validator.js +16 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +23 -6
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var RateLimitingRuleGuard_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.RateLimitingRuleGuard = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const core_1 = require("@nestjs/core");
|
|
8
|
+
const rate_limiting_rule_decorator_1 = require("../decorators/rate-limiting-rule.decorator");
|
|
9
|
+
const object_utils_1 = require("../object.utils");
|
|
10
|
+
const rule_service_1 = require("../rules/rule.service");
|
|
11
|
+
const rule_engine_service_1 = require("../shared/rule-engine/rule-engine.service");
|
|
12
|
+
let RateLimitingRuleGuard = RateLimitingRuleGuard_1 = class RateLimitingRuleGuard {
|
|
13
|
+
reflector;
|
|
14
|
+
ruleService;
|
|
15
|
+
ruleEngine;
|
|
16
|
+
logger = new common_1.Logger(RateLimitingRuleGuard_1.name);
|
|
17
|
+
constructor(reflector, ruleService, ruleEngine) {
|
|
18
|
+
this.reflector = reflector;
|
|
19
|
+
this.ruleService = ruleService;
|
|
20
|
+
this.ruleEngine = ruleEngine;
|
|
21
|
+
}
|
|
22
|
+
async canActivate(context) {
|
|
23
|
+
try {
|
|
24
|
+
const handler = context.getHandler();
|
|
25
|
+
const ruleMetaData = this.reflector.get(rate_limiting_rule_decorator_1.RULE_METADATA, handler);
|
|
26
|
+
if (!ruleMetaData)
|
|
27
|
+
return true;
|
|
28
|
+
const req = context.switchToHttp().getRequest();
|
|
29
|
+
const entity = req.body;
|
|
30
|
+
const { action, partnerPath, providerPath, productTypePath, marketPath } = ruleMetaData;
|
|
31
|
+
const partner = this._getValueFromPathOrRoot(entity, partnerPath);
|
|
32
|
+
if (!partner)
|
|
33
|
+
return true;
|
|
34
|
+
const provider = this._getValueFromPathOrRoot(entity, providerPath);
|
|
35
|
+
const productType = this._getValueFromPathOrRoot(entity, productTypePath);
|
|
36
|
+
const market = this._getValueFromPathOrRoot(entity, marketPath);
|
|
37
|
+
const parameters = {
|
|
38
|
+
enabled: 'true',
|
|
39
|
+
partner: partner,
|
|
40
|
+
provider: provider,
|
|
41
|
+
market: market,
|
|
42
|
+
targetAction: action,
|
|
43
|
+
productType: productType,
|
|
44
|
+
page: 1,
|
|
45
|
+
limit: 10,
|
|
46
|
+
};
|
|
47
|
+
const rules = await this.ruleService.findAllWithoutPagination(parameters);
|
|
48
|
+
this.logger.debug(`[RATE LIMITER] ${rules?.length} rules found - ${JSON.stringify(rules)}`);
|
|
49
|
+
if (!rules || !rules?.length)
|
|
50
|
+
return true;
|
|
51
|
+
for (const rule of rules) {
|
|
52
|
+
await this._injectFnsResults(rule.variables, entity);
|
|
53
|
+
const contextVars = this._resolveVariables(rule.variables, entity);
|
|
54
|
+
const invalidExpressionMessage = this.ruleService.validateRuleExpression(rule.expression);
|
|
55
|
+
if (invalidExpressionMessage && invalidExpressionMessage.length) {
|
|
56
|
+
throw new common_1.HttpException(invalidExpressionMessage, common_1.HttpStatus.TOO_MANY_REQUESTS);
|
|
57
|
+
}
|
|
58
|
+
const invalidFunctions = this.ruleEngine.validateFunctionsAreValid(rule.expression);
|
|
59
|
+
if (invalidFunctions && invalidFunctions.length) {
|
|
60
|
+
throw new common_1.HttpException(`these functions are not allowed ${invalidFunctions.join(', ')}`, common_1.HttpStatus.TOO_MANY_REQUESTS);
|
|
61
|
+
}
|
|
62
|
+
if (rule.variablesRequired) {
|
|
63
|
+
const missingVars = await this.ruleEngine.validateExpressionVariables(rule.expression, contextVars);
|
|
64
|
+
if (missingVars && missingVars.length) {
|
|
65
|
+
this.logger.debug(`[RATE LIMITER] rule - ${rule.name} requires theses variables ${JSON.stringify(missingVars)}`);
|
|
66
|
+
throw new common_1.HttpException(`For rate limiting validation these variables are required ${JSON.stringify(missingVars)}`, common_1.HttpStatus.TOO_MANY_REQUESTS);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const safeContextVars = (0, object_utils_1.withDefaultNull)(contextVars ?? {});
|
|
70
|
+
const notAllowed = await this.ruleEngine.evaluate(rule.expression, safeContextVars);
|
|
71
|
+
this.logger.debug(`[RATE LIMITER] rule - ${rule.name} reached the limit`);
|
|
72
|
+
if (notAllowed)
|
|
73
|
+
throw new common_1.HttpException(rule.errorMessage || `Rate Limiting rule reached limit`, common_1.HttpStatus.TOO_MANY_REQUESTS);
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
if (err instanceof common_1.HttpException && err.getStatus?.() === 429) {
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
_getValueFromPathOrRoot(obj, path) {
|
|
85
|
+
if (!obj)
|
|
86
|
+
return undefined;
|
|
87
|
+
const valueFromPath = path
|
|
88
|
+
.split('.')
|
|
89
|
+
.reduce((acc, key) => acc && typeof acc === 'object' && key in acc
|
|
90
|
+
? acc[key]
|
|
91
|
+
: undefined, obj);
|
|
92
|
+
if (valueFromPath !== undefined) {
|
|
93
|
+
return valueFromPath;
|
|
94
|
+
}
|
|
95
|
+
return obj[path];
|
|
96
|
+
}
|
|
97
|
+
_resolveDateExpr(expr) {
|
|
98
|
+
const match = expr.match(/^date\(([-\d]+)([dMy])?\)$/);
|
|
99
|
+
if (!match)
|
|
100
|
+
return expr;
|
|
101
|
+
const amount = parseInt(match[1], 10);
|
|
102
|
+
const unit = match[2] || 'd';
|
|
103
|
+
const date = new Date();
|
|
104
|
+
switch (unit) {
|
|
105
|
+
case 'd':
|
|
106
|
+
date.setDate(date.getDate() + amount);
|
|
107
|
+
break;
|
|
108
|
+
case 'M':
|
|
109
|
+
date.setMonth(date.getMonth() + amount);
|
|
110
|
+
break;
|
|
111
|
+
case 'y':
|
|
112
|
+
date.setFullYear(date.getFullYear() + amount);
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
if (amount !== 0) {
|
|
116
|
+
date.setHours(0, 0, 0, 0);
|
|
117
|
+
}
|
|
118
|
+
return date.toISOString();
|
|
119
|
+
}
|
|
120
|
+
_resolveValue(raw, context, defaultValue) {
|
|
121
|
+
if (typeof raw === 'string') {
|
|
122
|
+
if (!raw) {
|
|
123
|
+
if (defaultValue)
|
|
124
|
+
return this._resolveValue(defaultValue, context);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (raw.startsWith(':')) {
|
|
128
|
+
const path = raw.slice(1);
|
|
129
|
+
const val = this._getByPath(context, path);
|
|
130
|
+
return this._resolveValue(val, context);
|
|
131
|
+
}
|
|
132
|
+
if (/^\s*fn:/.test(raw)) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (raw.startsWith('date(')) {
|
|
136
|
+
return this._resolveDateExpr(raw);
|
|
137
|
+
}
|
|
138
|
+
return raw;
|
|
139
|
+
}
|
|
140
|
+
if (typeof raw === 'object' &&
|
|
141
|
+
raw !== null &&
|
|
142
|
+
'from' in raw &&
|
|
143
|
+
'to' in raw) {
|
|
144
|
+
const obj = raw;
|
|
145
|
+
return {
|
|
146
|
+
from: this._resolveValue(obj.from, context),
|
|
147
|
+
to: this._resolveValue(obj.to, context),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return raw;
|
|
151
|
+
}
|
|
152
|
+
_resolveVariables(vars, context) {
|
|
153
|
+
if (!vars ||
|
|
154
|
+
!vars.queries ||
|
|
155
|
+
!Array.isArray(vars.queries) ||
|
|
156
|
+
!vars.queries.length)
|
|
157
|
+
return vars;
|
|
158
|
+
return {
|
|
159
|
+
queries: vars.queries.map((q) => ({
|
|
160
|
+
...q,
|
|
161
|
+
query: Array.isArray(q.query)
|
|
162
|
+
? q.query.map(entry => ({
|
|
163
|
+
...entry,
|
|
164
|
+
value: this._resolveValue(entry.value, context ?? {}, entry?.defaultValue),
|
|
165
|
+
}))
|
|
166
|
+
: [],
|
|
167
|
+
})),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
_resolveParams(vars, context) {
|
|
171
|
+
if (!vars.variables ||
|
|
172
|
+
!vars.variables.queries ||
|
|
173
|
+
!vars.variables.queries.length)
|
|
174
|
+
return {};
|
|
175
|
+
return {
|
|
176
|
+
...vars.variables,
|
|
177
|
+
queries: vars.variables.queries.map(q => ({
|
|
178
|
+
...q,
|
|
179
|
+
value: this._resolveValue(q.value, context, q?.defaultValue),
|
|
180
|
+
})),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
_getByPath(obj, path) {
|
|
184
|
+
if (!path)
|
|
185
|
+
return undefined;
|
|
186
|
+
const parts = path.replace(/\[(\d+)\]/g, '.$1').split('.');
|
|
187
|
+
return parts.reduce((acc, key) => {
|
|
188
|
+
if (acc && typeof acc === 'object') {
|
|
189
|
+
return acc[key];
|
|
190
|
+
}
|
|
191
|
+
return undefined;
|
|
192
|
+
}, obj);
|
|
193
|
+
}
|
|
194
|
+
async _injectFnsResults(variables, entity) {
|
|
195
|
+
if (!variables || !variables.functions)
|
|
196
|
+
return;
|
|
197
|
+
const fns = variables.functions;
|
|
198
|
+
for (const fn of fns) {
|
|
199
|
+
const contextVars = this._resolveParams(fn, entity);
|
|
200
|
+
const value = await this.ruleEngine.getResultCustomFunction(fn.fn, contextVars);
|
|
201
|
+
this._updateFnsResults(variables, fn.name, value);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
_updateFnsResults(vars, fnName, value) {
|
|
205
|
+
if (!vars.queries || !Array.isArray(vars.queries))
|
|
206
|
+
return;
|
|
207
|
+
vars.queries.forEach((q) => {
|
|
208
|
+
if (!Array.isArray(q.query))
|
|
209
|
+
return;
|
|
210
|
+
q.query.forEach(cond => {
|
|
211
|
+
if (/^\s*fn:/.test(cond.value) || typeof cond.value === 'object') {
|
|
212
|
+
if (typeof cond.value === 'object') {
|
|
213
|
+
if (cond.value.to.replace(/^fn:/, '') === fnName) {
|
|
214
|
+
if (value) {
|
|
215
|
+
cond.value.to = value;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
cond.value.to = cond?.defaultValue?.to;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const fromStr = typeof cond.value?.from === 'string' ? cond.value.from : '';
|
|
222
|
+
if (fromStr.replace(/^fn:/, '') === fnName) {
|
|
223
|
+
if (value) {
|
|
224
|
+
cond.value.from = value;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
cond.value.from = cond?.defaultValue?.from;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const name = cond.value.replace(/^fn:/, '');
|
|
233
|
+
if (name === fnName) {
|
|
234
|
+
cond.value = value;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
exports.RateLimitingRuleGuard = RateLimitingRuleGuard;
|
|
242
|
+
exports.RateLimitingRuleGuard = RateLimitingRuleGuard = RateLimitingRuleGuard_1 = tslib_1.__decorate([
|
|
243
|
+
(0, common_1.Injectable)(),
|
|
244
|
+
tslib_1.__param(2, (0, common_1.Inject)('RULE_ENGINE_INSTANCE')),
|
|
245
|
+
tslib_1.__metadata("design:paramtypes", [core_1.Reflector,
|
|
246
|
+
rule_service_1.RuleService,
|
|
247
|
+
rule_engine_service_1.RuleEngineService])
|
|
248
|
+
], RateLimitingRuleGuard);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './decorators/rate-limiting-rule.decorator';
|
|
2
|
+
export * from './guards/rate-limiting.guard';
|
|
3
|
+
export * from './rate-limiting.module';
|
|
4
|
+
export * from './rules/rule.service';
|
|
5
|
+
export * from './shared/dsl/dsl.interface';
|
|
6
|
+
export * from './shared/dsl/dsl.service';
|
|
7
|
+
export * from './shared/rule-engine/rule-engine.module';
|
|
8
|
+
export * from './validators/rule.constraint';
|
|
9
|
+
export declare const RULE_ENGINE_INSTANCE = "RULE_ENGINE_INSTANCE";
|
|
10
|
+
export declare const CUSTOM_RULE_FUNCTIONS = "CUSTOM_RULE_FUNCTIONS";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2CAA2C,CAAC;AAC1D,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,yCAAyC,CAAC;AACxD,cAAc,8BAA8B,CAAC;AAE7C,eAAO,MAAM,oBAAoB,yBAAyB,CAAC;AAC3D,eAAO,MAAM,qBAAqB,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CUSTOM_RULE_FUNCTIONS = exports.RULE_ENGINE_INSTANCE = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
tslib_1.__exportStar(require("./decorators/rate-limiting-rule.decorator"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./guards/rate-limiting.guard"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./rate-limiting.module"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./rules/rule.service"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./shared/dsl/dsl.interface"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./shared/dsl/dsl.service"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./shared/rule-engine/rule-engine.module"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./validators/rule.constraint"), exports);
|
|
13
|
+
exports.RULE_ENGINE_INSTANCE = 'RULE_ENGINE_INSTANCE';
|
|
14
|
+
exports.CUSTOM_RULE_FUNCTIONS = 'CUSTOM_RULE_FUNCTIONS';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/main.ts"],"names":[],"mappings":""}
|
package/dist/src/main.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const core_1 = require("@nestjs/core");
|
|
4
|
+
const rate_limiting_module_1 = require("./rate-limiting.module");
|
|
5
|
+
async function bootstrap() {
|
|
6
|
+
const app = await core_1.NestFactory.create(rate_limiting_module_1.RateLimitingModule);
|
|
7
|
+
await app.listen(process.env.PORT ?? 3040);
|
|
8
|
+
}
|
|
9
|
+
bootstrap();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.utils.d.ts","sourceRoot":"","sources":["../../src/object.utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,GAAI,CAAC,SAAS,MAAM,EAAE,SAAS,CAAC,KAAG,CAqB9D,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,YAAY,MAAM,KAAG,MAAM,GAAG,SAgB5D,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateDepth = exports.withDefaultNull = void 0;
|
|
4
|
+
const withDefaultNull = (context) => {
|
|
5
|
+
return new Proxy(context, {
|
|
6
|
+
get(target, prop, receiver) {
|
|
7
|
+
const value = Reflect.get(target, prop, receiver);
|
|
8
|
+
if (value === undefined) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
if (value !== null &&
|
|
12
|
+
typeof value === 'object' &&
|
|
13
|
+
!Array.isArray(value) &&
|
|
14
|
+
value.constructor === Object) {
|
|
15
|
+
return (0, exports.withDefaultNull)(value);
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
exports.withDefaultNull = withDefaultNull;
|
|
22
|
+
const calculateDepth = (expression) => {
|
|
23
|
+
if (!expression)
|
|
24
|
+
return;
|
|
25
|
+
let depth = 0;
|
|
26
|
+
let maxDepth = 0;
|
|
27
|
+
for (const char of expression) {
|
|
28
|
+
if (char === '(') {
|
|
29
|
+
depth++;
|
|
30
|
+
maxDepth = Math.max(maxDepth, depth);
|
|
31
|
+
}
|
|
32
|
+
else if (char === ')') {
|
|
33
|
+
depth--;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return depth;
|
|
37
|
+
};
|
|
38
|
+
exports.calculateDepth = calculateDepth;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
export declare const RATE_LIMIT_DB_CONNECTION = "RATE_LIMIT_DB_CONNECTION";
|
|
3
|
+
export declare class RateLimitingModule {
|
|
4
|
+
static forRoot(connectionToken?: string): DynamicModule;
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=rate-limiting.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limiting.module.d.ts","sourceRoot":"","sources":["../../src/rate-limiting.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,aAAa,EAAU,MAAM,gBAAgB,CAAC;AAK/D,eAAO,MAAM,wBAAwB,6BAA6B,CAAC;AAEnE,qBAEa,kBAAkB;IAC7B,MAAM,CAAC,OAAO,CAAC,eAAe,SAAwB,GAAG,aAAa;CASvE"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var RateLimitingModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.RateLimitingModule = exports.RATE_LIMIT_DB_CONNECTION = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const core_1 = require("@nestjs/core");
|
|
8
|
+
const rule_module_1 = require("./rules/rule.module");
|
|
9
|
+
const rule_constraint_1 = require("./validators/rule.constraint");
|
|
10
|
+
exports.RATE_LIMIT_DB_CONNECTION = 'RATE_LIMIT_DB_CONNECTION';
|
|
11
|
+
let RateLimitingModule = RateLimitingModule_1 = class RateLimitingModule {
|
|
12
|
+
static forRoot(connectionToken = 'DATABASE_CONNECTION') {
|
|
13
|
+
return {
|
|
14
|
+
global: true,
|
|
15
|
+
module: RateLimitingModule_1,
|
|
16
|
+
imports: [rule_module_1.RuleModule.register(connectionToken)],
|
|
17
|
+
providers: [core_1.Reflector, rule_constraint_1.IsValidExpressionConstraint],
|
|
18
|
+
exports: [rule_module_1.RuleModule, core_1.Reflector],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.RateLimitingModule = RateLimitingModule;
|
|
23
|
+
exports.RateLimitingModule = RateLimitingModule = RateLimitingModule_1 = tslib_1.__decorate([
|
|
24
|
+
(0, common_1.Global)(),
|
|
25
|
+
(0, common_1.Module)({})
|
|
26
|
+
], RateLimitingModule);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pagination.dto.d.ts","sourceRoot":"","sources":["../../../../src/rules/dto/pagination.dto.ts"],"names":[],"mappings":"AAKA,qBAAa,aAAa;IAWxB,IAAI,SAAK;IAUT,KAAK,CAAC,qBAAM;IAIZ,KAAK,SAAM;CACZ"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PaginationDTO = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
7
|
+
const class_transformer_1 = require("class-transformer");
|
|
8
|
+
const class_validator_1 = require("class-validator");
|
|
9
|
+
class PaginationDTO {
|
|
10
|
+
page = 1;
|
|
11
|
+
order = -1;
|
|
12
|
+
limit = 10;
|
|
13
|
+
}
|
|
14
|
+
exports.PaginationDTO = PaginationDTO;
|
|
15
|
+
tslib_1.__decorate([
|
|
16
|
+
(0, swagger_1.ApiProperty)({ type: Number, required: false }),
|
|
17
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
18
|
+
(0, class_validator_1.IsInt)(),
|
|
19
|
+
(0, class_validator_1.IsOptional)(),
|
|
20
|
+
(0, class_transformer_1.Transform)(({ value }) => {
|
|
21
|
+
const parsed = Number.parseInt(value);
|
|
22
|
+
if (parsed === 0)
|
|
23
|
+
throw new common_1.BadRequestException('Page needs to be bigger than 0');
|
|
24
|
+
return parsed;
|
|
25
|
+
}),
|
|
26
|
+
tslib_1.__metadata("design:type", Object)
|
|
27
|
+
], PaginationDTO.prototype, "page", void 0);
|
|
28
|
+
tslib_1.__decorate([
|
|
29
|
+
(0, swagger_1.ApiProperty)({
|
|
30
|
+
type: Number,
|
|
31
|
+
required: false,
|
|
32
|
+
default: -1,
|
|
33
|
+
description: 'You can add more than one field by using trailing commas ex: 1,-1',
|
|
34
|
+
}),
|
|
35
|
+
(0, class_validator_1.IsOptional)(),
|
|
36
|
+
tslib_1.__metadata("design:type", Object)
|
|
37
|
+
], PaginationDTO.prototype, "order", void 0);
|
|
38
|
+
tslib_1.__decorate([
|
|
39
|
+
(0, swagger_1.ApiProperty)({ type: Number, required: false, default: 10 }),
|
|
40
|
+
(0, class_validator_1.IsOptional)(),
|
|
41
|
+
tslib_1.__metadata("design:type", Object)
|
|
42
|
+
], PaginationDTO.prototype, "limit", void 0);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { IFindRule, IRule } from '../interface/rule.interface';
|
|
2
|
+
import { PaginationDTO } from './pagination.dto';
|
|
3
|
+
export declare class CreateRuleDTO implements IRule {
|
|
4
|
+
name: string;
|
|
5
|
+
expression: string;
|
|
6
|
+
targetAction: string;
|
|
7
|
+
errorMessage?: string;
|
|
8
|
+
variables?: Record<string, unknown>;
|
|
9
|
+
variablesRequired?: boolean;
|
|
10
|
+
productType?: string;
|
|
11
|
+
partner?: string;
|
|
12
|
+
market?: string;
|
|
13
|
+
provider?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class FindRuleDTO extends PaginationDTO implements IFindRule {
|
|
16
|
+
name?: string;
|
|
17
|
+
targetAction?: string;
|
|
18
|
+
partner?: string;
|
|
19
|
+
productType?: string;
|
|
20
|
+
isEnabled?: boolean;
|
|
21
|
+
provider?: string;
|
|
22
|
+
market?: string;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=rule.dto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule.dto.d.ts","sourceRoot":"","sources":["../../../../src/rules/dto/rule.dto.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,qBAAa,aAAc,YAAW,KAAK;IAOzC,IAAI,EAAG,MAAM,CAAC;IASd,UAAU,EAAG,MAAM,CAAC;IAQpB,YAAY,EAAG,MAAM,CAAC;IAStB,YAAY,CAAC,EAAE,MAAM,CAAC;IAStB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IASpC,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAS5B,WAAW,CAAC,EAAE,MAAM,CAAC;IAQrB,OAAO,CAAC,EAAE,MAAM,CAAC;IASjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAShB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,WAAY,SAAQ,aAAc,YAAW,SAAS;IAQjE,IAAI,CAAC,EAAE,MAAM,CAAC;IASd,YAAY,CAAC,EAAE,MAAM,CAAC;IAStB,OAAO,CAAC,EAAE,MAAM,CAAC;IASjB,WAAW,CAAC,EAAE,MAAM,CAAC;IASrB,SAAS,CAAC,EAAE,OAAO,CAAC;IASpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IASlB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FindRuleDTO = exports.CreateRuleDTO = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
6
|
+
const class_validator_1 = require("class-validator");
|
|
7
|
+
const rule_validator_1 = require("../../validators/rule.validator");
|
|
8
|
+
const pagination_dto_1 = require("./pagination.dto");
|
|
9
|
+
class CreateRuleDTO {
|
|
10
|
+
name;
|
|
11
|
+
expression;
|
|
12
|
+
targetAction;
|
|
13
|
+
errorMessage;
|
|
14
|
+
variables;
|
|
15
|
+
variablesRequired;
|
|
16
|
+
productType;
|
|
17
|
+
partner;
|
|
18
|
+
market;
|
|
19
|
+
provider;
|
|
20
|
+
}
|
|
21
|
+
exports.CreateRuleDTO = CreateRuleDTO;
|
|
22
|
+
tslib_1.__decorate([
|
|
23
|
+
(0, swagger_1.ApiProperty)({
|
|
24
|
+
type: String,
|
|
25
|
+
required: true,
|
|
26
|
+
}),
|
|
27
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
28
|
+
(0, class_validator_1.IsString)(),
|
|
29
|
+
tslib_1.__metadata("design:type", String)
|
|
30
|
+
], CreateRuleDTO.prototype, "name", void 0);
|
|
31
|
+
tslib_1.__decorate([
|
|
32
|
+
(0, swagger_1.ApiProperty)({
|
|
33
|
+
type: String,
|
|
34
|
+
required: true,
|
|
35
|
+
}),
|
|
36
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
37
|
+
(0, class_validator_1.IsString)(),
|
|
38
|
+
(0, rule_validator_1.IsValidExpression)(),
|
|
39
|
+
tslib_1.__metadata("design:type", String)
|
|
40
|
+
], CreateRuleDTO.prototype, "expression", void 0);
|
|
41
|
+
tslib_1.__decorate([
|
|
42
|
+
(0, swagger_1.ApiProperty)({
|
|
43
|
+
type: String,
|
|
44
|
+
required: true,
|
|
45
|
+
}),
|
|
46
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
47
|
+
(0, class_validator_1.IsString)(),
|
|
48
|
+
tslib_1.__metadata("design:type", String)
|
|
49
|
+
], CreateRuleDTO.prototype, "targetAction", void 0);
|
|
50
|
+
tslib_1.__decorate([
|
|
51
|
+
(0, swagger_1.ApiProperty)({
|
|
52
|
+
type: String,
|
|
53
|
+
required: false,
|
|
54
|
+
}),
|
|
55
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
56
|
+
(0, class_validator_1.IsString)(),
|
|
57
|
+
(0, class_validator_1.IsOptional)(),
|
|
58
|
+
tslib_1.__metadata("design:type", String)
|
|
59
|
+
], CreateRuleDTO.prototype, "errorMessage", void 0);
|
|
60
|
+
tslib_1.__decorate([
|
|
61
|
+
(0, swagger_1.ApiProperty)({
|
|
62
|
+
type: Object,
|
|
63
|
+
required: false,
|
|
64
|
+
}),
|
|
65
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
66
|
+
(0, class_validator_1.IsObject)(),
|
|
67
|
+
(0, class_validator_1.IsOptional)(),
|
|
68
|
+
tslib_1.__metadata("design:type", Object)
|
|
69
|
+
], CreateRuleDTO.prototype, "variables", void 0);
|
|
70
|
+
tslib_1.__decorate([
|
|
71
|
+
(0, swagger_1.ApiProperty)({
|
|
72
|
+
type: Boolean,
|
|
73
|
+
required: false,
|
|
74
|
+
}),
|
|
75
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
76
|
+
(0, class_validator_1.IsBoolean)(),
|
|
77
|
+
(0, class_validator_1.IsOptional)(),
|
|
78
|
+
tslib_1.__metadata("design:type", Boolean)
|
|
79
|
+
], CreateRuleDTO.prototype, "variablesRequired", void 0);
|
|
80
|
+
tslib_1.__decorate([
|
|
81
|
+
(0, swagger_1.ApiProperty)({
|
|
82
|
+
type: String,
|
|
83
|
+
required: false,
|
|
84
|
+
}),
|
|
85
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
86
|
+
(0, class_validator_1.IsString)(),
|
|
87
|
+
(0, class_validator_1.IsOptional)(),
|
|
88
|
+
tslib_1.__metadata("design:type", String)
|
|
89
|
+
], CreateRuleDTO.prototype, "productType", void 0);
|
|
90
|
+
tslib_1.__decorate([
|
|
91
|
+
(0, swagger_1.ApiProperty)({
|
|
92
|
+
type: String,
|
|
93
|
+
required: true,
|
|
94
|
+
}),
|
|
95
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
96
|
+
(0, class_validator_1.IsString)(),
|
|
97
|
+
tslib_1.__metadata("design:type", String)
|
|
98
|
+
], CreateRuleDTO.prototype, "partner", void 0);
|
|
99
|
+
tslib_1.__decorate([
|
|
100
|
+
(0, swagger_1.ApiProperty)({
|
|
101
|
+
type: String,
|
|
102
|
+
required: true,
|
|
103
|
+
}),
|
|
104
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
105
|
+
(0, class_validator_1.IsString)(),
|
|
106
|
+
(0, class_validator_1.IsOptional)(),
|
|
107
|
+
tslib_1.__metadata("design:type", String)
|
|
108
|
+
], CreateRuleDTO.prototype, "market", void 0);
|
|
109
|
+
tslib_1.__decorate([
|
|
110
|
+
(0, swagger_1.ApiProperty)({
|
|
111
|
+
type: String,
|
|
112
|
+
required: false,
|
|
113
|
+
}),
|
|
114
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
115
|
+
(0, class_validator_1.IsString)(),
|
|
116
|
+
(0, class_validator_1.IsOptional)(),
|
|
117
|
+
tslib_1.__metadata("design:type", String)
|
|
118
|
+
], CreateRuleDTO.prototype, "provider", void 0);
|
|
119
|
+
class FindRuleDTO extends pagination_dto_1.PaginationDTO {
|
|
120
|
+
name;
|
|
121
|
+
targetAction;
|
|
122
|
+
partner;
|
|
123
|
+
productType;
|
|
124
|
+
isEnabled;
|
|
125
|
+
provider;
|
|
126
|
+
market;
|
|
127
|
+
}
|
|
128
|
+
exports.FindRuleDTO = FindRuleDTO;
|
|
129
|
+
tslib_1.__decorate([
|
|
130
|
+
(0, swagger_1.ApiProperty)({
|
|
131
|
+
type: String,
|
|
132
|
+
required: false,
|
|
133
|
+
}),
|
|
134
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
135
|
+
(0, class_validator_1.IsString)(),
|
|
136
|
+
(0, class_validator_1.IsOptional)(),
|
|
137
|
+
tslib_1.__metadata("design:type", String)
|
|
138
|
+
], FindRuleDTO.prototype, "name", void 0);
|
|
139
|
+
tslib_1.__decorate([
|
|
140
|
+
(0, swagger_1.ApiProperty)({
|
|
141
|
+
type: String,
|
|
142
|
+
required: false,
|
|
143
|
+
}),
|
|
144
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
145
|
+
(0, class_validator_1.IsString)(),
|
|
146
|
+
(0, class_validator_1.IsOptional)(),
|
|
147
|
+
tslib_1.__metadata("design:type", String)
|
|
148
|
+
], FindRuleDTO.prototype, "targetAction", void 0);
|
|
149
|
+
tslib_1.__decorate([
|
|
150
|
+
(0, swagger_1.ApiProperty)({
|
|
151
|
+
type: String,
|
|
152
|
+
required: true,
|
|
153
|
+
}),
|
|
154
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
155
|
+
(0, class_validator_1.IsString)(),
|
|
156
|
+
(0, class_validator_1.IsOptional)(),
|
|
157
|
+
tslib_1.__metadata("design:type", String)
|
|
158
|
+
], FindRuleDTO.prototype, "partner", void 0);
|
|
159
|
+
tslib_1.__decorate([
|
|
160
|
+
(0, swagger_1.ApiProperty)({
|
|
161
|
+
type: String,
|
|
162
|
+
required: true,
|
|
163
|
+
}),
|
|
164
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
165
|
+
(0, class_validator_1.IsString)(),
|
|
166
|
+
(0, class_validator_1.IsOptional)(),
|
|
167
|
+
tslib_1.__metadata("design:type", String)
|
|
168
|
+
], FindRuleDTO.prototype, "productType", void 0);
|
|
169
|
+
tslib_1.__decorate([
|
|
170
|
+
(0, swagger_1.ApiProperty)({
|
|
171
|
+
type: String,
|
|
172
|
+
required: false,
|
|
173
|
+
}),
|
|
174
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
175
|
+
(0, class_validator_1.IsBoolean)(),
|
|
176
|
+
(0, class_validator_1.IsOptional)(),
|
|
177
|
+
tslib_1.__metadata("design:type", Boolean)
|
|
178
|
+
], FindRuleDTO.prototype, "isEnabled", void 0);
|
|
179
|
+
tslib_1.__decorate([
|
|
180
|
+
(0, swagger_1.ApiProperty)({
|
|
181
|
+
type: String,
|
|
182
|
+
required: false,
|
|
183
|
+
}),
|
|
184
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
185
|
+
(0, class_validator_1.IsString)(),
|
|
186
|
+
(0, class_validator_1.IsOptional)(),
|
|
187
|
+
tslib_1.__metadata("design:type", String)
|
|
188
|
+
], FindRuleDTO.prototype, "provider", void 0);
|
|
189
|
+
tslib_1.__decorate([
|
|
190
|
+
(0, swagger_1.ApiProperty)({
|
|
191
|
+
type: String,
|
|
192
|
+
required: false,
|
|
193
|
+
}),
|
|
194
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
195
|
+
(0, class_validator_1.IsString)(),
|
|
196
|
+
(0, class_validator_1.IsOptional)(),
|
|
197
|
+
tslib_1.__metadata("design:type", String)
|
|
198
|
+
], FindRuleDTO.prototype, "market", void 0);
|