devdad-express-utils 1.6.0 → 1.7.1
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 +81 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/responseFormatter.d.ts +60 -2
- package/dist/responseFormatter.js +75 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,29 +54,71 @@ 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 intelligent auto-sending.
|
|
58
58
|
|
|
59
59
|
```typescript
|
|
60
|
-
import { sendSuccess, sendError,
|
|
60
|
+
import { sendSuccess, sendError, send } from "devdad-express-utils";
|
|
61
61
|
|
|
62
|
-
//
|
|
62
|
+
// Basic success response (auto-sends - no send() needed)
|
|
63
63
|
sendSuccess(res, { id: 1, name: "John" }, "User fetched", 200);
|
|
64
64
|
|
|
65
|
-
// Chain
|
|
66
|
-
sendSuccess(res, { id: 1, name: "John" })
|
|
65
|
+
// Chain native Express methods (use send() when done chaining)
|
|
66
|
+
sendSuccess(res, { id: 1, name: "John" }, "User fetched", 200)
|
|
67
67
|
.cookie("session", "abc123")
|
|
68
68
|
.setHeader("X-Custom", "value");
|
|
69
|
+
send(res); // Send when ready
|
|
69
70
|
|
|
70
|
-
// Error response
|
|
71
|
+
// Error response (sends immediately)
|
|
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 for native chaining:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
sendSuccess(res, { token: "abc123" }, "Login successful", 200)
|
|
106
|
+
.cookie("authToken", "abc123", { httpOnly: true })
|
|
107
|
+
.header("X-Rate-Limit-Remaining", "99")
|
|
108
|
+
.status(201); // Can even override status
|
|
109
|
+
send(res); // Send the prepared response
|
|
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,50 @@ errorHandler(err: any, req: Request, res: Response, next: NextFunction) => void
|
|
|
215
257
|
|
|
216
258
|
### sendSuccess
|
|
217
259
|
|
|
218
|
-
|
|
260
|
+
Prepares a standardized success response and returns response object for chaining.
|
|
219
261
|
|
|
220
262
|
```typescript
|
|
221
263
|
sendSuccess(res: Response, data: any, message?: string, statusCode?: number) => Response
|
|
222
264
|
```
|
|
223
265
|
|
|
266
|
+
**Features:**
|
|
267
|
+
- Includes `success: true` field by default
|
|
268
|
+
- **Auto-sends** when no chaining detected (no `send()` needed)
|
|
269
|
+
- Returns Express response object for native method chaining
|
|
270
|
+
- Use `send()` when chaining methods (explicit control)
|
|
271
|
+
- No magic - intelligent and predictable
|
|
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
|
+
- Includes `success: false` field by default
|
|
283
|
+
- Sends response immediately (no chaining needed for errors)
|
|
284
|
+
- Supports optional error data
|
|
285
|
+
|
|
286
|
+
### send
|
|
287
|
+
|
|
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
|
+
|
|
232
304
|
### connectDB
|
|
233
305
|
|
|
234
306
|
Connects to MongoDB with retry logic and automatic reconnection.
|
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 } from "./responseFormatter.js";
|
|
5
|
+
export { sendSuccess, sendError, send } 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 } from "./responseFormatter.js";
|
|
5
|
+
export { sendSuccess, sendError, send } from "./responseFormatter.js";
|
|
6
6
|
export { default as connectDB, getDBStatus, DatabaseConnection, } from "./DatabaseConnection.js";
|
|
@@ -1,18 +1,76 @@
|
|
|
1
1
|
import { Response } from "express";
|
|
2
2
|
/**
|
|
3
|
-
* Sends a success response.
|
|
3
|
+
* Sends a success response with intelligent auto-sending behavior.
|
|
4
|
+
*
|
|
5
|
+
* Features:
|
|
6
|
+
* - Includes success: true field by default
|
|
7
|
+
* - **Auto-sends** when no chaining detected (no send() needed)
|
|
8
|
+
* - Returns Express response object for native method chaining
|
|
9
|
+
* - Use send() only when chaining methods (.cookie(), .header(), etc.)
|
|
10
|
+
* - Clean and predictable approach
|
|
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 (auto-sends - no send() needed)
|
|
20
|
+
* sendSuccess(res, { userId: 123 }, "Login successful");
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Chain methods and send manually
|
|
24
|
+
* sendSuccess(res, { userId: 123 }, "Login successful", 200)
|
|
25
|
+
* .cookie("authToken", "xyz789", { httpOnly: true })
|
|
26
|
+
* .header("X-Custom", "value");
|
|
27
|
+
* send(res); // Send prepared response
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // Your exact use case
|
|
31
|
+
* sendSuccess(res, { accesstoken, refreshToken, user }, "Login Successful", 200)
|
|
32
|
+
* .cookie("accessToken", accesstoken, HTTP_OPTIONS)
|
|
33
|
+
* .cookie("refreshToken", refreshToken, HTTP_OPTIONS);
|
|
34
|
+
* send(res); // Send when ready
|
|
9
35
|
*/
|
|
10
36
|
export declare const sendSuccess: (res: Response, data: any, message?: string, statusCode?: number) => Response;
|
|
11
37
|
/**
|
|
12
|
-
* Sends
|
|
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;
|
|
53
|
+
/**
|
|
54
|
+
* Sends an error response immediately.
|
|
55
|
+
*
|
|
56
|
+
* Features:
|
|
57
|
+
* - Includes success: false field by default
|
|
58
|
+
* - Sends response immediately (no chaining needed)
|
|
59
|
+
* - Supports optional error data
|
|
60
|
+
*
|
|
13
61
|
* @param {Response} res - Express response object.
|
|
14
62
|
* @param {string} message - Error message.
|
|
15
63
|
* @param {number} [statusCode=400] - HTTP status code.
|
|
16
64
|
* @param {any} [data] - Additional error data.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* // Basic error
|
|
68
|
+
* sendError(res, "User not found", 404);
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* // Error with additional data
|
|
72
|
+
* sendError(res, "Validation failed", 400, {
|
|
73
|
+
* errors: ["Email is required", "Password too short"]
|
|
74
|
+
* });
|
|
17
75
|
*/
|
|
18
76
|
export declare const sendError: (res: Response, message: string, statusCode?: number, data?: any) => void;
|
|
@@ -1,29 +1,101 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Sends a success response.
|
|
2
|
+
* Sends a success response with intelligent auto-sending behavior.
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Includes success: true field by default
|
|
6
|
+
* - **Auto-sends** when no chaining detected (no send() needed)
|
|
7
|
+
* - Returns Express response object for native method chaining
|
|
8
|
+
* - Use send() only when chaining methods (.cookie(), .header(), etc.)
|
|
9
|
+
* - Clean and predictable approach
|
|
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 (auto-sends - no send() needed)
|
|
19
|
+
* sendSuccess(res, { userId: 123 }, "Login successful");
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Chain methods and send manually
|
|
23
|
+
* sendSuccess(res, { userId: 123 }, "Login successful", 200)
|
|
24
|
+
* .cookie("authToken", "xyz789", { httpOnly: true })
|
|
25
|
+
* .header("X-Custom", "value");
|
|
26
|
+
* send(res); // Send prepared response
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* // Your exact use case
|
|
30
|
+
* sendSuccess(res, { accesstoken, refreshToken, user }, "Login Successful", 200)
|
|
31
|
+
* .cookie("accessToken", accesstoken, HTTP_OPTIONS)
|
|
32
|
+
* .cookie("refreshToken", refreshToken, HTTP_OPTIONS);
|
|
33
|
+
* send(res); // Send when ready
|
|
8
34
|
*/
|
|
9
35
|
export const sendSuccess = (res, data, message = "Success", statusCode = 200) => {
|
|
10
36
|
const response = {
|
|
11
37
|
status: "success",
|
|
38
|
+
success: true,
|
|
12
39
|
message,
|
|
13
40
|
data,
|
|
14
41
|
};
|
|
15
|
-
|
|
42
|
+
// Set status and store response for later sending
|
|
43
|
+
res.status(statusCode);
|
|
44
|
+
res._responseData = response;
|
|
45
|
+
// Auto-send unless chaining is detected (using setImmediate to check)
|
|
46
|
+
setImmediate(() => {
|
|
47
|
+
if (!res._responseSent && res._responseData) {
|
|
48
|
+
res._responseSent = true;
|
|
49
|
+
res.json(res._responseData);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return res;
|
|
16
53
|
};
|
|
17
54
|
/**
|
|
18
|
-
* Sends
|
|
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);
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Sends an error response immediately.
|
|
74
|
+
*
|
|
75
|
+
* Features:
|
|
76
|
+
* - Includes success: false field by default
|
|
77
|
+
* - Sends response immediately (no chaining needed)
|
|
78
|
+
* - Supports optional error data
|
|
79
|
+
*
|
|
19
80
|
* @param {Response} res - Express response object.
|
|
20
81
|
* @param {string} message - Error message.
|
|
21
82
|
* @param {number} [statusCode=400] - HTTP status code.
|
|
22
83
|
* @param {any} [data] - Additional error data.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* // Basic error
|
|
87
|
+
* sendError(res, "User not found", 404);
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* // Error with additional data
|
|
91
|
+
* sendError(res, "Validation failed", 400, {
|
|
92
|
+
* errors: ["Email is required", "Password too short"]
|
|
93
|
+
* });
|
|
23
94
|
*/
|
|
24
95
|
export const sendError = (res, message, statusCode = 400, data) => {
|
|
25
96
|
const response = {
|
|
26
97
|
status: "error",
|
|
98
|
+
success: false,
|
|
27
99
|
message,
|
|
28
100
|
...(data && { data }),
|
|
29
101
|
};
|