@divine-lab/request 1.0.5 → 1.1.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.
@@ -52,6 +52,12 @@ export declare const SERVER_ERRORS: {
52
52
  readonly title: "Conflict";
53
53
  readonly detail: "The request conflicts with the current state of the server.";
54
54
  };
55
+ readonly TOO_MANY_REQUESTS: {
56
+ readonly status: 429;
57
+ readonly type: "about:blank";
58
+ readonly title: "Too Many Requests";
59
+ readonly detail: "The user has sent too many requests in a given amount of time.";
60
+ };
55
61
  };
56
62
  export type ServerErrorKey = keyof typeof SERVER_ERRORS;
57
63
  export type ServerErrorOptions = {
@@ -55,4 +55,10 @@ exports.SERVER_ERRORS = {
55
55
  title: "Conflict",
56
56
  detail: "The request conflicts with the current state of the server.",
57
57
  },
58
+ TOO_MANY_REQUESTS: {
59
+ status: 429,
60
+ type: "about:blank",
61
+ title: "Too Many Requests",
62
+ detail: "The user has sent too many requests in a given amount of time.",
63
+ },
58
64
  };
@@ -100,8 +100,14 @@ function REGISTER_ROUTE(fastify, method, path, handler, options) {
100
100
  async function globalErrorHandler(error, _request, reply) {
101
101
  if (error instanceof APIError_1.APIError)
102
102
  return errorResponse(reply, error.error, { title: error.title, detail: error.detail, type: error.type, data: error.data, status: error.status });
103
- if (error.validation)
104
- return errorResponse(reply, "BAD_REQUEST", { detail: error.message, data: null });
103
+ 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 });
105
+ if (error.validation) {
106
+ const data = [];
107
+ for (const err of error.validation)
108
+ data.push({ property: err.instancePath ? err.instancePath.substring(1) : err.params.missingProperty || "unknown", message: `${error.validationContext} ${err.message}` });
109
+ return errorResponse(reply, "BAD_REQUEST", { detail: "Invalid request data, try again with proper formatting", data: data });
110
+ }
105
111
  logger_1.default.error(`Error processing request: ${error.message}`);
106
112
  return errorResponse(reply, "INTERNAL_SERVER_ERROR", { title: "Internal Server Error", detail: "An unexpected error occurred", type: "about:blank", data: null, status: 500 });
107
113
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@divine-lab/request",
3
- "version": "1.0.5",
3
+ "version": "1.1.0",
4
4
  "description": "Simple utility for standardizing request and response objects.",
5
5
  "author": {
6
6
  "name": "Ishpreet Singh",