@konplit-services/common 1.0.255 → 1.0.256

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.
@@ -10,3 +10,4 @@ export * from "./invalid-input-error";
10
10
  export * from "./service-not-available";
11
11
  export * from "./axios-request-error";
12
12
  export * from "./gateway-error";
13
+ export * from "./toomany-request.errors";
@@ -26,3 +26,4 @@ __exportStar(require("./invalid-input-error"), exports);
26
26
  __exportStar(require("./service-not-available"), exports);
27
27
  __exportStar(require("./axios-request-error"), exports);
28
28
  __exportStar(require("./gateway-error"), exports);
29
+ __exportStar(require("./toomany-request.errors"), exports);
@@ -0,0 +1,17 @@
1
+ import { Status } from "../helper/status";
2
+ import { CustomError } from "./custom-error";
3
+ import { ErrorReturn } from "./error.interface";
4
+ export declare class TooManyRequestError extends CustomError {
5
+ message: string;
6
+ private error_data;
7
+ readonly statusCode: number;
8
+ constructor(message: string, error_data: ErrorReturn);
9
+ sequalizeErrors(): {
10
+ status: Status;
11
+ errors: {
12
+ message: string;
13
+ error_code: string;
14
+ code: number;
15
+ }[];
16
+ };
17
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TooManyRequestError = void 0;
4
+ const status_1 = require("../helper/status");
5
+ const custom_error_1 = require("./custom-error");
6
+ class TooManyRequestError extends custom_error_1.CustomError {
7
+ constructor(message, error_data) {
8
+ super(message);
9
+ this.message = message;
10
+ this.error_data = error_data;
11
+ this.statusCode = 429;
12
+ this.error_data = error_data;
13
+ Object.setPrototypeOf(this, TooManyRequestError.prototype);
14
+ }
15
+ sequalizeErrors() {
16
+ return {
17
+ status: status_1.Status.Failed,
18
+ errors: [
19
+ {
20
+ message: this.message,
21
+ error_code: this.error_data.error_code,
22
+ code: this.error_data.code,
23
+ },
24
+ ],
25
+ };
26
+ }
27
+ }
28
+ exports.TooManyRequestError = TooManyRequestError;
@@ -139,6 +139,10 @@ export declare const error_codes: {
139
139
  error_code: string;
140
140
  code: number;
141
141
  };
142
+ TOO_MANY_REQUEST: {
143
+ error_code: string;
144
+ code: number;
145
+ };
142
146
  SERVER_ERROR: {
143
147
  error_code: string;
144
148
  code: number;
@@ -147,6 +147,10 @@ exports.error_codes = {
147
147
  error_code: "RESOUCE_NOT_FOUND",
148
148
  code: 400006,
149
149
  },
150
+ TOO_MANY_REQUEST: {
151
+ error_code: "TOO_MANY_REQUEST",
152
+ code: 400009,
153
+ },
150
154
  SERVER_ERROR: {
151
155
  error_code: "SERVER_ERROR",
152
156
  code: 500000,
@@ -7,3 +7,4 @@ export * from "./validate-request";
7
7
  export * from "./validate-fields";
8
8
  export * from "./mono-webhook";
9
9
  export * from "./request-log";
10
+ export * from "./rate-limit";
@@ -23,3 +23,4 @@ __exportStar(require("./validate-request"), exports);
23
23
  __exportStar(require("./validate-fields"), exports);
24
24
  __exportStar(require("./mono-webhook"), exports);
25
25
  __exportStar(require("./request-log"), exports);
26
+ __exportStar(require("./rate-limit"), exports);
@@ -0,0 +1,2 @@
1
+ import { NextFunction, Request, Response } from "express";
2
+ export declare const konplitRateLimit: (duration: number) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.konplitRateLimit = void 0;
13
+ const error_codes_1 = require("../helper/errorCodes/error-codes");
14
+ const errors_1 = require("../errors");
15
+ const redis_1 = require("../redis");
16
+ //durations in seconds
17
+ const konplitRateLimit = (duration) => {
18
+ return (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
19
+ const clientIp = req.ip || req.socket.remoteAddress || "";
20
+ const key = `rate_limit:${clientIp}`;
21
+ try {
22
+ const lastRequestTime = yield redis_1.redisWrapper.get(key);
23
+ if (lastRequestTime) {
24
+ throw new errors_1.TooManyRequestError(`Too many requests, please wait ${duration} seconds.`, error_codes_1.error_codes.TOO_MANY_REQUEST);
25
+ }
26
+ // Set the rate limit key with an expiry of 6 seconds
27
+ yield redis_1.redisWrapper.client.set(key, Date.now(), "EX", duration);
28
+ next();
29
+ }
30
+ catch (error) {
31
+ console.error("Redis error:", error);
32
+ if (error instanceof errors_1.CustomError) {
33
+ throw error;
34
+ }
35
+ throw new errors_1.ServiceNotAvailableError("Please try again server error", error_codes_1.error_codes.SERVER_ERROR);
36
+ }
37
+ });
38
+ };
39
+ exports.konplitRateLimit = konplitRateLimit;
@@ -7,6 +7,8 @@ exports.useMorgan = void 0;
7
7
  const morgan_1 = __importDefault(require("morgan"));
8
8
  // Create a custom Morgan logging function
9
9
  exports.useMorgan = (0, morgan_1.default)((tokens, req, res) => {
10
+ const clientIp = req.ip || req.socket.remoteAddress || "";
11
+ console.log("user ip log", clientIp);
10
12
  return [
11
13
  tokens["remote-addr"](req, res),
12
14
  tokens.method(req, res),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konplit-services/common",
3
- "version": "1.0.255",
3
+ "version": "1.0.256",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",