devdad-express-utils 1.7.2 → 1.8.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.
- package/README.md +14 -30
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/responseFormatter.d.ts +1 -17
- package/dist/responseFormatter.js +11 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,19 +54,19 @@ throw new AppError("Validation failed", 400, ["Email is required"]);
|
|
|
54
54
|
|
|
55
55
|
### Response Formatting
|
|
56
56
|
|
|
57
|
-
Standardize API responses with consistent JSON structure and
|
|
57
|
+
Standardize API responses with consistent JSON structure and callback-based chaining.
|
|
58
58
|
|
|
59
59
|
```typescript
|
|
60
|
-
import { sendSuccess, sendError
|
|
60
|
+
import { sendSuccess, sendError } from "devdad-express-utils";
|
|
61
61
|
|
|
62
|
-
// Basic success response
|
|
62
|
+
// Basic success response
|
|
63
63
|
sendSuccess(res, { id: 1, name: "John" }, "User fetched", 200);
|
|
64
64
|
|
|
65
|
-
// Chain
|
|
66
|
-
sendSuccess(res, { id: 1, name: "John" }, "User fetched", 200
|
|
67
|
-
.cookie("session", "abc123")
|
|
68
|
-
.setHeader("X-Custom", "value")
|
|
69
|
-
|
|
65
|
+
// Chain methods using callbacks
|
|
66
|
+
sendSuccess(res, { id: 1, name: "John" }, "User fetched", 200, [
|
|
67
|
+
(res) => res.cookie("session", "abc123"),
|
|
68
|
+
(res) => res.setHeader("X-Custom", "value")
|
|
69
|
+
]);
|
|
70
70
|
|
|
71
71
|
// Error response (sends immediately)
|
|
72
72
|
sendError(res, "User not found", 404);
|
|
@@ -257,18 +257,18 @@ errorHandler(err: any, req: Request, res: Response, next: NextFunction) => void
|
|
|
257
257
|
|
|
258
258
|
### sendSuccess
|
|
259
259
|
|
|
260
|
-
|
|
260
|
+
Sends a standardized success response with optional callbacks for chaining response methods.
|
|
261
261
|
|
|
262
262
|
```typescript
|
|
263
|
-
sendSuccess(res: Response, data: any, message?: string, statusCode?: number) => Response
|
|
263
|
+
sendSuccess(res: Response, data: any, message?: string, statusCode?: number, callbacks?: Array<(res: Response) => Response>) => Response
|
|
264
264
|
```
|
|
265
265
|
|
|
266
266
|
**Features:**
|
|
267
267
|
- Includes `success: true` field by default
|
|
268
|
-
-
|
|
269
|
-
-
|
|
270
|
-
-
|
|
271
|
-
-
|
|
268
|
+
- Supports chaining via callbacks array
|
|
269
|
+
- Executes callbacks before sending response
|
|
270
|
+
- No timing issues - predictable order
|
|
271
|
+
- Clean and explicit approach
|
|
272
272
|
|
|
273
273
|
### sendError
|
|
274
274
|
|
|
@@ -283,23 +283,7 @@ sendError(res: Response, message: string, statusCode?: number, data?: any) => vo
|
|
|
283
283
|
- Sends response immediately (no chaining needed for errors)
|
|
284
284
|
- Supports optional error data
|
|
285
285
|
|
|
286
|
-
### send
|
|
287
286
|
|
|
288
|
-
Sends the prepared success response from `sendSuccess`. Only needed when chaining methods.
|
|
289
|
-
|
|
290
|
-
```typescript
|
|
291
|
-
send(res: Response) => void
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
**Features:**
|
|
295
|
-
- Sends the response prepared by `sendSuccess`
|
|
296
|
-
- **Only needed when chaining** (.cookie(), .header(), etc.)
|
|
297
|
-
- Basic usage: sendSuccess() auto-sends, no send() needed
|
|
298
|
-
- Explicit control over when response is sent
|
|
299
|
-
|
|
300
|
-
**When to Use:**
|
|
301
|
-
- ❌ Basic: `sendSuccess(res, data)` → NO send() needed
|
|
302
|
-
- ✅ Chaining: `sendSuccess(res, data).cookie(...)` → send() needed
|
|
303
287
|
|
|
304
288
|
### connectDB
|
|
305
289
|
|
package/dist/index.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ export { AppError } from "./AppError.js";
|
|
|
2
2
|
export { catchAsync } from "./catchAsync.js";
|
|
3
3
|
export { errorHandler } from "./errorHandler.js";
|
|
4
4
|
export { logger } from "./logger.js";
|
|
5
|
-
export { sendSuccess, sendError
|
|
5
|
+
export { sendSuccess, sendError } from "./responseFormatter.js";
|
|
6
6
|
export { default as connectDB, getDBStatus, DatabaseConnection, } from "./DatabaseConnection.js";
|
package/dist/index.js
CHANGED
|
@@ -2,5 +2,5 @@ export { AppError } from "./AppError.js";
|
|
|
2
2
|
export { catchAsync } from "./catchAsync.js";
|
|
3
3
|
export { errorHandler } from "./errorHandler.js";
|
|
4
4
|
export { logger } from "./logger.js";
|
|
5
|
-
export { sendSuccess, sendError
|
|
5
|
+
export { sendSuccess, sendError } from "./responseFormatter.js";
|
|
6
6
|
export { default as connectDB, getDBStatus, DatabaseConnection, } from "./DatabaseConnection.js";
|
|
@@ -33,23 +33,7 @@ import { Response } from "express";
|
|
|
33
33
|
* .cookie("refreshToken", refreshToken, HTTP_OPTIONS);
|
|
34
34
|
* send(res); // Send when ready
|
|
35
35
|
*/
|
|
36
|
-
export declare const sendSuccess: (res: Response, data: any, message?: string, statusCode?: number) => Response;
|
|
37
|
-
/**
|
|
38
|
-
* Sends the prepared success response.
|
|
39
|
-
* Use this only when chaining methods (.cookie(), .header(), etc.).
|
|
40
|
-
* For basic usage, sendSuccess() auto-sends and you don't need this.
|
|
41
|
-
*
|
|
42
|
-
* @param {Response} res - Express response object.
|
|
43
|
-
* @returns {void}
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* // Only needed when chaining methods
|
|
47
|
-
* sendSuccess(res, data, "Success", 200)
|
|
48
|
-
* .cookie("session", "abc123")
|
|
49
|
-
* .header("X-Custom", "value");
|
|
50
|
-
* send(res); // Send when ready
|
|
51
|
-
*/
|
|
52
|
-
export declare const send: (res: Response) => void;
|
|
36
|
+
export declare const sendSuccess: (res: Response, data: any, message?: string, statusCode?: number, callbacks?: ((res: Response) => Response)[]) => Response;
|
|
53
37
|
/**
|
|
54
38
|
* Sends an error response immediately.
|
|
55
39
|
*
|
|
@@ -32,42 +32,25 @@
|
|
|
32
32
|
* .cookie("refreshToken", refreshToken, HTTP_OPTIONS);
|
|
33
33
|
* send(res); // Send when ready
|
|
34
34
|
*/
|
|
35
|
-
export const sendSuccess = (res, data, message = "Success", statusCode = 200) => {
|
|
35
|
+
export const sendSuccess = (res, data, message = "Success", statusCode = 200, callbacks) => {
|
|
36
36
|
const response = {
|
|
37
37
|
status: "success",
|
|
38
38
|
success: true,
|
|
39
39
|
message,
|
|
40
40
|
data,
|
|
41
41
|
};
|
|
42
|
-
// Set status and store response for later sending
|
|
43
42
|
res.status(statusCode);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
// Execute callbacks for chaining if provided
|
|
44
|
+
if (callbacks && callbacks.length > 0) {
|
|
45
|
+
let currentRes = res;
|
|
46
|
+
for (const callback of callbacks) {
|
|
47
|
+
if (typeof callback === 'function') {
|
|
48
|
+
currentRes = callback(currentRes);
|
|
49
|
+
}
|
|
50
50
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Sends the prepared success response.
|
|
56
|
-
* Use this only when chaining methods (.cookie(), .header(), etc.).
|
|
57
|
-
* For basic usage, sendSuccess() auto-sends and you don't need this.
|
|
58
|
-
*
|
|
59
|
-
* @param {Response} res - Express response object.
|
|
60
|
-
* @returns {void}
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* // Only needed when chaining methods
|
|
64
|
-
* sendSuccess(res, data, "Success", 200)
|
|
65
|
-
* .cookie("session", "abc123")
|
|
66
|
-
* .header("X-Custom", "value");
|
|
67
|
-
* send(res); // Send when ready
|
|
68
|
-
*/
|
|
69
|
-
export const send = (res) => {
|
|
70
|
-
res.json(res._responseData);
|
|
51
|
+
}
|
|
52
|
+
// Send response
|
|
53
|
+
return res.json(response);
|
|
71
54
|
};
|
|
72
55
|
/**
|
|
73
56
|
* Sends an error response immediately.
|