express-restify-mongoose 6.1.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/LICENSE +19 -19
- package/README.md +57 -52
- 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 +80 -85
- package/CHANGELOG.md +0 -92
- 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 -115
- package/src/operations.js +0 -274
- package/src/resource_filter.js +0 -192
package/LICENSE
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
Copyright (C) 2013 by Florian Holzapfel
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
in the Software without restriction, including without limitation the rights
|
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
furnished to do so, subject to the following conditions:
|
|
9
|
-
|
|
10
|
-
The above copyright notice and this permission notice shall be included in
|
|
11
|
-
all copies or substantial portions of the Software.
|
|
12
|
-
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
THE SOFTWARE.
|
|
1
|
+
Copyright (C) 2013 by Florian Holzapfel
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,52 +1,57 @@
|
|
|
1
|
-
# express-restify-mongoose
|
|
2
|
-
|
|
3
|
-
Easily create a flexible REST interface for mongoose models.
|
|
4
|
-
|
|
5
|
-
[](https://github.com/florianholzapfel/express-restify-mongoose/actions/workflows/node.js.yml)
|
|
6
|
+
[](https://badge.fury.io/js/express-restify-mongoose)
|
|
7
|
+
|
|
8
|
+
## Getting started
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install express-restify-mongoose --save
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Documentation
|
|
15
|
+
|
|
16
|
+
[https://florianholzapfel.github.io/express-restify-mongoose/](https://florianholzapfel.github.io/express-restify-mongoose/)
|
|
17
|
+
|
|
18
|
+
## Compatibility
|
|
19
|
+
|
|
20
|
+
| This library | Mongoose |
|
|
21
|
+
| ------------ | -------- |
|
|
22
|
+
| >= 7.0.0 | >= 6.x |
|
|
23
|
+
| >= 6.0.0 | >= 5.8.0 |
|
|
24
|
+
| >= 1.0.0 | 4.x |
|
|
25
|
+
| 0.7.5 | 3.x |
|
|
26
|
+
|
|
27
|
+
## Contributing
|
|
28
|
+
|
|
29
|
+
Found a bug or have a suggestion to make? Have a took at [issues or open a new one](https://github.com/florianholzapfel/express-restify-mongoose/issues).
|
|
30
|
+
|
|
31
|
+
Everyone is welcome to contribute code by [creating a pull request](https://github.com/florianholzapfel/express-restify-mongoose/pulls), just make sure to follow [standard style](https://github.com/feross/standard).
|
|
32
|
+
|
|
33
|
+
Many thanks to all [contributors](https://github.com/florianholzapfel/express-restify-mongoose/graphs/contributors)!
|
|
34
|
+
|
|
35
|
+
## License (MIT)
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
Copyright (C) 2013 by Florian Holzapfel
|
|
39
|
+
|
|
40
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
41
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
42
|
+
in the Software without restriction, including without limitation the rights
|
|
43
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
44
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
45
|
+
furnished to do so, subject to the following conditions:
|
|
46
|
+
|
|
47
|
+
The above copyright notice and this permission notice shall be included in
|
|
48
|
+
all copies or substantial portions of the Software.
|
|
49
|
+
|
|
50
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
51
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
52
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
53
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
54
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
55
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
56
|
+
THE SOFTWARE.
|
|
57
|
+
```
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export function getBuildQuery(options) {
|
|
2
|
+
return function buildQuery(query, queryOptions) {
|
|
3
|
+
const promise = new Promise((resolve)=>{
|
|
4
|
+
if (!queryOptions) {
|
|
5
|
+
return resolve(query);
|
|
6
|
+
}
|
|
7
|
+
if (queryOptions.query) {
|
|
8
|
+
query.where(queryOptions.query);
|
|
9
|
+
}
|
|
10
|
+
if (queryOptions.skip) {
|
|
11
|
+
query.skip(queryOptions.skip);
|
|
12
|
+
}
|
|
13
|
+
if (options.limit && (!queryOptions.limit || queryOptions.limit > options.limit)) {
|
|
14
|
+
queryOptions.limit = options.limit;
|
|
15
|
+
}
|
|
16
|
+
if (queryOptions.limit && // @ts-expect-error this is fine πΆπ₯
|
|
17
|
+
query.op !== "countDocuments" && !queryOptions.distinct) {
|
|
18
|
+
query.limit(queryOptions.limit);
|
|
19
|
+
}
|
|
20
|
+
if (queryOptions.sort) {
|
|
21
|
+
// @ts-expect-error this is fine πΆπ₯
|
|
22
|
+
query.sort(queryOptions.sort);
|
|
23
|
+
}
|
|
24
|
+
if (queryOptions.populate) {
|
|
25
|
+
// @ts-expect-error this is fine πΆπ₯
|
|
26
|
+
query.populate(queryOptions.populate);
|
|
27
|
+
}
|
|
28
|
+
if (queryOptions.select) {
|
|
29
|
+
query.select(queryOptions.select);
|
|
30
|
+
}
|
|
31
|
+
if (queryOptions.distinct) {
|
|
32
|
+
query.distinct(queryOptions.distinct);
|
|
33
|
+
}
|
|
34
|
+
if (options.readPreference) {
|
|
35
|
+
query.read(options.readPreference);
|
|
36
|
+
}
|
|
37
|
+
if (options.lean) {
|
|
38
|
+
query.lean(options.lean);
|
|
39
|
+
}
|
|
40
|
+
resolve(query);
|
|
41
|
+
});
|
|
42
|
+
return promise;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
}
|