express-restify-mongoose 8.0.0 β†’ 9.0.1

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.
Files changed (36) hide show
  1. package/README.md +8 -6
  2. package/dist/buildQuery.js +18 -1
  3. package/dist/detective.js +11 -1
  4. package/dist/errorHandler.js +13 -3
  5. package/dist/express-restify-mongoose.js +32 -22
  6. package/dist/getQuerySchema.js +35 -25
  7. package/dist/middleware/access.js +13 -3
  8. package/dist/middleware/ensureContentType.js +13 -3
  9. package/dist/middleware/filterAndFindById.js +15 -5
  10. package/dist/middleware/onError.js +13 -3
  11. package/dist/middleware/outputFn.js +11 -1
  12. package/dist/middleware/prepareOutput.js +13 -3
  13. package/dist/middleware/prepareQuery.js +15 -5
  14. package/dist/moredots.js +18 -3
  15. package/dist/operations.js +38 -19
  16. package/dist/resource_filter.js +36 -7
  17. package/dist/types.js +4 -1
  18. package/dist/weedout.js +11 -1
  19. package/package.json +14 -20
  20. package/dist/cjs/buildQuery.js +0 -52
  21. package/dist/cjs/detective.js +0 -39
  22. package/dist/cjs/errorHandler.js +0 -20
  23. package/dist/cjs/express-restify-mongoose.js +0 -104
  24. package/dist/cjs/getQuerySchema.js +0 -161
  25. package/dist/cjs/middleware/access.js +0 -32
  26. package/dist/cjs/middleware/ensureContentType.js +0 -23
  27. package/dist/cjs/middleware/filterAndFindById.js +0 -32
  28. package/dist/cjs/middleware/onError.js +0 -29
  29. package/dist/cjs/middleware/outputFn.js +0 -26
  30. package/dist/cjs/middleware/prepareOutput.js +0 -89
  31. package/dist/cjs/middleware/prepareQuery.js +0 -25
  32. package/dist/cjs/moredots.js +0 -24
  33. package/dist/cjs/operations.js +0 -259
  34. package/dist/cjs/resource_filter.js +0 -113
  35. package/dist/cjs/types.js +0 -4
  36. package/dist/cjs/weedout.js +0 -27
@@ -1,7 +1,36 @@
1
- import { getProperty, hasProperty } from "dot-prop";
2
- import { detective } from "./detective.js";
3
- import { weedout } from "./weedout.js";
4
- export class Filter {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "Filter", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return Filter;
9
+ }
10
+ });
11
+ const _dotprop = /*#__PURE__*/ _interop_require_default(require("dot-prop"));
12
+ const _detective = require("./detective.js");
13
+ const _weedout = require("./weedout.js");
14
+ function _define_property(obj, key, value) {
15
+ if (key in obj) {
16
+ Object.defineProperty(obj, key, {
17
+ value: value,
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true
21
+ });
22
+ } else {
23
+ obj[key] = value;
24
+ }
25
+ return obj;
26
+ }
27
+ function _interop_require_default(obj) {
28
+ return obj && obj.__esModule ? obj : {
29
+ default: obj
30
+ };
31
+ }
32
+ const { get: getProperty, has: hasProperty } = _dotprop.default; // Because we're using an older version of dotProp that supports CommonJS
33
+ class Filter {
5
34
  add(model, options) {
6
35
  if (model.discriminators) {
7
36
  for(const modelName in model.discriminators){
@@ -43,7 +72,7 @@ export class Filter {
43
72
  item = item.toObject();
44
73
  }
45
74
  for(let i = 0; i < excluded.length; i++){
46
- weedout(item, excluded[i]);
75
+ (0, _weedout.weedout)(item, excluded[i]);
47
76
  }
48
77
  }
49
78
  return item;
@@ -64,7 +93,7 @@ export class Filter {
64
93
  }
65
94
  const excluded = this.getExcluded({
66
95
  access: options.access,
67
- modelName: detective(model, options.populate[i].path)
96
+ modelName: (0, _detective.detective)(model, options.populate[i].path)
68
97
  });
69
98
  if (hasProperty(item, options.populate[i].path)) {
70
99
  this.filterItem(getProperty(item, options.populate[i].path), excluded);
@@ -100,6 +129,6 @@ export class Filter {
100
129
  return filtered;
101
130
  }
102
131
  constructor(){
103
- this.excludedMap = new Map();
132
+ _define_property(this, "excludedMap", new Map());
104
133
  }
105
134
  }
package/dist/types.js CHANGED
@@ -1 +1,4 @@
1
- export { };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
package/dist/weedout.js CHANGED
@@ -1,4 +1,14 @@
1
- export function weedout(obj, path) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "weedout", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return weedout;
9
+ }
10
+ });
11
+ function weedout(obj, path) {
2
12
  const keys = path.split(".");
3
13
  for(let i = 0, length = keys.length; i < length; i++){
4
14
  if (Array.isArray(obj)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-restify-mongoose",
3
- "version": "8.0.0",
3
+ "version": "9.0.1",
4
4
  "description": "Easily create a flexible REST interface for mongoose models",
5
5
  "keywords": [
6
6
  "ReST",
@@ -23,33 +23,27 @@
23
23
  "name": "Florian Holzapfel",
24
24
  "email": "flo.holzapfel@gmail.com"
25
25
  },
26
- "type": "module",
27
- "exports": {
28
- "import": "./dist/express-restify-mongoose.js",
29
- "require": "./dist/cjs/express-restify-mongoose.js"
30
- },
31
- "main": "./dist/cjs/express-restify-mongoose.js",
26
+ "main": "./dist/express-restify-mongoose.js",
32
27
  "files": [
33
28
  "dist/"
34
29
  ],
35
30
  "scripts": {
36
31
  "build": "run-p build:*",
37
- "build:esm": "swc src --out-dir dist",
38
- "build:cjs": "swc src --config module.type=commonjs --out-dir dist/cjs",
32
+ "build:cjs": "swc src --config module.type=commonjs --out-dir dist",
39
33
  "build:dts": "tsup src/express-restify-mongoose.ts --dts-only",
40
34
  "lint": "eslint . --ext .js,.ts",
41
35
  "test": "run-s test:*",
42
- "test:express": "mocha -R spec ./test/express.js --timeout 10s",
43
- "test:filter": "mocha -R spec ./test/integration/resource_filter.js --timeout 10s",
44
- "test:restify": "mocha -R spec ./test/restify.js --timeout 10s",
45
- "test:unit": "mocha -R spec ./test/unit.js",
36
+ "test:express": "mocha -R spec ./test/express.mjs --timeout 10s",
37
+ "test:filter": "mocha -R spec ./test/integration/resource_filter.mjs --timeout 10s",
38
+ "test:restify": "mocha -R spec ./test/restify.mjs --timeout 10s",
39
+ "test:unit": "mocha -R spec ./test/unit.mjs",
46
40
  "tsc": "tsc --noEmit"
47
41
  },
48
42
  "dependencies": {
49
- "dot-prop": "^7.2.0",
43
+ "dot-prop": "^6.0.0",
50
44
  "lodash.isplainobject": "~4.0.6",
51
- "mongoose": "6.x",
52
- "serialize-error": "^11.0.0",
45
+ "mongoose": "^8.2.1",
46
+ "serialize-error": "^8.1.0",
53
47
  "zod": "^3.19.1"
54
48
  },
55
49
  "devDependencies": {
@@ -62,19 +56,19 @@
62
56
  "body-parser": "^1.19.0",
63
57
  "esbuild": "^0.15.12",
64
58
  "eslint": "^8.25.0",
65
- "eslint-plugin-mocha": "^10.1.0",
59
+ "eslint-plugin-mocha": "10.3.0",
66
60
  "express": "^4.17.1",
67
61
  "method-override": "^3.0.0",
68
62
  "mocha": "^10.1.0",
69
63
  "npm-run-all": "^4.1.5",
70
64
  "prettier": "^2.7.1",
71
65
  "request": "^2.88.2",
72
- "restify": "^8.5.1",
66
+ "restify": "^4.3.4",
73
67
  "sinon": "^14.0.1",
74
68
  "tsup": "^6.3.0",
75
69
  "typescript": "^4.8.4"
76
70
  },
77
71
  "engines": {
78
- "node": ">=14"
72
+ "node": ">=16"
79
73
  }
80
- }
74
+ }
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "getBuildQuery", {
6
- enumerable: true,
7
- get: ()=>getBuildQuery
8
- });
9
- function getBuildQuery(options) {
10
- return function buildQuery(query, queryOptions) {
11
- const promise = new Promise((resolve)=>{
12
- if (!queryOptions) {
13
- return resolve(query);
14
- }
15
- if (queryOptions.query) {
16
- query.where(queryOptions.query);
17
- }
18
- if (queryOptions.skip) {
19
- query.skip(queryOptions.skip);
20
- }
21
- if (options.limit && (!queryOptions.limit || queryOptions.limit > options.limit)) {
22
- queryOptions.limit = options.limit;
23
- }
24
- if (queryOptions.limit && // @ts-expect-error this is fine 🐢πŸ”₯
25
- query.op !== "countDocuments" && !queryOptions.distinct) {
26
- query.limit(queryOptions.limit);
27
- }
28
- if (queryOptions.sort) {
29
- // @ts-expect-error this is fine 🐢πŸ”₯
30
- query.sort(queryOptions.sort);
31
- }
32
- if (queryOptions.populate) {
33
- // @ts-expect-error this is fine 🐢πŸ”₯
34
- query.populate(queryOptions.populate);
35
- }
36
- if (queryOptions.select) {
37
- query.select(queryOptions.select);
38
- }
39
- if (queryOptions.distinct) {
40
- query.distinct(queryOptions.distinct);
41
- }
42
- if (options.readPreference) {
43
- query.read(options.readPreference);
44
- }
45
- if (options.lean) {
46
- query.lean(options.lean);
47
- }
48
- resolve(query);
49
- });
50
- return promise;
51
- };
52
- }
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "detective", {
6
- enumerable: true,
7
- get: ()=>detective
8
- });
9
- function detective(model, path) {
10
- const keys = path.split(".");
11
- let schema = model.schema;
12
- let schemaPath = "";
13
- for(let i = 0, length = keys.length; i < length; i++){
14
- if (schemaPath.length > 0) {
15
- schemaPath += ".";
16
- }
17
- schemaPath += keys[i];
18
- if (schema.path(schemaPath) && schema.path(schemaPath).schema) {
19
- schema = schema.path(schemaPath).schema;
20
- }
21
- }
22
- if (!schema) {
23
- return;
24
- }
25
- // @ts-expect-error this is fine 🐢πŸ”₯
26
- schemaPath = schema.path(keys[keys.length - 1]) || schema.path(schemaPath);
27
- if (!schemaPath && (!model || !model.discriminators)) {
28
- return;
29
- }
30
- // @ts-expect-error this is fine 🐢πŸ”₯
31
- if (schemaPath.caster && schemaPath.caster.options) {
32
- // @ts-expect-error this is fine 🐢πŸ”₯
33
- return schemaPath.caster.options.ref;
34
- // @ts-expect-error this is fine 🐢πŸ”₯
35
- } else if (schemaPath.options) {
36
- // @ts-expect-error this is fine 🐢πŸ”₯
37
- return schemaPath.options.ref;
38
- }
39
- }
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "getErrorHandler", {
6
- enumerable: true,
7
- get: ()=>getErrorHandler
8
- });
9
- const _http = require("http");
10
- function getErrorHandler(options) {
11
- const fn = function errorHandler(err, req, res, next) {
12
- if (err.message === _http.STATUS_CODES[404] || req.params?.id && err.path === options.idProperty && err.name === "CastError") {
13
- req.erm.statusCode = 404;
14
- } else {
15
- req.erm.statusCode = req.erm.statusCode && req.erm.statusCode >= 400 ? req.erm.statusCode : 400;
16
- }
17
- options.onError(err, req, res, next);
18
- };
19
- return fn;
20
- }
@@ -1,104 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "serve", {
6
- enumerable: true,
7
- get: ()=>serve
8
- });
9
- const _util = require("util");
10
- const _accessJs = require("./middleware/access.js");
11
- const _ensureContentTypeJs = require("./middleware/ensureContentType.js");
12
- const _filterAndFindByIdJs = require("./middleware/filterAndFindById.js");
13
- const _onErrorJs = require("./middleware/onError.js");
14
- const _outputFnJs = require("./middleware/outputFn.js");
15
- const _prepareOutputJs = require("./middleware/prepareOutput.js");
16
- const _prepareQueryJs = require("./middleware/prepareQuery.js");
17
- const _operationsJs = require("./operations.js");
18
- const _resourceFilterJs = require("./resource_filter.js");
19
- const defaultOptions = {
20
- prefix: "/api",
21
- version: "/v1",
22
- idProperty: "_id",
23
- restify: false,
24
- allowRegex: false,
25
- runValidators: false,
26
- readPreference: "primary",
27
- totalCountHeader: false,
28
- private: [],
29
- protected: [],
30
- lean: true,
31
- findOneAndUpdate: true,
32
- findOneAndRemove: true,
33
- upsert: false,
34
- preMiddleware: [],
35
- preCreate: [],
36
- preRead: [],
37
- preUpdate: [],
38
- preDelete: []
39
- };
40
- const filter = new _resourceFilterJs.Filter();
41
- function serve(app, model, options = {}) {
42
- const serveOptions = {
43
- ...defaultOptions,
44
- name: typeof options.name === "string" ? options.name : model.modelName,
45
- contextFilter: (model, req, done)=>done(model),
46
- outputFn: (0, _outputFnJs.getOutputFnHandler)(typeof options.restify === "boolean" ? !options.restify : !defaultOptions.restify),
47
- onError: (0, _onErrorJs.getOnErrorHandler)(typeof options.restify === "boolean" ? !options.restify : !defaultOptions.restify),
48
- ...options
49
- };
50
- model.schema.eachPath((name, path)=>{
51
- if (path.options.access) {
52
- switch(path.options.access.toLowerCase()){
53
- case "private":
54
- serveOptions.private.push(name);
55
- break;
56
- case "protected":
57
- serveOptions.protected.push(name);
58
- break;
59
- }
60
- }
61
- });
62
- filter.add(model, {
63
- filteredKeys: {
64
- private: serveOptions.private,
65
- protected: serveOptions.protected
66
- }
67
- });
68
- const ops = (0, _operationsJs.operations)(model, serveOptions, filter);
69
- let uriItem = `${serveOptions.prefix}${serveOptions.version}/${serveOptions.name}`;
70
- if (uriItem.indexOf("/:id") === -1) {
71
- uriItem += "/:id";
72
- }
73
- const uriItems = uriItem.replace("/:id", "");
74
- const uriCount = uriItems + "/count";
75
- const uriShallow = uriItem + "/shallow";
76
- if (typeof app.delete === "undefined") {
77
- // @ts-expect-error restify
78
- app.delete = app.del;
79
- }
80
- app.use((req, res, next)=>{
81
- req.erm = {};
82
- next();
83
- });
84
- const accessMiddleware = serveOptions.access ? (0, _accessJs.getAccessHandler)({
85
- access: serveOptions.access,
86
- idProperty: serveOptions.idProperty,
87
- onError: serveOptions.onError
88
- }) : [];
89
- const ensureContentType = (0, _ensureContentTypeJs.getEnsureContentTypeHandler)(serveOptions);
90
- const filterAndFindById = (0, _filterAndFindByIdJs.getFilterAndFindByIdHandler)(serveOptions, model);
91
- const prepareQuery = (0, _prepareQueryJs.getPrepareQueryHandler)(serveOptions);
92
- const prepareOutput = (0, _prepareOutputJs.getPrepareOutputHandler)(serveOptions, model.modelName, filter);
93
- app.get(uriItems, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItems, prepareOutput);
94
- app.get(uriCount, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getCount, prepareOutput);
95
- app.get(uriItem, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItem, prepareOutput);
96
- app.get(uriShallow, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getShallow, prepareOutput);
97
- app.post(uriItems, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.preCreate, accessMiddleware, ops.createObject, prepareOutput);
98
- app.post(uriItem, (0, _util.deprecate)(prepareQuery, "express-restify-mongoose: in a future major version, the POST method to update resources will be removed. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
99
- app.put(uriItem, (0, _util.deprecate)(prepareQuery, "express-restify-mongoose: in a future major version, the PUT method will replace rather than update a resource. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
100
- app.patch(uriItem, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
101
- app.delete(uriItems, prepareQuery, serveOptions.preMiddleware, serveOptions.preDelete, ops.deleteItems, prepareOutput);
102
- app.delete(uriItem, prepareQuery, serveOptions.preMiddleware, serveOptions.findOneAndRemove ? [] : filterAndFindById, serveOptions.preDelete, ops.deleteItem, prepareOutput);
103
- return uriItems;
104
- }
@@ -1,161 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "getQueryOptionsSchema", {
6
- enumerable: true,
7
- get: ()=>getQueryOptionsSchema
8
- });
9
- const _zod = require("zod");
10
- const PopulateOptionsSchema = _zod.z.object({
11
- path: _zod.z.string(),
12
- match: _zod.z.record(_zod.z.unknown()).optional(),
13
- options: _zod.z.record(_zod.z.unknown()).optional(),
14
- select: _zod.z.string().optional(),
15
- // Configure populate query to not use strict populate to maintain
16
- // behavior from Mongoose previous to v6 (unless already configured)
17
- strictPopulate: _zod.z.boolean().optional().default(false)
18
- });
19
- const PopulateSchema = _zod.z.preprocess((value)=>{
20
- if (typeof value === "string") {
21
- if (value.startsWith("{")) {
22
- return JSON.parse(`[${value}]`);
23
- }
24
- if (value.startsWith("[")) {
25
- return JSON.parse(value);
26
- }
27
- return value;
28
- }
29
- return Array.isArray(value) ? value : [
30
- value
31
- ];
32
- }, _zod.z.union([
33
- _zod.z.string(),
34
- _zod.z.array(PopulateOptionsSchema)
35
- ]));
36
- const SelectSchema = _zod.z.preprocess((value)=>{
37
- const fieldToRecord = (field)=>{
38
- if (field.startsWith("-")) {
39
- return [
40
- field.substring(1),
41
- 0
42
- ];
43
- }
44
- return [
45
- field,
46
- 1
47
- ];
48
- };
49
- if (typeof value === "string") {
50
- if (value.startsWith("{")) {
51
- return JSON.parse(value);
52
- }
53
- return Object.fromEntries(value.split(",").filter(Boolean).map(fieldToRecord));
54
- }
55
- if (Array.isArray(value)) {
56
- return Object.fromEntries(value.map(fieldToRecord));
57
- }
58
- return value;
59
- }, _zod.z.record(_zod.z.number().min(0).max(1)));
60
- const SortSchema = _zod.z.preprocess((value)=>{
61
- if (typeof value === "string" && value.startsWith("{")) {
62
- return JSON.parse(value);
63
- }
64
- return value;
65
- }, _zod.z.union([
66
- _zod.z.string(),
67
- _zod.z.record(_zod.z.enum([
68
- "asc",
69
- "desc",
70
- "ascending",
71
- "descending",
72
- "-1",
73
- "1"
74
- ])),
75
- _zod.z.record(_zod.z.number().min(-1).max(1))
76
- ]));
77
- const LimitSkipSchema = _zod.z.preprocess((value)=>{
78
- if (typeof value !== "string") {
79
- return value;
80
- }
81
- return Number(value);
82
- }, _zod.z.number());
83
- function getQueryOptionsSchema({ allowRegex }) {
84
- const QuerySchema = _zod.z.preprocess((value)=>{
85
- if (!allowRegex && `${value}`.toLowerCase().includes("$regex")) {
86
- throw new Error("regex_not_allowed");
87
- }
88
- if (typeof value !== "string") {
89
- return value;
90
- }
91
- return JSON.parse(value);
92
- }, _zod.z.record(_zod.z.unknown())).transform((value)=>{
93
- return Object.fromEntries(Object.entries(value).map(([key, value])=>{
94
- if (Array.isArray(value) && !key.startsWith("$")) {
95
- return [
96
- key,
97
- {
98
- $in: value
99
- }
100
- ];
101
- }
102
- return [
103
- key,
104
- value
105
- ];
106
- }));
107
- });
108
- return _zod.z.object({
109
- query: QuerySchema.optional(),
110
- populate: PopulateSchema.optional(),
111
- select: SelectSchema.optional(),
112
- sort: SortSchema.optional(),
113
- limit: LimitSkipSchema.optional(),
114
- skip: LimitSkipSchema.optional(),
115
- distinct: _zod.z.string().optional()
116
- }).transform((value)=>{
117
- if (typeof value.populate === "undefined") {
118
- return value;
119
- }
120
- const populate = typeof value.populate === "string" ? value.populate.split(",").filter(Boolean).map((field)=>{
121
- const pop = {
122
- path: field,
123
- strictPopulate: false
124
- };
125
- if (!value.select) {
126
- return pop;
127
- }
128
- for (const [k, v] of Object.entries(value.select)){
129
- if (k.startsWith(`${field}.`)) {
130
- if (pop.select) {
131
- pop.select += " ";
132
- } else {
133
- pop.select = "";
134
- }
135
- if (v === 0) {
136
- pop.select += "-";
137
- }
138
- pop.select += k.substring(field.length + 1);
139
- delete value.select[k];
140
- }
141
- }
142
- // If other specific fields are selected, add the populated field
143
- if (Object.keys(value.select).length > 0 && !value.select[field]) {
144
- value.select[field] = 1;
145
- }
146
- return pop;
147
- }) : value.populate;
148
- return {
149
- ...value,
150
- populate
151
- };
152
- }).transform((value)=>{
153
- if (!value.populate || Array.isArray(value.populate) && value.populate.length === 0) {
154
- delete value.populate;
155
- }
156
- if (!value.select || Object.keys(value.select).length === 0) {
157
- delete value.select;
158
- }
159
- return value;
160
- });
161
- }
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "getAccessHandler", {
6
- enumerable: true,
7
- get: ()=>getAccessHandler
8
- });
9
- const _errorHandlerJs = require("../errorHandler.js");
10
- function getAccessHandler(options) {
11
- const errorHandler = (0, _errorHandlerJs.getErrorHandler)(options);
12
- const fn = function access(req, res, next) {
13
- const handler = function(access) {
14
- if (![
15
- "public",
16
- "private",
17
- "protected"
18
- ].includes(access)) {
19
- throw new Error('Unsupported access, must be "public", "private" or "protected"');
20
- }
21
- req.access = access;
22
- next();
23
- };
24
- const result = options.access(req);
25
- if (typeof result === "string") {
26
- handler(result);
27
- } else {
28
- result.then(handler).catch((err)=>errorHandler(err, req, res, next));
29
- }
30
- };
31
- return fn;
32
- }
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "getEnsureContentTypeHandler", {
6
- enumerable: true,
7
- get: ()=>getEnsureContentTypeHandler
8
- });
9
- const _errorHandlerJs = require("../errorHandler.js");
10
- function getEnsureContentTypeHandler(options) {
11
- const errorHandler = (0, _errorHandlerJs.getErrorHandler)(options);
12
- const fn = function ensureContentType(req, res, next) {
13
- const contentType = req.headers["content-type"];
14
- if (!contentType) {
15
- return errorHandler(new Error("missing_content_type"), req, res, next);
16
- }
17
- if (!contentType.includes("application/json")) {
18
- return errorHandler(new Error("invalid_content_type"), req, res, next);
19
- }
20
- next();
21
- };
22
- return fn;
23
- }
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "getFilterAndFindByIdHandler", {
6
- enumerable: true,
7
- get: ()=>getFilterAndFindByIdHandler
8
- });
9
- const _http = require("http");
10
- const _errorHandlerJs = require("../errorHandler.js");
11
- function getFilterAndFindByIdHandler(options, model) {
12
- const errorHandler = (0, _errorHandlerJs.getErrorHandler)(options);
13
- const fn = function filterAndFindById(req, res, next) {
14
- const contextModel = model;
15
- if (!req.params.id) {
16
- return next();
17
- }
18
- options.contextFilter(contextModel, req, (filteredContext)=>{
19
- filteredContext// @ts-expect-error this is fine 🐢πŸ”₯
20
- .findOne().and({
21
- [options.idProperty]: req.params.id
22
- }).lean(false).read(options.readPreference || "p").exec().then((doc)=>{
23
- if (!doc) {
24
- return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
25
- }
26
- req.erm.document = doc;
27
- next();
28
- }).catch((err)=>errorHandler(err, req, res, next));
29
- });
30
- };
31
- return fn;
32
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "getOnErrorHandler", {
6
- enumerable: true,
7
- get: ()=>getOnErrorHandler
8
- });
9
- const _serializeError = require("serialize-error");
10
- function getOnErrorHandler(isExpress) {
11
- const fn = function onError(err, req, res) {
12
- const serializedErr = (0, _serializeError.serializeError)(err);
13
- delete serializedErr.stack;
14
- if (serializedErr.errors) {
15
- for(const key in serializedErr.errors){
16
- delete serializedErr.errors[key].reason;
17
- delete serializedErr.errors[key].stack;
18
- }
19
- }
20
- res.setHeader("Content-Type", "application/json");
21
- if (isExpress) {
22
- res.status(req.erm.statusCode || 500).send(serializedErr);
23
- } else {
24
- // @ts-expect-error restify
25
- res.send(req.erm.statusCode, serializedErr);
26
- }
27
- };
28
- return fn;
29
- }