express-restify-mongoose 6.1.1 → 7.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/CHANGELOG.md +92 -92
- package/LICENSE +19 -19
- package/README.md +59 -52
- package/index.d.ts +45 -0
- package/package.json +80 -86
- package/src/buildQuery.js +55 -55
- package/src/errorHandler.js +17 -17
- package/src/express-restify-mongoose.js +144 -144
- package/src/middleware/access.js +26 -26
- package/src/middleware/ensureContentType.js +19 -19
- package/src/middleware/filterAndFindById.js +35 -35
- package/src/middleware/onError.js +26 -26
- package/src/middleware/outputFn.js +15 -15
- package/src/middleware/prepareOutput.js +75 -75
- package/src/middleware/prepareQuery.js +123 -115
- package/src/operations.js +274 -274
- package/src/resource_filter.js +192 -192
package/src/buildQuery.js
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
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
|
-
}
|
|
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
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
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
|
+
'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 +1,144 @@
|
|
|
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() : 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
|
-
}
|
|
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
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
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
|
+
'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 +1,19 @@
|
|
|
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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const http = require('http')
|
|
4
|
-
|
|
5
|
-
module.exports = function (model, options) {
|
|
6
|
-
const errorHandler = require('../errorHandler')(options)
|
|
7
|
-
|
|
8
|
-
return function (req, res, next) {
|
|
9
|
-
const contextModel = (req.erm && req.erm.model) || model
|
|
10
|
-
|
|
11
|
-
if (!req.params.id) {
|
|
12
|
-
return next()
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
16
|
-
filteredContext
|
|
17
|
-
.findOne()
|
|
18
|
-
.and({
|
|
19
|
-
[options.idProperty]: req.params.id,
|
|
20
|
-
})
|
|
21
|
-
.lean(false)
|
|
22
|
-
.read(options.readPreference || 'p')
|
|
23
|
-
.exec()
|
|
24
|
-
.then((doc) => {
|
|
25
|
-
if (!doc) {
|
|
26
|
-
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
req.erm.document = doc
|
|
30
|
-
|
|
31
|
-
next()
|
|
32
|
-
}, errorHandler(req, res, next))
|
|
33
|
-
})
|
|
34
|
-
}
|
|
35
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const http = require('http')
|
|
4
|
+
|
|
5
|
+
module.exports = function (model, options) {
|
|
6
|
+
const errorHandler = require('../errorHandler')(options)
|
|
7
|
+
|
|
8
|
+
return function (req, res, next) {
|
|
9
|
+
const contextModel = (req.erm && req.erm.model) || model
|
|
10
|
+
|
|
11
|
+
if (!req.params.id) {
|
|
12
|
+
return next()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
16
|
+
filteredContext
|
|
17
|
+
.findOne()
|
|
18
|
+
.and({
|
|
19
|
+
[options.idProperty]: req.params.id,
|
|
20
|
+
})
|
|
21
|
+
.lean(false)
|
|
22
|
+
.read(options.readPreference || 'p')
|
|
23
|
+
.exec()
|
|
24
|
+
.then((doc) => {
|
|
25
|
+
if (!doc) {
|
|
26
|
+
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
req.erm.document = doc
|
|
30
|
+
|
|
31
|
+
next()
|
|
32
|
+
}, errorHandler(req, res, next))
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { serializeError } = require('serialize-error')
|
|
4
|
-
|
|
5
|
-
module.exports = function (isExpress) {
|
|
6
|
-
return function (err, req, res, next) {
|
|
7
|
-
const serializedErr = serializeError(err)
|
|
8
|
-
|
|
9
|
-
delete serializedErr.stack
|
|
10
|
-
|
|
11
|
-
if (serializedErr.errors) {
|
|
12
|
-
for (const key in serializedErr.errors) {
|
|
13
|
-
delete serializedErr.errors[key].reason
|
|
14
|
-
delete serializedErr.errors[key].stack
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
res.setHeader('Content-Type', 'application/json')
|
|
19
|
-
|
|
20
|
-
if (isExpress) {
|
|
21
|
-
res.status(req.erm.statusCode).send(serializedErr)
|
|
22
|
-
} else {
|
|
23
|
-
res.send(req.erm.statusCode, serializedErr)
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { serializeError } = require('serialize-error')
|
|
4
|
+
|
|
5
|
+
module.exports = function (isExpress) {
|
|
6
|
+
return function (err, req, res, next) {
|
|
7
|
+
const serializedErr = serializeError(err)
|
|
8
|
+
|
|
9
|
+
delete serializedErr.stack
|
|
10
|
+
|
|
11
|
+
if (serializedErr.errors) {
|
|
12
|
+
for (const key in serializedErr.errors) {
|
|
13
|
+
delete serializedErr.errors[key].reason
|
|
14
|
+
delete serializedErr.errors[key].stack
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
res.setHeader('Content-Type', 'application/json')
|
|
19
|
+
|
|
20
|
+
if (isExpress) {
|
|
21
|
+
res.status(req.erm.statusCode).send(serializedErr)
|
|
22
|
+
} else {
|
|
23
|
+
res.send(req.erm.statusCode, serializedErr)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|