@jpbs/common 1.1.2 → 1.1.4

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.
@@ -1,13 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.errorHandler = void 0;
4
+ const axios_1 = require("axios");
4
5
  const custom_error_1 = require("../errors/custom-error");
5
6
  const http_status_codes_1 = require("http-status-codes");
6
7
  const errorHandler = (err, _req, res, _next) => {
8
+ var _a, _b;
7
9
  if (err instanceof custom_error_1.CustomError) {
8
10
  res.status(err.statusCode).send({ errors: err.serializeErrors() });
9
11
  return;
10
12
  }
13
+ if (err instanceof axios_1.AxiosError) {
14
+ const message = ((_b = (_a = err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || 'An error occurred with the external service';
15
+ res.status(http_status_codes_1.StatusCodes.BAD_GATEWAY).send({ errors: [{ message }] });
16
+ return;
17
+ }
11
18
  res.status(http_status_codes_1.StatusCodes.BAD_REQUEST).send({ errors: [{ message: 'Something went wrong' }] });
12
19
  };
13
20
  exports.errorHandler = errorHandler;
@@ -7,7 +7,8 @@ exports.verifyGatewayToken = void 0;
7
7
  const crypto_1 = __importDefault(require("crypto"));
8
8
  const notauthorized_error_1 = require("../errors/notauthorized-error");
9
9
  const verifySignature = (data, secret, timestamp, receivedSignature) => {
10
- const expectedSignature = crypto_1.default.createHmac("sha256", secret).update(`${data}.${timestamp}`).digest("hex");
10
+ const expectedData = data && data.length > 0 ? data : '';
11
+ const expectedSignature = crypto_1.default.createHmac("sha256", secret).update(`${expectedData}.${timestamp}`).digest("hex");
11
12
  return expectedSignature === receivedSignature;
12
13
  };
13
14
  const verifyGatewayToken = (gatewaySecret) => (req, _res, next) => {
@@ -21,7 +22,10 @@ const verifyGatewayToken = (gatewaySecret) => (req, _res, next) => {
21
22
  if (Math.abs(currentTime - requestTime) > 2 * 60 * 1000) {
22
23
  throw new notauthorized_error_1.NotAuthorizedError("Unauthorized request source: Expired Request");
23
24
  }
24
- if (!verifySignature(JSON.stringify(req.body), gatewaySecret, timestamp, signature)) {
25
+ const serializedData = req.method.toUpperCase() !== 'GET' && req.body && Object.keys(req.body).length > 0
26
+ ? JSON.stringify(req.body)
27
+ : '';
28
+ if (!verifySignature(serializedData, gatewaySecret, timestamp, signature)) {
25
29
  throw new notauthorized_error_1.NotAuthorizedError("Unauthorized request source: Invalid Signature");
26
30
  }
27
31
  next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jpbs/common",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
@@ -20,6 +20,7 @@
20
20
  "@types/jsonwebtoken": "^9.0.9",
21
21
  "@types/node": "^22.13.5",
22
22
  "amqplib": "^0.10.5",
23
+ "axios": "^1.7.9",
23
24
  "express": "^4.21.2",
24
25
  "express-validator": "^7.2.1",
25
26
  "http-status-codes": "^2.3.0",