vanta-api 1.2.7 → 1.2.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vanta-api",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "description": "Advanced API features and security configuration for Node.js/MongoDB.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -62,21 +62,46 @@ export class ApiFeatures {
62
62
  return this;
63
63
  }
64
64
 
65
- limitFields() {
66
- if (!this.query.fields) return this;
67
- const validFields = Object.keys(this.model.schema.paths).filter(
68
- (f) => !securityConfig.forbiddenFields.includes(f)
69
- );
70
- const project = {};
65
+ limitFields(input = "") {
66
+ const rawFields = [input, this.query.fields].filter(Boolean).join(",");
67
+ if (!rawFields) return this;
68
+
69
+ const validFields = Object.keys(this.model.schema.paths).filter(
70
+ (f) => !securityConfig.forbiddenFields.includes(f)
71
+ );
72
+
73
+ const fieldsArray = rawFields
74
+ .split(",")
75
+ .map((f) => f.trim())
76
+ .filter(Boolean);
77
+
78
+ const includeFields = new Set();
79
+ const excludeFields = new Set();
80
+
81
+ fieldsArray.forEach((f) => {
82
+ if (f.startsWith("-")) excludeFields.add(f.slice(1));
83
+ else includeFields.add(f);
84
+ });
85
+
86
+ const project = {};
71
87
 
72
- this.query.fields.split(",").forEach((f) => {
88
+ if (includeFields.size > 0) {
89
+ includeFields.forEach((f) => {
73
90
  if (validFields.includes(f)) project[f] = 1;
74
91
  });
92
+ } else if (excludeFields.size > 0) {
93
+ validFields.forEach((f) => {
94
+ if (!excludeFields.has(f)) project[f] = 1;
95
+ });
96
+ }
75
97
 
76
- if (Object.keys(project).length) this.pipeline.push({ $project: project });
77
- return this;
98
+ if (Object.keys(project).length) {
99
+ this.pipeline.push({ $project: project });
78
100
  }
79
101
 
102
+ return this;
103
+ }
104
+
80
105
  paginate() {
81
106
  const { maxLimit } = securityConfig.accessLevels[this.userRole] || {
82
107
  maxLimit: 100,