vanta-api 1.0.2 → 1.0.4
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 +92 -23
- package/index.js +6 -0
- package/package.json +3 -3
- package/src/api-features.js +1 -1
- package/src/catchAsync.js +6 -0
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ This repository provides a robust, feature-rich, and secure solution for buildin
|
|
|
10
10
|
|
|
11
11
|
1. [Installation & Setup](#installation--setup)
|
|
12
12
|
2. [Overview](#overview)
|
|
13
|
-
3. [
|
|
13
|
+
3. [ApiFeatures Class Methods](#ApiFeatures-class-methods)
|
|
14
14
|
- [filter()](#filter)
|
|
15
15
|
- [sort()](#sort)
|
|
16
16
|
- [limitFields()](#limitfields)
|
|
@@ -54,7 +54,7 @@ npm install --save-dev jest
|
|
|
54
54
|
|
|
55
55
|
## Overview
|
|
56
56
|
|
|
57
|
-
The **
|
|
57
|
+
The **ApiFeatures** class processes incoming query parameters and progressively builds an aggregation pipeline. The package supports:
|
|
58
58
|
|
|
59
59
|
- **Advanced filtering, sorting, and field selection.**
|
|
60
60
|
- **Pagination with defaults** (defaults to page 1 and limit 10 if not provided), with the maximum limit based on the user's role.
|
|
@@ -67,7 +67,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
67
67
|
|
|
68
68
|
---
|
|
69
69
|
|
|
70
|
-
##
|
|
70
|
+
## ApiFeatures Class Methods
|
|
71
71
|
|
|
72
72
|
### filter()
|
|
73
73
|
- **Description:**
|
|
@@ -76,7 +76,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
76
76
|
|
|
77
77
|
```javascript
|
|
78
78
|
// URL: /api/products?status=active&price[gte]=100
|
|
79
|
-
const features = new
|
|
79
|
+
const features = new ApiFeatures(Product, req.query);
|
|
80
80
|
features.filter();
|
|
81
81
|
// Pipeline adds: { $match: { status: "active", price: { $gte: 100 }, isActive: true } }
|
|
82
82
|
```
|
|
@@ -88,7 +88,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
88
88
|
|
|
89
89
|
```javascript
|
|
90
90
|
// URL: /api/products?sort=-price,createdAt
|
|
91
|
-
const features = new
|
|
91
|
+
const features = new ApiFeatures(Product, req.query);
|
|
92
92
|
features.sort();
|
|
93
93
|
// Pipeline adds: { $sort: { price: -1, createdAt: 1 } }
|
|
94
94
|
```
|
|
@@ -100,7 +100,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
100
100
|
|
|
101
101
|
```javascript
|
|
102
102
|
// URL: /api/products?fields=name,price,category,password
|
|
103
|
-
const features = new
|
|
103
|
+
const features = new ApiFeatures(Product, req.query);
|
|
104
104
|
features.limitFields();
|
|
105
105
|
// Pipeline adds: { $project: { name: 1, price: 1, category: 1 } }
|
|
106
106
|
```
|
|
@@ -112,7 +112,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
112
112
|
|
|
113
113
|
```javascript
|
|
114
114
|
// URL: /api/products?page=2&limit=20
|
|
115
|
-
const features = new
|
|
115
|
+
const features = new ApiFeatures(Product, req.query, "user");
|
|
116
116
|
features.paginate();
|
|
117
117
|
// Pipeline adds: { $skip: 20 } and { $limit: 20 }
|
|
118
118
|
```
|
|
@@ -128,7 +128,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
128
128
|
|
|
129
129
|
```javascript
|
|
130
130
|
// URL: /api/products?populate=category,brand
|
|
131
|
-
const features = new
|
|
131
|
+
const features = new ApiFeatures(Product, req.query);
|
|
132
132
|
features.populate();
|
|
133
133
|
```
|
|
134
134
|
|
|
@@ -136,7 +136,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
136
136
|
|
|
137
137
|
```javascript
|
|
138
138
|
const populateOptions = { path: "category", select: "name description" };
|
|
139
|
-
const features = new
|
|
139
|
+
const features = new ApiFeatures(Product, req.query);
|
|
140
140
|
features.populate(populateOptions);
|
|
141
141
|
```
|
|
142
142
|
|
|
@@ -148,7 +148,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
148
148
|
{ path: "category", select: "name description" },
|
|
149
149
|
{ path: "category", select: "name", populate: { path: "subCategory", select: "title" } }
|
|
150
150
|
];
|
|
151
|
-
const features = new
|
|
151
|
+
const features = new ApiFeatures(Product, req.query, "admin");
|
|
152
152
|
features.populate(populateArray);
|
|
153
153
|
```
|
|
154
154
|
|
|
@@ -159,7 +159,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
159
159
|
|
|
160
160
|
```javascript
|
|
161
161
|
const manualFilter = { category: "electronics" };
|
|
162
|
-
const features = new
|
|
162
|
+
const features = new ApiFeatures(Product, { status: "active" });
|
|
163
163
|
features.addManualFilters(manualFilter).filter();
|
|
164
164
|
```
|
|
165
165
|
|
|
@@ -169,7 +169,7 @@ The **VantaApi** class processes incoming query parameters and progressively bui
|
|
|
169
169
|
- **Usage Example:**
|
|
170
170
|
|
|
171
171
|
```javascript
|
|
172
|
-
const features = new
|
|
172
|
+
const features = new ApiFeatures(Product, req.query);
|
|
173
173
|
const result = await features
|
|
174
174
|
.filter()
|
|
175
175
|
.sort()
|
|
@@ -258,29 +258,77 @@ These configurations are applied automatically in methods such as `filter()`, `p
|
|
|
258
258
|
|
|
259
259
|
## Error Handling Middleware
|
|
260
260
|
|
|
261
|
-
|
|
261
|
+
Vanta-API provides a centralized error handling system featuring three components:
|
|
262
|
+
|
|
263
|
+
### 1. handleError
|
|
264
|
+
|
|
265
|
+
**Purpose:**
|
|
266
|
+
A custom error class that extends the native JavaScript `Error`.
|
|
267
|
+
It adds:
|
|
268
|
+
- **`statusCode`:** HTTP status code.
|
|
269
|
+
- **`status`:** Determines if the error is a `"fail"` (client error) or `"error"` (server error).
|
|
270
|
+
- **`isOperational`:** Flags if the error is an expected operational error.
|
|
271
|
+
- **Stack Trace:** Captures the call stack for easier debugging.
|
|
272
|
+
|
|
273
|
+
**Usage Example in an Async Function:**
|
|
274
|
+
|
|
275
|
+
Inside an asynchronous function wrapped by **catchAsync**, you can use:
|
|
276
|
+
|
|
277
|
+
```javascript
|
|
278
|
+
// Inside your async route handler
|
|
279
|
+
if (someConditionFails) {
|
|
280
|
+
return next(new handleError("Custom error message", 400));
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### 2. catchAsync
|
|
285
|
+
|
|
286
|
+
**Purpose:**
|
|
287
|
+
A helper function that wraps asynchronous route handlers.
|
|
288
|
+
It automatically catches any thrown errors and forwards them via `next()`, avoiding repetitive try/catch blocks.
|
|
289
|
+
|
|
290
|
+
**Usage Example:**
|
|
262
291
|
|
|
263
292
|
```javascript
|
|
264
|
-
|
|
293
|
+
app.get("/example", catchAsync(async (req, res, next) => {
|
|
294
|
+
// Your async logic here
|
|
295
|
+
// If an error occurs, use handleError as shown above
|
|
296
|
+
res.status(200).json({ data: "Success" });
|
|
297
|
+
}));
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### 3. catchError
|
|
301
|
+
|
|
302
|
+
**Purpose:**
|
|
303
|
+
An Express middleware that catches any error passed along (either from synchronous or asynchronous routes).
|
|
304
|
+
It sets the appropriate HTTP status and returns a JSON response containing the error’s status and message.
|
|
305
|
+
|
|
306
|
+
**Usage Example:**
|
|
265
307
|
|
|
266
|
-
|
|
267
|
-
//
|
|
308
|
+
```javascript
|
|
309
|
+
// At the end of your middleware stack:
|
|
310
|
+
app.use(catchError);
|
|
268
311
|
```
|
|
269
312
|
|
|
270
|
-
|
|
313
|
+
---
|
|
314
|
+
|
|
271
315
|
|
|
272
316
|
---
|
|
273
317
|
|
|
274
318
|
## Full Examples
|
|
275
319
|
|
|
276
320
|
### Example 1: Basic Query
|
|
321
|
+
To import the package along with the error handling components, use:
|
|
322
|
+
|
|
323
|
+
```javascript
|
|
324
|
+
import ApiFeatures, { handleError, catchAsync, catchError } from "vanta-api";
|
|
325
|
+
```
|
|
277
326
|
|
|
278
327
|
```javascript
|
|
279
|
-
import VantaApi from "./api-features.js";
|
|
280
328
|
import Product from "./models/product.js";
|
|
281
329
|
|
|
282
330
|
// URL: /api/products?status=active&price[gte]=100&sort=-price,createdAt&fields=name,price,category&page=1&limit=10&populate=category,brand
|
|
283
|
-
const features = new
|
|
331
|
+
const features = new ApiFeatures(Product, req.query, "user");
|
|
284
332
|
const result = await features
|
|
285
333
|
.filter()
|
|
286
334
|
.sort()
|
|
@@ -298,7 +346,7 @@ console.log(result);
|
|
|
298
346
|
```javascript
|
|
299
347
|
const query = { status: "active" };
|
|
300
348
|
const manualFilter = { category: "electronics" };
|
|
301
|
-
const features = new
|
|
349
|
+
const features = new ApiFeatures(Product, query, "user");
|
|
302
350
|
features.addManualFilters(manualFilter).filter();
|
|
303
351
|
const result = await features.execute();
|
|
304
352
|
console.log(result);
|
|
@@ -312,7 +360,7 @@ const populateArray = [
|
|
|
312
360
|
{ path: "category", select: "name description" },
|
|
313
361
|
{ path: "category", select: "name", populate: { path: "subCategory", select: "title" } }
|
|
314
362
|
];
|
|
315
|
-
const features = new
|
|
363
|
+
const features = new ApiFeatures(Product, req.query, "admin");
|
|
316
364
|
const result = await features.populate(populateArray).execute();
|
|
317
365
|
console.log(result);
|
|
318
366
|
```
|
|
@@ -335,7 +383,7 @@ GET /api/products?
|
|
|
335
383
|
|
|
336
384
|
```javascript
|
|
337
385
|
// URL: /api/products?status=active
|
|
338
|
-
const features = new
|
|
386
|
+
const features = new ApiFeatures(Product, req.query);
|
|
339
387
|
const result = await features
|
|
340
388
|
.filter() // Defaults to page 1 and limit 10
|
|
341
389
|
.execute();
|
|
@@ -362,9 +410,30 @@ console.log(result);
|
|
|
362
410
|
Integrated advanced logging using winston and centralized error handling with a custom error class and middleware.
|
|
363
411
|
- **Performance Optimizations:**
|
|
364
412
|
Supports aggregation cursor for large datasets and optimizes aggregation pipelines for efficient resource usage.
|
|
413
|
+
|
|
414
|
+
- **ApiFeatures:**
|
|
415
|
+
Provides advanced query capabilities such as filtering, sorting, pagination, and document population for your MongoDB data.
|
|
416
|
+
|
|
417
|
+
- **Error Handling Components:**
|
|
418
|
+
- **handleError:**
|
|
419
|
+
Throw consistent, structured errors with custom messages and status codes.
|
|
420
|
+
- **catchAsync:**
|
|
421
|
+
Wrap asynchronous route handlers to automatically propagate errors.
|
|
422
|
+
- **catchError:**
|
|
423
|
+
Centralized middleware to catch and respond to errors uniformly.
|
|
424
|
+
|
|
425
|
+
- **Importing:**
|
|
426
|
+
Use the following statement to access all features:
|
|
427
|
+
|
|
428
|
+
```javascript
|
|
429
|
+
import ApiFeatures, { handleError, catchAsync, catchError } from "vanta-api";
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
By following these guidelines, you can integrate and use Vanta-API for advanced, secure query handling and robust error management in your Node.js/Express projects.
|
|
365
433
|
|
|
366
434
|
---
|
|
367
435
|
|
|
368
436
|
VantaApi provides a complete solution for integrating powerful, secure, and customizable query capabilities into any Node.js/MongoDB project.
|
|
369
437
|
|
|
370
|
-
---
|
|
438
|
+
---
|
|
439
|
+
|
package/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import ApiFeatures from "./src/api-features.js";
|
|
2
|
+
import catchError from "./src/errorHandler.js";
|
|
3
|
+
import HandleERROR from "./src/handleError.js";
|
|
4
|
+
import catchAsync from "./src/catchAsync.js";
|
|
5
|
+
export {catchError,HandleERROR,catchAsync}
|
|
6
|
+
export default ApiFeatures
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vanta-api",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Advanced API features and security configuration for Node.js/MongoDB.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"postinstall": "node bin/create-security-config.js",
|
|
8
8
|
"test": "jest",
|
|
9
|
-
"start": "node
|
|
9
|
+
"start": "node index.js"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [
|
|
12
12
|
"api",
|
package/src/api-features.js
CHANGED