express-restify-mongoose 7.0.2 → 8.0.0
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 +0 -2
- package/dist/buildQuery.js +44 -0
- package/dist/cjs/buildQuery.js +52 -0
- package/dist/cjs/detective.js +39 -0
- package/dist/cjs/errorHandler.js +20 -0
- package/dist/cjs/express-restify-mongoose.js +104 -0
- package/dist/cjs/getQuerySchema.js +161 -0
- package/dist/cjs/middleware/access.js +32 -0
- package/dist/cjs/middleware/ensureContentType.js +23 -0
- package/dist/cjs/middleware/filterAndFindById.js +32 -0
- package/dist/cjs/middleware/onError.js +29 -0
- package/dist/cjs/middleware/outputFn.js +26 -0
- package/dist/cjs/middleware/prepareOutput.js +89 -0
- package/dist/cjs/middleware/prepareQuery.js +25 -0
- package/dist/cjs/moredots.js +24 -0
- package/dist/cjs/operations.js +259 -0
- package/dist/cjs/resource_filter.js +113 -0
- package/dist/cjs/types.js +4 -0
- package/dist/cjs/weedout.js +27 -0
- package/dist/detective.js +31 -0
- package/dist/errorHandler.js +12 -0
- package/dist/express-restify-mongoose.d.ts +45 -0
- package/dist/express-restify-mongoose.js +96 -0
- package/dist/getQuerySchema.js +153 -0
- package/dist/middleware/access.js +24 -0
- package/dist/middleware/ensureContentType.js +15 -0
- package/dist/middleware/filterAndFindById.js +24 -0
- package/dist/middleware/onError.js +21 -0
- package/dist/middleware/outputFn.js +18 -0
- package/dist/middleware/prepareOutput.js +81 -0
- package/dist/middleware/prepareQuery.js +17 -0
- package/dist/moredots.js +11 -0
- package/dist/operations.js +246 -0
- package/dist/resource_filter.js +105 -0
- package/dist/types.js +1 -0
- package/dist/weedout.js +19 -0
- package/package.json +62 -62
- package/CHANGELOG.md +0 -92
- package/index.d.ts +0 -45
- package/src/buildQuery.js +0 -55
- package/src/errorHandler.js +0 -17
- package/src/express-restify-mongoose.js +0 -144
- package/src/middleware/access.js +0 -26
- package/src/middleware/ensureContentType.js +0 -19
- package/src/middleware/filterAndFindById.js +0 -35
- package/src/middleware/onError.js +0 -26
- package/src/middleware/outputFn.js +0 -15
- package/src/middleware/prepareOutput.js +0 -75
- package/src/middleware/prepareQuery.js +0 -123
- package/src/operations.js +0 -274
- package/src/resource_filter.js +0 -192
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { getProperty, hasProperty } from "dot-prop";
|
|
2
|
+
import { detective } from "./detective.js";
|
|
3
|
+
import { weedout } from "./weedout.js";
|
|
4
|
+
export class Filter {
|
|
5
|
+
add(model, options) {
|
|
6
|
+
if (model.discriminators) {
|
|
7
|
+
for(const modelName in model.discriminators){
|
|
8
|
+
const excluded = this.excludedMap.get(modelName);
|
|
9
|
+
if (excluded) {
|
|
10
|
+
options.filteredKeys.private = options.filteredKeys.private.concat(excluded.filteredKeys.private);
|
|
11
|
+
options.filteredKeys.protected = options.filteredKeys.protected.concat(excluded.filteredKeys.protected);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
this.excludedMap.set(model.modelName, {
|
|
16
|
+
filteredKeys: options.filteredKeys,
|
|
17
|
+
model
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Gets excluded keys for a given model and access.
|
|
22
|
+
*/ getExcluded(options) {
|
|
23
|
+
if (options.access === "private") {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
const filteredKeys = this.excludedMap.get(options.modelName)?.filteredKeys;
|
|
27
|
+
if (!filteredKeys) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
return options.access === "protected" ? filteredKeys.private : filteredKeys.private.concat(filteredKeys.protected);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Removes excluded keys from a document.
|
|
34
|
+
*/ filterItem(item, excluded) {
|
|
35
|
+
if (!item) {
|
|
36
|
+
return item;
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(item)) {
|
|
39
|
+
return item.map((i)=>this.filterItem(i, excluded));
|
|
40
|
+
}
|
|
41
|
+
if (excluded) {
|
|
42
|
+
if (typeof item.toObject === "function") {
|
|
43
|
+
item = item.toObject();
|
|
44
|
+
}
|
|
45
|
+
for(let i = 0; i < excluded.length; i++){
|
|
46
|
+
weedout(item, excluded[i]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return item;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Removes excluded keys from a document with populated subdocuments.
|
|
53
|
+
*/ filterPopulatedItem(item, options) {
|
|
54
|
+
if (Array.isArray(item)) {
|
|
55
|
+
return item.map((i)=>this.filterPopulatedItem(i, options));
|
|
56
|
+
}
|
|
57
|
+
for(let i = 0; i < options.populate.length; i++){
|
|
58
|
+
if (!options.populate[i].path) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const model = this.excludedMap.get(options.modelName)?.model;
|
|
62
|
+
if (!model) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const excluded = this.getExcluded({
|
|
66
|
+
access: options.access,
|
|
67
|
+
modelName: detective(model, options.populate[i].path)
|
|
68
|
+
});
|
|
69
|
+
if (hasProperty(item, options.populate[i].path)) {
|
|
70
|
+
this.filterItem(getProperty(item, options.populate[i].path), excluded);
|
|
71
|
+
} else {
|
|
72
|
+
const pathToArray = options.populate[i].path.split(".").slice(0, -1).join(".");
|
|
73
|
+
if (hasProperty(item, pathToArray)) {
|
|
74
|
+
const array = getProperty(item, pathToArray);
|
|
75
|
+
const pathToObject = options.populate[i].path.split(".").slice(-1).join(".");
|
|
76
|
+
if (Array.isArray(array)) {
|
|
77
|
+
this.filterItem(// @ts-expect-error this is fine 🐶🔥
|
|
78
|
+
array.map((element)=>getProperty(element, pathToObject)), excluded);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return item;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Removes excluded keys from a document.
|
|
87
|
+
*/ filterObject(resource, options) {
|
|
88
|
+
const excluded = this.getExcluded({
|
|
89
|
+
access: options.access,
|
|
90
|
+
modelName: options.modelName
|
|
91
|
+
});
|
|
92
|
+
const filtered = this.filterItem(resource, excluded);
|
|
93
|
+
if (options?.populate) {
|
|
94
|
+
this.filterPopulatedItem(filtered, {
|
|
95
|
+
access: options.access,
|
|
96
|
+
modelName: options.modelName,
|
|
97
|
+
populate: options.populate
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return filtered;
|
|
101
|
+
}
|
|
102
|
+
constructor(){
|
|
103
|
+
this.excludedMap = new Map();
|
|
104
|
+
}
|
|
105
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/weedout.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function weedout(obj, path) {
|
|
2
|
+
const keys = path.split(".");
|
|
3
|
+
for(let i = 0, length = keys.length; i < length; i++){
|
|
4
|
+
if (Array.isArray(obj)) {
|
|
5
|
+
for(let j = 0; j < obj.length; j++){
|
|
6
|
+
weedout(obj[j], keys.slice(1).join("."));
|
|
7
|
+
}
|
|
8
|
+
} else if (!obj || typeof obj[keys[i]] === "undefined") {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (i < keys.length - 1) {
|
|
12
|
+
// @ts-expect-error this is fine 🐶🔥
|
|
13
|
+
obj = obj[keys[i]];
|
|
14
|
+
} else {
|
|
15
|
+
delete obj[keys[i]];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return obj;
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,44 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
"
|
|
4
|
-
"email": "flo.holzapfel@gmail.com",
|
|
5
|
-
"name": "Florian Holzapfel"
|
|
6
|
-
},
|
|
7
|
-
"dependencies": {
|
|
8
|
-
"ensure-array": "~1.0.0",
|
|
9
|
-
"is-coordinates": "~2.0.0",
|
|
10
|
-
"lodash.defaults": "~4.2.0",
|
|
11
|
-
"lodash.get": "~4.4.2",
|
|
12
|
-
"lodash.has": "~4.5.2",
|
|
13
|
-
"lodash.isplainobject": "~4.0.6",
|
|
14
|
-
"mongoose-detective": "~2.0.0",
|
|
15
|
-
"moredots": "~1.0.1",
|
|
16
|
-
"run-series": "~1.1.8",
|
|
17
|
-
"serialize-error": "^7.0.1",
|
|
18
|
-
"weedout": "~1.0.1"
|
|
19
|
-
},
|
|
20
|
-
"peerDependencies": {
|
|
21
|
-
"mongoose": "6.x"
|
|
22
|
-
},
|
|
2
|
+
"name": "express-restify-mongoose",
|
|
3
|
+
"version": "8.0.0",
|
|
23
4
|
"description": "Easily create a flexible REST interface for mongoose models",
|
|
24
|
-
"devDependencies": {
|
|
25
|
-
"body-parser": "^1.19.0",
|
|
26
|
-
"eslint": "^7.4.0",
|
|
27
|
-
"eslint-config-prettier": "^6.11.0",
|
|
28
|
-
"eslint-plugin-prettier": "^3.1.4",
|
|
29
|
-
"express": "^4.17.1",
|
|
30
|
-
"in-publish": "^2.0.1",
|
|
31
|
-
"method-override": "^3.0.0",
|
|
32
|
-
"mocha": "^10.1.0",
|
|
33
|
-
"prettier": "^2.0.5",
|
|
34
|
-
"request": "^2.88.2",
|
|
35
|
-
"restify": "^8.5.1",
|
|
36
|
-
"sinon": "^9.0.2"
|
|
37
|
-
},
|
|
38
|
-
"engines": {
|
|
39
|
-
"node": ">=14"
|
|
40
|
-
},
|
|
41
|
-
"homepage": "http://florianholzapfel.github.io/express-restify-mongoose/",
|
|
42
5
|
"keywords": [
|
|
43
6
|
"ReST",
|
|
44
7
|
"express",
|
|
@@ -47,34 +10,71 @@
|
|
|
47
10
|
"mongoose",
|
|
48
11
|
"model"
|
|
49
12
|
],
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
13
|
+
"homepage": "http://florianholzapfel.github.io/express-restify-mongoose/",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/florianholzapfel/express-restify-mongoose/issues"
|
|
16
|
+
},
|
|
54
17
|
"repository": {
|
|
55
18
|
"type": "git",
|
|
56
19
|
"url": "git://github.com/florianholzapfel/express-restify-mongoose.git"
|
|
57
20
|
},
|
|
58
|
-
"
|
|
59
|
-
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Florian Holzapfel",
|
|
24
|
+
"email": "flo.holzapfel@gmail.com"
|
|
60
25
|
},
|
|
61
|
-
"
|
|
62
|
-
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"test": "npm run test-unit && npm run test-filter && npm run test-express && npm run test-restify",
|
|
66
|
-
"test-ci": "npm run test-unit && npm run test-filter && npm run test-restify",
|
|
67
|
-
"test-unit": "mocha -R spec ./test/unit.js",
|
|
68
|
-
"test-filter": "mocha -R spec ./test/integration/resource_filter.js --timeout 10s",
|
|
69
|
-
"test-express": "mocha -R spec ./test/express.js --timeout 10s",
|
|
70
|
-
"test-restify": "mocha -R spec ./test/restify.js --timeout 10s"
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
"import": "./dist/express-restify-mongoose.js",
|
|
29
|
+
"require": "./dist/cjs/express-restify-mongoose.js"
|
|
71
30
|
},
|
|
72
|
-
"
|
|
31
|
+
"main": "./dist/cjs/express-restify-mongoose.js",
|
|
73
32
|
"files": [
|
|
74
|
-
"
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
"
|
|
78
|
-
"src
|
|
79
|
-
|
|
33
|
+
"dist/"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"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",
|
|
39
|
+
"build:dts": "tsup src/express-restify-mongoose.ts --dts-only",
|
|
40
|
+
"lint": "eslint . --ext .js,.ts",
|
|
41
|
+
"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",
|
|
46
|
+
"tsc": "tsc --noEmit"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"dot-prop": "^7.2.0",
|
|
50
|
+
"lodash.isplainobject": "~4.0.6",
|
|
51
|
+
"mongoose": "6.x",
|
|
52
|
+
"serialize-error": "^11.0.0",
|
|
53
|
+
"zod": "^3.19.1"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@swc/cli": "^0.1.57",
|
|
57
|
+
"@swc/core": "^1.3.17",
|
|
58
|
+
"@swc/register": "^0.1.10",
|
|
59
|
+
"@types/express": "^4.17.14",
|
|
60
|
+
"@typescript-eslint/eslint-plugin": "^5.40.0",
|
|
61
|
+
"@typescript-eslint/parser": "^5.40.0",
|
|
62
|
+
"body-parser": "^1.19.0",
|
|
63
|
+
"esbuild": "^0.15.12",
|
|
64
|
+
"eslint": "^8.25.0",
|
|
65
|
+
"eslint-plugin-mocha": "^10.1.0",
|
|
66
|
+
"express": "^4.17.1",
|
|
67
|
+
"method-override": "^3.0.0",
|
|
68
|
+
"mocha": "^10.1.0",
|
|
69
|
+
"npm-run-all": "^4.1.5",
|
|
70
|
+
"prettier": "^2.7.1",
|
|
71
|
+
"request": "^2.88.2",
|
|
72
|
+
"restify": "^8.5.1",
|
|
73
|
+
"sinon": "^14.0.1",
|
|
74
|
+
"tsup": "^6.3.0",
|
|
75
|
+
"typescript": "^4.8.4"
|
|
76
|
+
},
|
|
77
|
+
"engines": {
|
|
78
|
+
"node": ">=14"
|
|
79
|
+
}
|
|
80
80
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
Changelog
|
|
2
|
-
=========
|
|
3
|
-
|
|
4
|
-
### 6.0.0
|
|
5
|
-
> **This release requires mongoose ~5.8**
|
|
6
|
-
|
|
7
|
-
* updated mongoose to version 5.8
|
|
8
|
-
|
|
9
|
-
### 5.0.0
|
|
10
|
-
|
|
11
|
-
* dropped support for Node 4 and added support for Node 10
|
|
12
|
-
* removed query operator parsing [#285](https://github.com/florianholzapfel/express-restify-mongoose/issues/285)
|
|
13
|
-
* moved request query in req.erm.query [#299](https://github.com/florianholzapfel/express-restify-mongoose/issues/299) [#353](https://github.com/florianholzapfel/express-restify-mongoose/issues/353)
|
|
14
|
-
* removed `next` from postProcess [#334](https://github.com/florianholzapfel/express-restify-mongoose/issues/334)
|
|
15
|
-
* added error when skip and/or limit is not a valid integer
|
|
16
|
-
* removed `_id` tinkering [#326](https://github.com/florianholzapfel/express-restify-mongoose/issues/326)
|
|
17
|
-
* removed dependency on `async`
|
|
18
|
-
|
|
19
|
-
### 4.3.0
|
|
20
|
-
|
|
21
|
-
* added support for async `outputFn` by returning a Promise
|
|
22
|
-
|
|
23
|
-
### 4.2.2
|
|
24
|
-
|
|
25
|
-
* removed dependency on `lodash`, use specific modules and native methods when possible [#352](https://github.com/florianholzapfel/express-restify-mongoose/pull/352)
|
|
26
|
-
|
|
27
|
-
### 4.2.1
|
|
28
|
-
|
|
29
|
-
* fixed issue [#294](https://github.com/florianholzapfel/express-restify-mongoose/issues/294)
|
|
30
|
-
|
|
31
|
-
### 4.2.0
|
|
32
|
-
|
|
33
|
-
* removed `compile` step, code now runs natively on Node 4+ and `babel` is only used for coverage
|
|
34
|
-
|
|
35
|
-
### 4.1.1
|
|
36
|
-
|
|
37
|
-
* fixed `distinct` queries when `options.totalCountHeader` is enabled
|
|
38
|
-
|
|
39
|
-
### 4.1.0
|
|
40
|
-
|
|
41
|
-
* improved sync error handling in `buildQuery` by wrapping in a promise
|
|
42
|
-
* fixed crash when `distinct` and `sort` operators were used in the same query
|
|
43
|
-
|
|
44
|
-
### 4.0.0
|
|
45
|
-
|
|
46
|
-
* improved default error middleware by serializing error objects and removing stack traces
|
|
47
|
-
* fixed Mongoose async middleware error propagation
|
|
48
|
-
* fixed requests to always set `req.erm.statusCode`
|
|
49
|
-
* removed `statusCode` from error object and response body
|
|
50
|
-
* removed undocumented `outputFn` parameter, use `req.erm.result` and `req.erm.statusCode`
|
|
51
|
-
|
|
52
|
-
### 3.2.0
|
|
53
|
-
|
|
54
|
-
* added an option to disable regex operations ([#195](https://github.com/florianholzapfel/express-restify-mongoose/issues/195))
|
|
55
|
-
* fixed queries with an `idProperty` resulting in a `CastError` to return `404` instead of `400` ([#184](https://github.com/florianholzapfel/express-restify-mongoose/issues/184))
|
|
56
|
-
* fixed query parser to handle geospatial operators ([#187](https://github.com/florianholzapfel/express-restify-mongoose/issues/187))
|
|
57
|
-
|
|
58
|
-
### 3.1.0
|
|
59
|
-
|
|
60
|
-
* critical security fix with the `distinct` operator, see [issue #252](https://github.com/florianholzapfel/express-restify-mongoose/issues/252) for details
|
|
61
|
-
|
|
62
|
-
### 3.0.0
|
|
63
|
-
|
|
64
|
-
* ported source to ES2015, compiled and published as ES5 with Babel
|
|
65
|
-
* document filtering is now done right before output allowing access to the full document in post middleware
|
|
66
|
-
* removed `options.lowercase` and `options.plural`, use `options.name = require('inflection').pluralize('modelName').toLowerCase()`
|
|
67
|
-
|
|
68
|
-
### 2.0.0
|
|
69
|
-
|
|
70
|
-
* changed `serve` to no longer returns an Express 4 router, now returns the resource's base path (ie.: `/api/v1/Customer`)
|
|
71
|
-
* changed `options.private` and `options.protected` to no longer accept comma separated fields, pass an array instead
|
|
72
|
-
* removed `options.excluded`, use `options.private`
|
|
73
|
-
* removed support for querying directly with query parameters, use `url?query={"name":"hello"}`
|
|
74
|
-
* removed $and and $or query parameters, use `url?query={"$or":[...]}`
|
|
75
|
-
* removed `prereq`, use `preMiddleware` instead
|
|
76
|
-
* changed `postCreate`, `postUpdate`, and `postDelete` signatures to `(req, res, next)`
|
|
77
|
-
* deprecated `outputFn`'s `data` parameter, data now available on `req.erm.result` and `req.erm.statusCode`
|
|
78
|
-
|
|
79
|
-
### 1.0.0
|
|
80
|
-
|
|
81
|
-
> **This release requires mongoose ~4**
|
|
82
|
-
|
|
83
|
-
* updated mongoose to version 4
|
|
84
|
-
* removed `fullErrors`, implement a custom `onError` handler instead
|
|
85
|
-
* removed `strict` option, allows DELETE without id and POST with id, disallows PUT without id
|
|
86
|
-
* async `prereq` and `access` now use the standard `(err, data)` callback signature
|
|
87
|
-
* `access` will throw an exception when an unsupported value is passed
|
|
88
|
-
* changed `outputFn`'s signature to: `(req, res, { result: result, statusCode: statusCode })`
|
|
89
|
-
|
|
90
|
-
### 0.7.0
|
|
91
|
-
|
|
92
|
-
> **This release requires mongoose ~3**
|
package/index.d.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
// Type definitions for express-restify-mongoose 3.2.0
|
|
2
|
-
// Project: https://github.com/florianholzapfel/express-restify-mongoose
|
|
3
|
-
// Definitions by: pedroraft <https://github.com/pedroraft> & ravishivt <https://github.com/ravishivt>
|
|
4
|
-
// Definitions: https://github.com/florianholzapfel/express-restify-mongoose
|
|
5
|
-
// TypeScript Version: 2.5.3
|
|
6
|
-
|
|
7
|
-
///<reference types="express" />
|
|
8
|
-
///<reference types="mongoose" />
|
|
9
|
-
|
|
10
|
-
// DOCS: https://florianholzapfel.github.io/express-restify-mongoose/
|
|
11
|
-
declare module 'express-restify-mongoose' {
|
|
12
|
-
import * as express from 'express';
|
|
13
|
-
import * as mongoose from 'mongoose';
|
|
14
|
-
|
|
15
|
-
export interface Options {
|
|
16
|
-
prefix?: string;
|
|
17
|
-
version?: string;
|
|
18
|
-
idProperty?: string;
|
|
19
|
-
restify?: boolean;
|
|
20
|
-
plural?: boolean;
|
|
21
|
-
lowercase?: boolean;
|
|
22
|
-
name?: string;
|
|
23
|
-
private?: string[];
|
|
24
|
-
protected?: string[];
|
|
25
|
-
lean?: boolean;
|
|
26
|
-
findOneAndUpdate?: boolean;
|
|
27
|
-
preMiddleware?: (req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
28
|
-
preCreate?: (req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
29
|
-
preRead?: (req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
30
|
-
preUpdate?: (req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
31
|
-
preDelete?: (req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
32
|
-
access?: (req: express.Request, done?: any) => any;
|
|
33
|
-
contextFilter?: (model: mongoose.Model<any>, req: express.Request, done: any) => any;
|
|
34
|
-
postCreate?: (req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
35
|
-
postRead?: (req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
36
|
-
postUpdate?: (req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
37
|
-
postDelete?: (req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
38
|
-
outputFn?: (req: express.Request, res: express.Response) => any;
|
|
39
|
-
onError?: (err: any, req: express.Request, res: express.Response, next: express.NextFunction) => any;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function serve(router: express.Router, mongooseModel: mongoose.Model<any>, options?: Options): string;
|
|
43
|
-
|
|
44
|
-
export function defaults(options: Options): void;
|
|
45
|
-
}
|
package/src/buildQuery.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = function (options) {
|
|
4
|
-
return function (query, queryOptions) {
|
|
5
|
-
const promise = new Promise((resolve, reject) => {
|
|
6
|
-
if (!queryOptions) {
|
|
7
|
-
return resolve(query)
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (queryOptions.query) {
|
|
11
|
-
query.where(queryOptions.query)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (queryOptions.skip) {
|
|
15
|
-
query.skip(queryOptions.skip)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (options.limit && (!queryOptions.limit || queryOptions.limit === '0' || queryOptions.limit > options.limit)) {
|
|
19
|
-
queryOptions.limit = options.limit
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (queryOptions.limit && query.op !== 'count' && !queryOptions.distinct) {
|
|
23
|
-
query.limit(queryOptions.limit)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (queryOptions.sort) {
|
|
27
|
-
query.sort(queryOptions.sort)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (queryOptions.populate) {
|
|
31
|
-
query.populate(queryOptions.populate)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (queryOptions.select) {
|
|
35
|
-
query.select(queryOptions.select)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (queryOptions.distinct) {
|
|
39
|
-
query.distinct(queryOptions.distinct)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (options.readPreference) {
|
|
43
|
-
query.read(options.readPreference)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (options.lean) {
|
|
47
|
-
query.lean(options.lean)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
resolve(query)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
return promise
|
|
54
|
-
}
|
|
55
|
-
}
|
package/src/errorHandler.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const http = require('http')
|
|
4
|
-
|
|
5
|
-
module.exports = function (options) {
|
|
6
|
-
return function (req, res, next) {
|
|
7
|
-
return function (err) {
|
|
8
|
-
if (err.message === http.STATUS_CODES[404] || (req.params.id && err.path === options.idProperty && err.name === 'CastError')) {
|
|
9
|
-
req.erm.statusCode = 404
|
|
10
|
-
} else {
|
|
11
|
-
req.erm.statusCode = req.erm.statusCode && req.erm.statusCode >= 400 ? req.erm.statusCode : 400
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
options.onError(err, req, res, next)
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const defaults = require('lodash.defaults')
|
|
4
|
-
const ensureArray = require('ensure-array')
|
|
5
|
-
const util = require('util')
|
|
6
|
-
const Filter = require('./resource_filter')
|
|
7
|
-
let customDefaults = null
|
|
8
|
-
const excludedMap = {}
|
|
9
|
-
|
|
10
|
-
function getDefaults() {
|
|
11
|
-
return defaults(Object.assign({}, customDefaults), {
|
|
12
|
-
prefix: '/api',
|
|
13
|
-
version: '/v1',
|
|
14
|
-
idProperty: '_id',
|
|
15
|
-
findOneAndUpdate: true,
|
|
16
|
-
findOneAndRemove: true,
|
|
17
|
-
lean: true,
|
|
18
|
-
restify: false,
|
|
19
|
-
runValidators: false,
|
|
20
|
-
allowRegex: true,
|
|
21
|
-
private: [],
|
|
22
|
-
protected: [],
|
|
23
|
-
upsert: false,
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const restify = function (app, model, opts) {
|
|
28
|
-
const options = Object.assign({}, getDefaults(), opts || {})
|
|
29
|
-
|
|
30
|
-
const access = require('./middleware/access')
|
|
31
|
-
const ensureContentType = require('./middleware/ensureContentType')(options)
|
|
32
|
-
const filterAndFindById = require('./middleware/filterAndFindById')(model, options)
|
|
33
|
-
const onError = require('./middleware/onError')
|
|
34
|
-
const outputFn = require('./middleware/outputFn')
|
|
35
|
-
const prepareQuery = require('./middleware/prepareQuery')(options)
|
|
36
|
-
const prepareOutput = require('./middleware/prepareOutput')(options, excludedMap)
|
|
37
|
-
|
|
38
|
-
if (!Array.isArray(options.private)) {
|
|
39
|
-
throw new Error('"options.private" must be an array of fields')
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (!Array.isArray(options.protected)) {
|
|
43
|
-
throw new Error('"options.protected" must be an array of fields')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
model.schema.eachPath((name, path) => {
|
|
47
|
-
if (path.options.access) {
|
|
48
|
-
switch (path.options.access.toLowerCase()) {
|
|
49
|
-
case 'private':
|
|
50
|
-
options.private.push(name)
|
|
51
|
-
break
|
|
52
|
-
case 'protected':
|
|
53
|
-
options.protected.push(name)
|
|
54
|
-
break
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
options.filter = new Filter({
|
|
60
|
-
model,
|
|
61
|
-
excludedMap,
|
|
62
|
-
filteredKeys: {
|
|
63
|
-
private: options.private,
|
|
64
|
-
protected: options.protected,
|
|
65
|
-
},
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
excludedMap[model.modelName] = options.filter.filteredKeys
|
|
69
|
-
|
|
70
|
-
options.preMiddleware = ensureArray(options.preMiddleware)
|
|
71
|
-
options.preCreate = ensureArray(options.preCreate)
|
|
72
|
-
options.preRead = ensureArray(options.preRead)
|
|
73
|
-
options.preUpdate = ensureArray(options.preUpdate)
|
|
74
|
-
options.preDelete = ensureArray(options.preDelete)
|
|
75
|
-
|
|
76
|
-
if (!options.contextFilter) {
|
|
77
|
-
options.contextFilter = (model, req, done) => done(model)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
options.postCreate = ensureArray(options.postCreate)
|
|
81
|
-
options.postRead = ensureArray(options.postRead)
|
|
82
|
-
options.postUpdate = ensureArray(options.postUpdate)
|
|
83
|
-
options.postDelete = ensureArray(options.postDelete)
|
|
84
|
-
|
|
85
|
-
if (!options.onError) {
|
|
86
|
-
options.onError = onError(!options.restify)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (!options.outputFn) {
|
|
90
|
-
options.outputFn = outputFn(!options.restify)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
options.name = options.name || model.modelName
|
|
94
|
-
|
|
95
|
-
const ops = require('./operations')(model, options, excludedMap)
|
|
96
|
-
|
|
97
|
-
let uriItem = `${options.prefix}${options.version}/${options.name}`
|
|
98
|
-
if (uriItem.indexOf('/:id') === -1) {
|
|
99
|
-
uriItem += '/:id'
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const uriItems = uriItem.replace('/:id', '')
|
|
103
|
-
const uriCount = uriItems + '/count'
|
|
104
|
-
const uriShallow = uriItem + '/shallow'
|
|
105
|
-
|
|
106
|
-
if (typeof app.delete === 'undefined') {
|
|
107
|
-
app.delete = app.del
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
app.use((req, res, next) => {
|
|
111
|
-
const getModel = options.modelFactory && options.modelFactory.getModel
|
|
112
|
-
|
|
113
|
-
req.erm = {
|
|
114
|
-
model: typeof getModel === 'function' ? getModel(req) : model,
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
next()
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
const accessMiddleware = options.access ? access(options) : []
|
|
121
|
-
|
|
122
|
-
app.get(uriItems, prepareQuery, options.preMiddleware, options.preRead, accessMiddleware, ops.getItems, prepareOutput)
|
|
123
|
-
app.get(uriCount, prepareQuery, options.preMiddleware, options.preRead, accessMiddleware, ops.getCount, prepareOutput)
|
|
124
|
-
app.get(uriItem, prepareQuery, options.preMiddleware, options.preRead, accessMiddleware, ops.getItem, prepareOutput)
|
|
125
|
-
app.get(uriShallow, prepareQuery, options.preMiddleware, options.preRead, accessMiddleware, ops.getShallow, prepareOutput)
|
|
126
|
-
|
|
127
|
-
app.post(uriItems, prepareQuery, ensureContentType, options.preMiddleware, options.preCreate, accessMiddleware, ops.createObject, prepareOutput)
|
|
128
|
-
app.post(uriItem, util.deprecate(prepareQuery, 'express-restify-mongoose: in a future major version, the POST method to update resources will be removed. Use PATCH instead.'), ensureContentType, options.preMiddleware, options.findOneAndUpdate ? [] : filterAndFindById, options.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput)
|
|
129
|
-
|
|
130
|
-
app.put(uriItem, 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, options.preMiddleware, options.findOneAndUpdate ? [] : filterAndFindById, options.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput)
|
|
131
|
-
app.patch(uriItem, prepareQuery, ensureContentType, options.preMiddleware, options.findOneAndUpdate ? [] : filterAndFindById, options.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput)
|
|
132
|
-
|
|
133
|
-
app.delete(uriItems, prepareQuery, options.preMiddleware, options.preDelete, ops.deleteItems, prepareOutput)
|
|
134
|
-
app.delete(uriItem, prepareQuery, options.preMiddleware, options.findOneAndRemove ? [] : filterAndFindById, options.preDelete, ops.deleteItem, prepareOutput)
|
|
135
|
-
|
|
136
|
-
return uriItems
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
module.exports = {
|
|
140
|
-
defaults: function (options) {
|
|
141
|
-
customDefaults = options
|
|
142
|
-
},
|
|
143
|
-
serve: restify,
|
|
144
|
-
}
|
package/src/middleware/access.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = function (options) {
|
|
4
|
-
const errorHandler = require('../errorHandler')(options)
|
|
5
|
-
|
|
6
|
-
return function (req, res, next) {
|
|
7
|
-
const handler = function (err, access) {
|
|
8
|
-
if (err) {
|
|
9
|
-
return errorHandler(req, res, next)(err)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (['public', 'private', 'protected'].indexOf(access) < 0) {
|
|
13
|
-
throw new Error('Unsupported access, must be "private", "protected" or "public"')
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
req.access = access
|
|
17
|
-
next()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (options.access.length > 1) {
|
|
21
|
-
options.access(req, handler)
|
|
22
|
-
} else {
|
|
23
|
-
handler(null, options.access(req))
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = function (options) {
|
|
4
|
-
const errorHandler = require('../errorHandler')(options)
|
|
5
|
-
|
|
6
|
-
return function ensureContentType(req, res, next) {
|
|
7
|
-
const ct = req.headers['content-type']
|
|
8
|
-
|
|
9
|
-
if (!ct) {
|
|
10
|
-
return errorHandler(req, res, next)(new Error('missing_content_type'))
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (ct.indexOf('application/json') === -1) {
|
|
14
|
-
return errorHandler(req, res, next)(new Error('invalid_content_type'))
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
next()
|
|
18
|
-
}
|
|
19
|
-
}
|