devdad-express-utils 1.8.1 → 1.8.2

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.
@@ -33,7 +33,7 @@ import { Response } from "express";
33
33
  * (res) => res.cookie("refreshToken", refreshToken, HTTP_OPTIONS)
34
34
  * ]);
35
35
  */
36
- export declare const sendSuccess: (res: Response, data: any, message?: string, statusCode?: number, callbacks?: ((res: Response) => Response)[]) => Response;
36
+ export declare const sendSuccess: (res: Response, data: any, message?: string, statusCode?: number, callbacks?: ((res: Response) => void)[]) => Response;
37
37
  /**
38
38
  * Sends an error response immediately.
39
39
  *
@@ -33,6 +33,8 @@
33
33
  * ]);
34
34
  */
35
35
  export const sendSuccess = (res, data, message = "Success", statusCode = 200, callbacks) => {
36
+ if (res.headersSent)
37
+ return res;
36
38
  const response = {
37
39
  status: "success",
38
40
  success: true,
@@ -40,17 +42,20 @@ export const sendSuccess = (res, data, message = "Success", statusCode = 200, ca
40
42
  data,
41
43
  };
42
44
  res.status(statusCode);
43
- // Execute callbacks for chaining if provided
44
- if (callbacks && callbacks.length > 0) {
45
- let currentRes = res;
45
+ if (callbacks?.length) {
46
46
  for (const callback of callbacks) {
47
- if (typeof callback === 'function') {
48
- currentRes = callback(currentRes);
47
+ try {
48
+ callback(res);
49
+ }
50
+ catch (err) {
51
+ console.error("sendSuccess callback error:", err);
49
52
  }
50
53
  }
51
54
  }
52
- // Send response
53
- return res.json(response);
55
+ if (!res.headersSent) {
56
+ res.json(response);
57
+ }
58
+ return res;
54
59
  };
55
60
  /**
56
61
  * Sends an error response immediately.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devdad-express-utils",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "description": "Reusable Express.js utilities for error handling, async wrapping, and more",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",