@divine-lab/request 1.1.0 → 1.2.0

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.
@@ -13,6 +13,8 @@ const APIError_1 = require("../errors/APIError");
13
13
  const logger_1 = __importDefault(require("@divine-lab/logger"));
14
14
  const API_LOG = process.env.DIVINE_LAB_REQUEST_API_LOG === "true";
15
15
  logger_1.default.raw(`${(0, colors_1.colorize)("blue", "[API]")} - API logging is ${API_LOG ? (0, colors_1.colorize)("green", "enabled") : (0, colors_1.colorize)("red", "disabled")} - ${API_LOG ? "API requests and responses will be logged." : `to enable, set ${(0, colors_1.colorize)("yellow", "DIVINE_LAB_REQUEST_API_LOG")}=true in environment variables`}`);
16
+ const INSTANCE_BASE = process.env.DIVINE_LAB_REQUEST_INSTANCE_BASE || "";
17
+ logger_1.default.raw(`${(0, colors_1.colorize)("blue", "[API]")} - API instance base path is set to ${(0, colors_1.colorize)("yellow", INSTANCE_BASE)}. Configure this with the ${(0, colors_1.colorize)("yellow", "DIVINE_LAB_REQUEST_INSTANCE_BASE")} environment variable.`);
16
18
  /** Sends a standardized error response.
17
19
  * Uses the RFC 9457 Format.
18
20
  * @param res - Fastify reply object
@@ -30,7 +32,7 @@ function errorResponse(res, error, { title, detail, type, data, status } = {}) {
30
32
  logger_1.default.raw(`${new Date().toISOString()} ${(0, colors_1.colorize)("red", "[API]")} - ${(0, colors_1.colorize)("gray", res.request.ip)} - ${(0, colors_1.colorize)("red", (status || errorDef.status))} - ${res.request.url} : ${(0, colors_1.colorize)("gray", `${title || errorDef.title} - ${detail || errorDef.detail}`)}`);
31
33
  res.code(status || errorDef.status).send({
32
34
  status: status || errorDef.status,
33
- instance: res.request.url,
35
+ instance: `${INSTANCE_BASE}${res.request.url}`,
34
36
  title: title || errorDef.title,
35
37
  detail: detail || errorDef.detail,
36
38
  type: type || errorDef.type,
@@ -101,7 +103,7 @@ async function globalErrorHandler(error, _request, reply) {
101
103
  if (error instanceof APIError_1.APIError)
102
104
  return errorResponse(reply, error.error, { title: error.title, detail: error.detail, type: error.type, data: error.data, status: error.status });
103
105
  if (error.statusCode && error.statusCode === 429)
104
- return errorResponse(reply, "TOO_MANY_REQUESTS", { title: "Too Many Requests", detail: "You have sent too many requests in a given amount of time. Please try again later.", data: error.message.match(/\d+\s+\w+/)?.[0], status: 429 });
106
+ return errorResponse(reply, "TOO_MANY_REQUESTS", { title: "Too Many Requests", detail: "You have sent too many requests in a given amount of time. Please try again later.", data: `Retry in ${error.message.match(/\d+\s+\w+/)?.[0]}`, status: 429 });
105
107
  if (error.validation) {
106
108
  const data = [];
107
109
  for (const err of error.validation)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@divine-lab/request",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Simple utility for standardizing request and response objects.",
5
5
  "author": {
6
6
  "name": "Ishpreet Singh",
package/readme.md CHANGED
@@ -110,8 +110,16 @@ Additionally if a `400 Bad request` is made, then this handler should handle it
110
110
 
111
111
  ### Environment Variables
112
112
 
113
- Supports API response logging via <a>`@divine-lab/logger`</a>.
114
- Enable by setting ENV Variable `DIVINE_LAB_REQUEST_API_LOG` to `true`.
113
+ - Supports API response logging via <a>`@divine-lab/logger`</a>.
114
+ Enable by setting ENV Variable `DIVINE_LAB_REQUEST_API_LOG` to `true`.
115
+ - Supports base path for `instance` property of error responses via `DIVINE_LAB_REQUEST_INSTANCE_BASE`,
116
+ Defaults to empty string.
117
+
118
+ #### Updates:
119
+
120
+ - 1.1.0: added rate limit error response in backend errors and handling it in global error handler (when using `@fastify/rate-limit`)
121
+ - 1.1.1: fixed data response for the rate-limit in global error handler for fastify.
122
+ - 1.2.0: added support for instance base path in error responses via environment variable.
115
123
 
116
124
  #### Contact:
117
125