vanta-api 1.0.1 → 1.0.3
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 +17 -17
- package/bin/create-security-config.js +1 -1
- package/package.json +1 -1
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()
|
|
@@ -276,11 +276,11 @@ This middleware sets the appropriate HTTP status code and JSON error message whe
|
|
|
276
276
|
### Example 1: Basic Query
|
|
277
277
|
|
|
278
278
|
```javascript
|
|
279
|
-
import
|
|
279
|
+
import ApiFeatures from "./api-features.js";
|
|
280
280
|
import Product from "./models/product.js";
|
|
281
281
|
|
|
282
282
|
// 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
|
|
283
|
+
const features = new ApiFeatures(Product, req.query, "user");
|
|
284
284
|
const result = await features
|
|
285
285
|
.filter()
|
|
286
286
|
.sort()
|
|
@@ -298,7 +298,7 @@ console.log(result);
|
|
|
298
298
|
```javascript
|
|
299
299
|
const query = { status: "active" };
|
|
300
300
|
const manualFilter = { category: "electronics" };
|
|
301
|
-
const features = new
|
|
301
|
+
const features = new ApiFeatures(Product, query, "user");
|
|
302
302
|
features.addManualFilters(manualFilter).filter();
|
|
303
303
|
const result = await features.execute();
|
|
304
304
|
console.log(result);
|
|
@@ -312,7 +312,7 @@ const populateArray = [
|
|
|
312
312
|
{ path: "category", select: "name description" },
|
|
313
313
|
{ path: "category", select: "name", populate: { path: "subCategory", select: "title" } }
|
|
314
314
|
];
|
|
315
|
-
const features = new
|
|
315
|
+
const features = new ApiFeatures(Product, req.query, "admin");
|
|
316
316
|
const result = await features.populate(populateArray).execute();
|
|
317
317
|
console.log(result);
|
|
318
318
|
```
|
|
@@ -335,7 +335,7 @@ GET /api/products?
|
|
|
335
335
|
|
|
336
336
|
```javascript
|
|
337
337
|
// URL: /api/products?status=active
|
|
338
|
-
const features = new
|
|
338
|
+
const features = new ApiFeatures(Product, req.query);
|
|
339
339
|
const result = await features
|
|
340
340
|
.filter() // Defaults to page 1 and limit 10
|
|
341
341
|
.execute();
|
|
@@ -4,7 +4,7 @@ import { join } from 'path';
|
|
|
4
4
|
|
|
5
5
|
// Define the target path for the security-config file.
|
|
6
6
|
// This example creates it in the current working directory.
|
|
7
|
-
const configPath = join(process.cwd(), 'security-config.js');
|
|
7
|
+
const configPath = join(process.env.INIT_CWD || process.cwd(), 'security-config.js');
|
|
8
8
|
|
|
9
9
|
// If the file does not exist, create it with default content.
|
|
10
10
|
if (!existsSync(configPath)) {
|