devdad-express-utils 1.4.0 → 1.5.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 +11 -32
- package/dist/responseFormatter.d.ts +2 -1
- package/dist/responseFormatter.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,27 +60,15 @@ Standardize API responses with consistent JSON structure.
|
|
|
60
60
|
import { sendSuccess, sendError, sendPaginated } from "devdad-express-utils";
|
|
61
61
|
|
|
62
62
|
// Success response
|
|
63
|
-
sendSuccess(res, { id: 1, name:
|
|
63
|
+
sendSuccess(res, { id: 1, name: "John" }, "User fetched", 200);
|
|
64
64
|
|
|
65
|
-
//
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### Authentication Middleware
|
|
65
|
+
// Chain additional response methods
|
|
66
|
+
sendSuccess(res, { id: 1, name: "John" })
|
|
67
|
+
.cookie("session", "abc123")
|
|
68
|
+
.setHeader("X-Custom", "value");
|
|
72
69
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
```typescript
|
|
76
|
-
import { requireAuth } from "devdad-express-utils";
|
|
77
|
-
|
|
78
|
-
const authMiddleware = requireAuth({ secret: process.env.JWT_SECRET });
|
|
79
|
-
|
|
80
|
-
app.get('/profile', authMiddleware, (req, res) => {
|
|
81
|
-
// req.user contains decoded JWT
|
|
82
|
-
res.json(req.user);
|
|
83
|
-
});
|
|
70
|
+
// Error response
|
|
71
|
+
sendError(res, "User not found", 404);
|
|
84
72
|
```
|
|
85
73
|
|
|
86
74
|
### Database Connection
|
|
@@ -122,6 +110,7 @@ logger.debug("Processing request", { requestId: "abc-123" });
|
|
|
122
110
|
#### Log Files
|
|
123
111
|
|
|
124
112
|
In development, logs are written to:
|
|
113
|
+
|
|
125
114
|
- `error.log`: Error level and above
|
|
126
115
|
- `combined.log`: All log levels
|
|
127
116
|
|
|
@@ -208,10 +197,10 @@ errorHandler(err: any, req: Request, res: Response, next: NextFunction) => void
|
|
|
208
197
|
|
|
209
198
|
### sendSuccess
|
|
210
199
|
|
|
211
|
-
Sends a standardized success response.
|
|
200
|
+
Sends a standardized success response. Returns the Response object for method chaining.
|
|
212
201
|
|
|
213
202
|
```typescript
|
|
214
|
-
sendSuccess(res: Response, data: any, message?: string, statusCode?: number) =>
|
|
203
|
+
sendSuccess(res: Response, data: any, message?: string, statusCode?: number) => Response
|
|
215
204
|
```
|
|
216
205
|
|
|
217
206
|
### sendError
|
|
@@ -222,16 +211,6 @@ Sends a standardized error response.
|
|
|
222
211
|
sendError(res: Response, message: string, statusCode?: number, data?: any) => void
|
|
223
212
|
```
|
|
224
213
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
### requireAuth
|
|
228
|
-
|
|
229
|
-
Middleware for JWT authentication.
|
|
230
|
-
|
|
231
|
-
```typescript
|
|
232
|
-
requireAuth(options: { secret: string, algorithms?: Algorithm[] }) => (req, res, next) => void
|
|
233
|
-
```
|
|
234
|
-
|
|
235
214
|
### connectDB
|
|
236
215
|
|
|
237
216
|
Connects to MongoDB with retry logic and automatic reconnection.
|
|
@@ -253,7 +232,7 @@ getDBStatus() => { isConnected: boolean; readyState: number; host: string; name:
|
|
|
253
232
|
Winston logger instance with JSON formatting, timestamps, and error stack traces.
|
|
254
233
|
|
|
255
234
|
```typescript
|
|
256
|
-
logger: winston.Logger
|
|
235
|
+
logger: winston.Logger;
|
|
257
236
|
```
|
|
258
237
|
|
|
259
238
|
## Development
|
|
@@ -5,8 +5,9 @@ import { Response } from "express";
|
|
|
5
5
|
* @param {any} data - Response data.
|
|
6
6
|
* @param {string} [message='Success'] - Response message.
|
|
7
7
|
* @param {number} [statusCode=200] - HTTP status code.
|
|
8
|
+
* @returns {Response} The Express response object for chaining.
|
|
8
9
|
*/
|
|
9
|
-
export declare const sendSuccess: (res: Response, data: any, message?: string, statusCode?: number) =>
|
|
10
|
+
export declare const sendSuccess: (res: Response, data: any, message?: string, statusCode?: number) => Response;
|
|
10
11
|
/**
|
|
11
12
|
* Sends an error response.
|
|
12
13
|
* @param {Response} res - Express response object.
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @param {any} data - Response data.
|
|
5
5
|
* @param {string} [message='Success'] - Response message.
|
|
6
6
|
* @param {number} [statusCode=200] - HTTP status code.
|
|
7
|
+
* @returns {Response} The Express response object for chaining.
|
|
7
8
|
*/
|
|
8
9
|
export const sendSuccess = (res, data, message = "Success", statusCode = 200) => {
|
|
9
10
|
const response = {
|
|
@@ -11,7 +12,7 @@ export const sendSuccess = (res, data, message = "Success", statusCode = 200) =>
|
|
|
11
12
|
message,
|
|
12
13
|
data,
|
|
13
14
|
};
|
|
14
|
-
res.status(statusCode).json(response);
|
|
15
|
+
return res.status(statusCode).json(response);
|
|
15
16
|
};
|
|
16
17
|
/**
|
|
17
18
|
* Sends an error response.
|