response-kit 1.0.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/LICENSE +21 -0
- package/README.md +293 -0
- package/docs/error-response.md +222 -0
- package/docs/getting-started.md +83 -0
- package/docs/pagination.md +138 -0
- package/docs/success-response.md +133 -0
- package/index.d.ts +56 -0
- package/package.json +51 -0
- package/src/error.js +41 -0
- package/src/index.js +6 -0
- package/src/pagination.js +22 -0
- package/src/success.js +25 -0
- package/src/validation.js +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Piyush Yadav
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
# Response Kit
|
|
6
|
+
|
|
7
|
+
A lightweight, developer-friendly utility for standardized API responses in Express.js and Node.js applications.
|
|
8
|
+
|
|
9
|
+
Stop writing repetitive response code in every project.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
* Standard Success Responses
|
|
14
|
+
* Standard Error Responses
|
|
15
|
+
* Validation Error Handling
|
|
16
|
+
* Pagination Support
|
|
17
|
+
* TypeScript Support
|
|
18
|
+
* ESM + CommonJS Support
|
|
19
|
+
* Lightweight & Zero Dependencies
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install response-kit
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
### CommonJS
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
const response = require("response-kit");
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### ESM
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import response from "response-kit";
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Basic Usage
|
|
48
|
+
|
|
49
|
+
### Success Response
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
app.get("/user", (req, res) => {
|
|
53
|
+
const user = {
|
|
54
|
+
id: 1,
|
|
55
|
+
name: "John Doe",
|
|
56
|
+
email: "john@example.com"
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
response.success(
|
|
60
|
+
res,
|
|
61
|
+
user,
|
|
62
|
+
"User fetched successfully"
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Output
|
|
68
|
+
|
|
69
|
+
```json
|
|
70
|
+
{
|
|
71
|
+
"success": true,
|
|
72
|
+
"message": "User fetched successfully",
|
|
73
|
+
"data": {
|
|
74
|
+
"id": 1,
|
|
75
|
+
"name": "John Doe",
|
|
76
|
+
"email": "john@example.com"
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### Created Response
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
app.post("/user", (req, res) => {
|
|
87
|
+
const user = {
|
|
88
|
+
id: 1,
|
|
89
|
+
name: "John Doe"
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
response.created(
|
|
93
|
+
res,
|
|
94
|
+
user,
|
|
95
|
+
"User created successfully"
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Status Code
|
|
101
|
+
|
|
102
|
+
```text
|
|
103
|
+
201 Created
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## Error Responses
|
|
109
|
+
|
|
110
|
+
### Bad Request
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
response.badRequest(
|
|
114
|
+
res,
|
|
115
|
+
"Invalid Request"
|
|
116
|
+
);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Output
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
{
|
|
123
|
+
"success": false,
|
|
124
|
+
"message": "Invalid Request",
|
|
125
|
+
"errors": null
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
### Unauthorized
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
response.unauthorized(
|
|
135
|
+
res,
|
|
136
|
+
"Invalid Token"
|
|
137
|
+
);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
### Forbidden
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
response.forbidden(
|
|
146
|
+
res,
|
|
147
|
+
"Access Denied"
|
|
148
|
+
);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
### Not Found
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
response.notFound(
|
|
157
|
+
res,
|
|
158
|
+
"User Not Found"
|
|
159
|
+
);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
### Conflict
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
response.conflict(
|
|
168
|
+
res,
|
|
169
|
+
"Email Already Exists"
|
|
170
|
+
);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
### Internal Server Error
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
response.internalError(
|
|
179
|
+
res,
|
|
180
|
+
"Something Went Wrong"
|
|
181
|
+
);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## Validation Error
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
response.validationError(
|
|
190
|
+
res,
|
|
191
|
+
{
|
|
192
|
+
email: "Email is required",
|
|
193
|
+
password: "Password is required"
|
|
194
|
+
}
|
|
195
|
+
);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Output
|
|
199
|
+
|
|
200
|
+
```json
|
|
201
|
+
{
|
|
202
|
+
"success": false,
|
|
203
|
+
"message": "Validation Failed",
|
|
204
|
+
"errors": {
|
|
205
|
+
"email": "Email is required",
|
|
206
|
+
"password": "Password is required"
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Pagination
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
response.paginate(
|
|
217
|
+
res,
|
|
218
|
+
users,
|
|
219
|
+
1,
|
|
220
|
+
10,
|
|
221
|
+
100
|
|
222
|
+
);
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Output
|
|
226
|
+
|
|
227
|
+
```json
|
|
228
|
+
{
|
|
229
|
+
"success": true,
|
|
230
|
+
"data": [],
|
|
231
|
+
"pagination": {
|
|
232
|
+
"page": 1,
|
|
233
|
+
"limit": 10,
|
|
234
|
+
"total": 100,
|
|
235
|
+
"totalPages": 10
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## API Reference
|
|
243
|
+
|
|
244
|
+
| Method | Description |
|
|
245
|
+
| ----------------- | ------------------------- |
|
|
246
|
+
| success() | Send success response |
|
|
247
|
+
| created() | Send 201 created response |
|
|
248
|
+
| badRequest() | Send 400 response |
|
|
249
|
+
| unauthorized() | Send 401 response |
|
|
250
|
+
| forbidden() | Send 403 response |
|
|
251
|
+
| notFound() | Send 404 response |
|
|
252
|
+
| conflict() | Send 409 response |
|
|
253
|
+
| internalError() | Send 500 response |
|
|
254
|
+
| validationError() | Send validation errors |
|
|
255
|
+
| paginate() | Send paginated response |
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## TypeScript
|
|
260
|
+
|
|
261
|
+
Type definitions are included.
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
import response from "response-kit";
|
|
265
|
+
|
|
266
|
+
response.success(res, user);
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
No additional setup required.
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## Examples
|
|
274
|
+
|
|
275
|
+
Check examples folder for complete working examples.
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
examples/
|
|
279
|
+
├── commonjs-example.js
|
|
280
|
+
└── esm-example.js
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
---
|
|
284
|
+
|
|
285
|
+
## License
|
|
286
|
+
|
|
287
|
+
MIT
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
## Author
|
|
292
|
+
|
|
293
|
+
Piyush Yadav
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# Error Responses
|
|
2
|
+
|
|
3
|
+
Error helpers make API error responses consistent across your application.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# badRequest()
|
|
8
|
+
|
|
9
|
+
Status Code:
|
|
10
|
+
|
|
11
|
+
```http
|
|
12
|
+
400 Bad Request
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
response.badRequest(
|
|
19
|
+
res,
|
|
20
|
+
"Invalid Request Body"
|
|
21
|
+
);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Output:
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"success": false,
|
|
29
|
+
"message": "Invalid Request Body",
|
|
30
|
+
"errors": null
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
# unauthorized()
|
|
37
|
+
|
|
38
|
+
Status Code:
|
|
39
|
+
|
|
40
|
+
```http
|
|
41
|
+
401 Unauthorized
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Example:
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
response.unauthorized(
|
|
48
|
+
res,
|
|
49
|
+
"Invalid Token"
|
|
50
|
+
);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Output:
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"success": false,
|
|
58
|
+
"message": "Invalid Token",
|
|
59
|
+
"errors": null
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
# forbidden()
|
|
66
|
+
|
|
67
|
+
Status Code:
|
|
68
|
+
|
|
69
|
+
```http
|
|
70
|
+
403 Forbidden
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Example:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
response.forbidden(
|
|
77
|
+
res,
|
|
78
|
+
"Access Denied"
|
|
79
|
+
);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Output:
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"success": false,
|
|
87
|
+
"message": "Access Denied",
|
|
88
|
+
"errors": null
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
# notFound()
|
|
95
|
+
|
|
96
|
+
Status Code:
|
|
97
|
+
|
|
98
|
+
```http
|
|
99
|
+
404 Not Found
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Example:
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
response.notFound(
|
|
106
|
+
res,
|
|
107
|
+
"User Not Found"
|
|
108
|
+
);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Output:
|
|
112
|
+
|
|
113
|
+
```json
|
|
114
|
+
{
|
|
115
|
+
"success": false,
|
|
116
|
+
"message": "User Not Found",
|
|
117
|
+
"errors": null
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
# conflict()
|
|
124
|
+
|
|
125
|
+
Status Code:
|
|
126
|
+
|
|
127
|
+
```http
|
|
128
|
+
409 Conflict
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Example:
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
response.conflict(
|
|
135
|
+
res,
|
|
136
|
+
"Email Already Exists"
|
|
137
|
+
);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Output:
|
|
141
|
+
|
|
142
|
+
```json
|
|
143
|
+
{
|
|
144
|
+
"success": false,
|
|
145
|
+
"message": "Email Already Exists",
|
|
146
|
+
"errors": null
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
# internalError()
|
|
153
|
+
|
|
154
|
+
Status Code:
|
|
155
|
+
|
|
156
|
+
```http
|
|
157
|
+
500 Internal Server Error
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Example:
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
response.internalError(
|
|
164
|
+
res,
|
|
165
|
+
"Something Went Wrong"
|
|
166
|
+
);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Output:
|
|
170
|
+
|
|
171
|
+
```json
|
|
172
|
+
{
|
|
173
|
+
"success": false,
|
|
174
|
+
"message": "Something Went Wrong",
|
|
175
|
+
"errors": null
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
# validationError()
|
|
182
|
+
|
|
183
|
+
Used when user input validation fails.
|
|
184
|
+
|
|
185
|
+
Example:
|
|
186
|
+
|
|
187
|
+
```js
|
|
188
|
+
response.validationError(
|
|
189
|
+
res,
|
|
190
|
+
{
|
|
191
|
+
email: "Email is required",
|
|
192
|
+
password: "Password is required"
|
|
193
|
+
}
|
|
194
|
+
);
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Output:
|
|
198
|
+
|
|
199
|
+
```json
|
|
200
|
+
{
|
|
201
|
+
"success": false,
|
|
202
|
+
"message": "Validation Failed",
|
|
203
|
+
"errors": {
|
|
204
|
+
"email": "Email is required",
|
|
205
|
+
"password": "Password is required"
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
# Best Practices
|
|
213
|
+
|
|
214
|
+
Use:
|
|
215
|
+
|
|
216
|
+
* badRequest() for invalid requests
|
|
217
|
+
* unauthorized() for invalid authentication
|
|
218
|
+
* forbidden() for permission issues
|
|
219
|
+
* notFound() when resource doesn't exist
|
|
220
|
+
* conflict() for duplicate data
|
|
221
|
+
* validationError() for validation failures
|
|
222
|
+
* internalError() for server errors
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Getting Started
|
|
2
|
+
|
|
3
|
+
This guide will help you integrate API Response Kit into your Express.js application.
|
|
4
|
+
|
|
5
|
+
## Step 1: Install Package
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install response-kit
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Step 2: Import Package
|
|
14
|
+
|
|
15
|
+
### CommonJS
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
const response = require("response-kit");
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### ESM
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import response from "response-kit";
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Step 3: Create Express Server
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
const express = require("express");
|
|
33
|
+
const response = require("response-kit");
|
|
34
|
+
|
|
35
|
+
const app = express();
|
|
36
|
+
|
|
37
|
+
app.get("/", (req, res) => {
|
|
38
|
+
response.success(
|
|
39
|
+
res,
|
|
40
|
+
{
|
|
41
|
+
project: "API Response Kit"
|
|
42
|
+
},
|
|
43
|
+
"Server Running Successfully"
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
app.listen(5000);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Step 4: Run Application
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
node app.js
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Visit
|
|
59
|
+
|
|
60
|
+
```text
|
|
61
|
+
http://localhost:5000
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Response
|
|
65
|
+
|
|
66
|
+
```json
|
|
67
|
+
{
|
|
68
|
+
"success": true,
|
|
69
|
+
"message": "Server Running Successfully",
|
|
70
|
+
"data": {
|
|
71
|
+
"project": "API Response Kit"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## Next Steps
|
|
79
|
+
|
|
80
|
+
* Learn Success Responses
|
|
81
|
+
* Learn Error Responses
|
|
82
|
+
* Learn Validation Errors
|
|
83
|
+
* Learn Pagination
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Pagination
|
|
2
|
+
|
|
3
|
+
Pagination helper provides a standard structure for paginated APIs.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Why Pagination?
|
|
8
|
+
|
|
9
|
+
Imagine you have:
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
10000 Users
|
|
13
|
+
50000 Products
|
|
14
|
+
100000 Orders
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Returning everything in one request is inefficient.
|
|
18
|
+
|
|
19
|
+
Pagination helps return data in chunks.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
# Syntax
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
response.paginate(
|
|
27
|
+
res,
|
|
28
|
+
data,
|
|
29
|
+
page,
|
|
30
|
+
limit,
|
|
31
|
+
total
|
|
32
|
+
);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
# Example
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
app.get("/users", (req, res) => {
|
|
41
|
+
|
|
42
|
+
const users = [
|
|
43
|
+
{
|
|
44
|
+
id: 1,
|
|
45
|
+
name: "John"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
id: 2,
|
|
49
|
+
name: "Jane"
|
|
50
|
+
}
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
response.paginate(
|
|
54
|
+
res,
|
|
55
|
+
users,
|
|
56
|
+
1,
|
|
57
|
+
10,
|
|
58
|
+
100
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
# Output
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{
|
|
70
|
+
"success": true,
|
|
71
|
+
"data": [
|
|
72
|
+
{
|
|
73
|
+
"id": 1,
|
|
74
|
+
"name": "John"
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"id": 2,
|
|
78
|
+
"name": "Jane"
|
|
79
|
+
}
|
|
80
|
+
],
|
|
81
|
+
"pagination": {
|
|
82
|
+
"page": 1,
|
|
83
|
+
"limit": 10,
|
|
84
|
+
"total": 100,
|
|
85
|
+
"totalPages": 10
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
# Explanation
|
|
93
|
+
|
|
94
|
+
| Property | Description |
|
|
95
|
+
| ---------- | --------------------- |
|
|
96
|
+
| page | Current page |
|
|
97
|
+
| limit | Items per page |
|
|
98
|
+
| total | Total records |
|
|
99
|
+
| totalPages | Total pages available |
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
# MongoDB Example
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
const page = Number(req.query.page) || 1;
|
|
107
|
+
const limit = Number(req.query.limit) || 10;
|
|
108
|
+
|
|
109
|
+
const skip = (page - 1) * limit;
|
|
110
|
+
|
|
111
|
+
const users = await User.find()
|
|
112
|
+
.skip(skip)
|
|
113
|
+
.limit(limit);
|
|
114
|
+
|
|
115
|
+
const total = await User.countDocuments();
|
|
116
|
+
|
|
117
|
+
response.paginate(
|
|
118
|
+
res,
|
|
119
|
+
users,
|
|
120
|
+
page,
|
|
121
|
+
limit,
|
|
122
|
+
total
|
|
123
|
+
);
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
# Best Practices
|
|
129
|
+
|
|
130
|
+
✅ Always return pagination object
|
|
131
|
+
|
|
132
|
+
✅ Include total count
|
|
133
|
+
|
|
134
|
+
✅ Use page & limit query parameters
|
|
135
|
+
|
|
136
|
+
✅ Keep response structure consistent
|
|
137
|
+
|
|
138
|
+
This makes frontend pagination implementation much easier.
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Success Responses
|
|
2
|
+
|
|
3
|
+
Success responses are used when an operation completes successfully.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# success()
|
|
8
|
+
|
|
9
|
+
Use this method when data is fetched successfully.
|
|
10
|
+
|
|
11
|
+
## Syntax
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
response.success(
|
|
15
|
+
res,
|
|
16
|
+
data,
|
|
17
|
+
message
|
|
18
|
+
);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Example
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
app.get("/users", (req, res) => {
|
|
25
|
+
const users = [
|
|
26
|
+
{
|
|
27
|
+
id: 1,
|
|
28
|
+
name: "John"
|
|
29
|
+
}
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
response.success(
|
|
33
|
+
res,
|
|
34
|
+
users,
|
|
35
|
+
"Users fetched successfully"
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Output
|
|
43
|
+
|
|
44
|
+
```json
|
|
45
|
+
{
|
|
46
|
+
"success": true,
|
|
47
|
+
"message": "Users fetched successfully",
|
|
48
|
+
"data": [
|
|
49
|
+
{
|
|
50
|
+
"id": 1,
|
|
51
|
+
"name": "John"
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
# created()
|
|
60
|
+
|
|
61
|
+
Use this helper after creating a new resource.
|
|
62
|
+
|
|
63
|
+
Examples:
|
|
64
|
+
|
|
65
|
+
* User Registration
|
|
66
|
+
* Product Creation
|
|
67
|
+
* Order Creation
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Syntax
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
response.created(
|
|
75
|
+
res,
|
|
76
|
+
data,
|
|
77
|
+
message
|
|
78
|
+
);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## Example
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
app.post("/users", (req, res) => {
|
|
87
|
+
const user = {
|
|
88
|
+
id: 1,
|
|
89
|
+
name: "John"
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
response.created(
|
|
93
|
+
res,
|
|
94
|
+
user,
|
|
95
|
+
"User created successfully"
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Output
|
|
103
|
+
|
|
104
|
+
```json
|
|
105
|
+
{
|
|
106
|
+
"success": true,
|
|
107
|
+
"message": "User created successfully",
|
|
108
|
+
"data": {
|
|
109
|
+
"id": 1,
|
|
110
|
+
"name": "John"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Status Code
|
|
118
|
+
|
|
119
|
+
```http
|
|
120
|
+
201 Created
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
# Best Practices
|
|
126
|
+
|
|
127
|
+
✅ Use success() for fetching data
|
|
128
|
+
|
|
129
|
+
✅ Use created() for creating resources
|
|
130
|
+
|
|
131
|
+
❌ Don't use success() after creating resources
|
|
132
|
+
|
|
133
|
+
❌ Don't manually write JSON responses if using API Response Kit
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export function success(
|
|
2
|
+
res: any,
|
|
3
|
+
data?: any,
|
|
4
|
+
message?: string,
|
|
5
|
+
statusCode?: number
|
|
6
|
+
): any;
|
|
7
|
+
|
|
8
|
+
export function created(
|
|
9
|
+
res: any,
|
|
10
|
+
data?: any,
|
|
11
|
+
message?: string
|
|
12
|
+
): any;
|
|
13
|
+
|
|
14
|
+
export function badRequest(
|
|
15
|
+
res: any,
|
|
16
|
+
message?: string
|
|
17
|
+
): any;
|
|
18
|
+
|
|
19
|
+
export function unauthorized(
|
|
20
|
+
res: any,
|
|
21
|
+
message?: string
|
|
22
|
+
): any;
|
|
23
|
+
|
|
24
|
+
export function forbidden(
|
|
25
|
+
res: any,
|
|
26
|
+
message?: string
|
|
27
|
+
): any;
|
|
28
|
+
|
|
29
|
+
export function notFound(
|
|
30
|
+
res: any,
|
|
31
|
+
message?: string
|
|
32
|
+
): any;
|
|
33
|
+
|
|
34
|
+
export function conflict(
|
|
35
|
+
res: any,
|
|
36
|
+
message?: string
|
|
37
|
+
): any;
|
|
38
|
+
|
|
39
|
+
export function internalError(
|
|
40
|
+
res: any,
|
|
41
|
+
message?: string
|
|
42
|
+
): any;
|
|
43
|
+
|
|
44
|
+
export function validationError(
|
|
45
|
+
res: any,
|
|
46
|
+
errors?: object,
|
|
47
|
+
message?: string
|
|
48
|
+
): any;
|
|
49
|
+
|
|
50
|
+
export function paginate(
|
|
51
|
+
res: any,
|
|
52
|
+
data: any[],
|
|
53
|
+
page: number,
|
|
54
|
+
limit: number,
|
|
55
|
+
total: number
|
|
56
|
+
): any;
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "response-kit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Standardized API response utility for Node.js and Express.js",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"types": "./index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": "./src/index.js",
|
|
10
|
+
"import": "./src/index.js",
|
|
11
|
+
"types": "./index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"docs",
|
|
17
|
+
"index.d.ts",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"lint": "eslint .",
|
|
23
|
+
"format": "prettier --write ."
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"express",
|
|
27
|
+
"nodejs",
|
|
28
|
+
"api",
|
|
29
|
+
"response",
|
|
30
|
+
"response-helper",
|
|
31
|
+
"api-response",
|
|
32
|
+
"backend"
|
|
33
|
+
],
|
|
34
|
+
"author": "Piyush Yadav",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"type": "commonjs",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/piyush72yaduvanshi/api-response-kit.git"
|
|
40
|
+
},
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/piyush72yaduvanshi/api-response-kit/issues"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/piyush72yaduvanshi/api-response-kit#readme",
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"eslint": "^10.5.0",
|
|
47
|
+
"jest": "^30.4.2",
|
|
48
|
+
"prettier": "^3.8.4",
|
|
49
|
+
"typescript": "^6.0.3"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/error.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const sendError = (
|
|
2
|
+
res,
|
|
3
|
+
statusCode,
|
|
4
|
+
message,
|
|
5
|
+
errors = null
|
|
6
|
+
) => {
|
|
7
|
+
return res.status(statusCode).json({
|
|
8
|
+
success: false,
|
|
9
|
+
message,
|
|
10
|
+
errors,
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const badRequest = (res, msg = "Bad Request") =>
|
|
15
|
+
sendError(res, 400, msg);
|
|
16
|
+
|
|
17
|
+
const unauthorized = (res, msg = "Unauthorized") =>
|
|
18
|
+
sendError(res, 401, msg);
|
|
19
|
+
|
|
20
|
+
const forbidden = (res, msg = "Forbidden") =>
|
|
21
|
+
sendError(res, 403, msg);
|
|
22
|
+
|
|
23
|
+
const notFound = (res, msg = "Not Found") =>
|
|
24
|
+
sendError(res, 404, msg);
|
|
25
|
+
|
|
26
|
+
const conflict = (res, msg = "Conflict") =>
|
|
27
|
+
sendError(res, 409, msg);
|
|
28
|
+
|
|
29
|
+
const internalError = (
|
|
30
|
+
res,
|
|
31
|
+
msg = "Internal Server Error"
|
|
32
|
+
) => sendError(res, 500, msg);
|
|
33
|
+
|
|
34
|
+
module.exports = {
|
|
35
|
+
badRequest,
|
|
36
|
+
unauthorized,
|
|
37
|
+
forbidden,
|
|
38
|
+
notFound,
|
|
39
|
+
conflict,
|
|
40
|
+
internalError,
|
|
41
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const paginate = (
|
|
2
|
+
res,
|
|
3
|
+
data,
|
|
4
|
+
page,
|
|
5
|
+
limit,
|
|
6
|
+
total
|
|
7
|
+
) => {
|
|
8
|
+
return res.status(200).json({
|
|
9
|
+
success: true,
|
|
10
|
+
data,
|
|
11
|
+
pagination: {
|
|
12
|
+
page,
|
|
13
|
+
limit,
|
|
14
|
+
total,
|
|
15
|
+
totalPages: Math.ceil(total / limit),
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
paginate,
|
|
22
|
+
};
|
package/src/success.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const success = (
|
|
2
|
+
res,
|
|
3
|
+
data = null,
|
|
4
|
+
message = "Success",
|
|
5
|
+
statusCode = 200
|
|
6
|
+
) => {
|
|
7
|
+
return res.status(statusCode).json({
|
|
8
|
+
success: true,
|
|
9
|
+
message,
|
|
10
|
+
data,
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const created = (
|
|
15
|
+
res,
|
|
16
|
+
data = null,
|
|
17
|
+
message = "Created Successfully"
|
|
18
|
+
) => {
|
|
19
|
+
return success(res, data, message, 201);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
module.exports = {
|
|
23
|
+
success,
|
|
24
|
+
created,
|
|
25
|
+
};
|