express-restify-mongoose 8.0.0 β 9.0.2
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 +8 -6
- package/dist/buildQuery.js +18 -1
- package/dist/detective.js +11 -1
- package/dist/errorHandler.js +13 -3
- package/dist/express-restify-mongoose.js +37 -24
- package/dist/getQuerySchema.js +35 -25
- package/dist/middleware/access.js +13 -3
- package/dist/middleware/ensureContentType.js +13 -3
- package/dist/middleware/filterAndFindById.js +15 -5
- package/dist/middleware/onError.js +13 -3
- package/dist/middleware/outputFn.js +11 -1
- package/dist/middleware/prepareOutput.js +13 -3
- package/dist/middleware/prepareQuery.js +15 -5
- package/dist/moredots.js +18 -3
- package/dist/operations.js +38 -19
- package/dist/resource_filter.js +36 -7
- package/dist/types.js +4 -1
- package/dist/weedout.js +11 -1
- package/package.json +14 -20
- package/dist/cjs/buildQuery.js +0 -52
- package/dist/cjs/detective.js +0 -39
- package/dist/cjs/errorHandler.js +0 -20
- package/dist/cjs/express-restify-mongoose.js +0 -104
- package/dist/cjs/getQuerySchema.js +0 -161
- package/dist/cjs/middleware/access.js +0 -32
- package/dist/cjs/middleware/ensureContentType.js +0 -23
- package/dist/cjs/middleware/filterAndFindById.js +0 -32
- package/dist/cjs/middleware/onError.js +0 -29
- package/dist/cjs/middleware/outputFn.js +0 -26
- package/dist/cjs/middleware/prepareOutput.js +0 -89
- package/dist/cjs/middleware/prepareQuery.js +0 -25
- package/dist/cjs/moredots.js +0 -24
- package/dist/cjs/operations.js +0 -259
- package/dist/cjs/resource_filter.js +0 -113
- package/dist/cjs/types.js +0 -4
- package/dist/cjs/weedout.js +0 -27
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "getOutputFnHandler", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>getOutputFnHandler
|
|
8
|
-
});
|
|
9
|
-
function getOutputFnHandler(isExpress) {
|
|
10
|
-
const fn = function outputFn(req, res) {
|
|
11
|
-
if (!req.erm.statusCode) {
|
|
12
|
-
throw new Error("statusCode not set");
|
|
13
|
-
}
|
|
14
|
-
if (isExpress) {
|
|
15
|
-
if (req.erm.result) {
|
|
16
|
-
res.status(req.erm.statusCode).json(req.erm.result);
|
|
17
|
-
} else {
|
|
18
|
-
res.sendStatus(req.erm.statusCode);
|
|
19
|
-
}
|
|
20
|
-
} else {
|
|
21
|
-
// @ts-expect-error restify
|
|
22
|
-
res.send(req.erm.statusCode, req.erm.result);
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
return fn;
|
|
26
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "getPrepareOutputHandler", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>getPrepareOutputHandler
|
|
8
|
-
});
|
|
9
|
-
const _errorHandlerJs = require("../errorHandler.js");
|
|
10
|
-
function isDefined(arg) {
|
|
11
|
-
return typeof arg !== "undefined";
|
|
12
|
-
}
|
|
13
|
-
function getPrepareOutputHandler(options, modelName, filter) {
|
|
14
|
-
const errorHandler = (0, _errorHandlerJs.getErrorHandler)(options);
|
|
15
|
-
const fn = function prepareOutput(req, res, next) {
|
|
16
|
-
const postMiddleware = (()=>{
|
|
17
|
-
switch(req.method.toLowerCase()){
|
|
18
|
-
case "get":
|
|
19
|
-
{
|
|
20
|
-
return Array.isArray(options.postRead) ? options.postRead : [
|
|
21
|
-
options.postRead
|
|
22
|
-
];
|
|
23
|
-
}
|
|
24
|
-
case "post":
|
|
25
|
-
{
|
|
26
|
-
if (req.erm.statusCode === 201) {
|
|
27
|
-
return Array.isArray(options.postCreate) ? options.postCreate : [
|
|
28
|
-
options.postCreate
|
|
29
|
-
];
|
|
30
|
-
}
|
|
31
|
-
return Array.isArray(options.postUpdate) ? options.postUpdate : [
|
|
32
|
-
options.postUpdate
|
|
33
|
-
];
|
|
34
|
-
}
|
|
35
|
-
case "put":
|
|
36
|
-
case "patch":
|
|
37
|
-
{
|
|
38
|
-
return Array.isArray(options.postUpdate) ? options.postUpdate : [
|
|
39
|
-
options.postUpdate
|
|
40
|
-
];
|
|
41
|
-
}
|
|
42
|
-
case "delete":
|
|
43
|
-
{
|
|
44
|
-
return Array.isArray(options.postDelete) ? options.postDelete : [
|
|
45
|
-
options.postDelete
|
|
46
|
-
];
|
|
47
|
-
}
|
|
48
|
-
default:
|
|
49
|
-
{
|
|
50
|
-
return [];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
})().filter(isDefined);
|
|
54
|
-
const callback = ()=>{
|
|
55
|
-
// TODO: this will, but should not, filter /count queries
|
|
56
|
-
if (req.erm.result) {
|
|
57
|
-
req.erm.result = filter.filterObject(req.erm.result, {
|
|
58
|
-
access: req.access,
|
|
59
|
-
modelName,
|
|
60
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
61
|
-
populate: req.erm.query?.populate
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
if (options.totalCountHeader && typeof req.erm.totalCount === "number") {
|
|
65
|
-
res.header(typeof options.totalCountHeader === "string" ? options.totalCountHeader : "X-Total-Count", `${req.erm.totalCount}`);
|
|
66
|
-
}
|
|
67
|
-
const promise = options.outputFn(req, res);
|
|
68
|
-
if (options.postProcess) {
|
|
69
|
-
if (promise && typeof promise.then === "function") {
|
|
70
|
-
promise.then(()=>{
|
|
71
|
-
options.postProcess?.(req, res);
|
|
72
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
73
|
-
} else {
|
|
74
|
-
options.postProcess(req, res);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
if (!postMiddleware || postMiddleware.length === 0) {
|
|
79
|
-
return callback();
|
|
80
|
-
}
|
|
81
|
-
postMiddleware.reduce(async (acc, middleware)=>{
|
|
82
|
-
await acc;
|
|
83
|
-
return new Promise((resolve, reject)=>{
|
|
84
|
-
middleware(req, res, (err)=>err ? reject(err) : resolve(err));
|
|
85
|
-
});
|
|
86
|
-
}, Promise.resolve()).then(callback).catch((err)=>errorHandler(err, req, res, next));
|
|
87
|
-
};
|
|
88
|
-
return fn;
|
|
89
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "getPrepareQueryHandler", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>getPrepareQueryHandler
|
|
8
|
-
});
|
|
9
|
-
const _errorHandlerJs = require("../errorHandler.js");
|
|
10
|
-
const _getQuerySchemaJs = require("../getQuerySchema.js");
|
|
11
|
-
function getPrepareQueryHandler(options) {
|
|
12
|
-
const errorHandler = (0, _errorHandlerJs.getErrorHandler)(options);
|
|
13
|
-
const fn = function prepareQuery(req, res, next) {
|
|
14
|
-
req.erm = req.erm || {};
|
|
15
|
-
try {
|
|
16
|
-
req.erm.query = (0, _getQuerySchemaJs.getQueryOptionsSchema)({
|
|
17
|
-
allowRegex: options.allowRegex
|
|
18
|
-
}).parse(req.query || {});
|
|
19
|
-
next();
|
|
20
|
-
} catch (e) {
|
|
21
|
-
return errorHandler(new Error("invalid_json_query"), req, res, next);
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
return fn;
|
|
25
|
-
}
|
package/dist/cjs/moredots.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "moredots", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>moredots
|
|
8
|
-
});
|
|
9
|
-
const _lodashIsplainobject = /*#__PURE__*/ _interopRequireDefault(require("lodash.isplainobject"));
|
|
10
|
-
function _interopRequireDefault(obj) {
|
|
11
|
-
return obj && obj.__esModule ? obj : {
|
|
12
|
-
default: obj
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
function moredots(src, dst = {}, prefix = "") {
|
|
16
|
-
for (const [key, value] of Object.entries(src)){
|
|
17
|
-
if ((0, _lodashIsplainobject.default)(value)) {
|
|
18
|
-
moredots(value, dst, `${prefix}${key}.`);
|
|
19
|
-
} else {
|
|
20
|
-
dst[`${prefix}${key}`] = value;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return dst;
|
|
24
|
-
}
|
package/dist/cjs/operations.js
DELETED
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "operations", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>operations
|
|
8
|
-
});
|
|
9
|
-
const _http = require("http");
|
|
10
|
-
const _lodashIsplainobject = /*#__PURE__*/ _interopRequireDefault(require("lodash.isplainobject"));
|
|
11
|
-
const _buildQueryJs = require("./buildQuery.js");
|
|
12
|
-
const _errorHandlerJs = require("./errorHandler.js");
|
|
13
|
-
const _moredotsJs = require("./moredots.js");
|
|
14
|
-
function _interopRequireDefault(obj) {
|
|
15
|
-
return obj && obj.__esModule ? obj : {
|
|
16
|
-
default: obj
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
function operations(model, options, filter) {
|
|
20
|
-
const buildQuery = (0, _buildQueryJs.getBuildQuery)(options);
|
|
21
|
-
const errorHandler = (0, _errorHandlerJs.getErrorHandler)(options);
|
|
22
|
-
function findById(filteredContext, id) {
|
|
23
|
-
return filteredContext.findOne().and([
|
|
24
|
-
{
|
|
25
|
-
[options.idProperty]: id
|
|
26
|
-
}
|
|
27
|
-
]);
|
|
28
|
-
}
|
|
29
|
-
function isDistinctExcluded(req) {
|
|
30
|
-
if (!req.erm.query?.distinct) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
return filter.getExcluded({
|
|
34
|
-
access: req.access,
|
|
35
|
-
modelName: model.modelName
|
|
36
|
-
}).includes(req.erm.query.distinct);
|
|
37
|
-
}
|
|
38
|
-
const getItems = function(req, res, next) {
|
|
39
|
-
const contextModel = model;
|
|
40
|
-
if (isDistinctExcluded(req)) {
|
|
41
|
-
req.erm.result = [];
|
|
42
|
-
req.erm.statusCode = 200;
|
|
43
|
-
return next();
|
|
44
|
-
}
|
|
45
|
-
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
46
|
-
buildQuery(// @ts-expect-error this is fine πΆπ₯
|
|
47
|
-
filteredContext.find(), req.erm.query).then((items)=>{
|
|
48
|
-
req.erm.result = items;
|
|
49
|
-
req.erm.statusCode = 200;
|
|
50
|
-
if (options.totalCountHeader && !req.erm.query?.distinct) {
|
|
51
|
-
options.contextFilter(contextModel, req, (countFilteredContext)=>{
|
|
52
|
-
buildQuery(countFilteredContext.countDocuments(), {
|
|
53
|
-
...req.erm.query,
|
|
54
|
-
skip: 0,
|
|
55
|
-
limit: 0
|
|
56
|
-
}).then((count)=>{
|
|
57
|
-
req.erm.totalCount = count;
|
|
58
|
-
next();
|
|
59
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
60
|
-
});
|
|
61
|
-
} else {
|
|
62
|
-
next();
|
|
63
|
-
}
|
|
64
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
const getCount = function(req, res, next) {
|
|
68
|
-
const contextModel = model;
|
|
69
|
-
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
70
|
-
buildQuery(filteredContext.countDocuments(), req.erm.query).then((count)=>{
|
|
71
|
-
req.erm.result = {
|
|
72
|
-
count: count
|
|
73
|
-
};
|
|
74
|
-
req.erm.statusCode = 200;
|
|
75
|
-
next();
|
|
76
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
77
|
-
});
|
|
78
|
-
};
|
|
79
|
-
const getShallow = function(req, res, next) {
|
|
80
|
-
const contextModel = model;
|
|
81
|
-
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
82
|
-
buildQuery(// @ts-expect-error this is fine πΆπ₯
|
|
83
|
-
findById(filteredContext, req.params.id), req.erm.query).then((item)=>{
|
|
84
|
-
if (!item) {
|
|
85
|
-
return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
|
|
86
|
-
}
|
|
87
|
-
for(const prop in item){
|
|
88
|
-
item[prop] = typeof item[prop] === "object" && prop !== "_id" ? true : item[prop];
|
|
89
|
-
}
|
|
90
|
-
req.erm.result = item;
|
|
91
|
-
req.erm.statusCode = 200;
|
|
92
|
-
next();
|
|
93
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
94
|
-
});
|
|
95
|
-
};
|
|
96
|
-
const deleteItems = function(req, res, next) {
|
|
97
|
-
const contextModel = model;
|
|
98
|
-
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
99
|
-
buildQuery(filteredContext.deleteMany(), req.erm.query).then(()=>{
|
|
100
|
-
req.erm.statusCode = 204;
|
|
101
|
-
next();
|
|
102
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
103
|
-
});
|
|
104
|
-
};
|
|
105
|
-
const getItem = function(req, res, next) {
|
|
106
|
-
const contextModel = model;
|
|
107
|
-
if (isDistinctExcluded(req)) {
|
|
108
|
-
req.erm.result = [];
|
|
109
|
-
req.erm.statusCode = 200;
|
|
110
|
-
return next();
|
|
111
|
-
}
|
|
112
|
-
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
113
|
-
buildQuery(// @ts-expect-error this is fine πΆπ₯
|
|
114
|
-
findById(filteredContext, req.params.id), req.erm.query).then((item)=>{
|
|
115
|
-
if (!item) {
|
|
116
|
-
return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
|
|
117
|
-
}
|
|
118
|
-
req.erm.result = item;
|
|
119
|
-
req.erm.statusCode = 200;
|
|
120
|
-
next();
|
|
121
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
122
|
-
});
|
|
123
|
-
};
|
|
124
|
-
const deleteItem = function(req, res, next) {
|
|
125
|
-
const contextModel = model;
|
|
126
|
-
if (options.findOneAndRemove) {
|
|
127
|
-
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
128
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
129
|
-
findById(filteredContext, req.params.id).findOneAndRemove().then((item)=>{
|
|
130
|
-
if (!item) {
|
|
131
|
-
return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
|
|
132
|
-
}
|
|
133
|
-
req.erm.statusCode = 204;
|
|
134
|
-
next();
|
|
135
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
136
|
-
});
|
|
137
|
-
} else {
|
|
138
|
-
req.erm.document?.remove().then(()=>{
|
|
139
|
-
req.erm.statusCode = 204;
|
|
140
|
-
next();
|
|
141
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
const createObject = function(req, res, next) {
|
|
145
|
-
const contextModel = model;
|
|
146
|
-
req.body = filter.filterObject(req.body || {}, {
|
|
147
|
-
access: req.access,
|
|
148
|
-
modelName: model.modelName,
|
|
149
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
150
|
-
populate: req.erm.query?.populate
|
|
151
|
-
});
|
|
152
|
-
if (req.body._id === null) {
|
|
153
|
-
delete req.body._id;
|
|
154
|
-
}
|
|
155
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
156
|
-
if (contextModel.schema.options.versionKey) {
|
|
157
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
158
|
-
delete req.body[contextModel.schema.options.versionKey];
|
|
159
|
-
}
|
|
160
|
-
contextModel.create(req.body).then((item)=>{
|
|
161
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
162
|
-
return contextModel.populate(item, req.erm.query?.populate || []);
|
|
163
|
-
}).then((item)=>{
|
|
164
|
-
req.erm.result = item;
|
|
165
|
-
req.erm.statusCode = 201;
|
|
166
|
-
next();
|
|
167
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
168
|
-
};
|
|
169
|
-
const modifyObject = function(req, res, next) {
|
|
170
|
-
const contextModel = model;
|
|
171
|
-
req.body = filter.filterObject(req.body || {}, {
|
|
172
|
-
access: req.access,
|
|
173
|
-
modelName: model.modelName,
|
|
174
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
175
|
-
populate: req.erm.query?.populate
|
|
176
|
-
});
|
|
177
|
-
delete req.body._id;
|
|
178
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
179
|
-
if (contextModel.schema.options.versionKey) {
|
|
180
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
181
|
-
delete req.body[contextModel.schema.options.versionKey];
|
|
182
|
-
}
|
|
183
|
-
function depopulate(src) {
|
|
184
|
-
const dst = {};
|
|
185
|
-
for (const [key, value] of Object.entries(src)){
|
|
186
|
-
const path = contextModel.schema.path(key);
|
|
187
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
188
|
-
if (path && path.caster && path.caster.instance === "ObjectID") {
|
|
189
|
-
if (Array.isArray(value)) {
|
|
190
|
-
for(let j = 0; j < value.length; ++j){
|
|
191
|
-
if (typeof value[j] === "object") {
|
|
192
|
-
dst[key] = dst[key] || [];
|
|
193
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
194
|
-
dst[key].push(value[j]._id);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
} else if ((0, _lodashIsplainobject.default)(value)) {
|
|
198
|
-
dst[key] = value._id;
|
|
199
|
-
}
|
|
200
|
-
} else if ((0, _lodashIsplainobject.default)(value)) {
|
|
201
|
-
if (path && path.instance === "ObjectID") {
|
|
202
|
-
dst[key] = value._id;
|
|
203
|
-
} else {
|
|
204
|
-
dst[key] = depopulate(value);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (typeof dst[key] === "undefined") {
|
|
208
|
-
dst[key] = value;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return dst;
|
|
212
|
-
}
|
|
213
|
-
const cleanBody = (0, _moredotsJs.moredots)(depopulate(req.body));
|
|
214
|
-
if (options.findOneAndUpdate) {
|
|
215
|
-
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
216
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
217
|
-
findById(filteredContext, req.params.id).findOneAndUpdate({}, {
|
|
218
|
-
$set: cleanBody
|
|
219
|
-
}, {
|
|
220
|
-
new: true,
|
|
221
|
-
upsert: options.upsert,
|
|
222
|
-
runValidators: options.runValidators
|
|
223
|
-
}).exec().then((item)=>{
|
|
224
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
225
|
-
return contextModel.populate(item, req.erm.query?.populate || []);
|
|
226
|
-
}).then((item)=>{
|
|
227
|
-
if (!item) {
|
|
228
|
-
return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
|
|
229
|
-
}
|
|
230
|
-
req.erm.result = item;
|
|
231
|
-
req.erm.statusCode = 200;
|
|
232
|
-
next();
|
|
233
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
234
|
-
});
|
|
235
|
-
} else {
|
|
236
|
-
for (const [key, value] of Object.entries(cleanBody)){
|
|
237
|
-
req.erm.document?.set(key, value);
|
|
238
|
-
}
|
|
239
|
-
req.erm.document?.save().then((item)=>{
|
|
240
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
241
|
-
return contextModel.populate(item, req.erm.query?.populate || []);
|
|
242
|
-
}).then((item)=>{
|
|
243
|
-
req.erm.result = item;
|
|
244
|
-
req.erm.statusCode = 200;
|
|
245
|
-
next();
|
|
246
|
-
}).catch((err)=>errorHandler(err, req, res, next));
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
return {
|
|
250
|
-
getItems,
|
|
251
|
-
getCount,
|
|
252
|
-
getItem,
|
|
253
|
-
getShallow,
|
|
254
|
-
createObject,
|
|
255
|
-
modifyObject,
|
|
256
|
-
deleteItems,
|
|
257
|
-
deleteItem
|
|
258
|
-
};
|
|
259
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "Filter", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>Filter
|
|
8
|
-
});
|
|
9
|
-
const _dotProp = require("dot-prop");
|
|
10
|
-
const _detectiveJs = require("./detective.js");
|
|
11
|
-
const _weedoutJs = require("./weedout.js");
|
|
12
|
-
class Filter {
|
|
13
|
-
add(model, options) {
|
|
14
|
-
if (model.discriminators) {
|
|
15
|
-
for(const modelName in model.discriminators){
|
|
16
|
-
const excluded = this.excludedMap.get(modelName);
|
|
17
|
-
if (excluded) {
|
|
18
|
-
options.filteredKeys.private = options.filteredKeys.private.concat(excluded.filteredKeys.private);
|
|
19
|
-
options.filteredKeys.protected = options.filteredKeys.protected.concat(excluded.filteredKeys.protected);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
this.excludedMap.set(model.modelName, {
|
|
24
|
-
filteredKeys: options.filteredKeys,
|
|
25
|
-
model
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Gets excluded keys for a given model and access.
|
|
30
|
-
*/ getExcluded(options) {
|
|
31
|
-
if (options.access === "private") {
|
|
32
|
-
return [];
|
|
33
|
-
}
|
|
34
|
-
const filteredKeys = this.excludedMap.get(options.modelName)?.filteredKeys;
|
|
35
|
-
if (!filteredKeys) {
|
|
36
|
-
return [];
|
|
37
|
-
}
|
|
38
|
-
return options.access === "protected" ? filteredKeys.private : filteredKeys.private.concat(filteredKeys.protected);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Removes excluded keys from a document.
|
|
42
|
-
*/ filterItem(item, excluded) {
|
|
43
|
-
if (!item) {
|
|
44
|
-
return item;
|
|
45
|
-
}
|
|
46
|
-
if (Array.isArray(item)) {
|
|
47
|
-
return item.map((i)=>this.filterItem(i, excluded));
|
|
48
|
-
}
|
|
49
|
-
if (excluded) {
|
|
50
|
-
if (typeof item.toObject === "function") {
|
|
51
|
-
item = item.toObject();
|
|
52
|
-
}
|
|
53
|
-
for(let i = 0; i < excluded.length; i++){
|
|
54
|
-
(0, _weedoutJs.weedout)(item, excluded[i]);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return item;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Removes excluded keys from a document with populated subdocuments.
|
|
61
|
-
*/ filterPopulatedItem(item, options) {
|
|
62
|
-
if (Array.isArray(item)) {
|
|
63
|
-
return item.map((i)=>this.filterPopulatedItem(i, options));
|
|
64
|
-
}
|
|
65
|
-
for(let i = 0; i < options.populate.length; i++){
|
|
66
|
-
if (!options.populate[i].path) {
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
const model = this.excludedMap.get(options.modelName)?.model;
|
|
70
|
-
if (!model) {
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
const excluded = this.getExcluded({
|
|
74
|
-
access: options.access,
|
|
75
|
-
modelName: (0, _detectiveJs.detective)(model, options.populate[i].path)
|
|
76
|
-
});
|
|
77
|
-
if ((0, _dotProp.hasProperty)(item, options.populate[i].path)) {
|
|
78
|
-
this.filterItem((0, _dotProp.getProperty)(item, options.populate[i].path), excluded);
|
|
79
|
-
} else {
|
|
80
|
-
const pathToArray = options.populate[i].path.split(".").slice(0, -1).join(".");
|
|
81
|
-
if ((0, _dotProp.hasProperty)(item, pathToArray)) {
|
|
82
|
-
const array = (0, _dotProp.getProperty)(item, pathToArray);
|
|
83
|
-
const pathToObject = options.populate[i].path.split(".").slice(-1).join(".");
|
|
84
|
-
if (Array.isArray(array)) {
|
|
85
|
-
this.filterItem(// @ts-expect-error this is fine πΆπ₯
|
|
86
|
-
array.map((element)=>(0, _dotProp.getProperty)(element, pathToObject)), excluded);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return item;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Removes excluded keys from a document.
|
|
95
|
-
*/ filterObject(resource, options) {
|
|
96
|
-
const excluded = this.getExcluded({
|
|
97
|
-
access: options.access,
|
|
98
|
-
modelName: options.modelName
|
|
99
|
-
});
|
|
100
|
-
const filtered = this.filterItem(resource, excluded);
|
|
101
|
-
if (options?.populate) {
|
|
102
|
-
this.filterPopulatedItem(filtered, {
|
|
103
|
-
access: options.access,
|
|
104
|
-
modelName: options.modelName,
|
|
105
|
-
populate: options.populate
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
return filtered;
|
|
109
|
-
}
|
|
110
|
-
constructor(){
|
|
111
|
-
this.excludedMap = new Map();
|
|
112
|
-
}
|
|
113
|
-
}
|
package/dist/cjs/types.js
DELETED
package/dist/cjs/weedout.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "weedout", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>weedout
|
|
8
|
-
});
|
|
9
|
-
function weedout(obj, path) {
|
|
10
|
-
const keys = path.split(".");
|
|
11
|
-
for(let i = 0, length = keys.length; i < length; i++){
|
|
12
|
-
if (Array.isArray(obj)) {
|
|
13
|
-
for(let j = 0; j < obj.length; j++){
|
|
14
|
-
weedout(obj[j], keys.slice(1).join("."));
|
|
15
|
-
}
|
|
16
|
-
} else if (!obj || typeof obj[keys[i]] === "undefined") {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
if (i < keys.length - 1) {
|
|
20
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
21
|
-
obj = obj[keys[i]];
|
|
22
|
-
} else {
|
|
23
|
-
delete obj[keys[i]];
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return obj;
|
|
27
|
-
}
|