@divine-lab/request 1.0.1 → 1.0.3
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,9 +10,9 @@ type httpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
|
10
10
|
* @property {any} data - any additional data to include in the response
|
|
11
11
|
*/
|
|
12
12
|
type SuccessResponse = {
|
|
13
|
-
title
|
|
14
|
-
detail
|
|
15
|
-
data
|
|
13
|
+
title?: string;
|
|
14
|
+
detail?: string;
|
|
15
|
+
data?: any;
|
|
16
16
|
};
|
|
17
17
|
/** Sends a standardized error response.
|
|
18
18
|
* Uses the RFC 9457 Format.
|
|
@@ -38,7 +38,7 @@ export declare function errorResponse(res: FastifyReply, error: ServerErrorKey,
|
|
|
38
38
|
* return successResponse(res, 200, "User Created", "The user was created successfully, you should recieve an email in a short while.", { id: 1, name: "Example" });
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
|
-
export declare function successResponse(res: FastifyReply, status
|
|
41
|
+
export declare function successResponse(res: FastifyReply, status?: number, { title, detail, data }?: SuccessResponse): void;
|
|
42
42
|
/** Utility function to register a route with standardized request typing and optional schema validation.
|
|
43
43
|
* @param fastify - The Fastify instance to register the route on
|
|
44
44
|
* @param method - HTTP method (e.g., "get", "post")
|
|
@@ -12,7 +12,7 @@ const colors_1 = require("@divine-lab/logger/colors");
|
|
|
12
12
|
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
|
-
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." :
|
|
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
16
|
/** Sends a standardized error response.
|
|
17
17
|
* Uses the RFC 9457 Format.
|
|
18
18
|
* @param res - Fastify reply object
|
|
@@ -49,7 +49,7 @@ function errorResponse(res, error, { title, detail, type, data, status } = {}) {
|
|
|
49
49
|
* return successResponse(res, 200, "User Created", "The user was created successfully, you should recieve an email in a short while.", { id: 1, name: "Example" });
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
|
-
function successResponse(res, status = 200, { title = "Success", detail = "Success", data = "Success" }) {
|
|
52
|
+
function successResponse(res, status = 200, { title = "Success", detail = "Success", data = null } = { title: "Success", detail: "Success", data: null }) {
|
|
53
53
|
if (API_LOG)
|
|
54
54
|
logger_1.default.raw(`${new Date().toISOString()} ${(0, colors_1.colorize)("green", "[API]")} - ${(0, colors_1.colorize)("green", status)} - ${res.request.url} : ${(0, colors_1.colorize)("gray", title)}`);
|
|
55
55
|
res.code(status).send({
|