@divine-lab/request 1.1.1 → 1.3.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.
@@ -4,6 +4,11 @@ import { type FastifyRequest } from "fastify/types/request";
4
4
  import { type FastifyReply } from "fastify/types/reply";
5
5
  import { type FastifyInstance } from "fastify";
6
6
  type httpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
7
+ type RateLimitConfig = {
8
+ max: number;
9
+ TimeWindow: string;
10
+ keyGenerator?: (req: FastifyRequest) => string;
11
+ };
7
12
  /** Type definition for the success response payload.
8
13
  * @property {string} title - short, human-readable summary of the response
9
14
  * @property {string} detail - detailed description of the response
@@ -64,10 +69,11 @@ export declare function successResponse(res: FastifyReply, status?: number, { ti
64
69
  * });
65
70
  * ```
66
71
  */
67
- export declare function REGISTER_ROUTE<BodyType extends TObject, QueryType extends TObject, ParamsType extends TObject>(fastify: FastifyInstance, method: httpMethod, path: string, handler: (req: FastifyRequest<{
72
+ export declare function REGISTER_ROUTE<BodyType extends TObject, QueryType extends TObject, ParamsType extends TObject, RateLimitType extends RateLimitConfig | undefined = undefined>(fastify: FastifyInstance, method: httpMethod, path: string, handler: (req: FastifyRequest<{
68
73
  Body: Static<BodyType>;
69
74
  Querystring: Static<QueryType>;
70
75
  Params: Static<ParamsType>;
76
+ RateLimit: RateLimitType;
71
77
  }>, reply: any) => void, options?: {
72
78
  schema?: {
73
79
  body?: BodyType;
@@ -75,7 +81,13 @@ export declare function REGISTER_ROUTE<BodyType extends TObject, QueryType exten
75
81
  params?: ParamsType;
76
82
  };
77
83
  preHandler?: Array<(req: FastifyRequest, reply: any) => void> | ((req: FastifyRequest, reply: any) => void);
78
- }): void;
84
+ } & (RateLimitType extends undefined ? {
85
+ config?: any;
86
+ } : {
87
+ config: {
88
+ rateLimit: RateLimitType;
89
+ };
90
+ })): void;
79
91
  /** Centralized error handler for Fastify.
80
92
  * @param error - The error object caught by Fastify
81
93
  * @param request - The incoming request object
@@ -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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@divine-lab/request",
3
- "version": "1.1.1",
3
+ "version": "1.3.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,13 +110,17 @@ 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.
115
117
 
116
118
  #### Updates:
117
119
 
118
120
  - 1.1.0: added rate limit error response in backend errors and handling it in global error handler (when using `@fastify/rate-limit`)
119
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.
123
+ - 1.3.0: added support for rate-limiting when registering routes in the REGISTER_ROUTE function.
120
124
 
121
125
  #### Contact:
122
126