express-restify-mongoose 9.0.13 β†’ 9.0.15

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 CHANGED
@@ -19,6 +19,7 @@ npm install express-restify-mongoose --save
19
19
 
20
20
  | This library | Mongoose | MongoDB | NodeJS
21
21
  | ------------ | ----------- | ----------- | --------
22
+ | >= 10.0.0 | 8.x | 5.x - 8.x | >=22
22
23
  | >= 9.0.0 | 6.x - 8.x | 3.6.x - 7.x | >=16
23
24
  | >= 8.0.0 | 6.x | 3.6.x - 6.x | >=18
24
25
  | >= 7.0.0 | 6.x | 3.6.x - 6.x | >=14
@@ -53,10 +53,10 @@ const BLOCKED_OPERATORS = new Set([
53
53
  }
54
54
  const PopulateOptionsSchema = _zod.z.object({
55
55
  path: _zod.z.string(),
56
- match: _zod.z.record(_zod.z.unknown()).optional(),
57
- options: _zod.z.record(_zod.z.unknown()).optional(),
56
+ match: _zod.z.record(_zod.z.string(), _zod.z.unknown()).optional(),
57
+ options: _zod.z.record(_zod.z.string(), _zod.z.unknown()).optional(),
58
58
  select: _zod.z.string().optional(),
59
- populate: _zod.z.record(_zod.z.unknown()).optional(),
59
+ populate: _zod.z.record(_zod.z.string(), _zod.z.unknown()).optional(),
60
60
  // Configure populate query to not use strict populate to maintain
61
61
  // behavior from Mongoose previous to v6 (unless already configured)
62
62
  strictPopulate: _zod.z.boolean().optional().default(false)
@@ -101,7 +101,7 @@ const SelectSchema = _zod.z.preprocess((value)=>{
101
101
  return Object.fromEntries(value.map(fieldToRecord));
102
102
  }
103
103
  return value;
104
- }, _zod.z.record(_zod.z.number().min(0).max(1)));
104
+ }, _zod.z.record(_zod.z.string(), _zod.z.number().min(0).max(1)));
105
105
  const SortSchema = _zod.z.preprocess((value)=>{
106
106
  if (typeof value === "string" && value.startsWith("{")) {
107
107
  return JSON.parse(value);
@@ -109,7 +109,7 @@ const SortSchema = _zod.z.preprocess((value)=>{
109
109
  return value;
110
110
  }, _zod.z.union([
111
111
  _zod.z.string(),
112
- _zod.z.record(_zod.z.enum([
112
+ _zod.z.record(_zod.z.string(), _zod.z.enum([
113
113
  "asc",
114
114
  "desc",
115
115
  "ascending",
@@ -117,7 +117,7 @@ const SortSchema = _zod.z.preprocess((value)=>{
117
117
  "-1",
118
118
  "1"
119
119
  ])),
120
- _zod.z.record(_zod.z.number().min(-1).max(1))
120
+ _zod.z.record(_zod.z.string(), _zod.z.number().min(-1).max(1))
121
121
  ]));
122
122
  const LimitSkipSchema = _zod.z.preprocess((value)=>{
123
123
  if (typeof value !== "string") {
@@ -132,7 +132,7 @@ function getQueryOptionsSchema({ allowRegex }) {
132
132
  allowRegex
133
133
  });
134
134
  return query;
135
- }, _zod.z.record(_zod.z.unknown())).transform((value)=>{
135
+ }, _zod.z.record(_zod.z.string(), _zod.z.unknown())).transform((value)=>{
136
136
  return Object.fromEntries(Object.entries(value).map(([key, value])=>{
137
137
  if (Array.isArray(value) && !key.startsWith("$")) {
138
138
  return [
@@ -168,7 +168,7 @@ function getQueryOptionsSchema({ allowRegex }) {
168
168
  });
169
169
  } catch {
170
170
  ctx.addIssue({
171
- code: _zod.z.ZodIssueCode.custom,
171
+ code: "custom",
172
172
  message: "operator_not_allowed"
173
173
  });
174
174
  }
@@ -9,16 +9,48 @@ Object.defineProperty(exports, "getOnErrorHandler", {
9
9
  }
10
10
  });
11
11
  const _serializeerror = require("serialize-error");
12
+ // Properties that hold an underlying error (mongoose's `CastError.reason`,
13
+ // `Error.cause`) or a stack trace. They describe the server, not the request,
14
+ // so they never go to the client.
15
+ const INTERNAL_KEYS = [
16
+ "stack",
17
+ "reason",
18
+ "cause"
19
+ ];
20
+ function stripInternals(serializedErr) {
21
+ for (const key of INTERNAL_KEYS){
22
+ delete serializedErr[key];
23
+ }
24
+ }
25
+ // Whether a stack is emitted for a nested error depends on the version of
26
+ // serialize-error, so remove any that is left, at any depth.
27
+ function stripStacks(value, seen = new WeakSet()) {
28
+ if (typeof value !== "object" || value === null || seen.has(value)) {
29
+ return;
30
+ }
31
+ seen.add(value);
32
+ const obj = value;
33
+ for (const key of Object.keys(obj)){
34
+ if (key === "stack" && typeof obj[key] === "string") {
35
+ delete obj[key];
36
+ } else {
37
+ stripStacks(obj[key], seen);
38
+ }
39
+ }
40
+ }
12
41
  function getOnErrorHandler(isExpress) {
13
42
  const fn = function onError(err, req, res) {
14
43
  const serializedErr = (0, _serializeerror.serializeError)(err);
15
- delete serializedErr.stack;
44
+ stripInternals(serializedErr);
16
45
  if (serializedErr.errors) {
17
46
  for(const key in serializedErr.errors){
18
- delete serializedErr.errors[key].reason;
19
- delete serializedErr.errors[key].stack;
47
+ const nested = serializedErr.errors[key];
48
+ if (typeof nested === "object" && nested !== null) {
49
+ stripInternals(nested);
50
+ }
20
51
  }
21
52
  }
53
+ stripStacks(serializedErr);
22
54
  res.setHeader("Content-Type", "application/json");
23
55
  if (isExpress) {
24
56
  res.status(req.erm.statusCode || 500).send(serializedErr);
@@ -32,10 +32,19 @@ function operations(model, options, filter) {
32
32
  if (!req.erm.query?.distinct) {
33
33
  return false;
34
34
  }
35
- return filter.getExcluded({
35
+ return filter.isPathExcluded(req.erm.query.distinct, {
36
36
  access: req.access,
37
37
  modelName: model.modelName
38
- }).includes(req.erm.query.distinct);
38
+ });
39
+ }
40
+ /**
41
+ * distinct replaces the operation of the query it is applied to, it is only
42
+ * meant for getItems and getItem, where isDistinctExcluded guards it.
43
+ */ function withoutDistinct(req) {
44
+ return req.erm.query && {
45
+ ...req.erm.query,
46
+ distinct: undefined
47
+ };
39
48
  }
40
49
  const getItems = function(req, res, next) {
41
50
  const contextModel = req.erm.model;
@@ -53,8 +62,7 @@ function operations(model, options, filter) {
53
62
  req.erm.statusCode = 200;
54
63
  if (options.totalCountHeader && !req.erm.query?.distinct) {
55
64
  options.contextFilter(contextModel, req, (countFilteredContext)=>{
56
- buildQuery(// @ts-expect-error this is fine 🐢πŸ”₯
57
- countFilteredContext.countDocuments(), {
65
+ buildQuery(countFilteredContext.countDocuments(), {
58
66
  ...req.erm.query,
59
67
  skip: 0,
60
68
  limit: 0
@@ -75,8 +83,7 @@ function operations(model, options, filter) {
75
83
  return errorHandler(new Error('Model is undefined.'), req, res, next);
76
84
  }
77
85
  options.contextFilter(contextModel, req, (filteredContext)=>{
78
- buildQuery(// @ts-expect-error this is fine 🐢πŸ”₯
79
- filteredContext.countDocuments(), req.erm.query).then((count)=>{
86
+ buildQuery(filteredContext.countDocuments(), withoutDistinct(req)).then((count)=>{
80
87
  req.erm.result = {
81
88
  count: count
82
89
  };
@@ -92,7 +99,7 @@ function operations(model, options, filter) {
92
99
  }
93
100
  options.contextFilter(contextModel, req, (filteredContext)=>{
94
101
  buildQuery(// @ts-expect-error this is fine 🐢πŸ”₯
95
- findById(filteredContext, req.params.id), req.erm.query).then((item)=>{
102
+ findById(filteredContext, req.params.id), withoutDistinct(req)).then((item)=>{
96
103
  if (!item) {
97
104
  return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
98
105
  }
@@ -111,7 +118,7 @@ function operations(model, options, filter) {
111
118
  return errorHandler(new Error('Model is undefined.'), req, res, next);
112
119
  }
113
120
  options.contextFilter(contextModel, req, (filteredContext)=>{
114
- buildQuery(filteredContext.deleteMany(), req.erm.query).then(()=>{
121
+ buildQuery(filteredContext.deleteMany(), withoutDistinct(req)).then(()=>{
115
122
  req.erm.statusCode = 204;
116
123
  next();
117
124
  }).catch((err)=>errorHandler(err, req, res, next));
@@ -19,9 +19,7 @@ function _define_property(obj, key, value) {
19
19
  configurable: true,
20
20
  writable: true
21
21
  });
22
- } else {
23
- obj[key] = value;
24
- }
22
+ } else obj[key] = value;
25
23
  return obj;
26
24
  }
27
25
  function _interop_require_default(obj) {
@@ -59,6 +57,21 @@ class Filter {
59
57
  return options.access === "protected" ? filteredKeys.private : filteredKeys.private.concat(filteredKeys.protected);
60
58
  }
61
59
  /**
60
+ * Tells whether reading a path would disclose an excluded key.
61
+ *
62
+ * A path is excluded when it is an excluded key, a parent of one (the value
63
+ * would embed the excluded key) or a child of one (the value is part of the
64
+ * excluded key). Array positions are ignored, `purchases.0.number` is the
65
+ * same key as `purchases.number`.
66
+ */ isPathExcluded(path, options) {
67
+ const normalize = (value)=>value.split(".").filter((key)=>!/^\d+$/.test(key)).join(".");
68
+ const normalizedPath = normalize(path);
69
+ return this.getExcluded(options).some((excluded)=>{
70
+ const normalizedExcluded = normalize(excluded);
71
+ return normalizedPath === normalizedExcluded || normalizedPath.startsWith(`${normalizedExcluded}.`) || normalizedExcluded.startsWith(`${normalizedPath}.`);
72
+ });
73
+ }
74
+ /**
62
75
  * Removes excluded keys from a document.
63
76
  */ filterItem(item, excluded) {
64
77
  if (!item) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-restify-mongoose",
3
- "version": "9.0.13",
3
+ "version": "9.0.15",
4
4
  "description": "Easily create a flexible REST interface for mongoose models",
5
5
  "keywords": [
6
6
  "ReST",
@@ -29,7 +29,7 @@
29
29
  ],
30
30
  "scripts": {
31
31
  "build": "run-p build:*",
32
- "build:cjs": "swc src --config module.type=commonjs --out-dir dist",
32
+ "build:cjs": "swc src --config module.type=commonjs --strip-leading-paths --out-dir dist",
33
33
  "build:dts": "tsup src/express-restify-mongoose.ts --dts-only",
34
34
  "lint": "eslint . --ext .js,.ts",
35
35
  "test": "run-s test:*",
@@ -43,30 +43,35 @@
43
43
  "lodash.isplainobject": "~4.0.6",
44
44
  "mongoose": "^8.22.1",
45
45
  "serialize-error": "^8.1.0",
46
- "zod": "^3.19.1"
46
+ "zod": "^4.6.6"
47
47
  },
48
48
  "devDependencies": {
49
- "@swc/cli": "^0.1.57",
50
- "@swc/core": "^1.3.17",
49
+ "@swc/cli": "^0.8.2",
50
+ "@swc/core": "^1.16.22",
51
51
  "@swc/register": "^0.1.10",
52
52
  "@types/express": "^4.17.14",
53
+ "@types/node": "^22",
53
54
  "@typescript-eslint/eslint-plugin": "^5.40.0",
54
55
  "@typescript-eslint/parser": "^5.40.0",
55
- "body-parser": "^1.19.0",
56
- "esbuild": "^0.15.12",
56
+ "body-parser": "^1.20.9",
57
+ "esbuild": "^0.28.2",
57
58
  "eslint": "^8.25.0",
58
59
  "eslint-plugin-mocha": "10.3.0",
59
- "express": "^4.17.1",
60
+ "express": "^4.22.3",
60
61
  "method-override": "^3.0.0",
61
- "mocha": "^10.1.0",
62
- "npm-run-all": "^4.1.5",
62
+ "mocha": "^12.0.3",
63
+ "npm-run-all2": "^9.0.4",
63
64
  "prettier": "^2.7.1",
64
- "request": "^2.88.2",
65
- "sinon": "^14.0.1",
66
- "tsup": "^6.3.0",
67
- "typescript": "^4.8.4"
65
+ "sinon": "^22.1.0",
66
+ "tsup": "^9.0.0",
67
+ "typescript": "^5.9.3"
68
+ },
69
+ "resolutions": {
70
+ "mocha/glob": "^14.0.4",
71
+ "mocha/minimatch": "^11.0.5",
72
+ "@swc/cli/minimatch": "^11.0.5"
68
73
  },
69
74
  "engines": {
70
- "node": ">=16"
75
+ "node": ">=22"
71
76
  }
72
77
  }