devdad-express-utils 1.6.0 → 1.7.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 +59 -5
- package/dist/responseFormatter.d.ts +37 -2
- package/dist/responseFormatter.js +48 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,10 +54,10 @@ 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.
|
|
57
|
+
Standardize API responses with consistent JSON structure and automatic response sending.
|
|
58
58
|
|
|
59
59
|
```typescript
|
|
60
|
-
import { sendSuccess, sendError
|
|
60
|
+
import { sendSuccess, sendError } from "devdad-express-utils";
|
|
61
61
|
|
|
62
62
|
// Success response
|
|
63
63
|
sendSuccess(res, { id: 1, name: "John" }, "User fetched", 200);
|
|
@@ -66,17 +66,59 @@ sendSuccess(res, { id: 1, name: "John" }, "User fetched", 200);
|
|
|
66
66
|
sendSuccess(res, { id: 1, name: "John" })
|
|
67
67
|
.cookie("session", "abc123")
|
|
68
68
|
.setHeader("X-Custom", "value");
|
|
69
|
+
// Response sent automatically! 🎉
|
|
69
70
|
|
|
70
71
|
// Error response
|
|
71
72
|
sendError(res, "User not found", 404);
|
|
72
73
|
```
|
|
73
74
|
|
|
75
|
+
#### Response Format
|
|
76
|
+
|
|
77
|
+
All responses include a `success` field by default:
|
|
78
|
+
|
|
79
|
+
**Success Response:**
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"status": "success",
|
|
84
|
+
"success": true,
|
|
85
|
+
"message": "User fetched",
|
|
86
|
+
"data": { "id": 1, "name": "John" }
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**Error Response:**
|
|
91
|
+
|
|
92
|
+
```json
|
|
93
|
+
{
|
|
94
|
+
"status": "error",
|
|
95
|
+
"success": false,
|
|
96
|
+
"message": "User not found"
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### Method Chaining
|
|
101
|
+
|
|
102
|
+
`sendSuccess` returns the Express response object, allowing you to chain any Express response methods:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
sendSuccess(res, { token: "abc123" }, "Login successful", 200)
|
|
106
|
+
.cookie("authToken", "abc123", { httpOnly: true, secure: true })
|
|
107
|
+
.header("X-Rate-Limit-Remaining", "99")
|
|
108
|
+
.status(200); // Can even override status
|
|
109
|
+
// Response automatically sent after chaining completes
|
|
110
|
+
```
|
|
111
|
+
|
|
74
112
|
### Database Connection
|
|
75
113
|
|
|
76
114
|
MongoDB connection utility with automatic reconnection, exponential backoff, and configurable retry logic.
|
|
77
115
|
|
|
78
116
|
```typescript
|
|
79
|
-
import {
|
|
117
|
+
import {
|
|
118
|
+
connectDB,
|
|
119
|
+
getDBStatus,
|
|
120
|
+
resetDBConnection,
|
|
121
|
+
} from "devdad-express-utils";
|
|
80
122
|
|
|
81
123
|
// Connect to MongoDB (ensure MONGO_URI is set in environment)
|
|
82
124
|
await connectDB();
|
|
@@ -215,20 +257,32 @@ errorHandler(err: any, req: Request, res: Response, next: NextFunction) => void
|
|
|
215
257
|
|
|
216
258
|
### sendSuccess
|
|
217
259
|
|
|
218
|
-
Sends a standardized success response. Returns the Response object for
|
|
260
|
+
Sends a standardized success response with automatic sending after method chaining. Returns the Response object for chaining Express response methods like `.cookie()`, `.header()`, etc.
|
|
219
261
|
|
|
220
262
|
```typescript
|
|
221
263
|
sendSuccess(res: Response, data: any, message?: string, statusCode?: number) => Response
|
|
222
264
|
```
|
|
223
265
|
|
|
266
|
+
**Features:**
|
|
267
|
+
|
|
268
|
+
- Includes `success: true` field by default
|
|
269
|
+
- Automatically sends response after chaining completes
|
|
270
|
+
- Supports all Express response method chaining
|
|
271
|
+
- No extra `.send()` call needed
|
|
272
|
+
|
|
224
273
|
### sendError
|
|
225
274
|
|
|
226
|
-
Sends a standardized error response.
|
|
275
|
+
Sends a standardized error response immediately.
|
|
227
276
|
|
|
228
277
|
```typescript
|
|
229
278
|
sendError(res: Response, message: string, statusCode?: number, data?: any) => void
|
|
230
279
|
```
|
|
231
280
|
|
|
281
|
+
**Features:**
|
|
282
|
+
|
|
283
|
+
- Includes `success: false` field by default
|
|
284
|
+
- Sends response immediately (no chaining needed for errors)
|
|
285
|
+
|
|
232
286
|
### connectDB
|
|
233
287
|
|
|
234
288
|
Connects to MongoDB with retry logic and automatic reconnection.
|
|
@@ -1,18 +1,53 @@
|
|
|
1
1
|
import { Response } from "express";
|
|
2
2
|
/**
|
|
3
|
-
* Sends a success response.
|
|
3
|
+
* Sends a success response and returns the response object for chaining.
|
|
4
|
+
* Automatically sends the response after chaining operations complete - no extra .send() needed!
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - Includes success: true field by default
|
|
8
|
+
* - Supports method chaining (.cookie(), .header(), etc.)
|
|
9
|
+
* - Automatically sends response after chaining completes
|
|
10
|
+
* - Returns Express response object for chaining
|
|
11
|
+
*
|
|
4
12
|
* @param {Response} res - Express response object.
|
|
5
13
|
* @param {any} data - Response data.
|
|
6
14
|
* @param {string} [message='Success'] - Response message.
|
|
7
15
|
* @param {number} [statusCode=200] - HTTP status code.
|
|
8
16
|
* @returns {Response} The Express response object for chaining.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Basic usage
|
|
20
|
+
* sendSuccess(res, { userId: 123 }, "Login successful");
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Chain methods without calling .send()
|
|
24
|
+
* sendSuccess(res, { userId: 123 }, "Login successful", 200)
|
|
25
|
+
* .cookie("authToken", "xyz789", { httpOnly: true })
|
|
26
|
+
* .header("X-Custom", "value");
|
|
27
|
+
* // Response sent automatically!
|
|
9
28
|
*/
|
|
10
29
|
export declare const sendSuccess: (res: Response, data: any, message?: string, statusCode?: number) => Response;
|
|
11
30
|
/**
|
|
12
|
-
* Sends an error response.
|
|
31
|
+
* Sends an error response immediately.
|
|
32
|
+
*
|
|
33
|
+
* Features:
|
|
34
|
+
* - Includes success: false field by default
|
|
35
|
+
* - Sends response immediately (no chaining needed)
|
|
36
|
+
* - Supports optional error data
|
|
37
|
+
*
|
|
13
38
|
* @param {Response} res - Express response object.
|
|
14
39
|
* @param {string} message - Error message.
|
|
15
40
|
* @param {number} [statusCode=400] - HTTP status code.
|
|
16
41
|
* @param {any} [data] - Additional error data.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* // Basic error
|
|
45
|
+
* sendError(res, "User not found", 404);
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* // Error with additional data
|
|
49
|
+
* sendError(res, "Validation failed", 400, {
|
|
50
|
+
* errors: ["Email is required", "Password too short"]
|
|
51
|
+
* });
|
|
17
52
|
*/
|
|
18
53
|
export declare const sendError: (res: Response, message: string, statusCode?: number, data?: any) => void;
|
|
@@ -1,29 +1,74 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Sends a success response.
|
|
2
|
+
* Sends a success response and returns the response object for chaining.
|
|
3
|
+
* Automatically sends the response after chaining operations complete - no extra .send() needed!
|
|
4
|
+
*
|
|
5
|
+
* Features:
|
|
6
|
+
* - Includes success: true field by default
|
|
7
|
+
* - Supports method chaining (.cookie(), .header(), etc.)
|
|
8
|
+
* - Automatically sends response after chaining completes
|
|
9
|
+
* - Returns Express response object for chaining
|
|
10
|
+
*
|
|
3
11
|
* @param {Response} res - Express response object.
|
|
4
12
|
* @param {any} data - Response data.
|
|
5
13
|
* @param {string} [message='Success'] - Response message.
|
|
6
14
|
* @param {number} [statusCode=200] - HTTP status code.
|
|
7
15
|
* @returns {Response} The Express response object for chaining.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Basic usage
|
|
19
|
+
* sendSuccess(res, { userId: 123 }, "Login successful");
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Chain methods without calling .send()
|
|
23
|
+
* sendSuccess(res, { userId: 123 }, "Login successful", 200)
|
|
24
|
+
* .cookie("authToken", "xyz789", { httpOnly: true })
|
|
25
|
+
* .header("X-Custom", "value");
|
|
26
|
+
* // Response sent automatically!
|
|
8
27
|
*/
|
|
9
28
|
export const sendSuccess = (res, data, message = "Success", statusCode = 200) => {
|
|
10
29
|
const response = {
|
|
11
30
|
status: "success",
|
|
31
|
+
success: true,
|
|
12
32
|
message,
|
|
13
33
|
data,
|
|
14
34
|
};
|
|
15
|
-
|
|
35
|
+
res.status(statusCode);
|
|
36
|
+
// Use setImmediate to send response after current execution stack
|
|
37
|
+
setImmediate(() => {
|
|
38
|
+
if (!res._responseSent) {
|
|
39
|
+
res._responseSent = true;
|
|
40
|
+
res.json(response);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return res;
|
|
16
44
|
};
|
|
17
45
|
/**
|
|
18
|
-
* Sends an error response.
|
|
46
|
+
* Sends an error response immediately.
|
|
47
|
+
*
|
|
48
|
+
* Features:
|
|
49
|
+
* - Includes success: false field by default
|
|
50
|
+
* - Sends response immediately (no chaining needed)
|
|
51
|
+
* - Supports optional error data
|
|
52
|
+
*
|
|
19
53
|
* @param {Response} res - Express response object.
|
|
20
54
|
* @param {string} message - Error message.
|
|
21
55
|
* @param {number} [statusCode=400] - HTTP status code.
|
|
22
56
|
* @param {any} [data] - Additional error data.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // Basic error
|
|
60
|
+
* sendError(res, "User not found", 404);
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Error with additional data
|
|
64
|
+
* sendError(res, "Validation failed", 400, {
|
|
65
|
+
* errors: ["Email is required", "Password too short"]
|
|
66
|
+
* });
|
|
23
67
|
*/
|
|
24
68
|
export const sendError = (res, message, statusCode = 400, data) => {
|
|
25
69
|
const response = {
|
|
26
70
|
status: "error",
|
|
71
|
+
success: false,
|
|
27
72
|
message,
|
|
28
73
|
...(data && { data }),
|
|
29
74
|
};
|