devdad-express-utils 1.7.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 +35 -17
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/responseFormatter.d.ts +31 -8
- package/dist/responseFormatter.js +38 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,21 +54,21 @@ 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 intelligent auto-sending.
|
|
58
58
|
|
|
59
59
|
```typescript
|
|
60
|
-
import { sendSuccess, sendError } from "devdad-express-utils";
|
|
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
|
-
//
|
|
69
|
+
send(res); // Send when ready
|
|
70
70
|
|
|
71
|
-
// Error response
|
|
71
|
+
// Error response (sends immediately)
|
|
72
72
|
sendError(res, "User not found", 404);
|
|
73
73
|
```
|
|
74
74
|
|
|
@@ -99,14 +99,14 @@ All responses include a `success` field by default:
|
|
|
99
99
|
|
|
100
100
|
#### Method Chaining
|
|
101
101
|
|
|
102
|
-
`sendSuccess` returns the Express response object
|
|
102
|
+
`sendSuccess` returns the Express response object for native chaining:
|
|
103
103
|
|
|
104
104
|
```typescript
|
|
105
105
|
sendSuccess(res, { token: "abc123" }, "Login successful", 200)
|
|
106
|
-
.cookie("authToken", "abc123", { httpOnly: true
|
|
106
|
+
.cookie("authToken", "abc123", { httpOnly: true })
|
|
107
107
|
.header("X-Rate-Limit-Remaining", "99")
|
|
108
|
-
.status(
|
|
109
|
-
//
|
|
108
|
+
.status(201); // Can even override status
|
|
109
|
+
send(res); // Send the prepared response
|
|
110
110
|
```
|
|
111
111
|
|
|
112
112
|
### Database Connection
|
|
@@ -257,18 +257,18 @@ errorHandler(err: any, req: Request, res: Response, next: NextFunction) => void
|
|
|
257
257
|
|
|
258
258
|
### sendSuccess
|
|
259
259
|
|
|
260
|
-
|
|
260
|
+
Prepares a standardized success response and returns response object for chaining.
|
|
261
261
|
|
|
262
262
|
```typescript
|
|
263
263
|
sendSuccess(res: Response, data: any, message?: string, statusCode?: number) => Response
|
|
264
264
|
```
|
|
265
265
|
|
|
266
266
|
**Features:**
|
|
267
|
-
|
|
268
267
|
- Includes `success: true` field by default
|
|
269
|
-
-
|
|
270
|
-
-
|
|
271
|
-
-
|
|
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
272
|
|
|
273
273
|
### sendError
|
|
274
274
|
|
|
@@ -279,9 +279,27 @@ sendError(res: Response, message: string, statusCode?: number, data?: any) => vo
|
|
|
279
279
|
```
|
|
280
280
|
|
|
281
281
|
**Features:**
|
|
282
|
-
|
|
283
282
|
- Includes `success: false` field by default
|
|
284
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
|
|
285
303
|
|
|
286
304
|
### connectDB
|
|
287
305
|
|
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,13 +1,13 @@
|
|
|
1
1
|
import { Response } from "express";
|
|
2
2
|
/**
|
|
3
|
-
* Sends a success response
|
|
4
|
-
* Automatically sends the response after chaining operations complete - no extra .send() needed!
|
|
3
|
+
* Sends a success response with intelligent auto-sending behavior.
|
|
5
4
|
*
|
|
6
5
|
* Features:
|
|
7
6
|
* - Includes success: true field by default
|
|
8
|
-
* -
|
|
9
|
-
* -
|
|
10
|
-
* -
|
|
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
11
|
*
|
|
12
12
|
* @param {Response} res - Express response object.
|
|
13
13
|
* @param {any} data - Response data.
|
|
@@ -16,17 +16,40 @@ import { Response } from "express";
|
|
|
16
16
|
* @returns {Response} The Express response object for chaining.
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
|
-
* // Basic usage
|
|
19
|
+
* // Basic usage (auto-sends - no send() needed)
|
|
20
20
|
* sendSuccess(res, { userId: 123 }, "Login successful");
|
|
21
21
|
*
|
|
22
22
|
* @example
|
|
23
|
-
* // Chain methods
|
|
23
|
+
* // Chain methods and send manually
|
|
24
24
|
* sendSuccess(res, { userId: 123 }, "Login successful", 200)
|
|
25
25
|
* .cookie("authToken", "xyz789", { httpOnly: true })
|
|
26
26
|
* .header("X-Custom", "value");
|
|
27
|
-
* //
|
|
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
|
|
28
35
|
*/
|
|
29
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;
|
|
30
53
|
/**
|
|
31
54
|
* Sends an error response immediately.
|
|
32
55
|
*
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Sends a success response
|
|
3
|
-
* Automatically sends the response after chaining operations complete - no extra .send() needed!
|
|
2
|
+
* Sends a success response with intelligent auto-sending behavior.
|
|
4
3
|
*
|
|
5
4
|
* Features:
|
|
6
5
|
* - Includes success: true field by default
|
|
7
|
-
* -
|
|
8
|
-
* -
|
|
9
|
-
* -
|
|
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
10
|
*
|
|
11
11
|
* @param {Response} res - Express response object.
|
|
12
12
|
* @param {any} data - Response data.
|
|
@@ -15,15 +15,22 @@
|
|
|
15
15
|
* @returns {Response} The Express response object for chaining.
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
|
-
* // Basic usage
|
|
18
|
+
* // Basic usage (auto-sends - no send() needed)
|
|
19
19
|
* sendSuccess(res, { userId: 123 }, "Login successful");
|
|
20
20
|
*
|
|
21
21
|
* @example
|
|
22
|
-
* // Chain methods
|
|
22
|
+
* // Chain methods and send manually
|
|
23
23
|
* sendSuccess(res, { userId: 123 }, "Login successful", 200)
|
|
24
24
|
* .cookie("authToken", "xyz789", { httpOnly: true })
|
|
25
25
|
* .header("X-Custom", "value");
|
|
26
|
-
* //
|
|
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
|
|
27
34
|
*/
|
|
28
35
|
export const sendSuccess = (res, data, message = "Success", statusCode = 200) => {
|
|
29
36
|
const response = {
|
|
@@ -32,16 +39,36 @@ export const sendSuccess = (res, data, message = "Success", statusCode = 200) =>
|
|
|
32
39
|
message,
|
|
33
40
|
data,
|
|
34
41
|
};
|
|
42
|
+
// Set status and store response for later sending
|
|
35
43
|
res.status(statusCode);
|
|
36
|
-
|
|
44
|
+
res._responseData = response;
|
|
45
|
+
// Auto-send unless chaining is detected (using setImmediate to check)
|
|
37
46
|
setImmediate(() => {
|
|
38
|
-
if (!res._responseSent) {
|
|
47
|
+
if (!res._responseSent && res._responseData) {
|
|
39
48
|
res._responseSent = true;
|
|
40
|
-
res.json(
|
|
49
|
+
res.json(res._responseData);
|
|
41
50
|
}
|
|
42
51
|
});
|
|
43
52
|
return res;
|
|
44
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);
|
|
71
|
+
};
|
|
45
72
|
/**
|
|
46
73
|
* Sends an error response immediately.
|
|
47
74
|
*
|