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
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = function (isExpress) {
|
|
4
|
-
return function output(req, res) {
|
|
5
|
-
if (isExpress) {
|
|
6
|
-
if (req.erm.result) {
|
|
7
|
-
res.status(req.erm.statusCode).json(req.erm.result)
|
|
8
|
-
} else {
|
|
9
|
-
res.sendStatus(req.erm.statusCode)
|
|
10
|
-
}
|
|
11
|
-
} else {
|
|
12
|
-
res.send(req.erm.statusCode, req.erm.result)
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
module.exports = function (isExpress) {
|
|
4
|
+
return function output(req, res) {
|
|
5
|
+
if (isExpress) {
|
|
6
|
+
if (req.erm.result) {
|
|
7
|
+
res.status(req.erm.statusCode).json(req.erm.result)
|
|
8
|
+
} else {
|
|
9
|
+
res.sendStatus(req.erm.statusCode)
|
|
10
|
+
}
|
|
11
|
+
} else {
|
|
12
|
+
res.send(req.erm.statusCode, req.erm.result)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const runSeries = require('run-series')
|
|
4
|
-
|
|
5
|
-
module.exports = function (options, excludedMap) {
|
|
6
|
-
const errorHandler = require('../errorHandler')(options)
|
|
7
|
-
|
|
8
|
-
return function (req, res, next) {
|
|
9
|
-
const postMiddleware = (() => {
|
|
10
|
-
switch (req.method.toLowerCase()) {
|
|
11
|
-
case 'get':
|
|
12
|
-
return options.postRead
|
|
13
|
-
case 'post':
|
|
14
|
-
if (req.erm.statusCode === 201) {
|
|
15
|
-
return options.postCreate
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return options.postUpdate
|
|
19
|
-
case 'put':
|
|
20
|
-
case 'patch':
|
|
21
|
-
return options.postUpdate
|
|
22
|
-
case 'delete':
|
|
23
|
-
return options.postDelete
|
|
24
|
-
}
|
|
25
|
-
})()
|
|
26
|
-
|
|
27
|
-
const callback = (err) => {
|
|
28
|
-
if (err) {
|
|
29
|
-
return errorHandler(req, res, next)(err)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// TODO: this will, but should not, filter /count queries
|
|
33
|
-
if (req.erm.result) {
|
|
34
|
-
const opts = {
|
|
35
|
-
access: req.access,
|
|
36
|
-
excludedMap: excludedMap,
|
|
37
|
-
populate: req.erm && req.erm.query ? req.erm.query.populate : null,
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
req.erm.result = options.filter ? options.filter.filterObject(req.erm.result, opts) : req.erm.result
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (options.totalCountHeader && req.erm.totalCount
|
|
44
|
-
res.header(typeof options.totalCountHeader === 'string' ? options.totalCountHeader : 'X-Total-Count', req.erm.totalCount)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const promise = options.outputFn(req, res)
|
|
48
|
-
|
|
49
|
-
if (options.postProcess) {
|
|
50
|
-
if (promise && typeof promise.then === 'function') {
|
|
51
|
-
promise
|
|
52
|
-
.then(() => {
|
|
53
|
-
options.postProcess(req, res)
|
|
54
|
-
})
|
|
55
|
-
.catch(errorHandler(req, res, next))
|
|
56
|
-
} else {
|
|
57
|
-
options.postProcess(req, res)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (!postMiddleware || postMiddleware.length === 0) {
|
|
63
|
-
return callback()
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
runSeries(
|
|
67
|
-
postMiddleware.map((middleware, i) => {
|
|
68
|
-
return (cb) => {
|
|
69
|
-
middleware(req, res, cb)
|
|
70
|
-
}
|
|
71
|
-
}),
|
|
72
|
-
callback
|
|
73
|
-
)
|
|
74
|
-
}
|
|
75
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const runSeries = require('run-series')
|
|
4
|
+
|
|
5
|
+
module.exports = function (options, excludedMap) {
|
|
6
|
+
const errorHandler = require('../errorHandler')(options)
|
|
7
|
+
|
|
8
|
+
return function (req, res, next) {
|
|
9
|
+
const postMiddleware = (() => {
|
|
10
|
+
switch (req.method.toLowerCase()) {
|
|
11
|
+
case 'get':
|
|
12
|
+
return options.postRead
|
|
13
|
+
case 'post':
|
|
14
|
+
if (req.erm.statusCode === 201) {
|
|
15
|
+
return options.postCreate
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return options.postUpdate
|
|
19
|
+
case 'put':
|
|
20
|
+
case 'patch':
|
|
21
|
+
return options.postUpdate
|
|
22
|
+
case 'delete':
|
|
23
|
+
return options.postDelete
|
|
24
|
+
}
|
|
25
|
+
})()
|
|
26
|
+
|
|
27
|
+
const callback = (err) => {
|
|
28
|
+
if (err) {
|
|
29
|
+
return errorHandler(req, res, next)(err)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// TODO: this will, but should not, filter /count queries
|
|
33
|
+
if (req.erm.result) {
|
|
34
|
+
const opts = {
|
|
35
|
+
access: req.access,
|
|
36
|
+
excludedMap: excludedMap,
|
|
37
|
+
populate: req.erm && req.erm.query ? req.erm.query.populate : null,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
req.erm.result = options.filter ? options.filter.filterObject(req.erm.result, opts) : req.erm.result
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (options.totalCountHeader && typeof req.erm.totalCount === 'number') {
|
|
44
|
+
res.header(typeof options.totalCountHeader === 'string' ? options.totalCountHeader : 'X-Total-Count', req.erm.totalCount)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const promise = options.outputFn(req, res)
|
|
48
|
+
|
|
49
|
+
if (options.postProcess) {
|
|
50
|
+
if (promise && typeof promise.then === 'function') {
|
|
51
|
+
promise
|
|
52
|
+
.then(() => {
|
|
53
|
+
options.postProcess(req, res)
|
|
54
|
+
})
|
|
55
|
+
.catch(errorHandler(req, res, next))
|
|
56
|
+
} else {
|
|
57
|
+
options.postProcess(req, res)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!postMiddleware || postMiddleware.length === 0) {
|
|
63
|
+
return callback()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
runSeries(
|
|
67
|
+
postMiddleware.map((middleware, i) => {
|
|
68
|
+
return (cb) => {
|
|
69
|
+
middleware(req, res, cb)
|
|
70
|
+
}
|
|
71
|
+
}),
|
|
72
|
+
callback
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -1,115 +1,123 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const isCoordinates = require('is-coordinates')
|
|
4
|
-
|
|
5
|
-
module.exports = function (options) {
|
|
6
|
-
const errorHandler = require('../errorHandler')(options)
|
|
7
|
-
|
|
8
|
-
function jsonQueryParser(key, value) {
|
|
9
|
-
if (key === '$regex' && !options.allowRegex) {
|
|
10
|
-
return undefined
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (Array.isArray(value) && key[0] !== '$' && key !== 'coordinates' && !isCoordinates(value)) {
|
|
14
|
-
return { $in: value }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return value
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function parseQueryOptions(queryOptions) {
|
|
21
|
-
if (queryOptions.select && typeof queryOptions.select === 'string') {
|
|
22
|
-
const select = queryOptions.select.split(',')
|
|
23
|
-
queryOptions.select = {}
|
|
24
|
-
|
|
25
|
-
for (let i = 0, length = select.length; i < length; i++) {
|
|
26
|
-
if (select[i][0] === '-') {
|
|
27
|
-
queryOptions.select[select[i].substring(1)] = 0
|
|
28
|
-
} else {
|
|
29
|
-
queryOptions.select[select[i]] = 1
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (queryOptions.populate) {
|
|
35
|
-
if (typeof queryOptions.populate === 'string') {
|
|
36
|
-
const populate = queryOptions.populate.split(',')
|
|
37
|
-
queryOptions.populate = []
|
|
38
|
-
|
|
39
|
-
for (let i = 0, length = populate.length; i < length; i++) {
|
|
40
|
-
queryOptions.populate.push({
|
|
41
|
-
path: populate[i],
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
for (const key in queryOptions.select) {
|
|
45
|
-
if (key.indexOf(populate[i] + '.') === 0) {
|
|
46
|
-
if (queryOptions.populate[i].select) {
|
|
47
|
-
queryOptions.populate[i].select += ' '
|
|
48
|
-
} else {
|
|
49
|
-
queryOptions.populate[i].select = ''
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (queryOptions.select[key] === 0) {
|
|
53
|
-
queryOptions.populate[i].select += '-'
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
queryOptions.populate[i].select += key.substring(populate[i].length + 1)
|
|
57
|
-
delete queryOptions.select[key]
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// If other specific fields are selected, add the populated field
|
|
62
|
-
if (queryOptions.select) {
|
|
63
|
-
if (Object.keys(queryOptions.select).length > 0 && !queryOptions.select[populate[i]]) {
|
|
64
|
-
queryOptions.select[populate[i]] = 1
|
|
65
|
-
} else if (Object.keys(queryOptions.select).length === 0) {
|
|
66
|
-
delete queryOptions.select
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
} else if (!Array.isArray(queryOptions.populate)) {
|
|
71
|
-
queryOptions.populate = [queryOptions.populate]
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
query[key] = req.query[key]
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const isCoordinates = require('is-coordinates')
|
|
4
|
+
|
|
5
|
+
module.exports = function (options) {
|
|
6
|
+
const errorHandler = require('../errorHandler')(options)
|
|
7
|
+
|
|
8
|
+
function jsonQueryParser(key, value) {
|
|
9
|
+
if (key === '$regex' && !options.allowRegex) {
|
|
10
|
+
return undefined
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (Array.isArray(value) && key[0] !== '$' && key !== 'coordinates' && !isCoordinates(value)) {
|
|
14
|
+
return { $in: value }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return value
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function parseQueryOptions(queryOptions) {
|
|
21
|
+
if (queryOptions.select && typeof queryOptions.select === 'string') {
|
|
22
|
+
const select = queryOptions.select.split(',')
|
|
23
|
+
queryOptions.select = {}
|
|
24
|
+
|
|
25
|
+
for (let i = 0, length = select.length; i < length; i++) {
|
|
26
|
+
if (select[i][0] === '-') {
|
|
27
|
+
queryOptions.select[select[i].substring(1)] = 0
|
|
28
|
+
} else {
|
|
29
|
+
queryOptions.select[select[i]] = 1
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (queryOptions.populate) {
|
|
35
|
+
if (typeof queryOptions.populate === 'string') {
|
|
36
|
+
const populate = queryOptions.populate.split(',')
|
|
37
|
+
queryOptions.populate = []
|
|
38
|
+
|
|
39
|
+
for (let i = 0, length = populate.length; i < length; i++) {
|
|
40
|
+
queryOptions.populate.push({
|
|
41
|
+
path: populate[i],
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
for (const key in queryOptions.select) {
|
|
45
|
+
if (key.indexOf(populate[i] + '.') === 0) {
|
|
46
|
+
if (queryOptions.populate[i].select) {
|
|
47
|
+
queryOptions.populate[i].select += ' '
|
|
48
|
+
} else {
|
|
49
|
+
queryOptions.populate[i].select = ''
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (queryOptions.select[key] === 0) {
|
|
53
|
+
queryOptions.populate[i].select += '-'
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
queryOptions.populate[i].select += key.substring(populate[i].length + 1)
|
|
57
|
+
delete queryOptions.select[key]
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// If other specific fields are selected, add the populated field
|
|
62
|
+
if (queryOptions.select) {
|
|
63
|
+
if (Object.keys(queryOptions.select).length > 0 && !queryOptions.select[populate[i]]) {
|
|
64
|
+
queryOptions.select[populate[i]] = 1
|
|
65
|
+
} else if (Object.keys(queryOptions.select).length === 0) {
|
|
66
|
+
delete queryOptions.select
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
} else if (!Array.isArray(queryOptions.populate)) {
|
|
71
|
+
queryOptions.populate = [queryOptions.populate]
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Configure populate query to not use strict populate to maintain
|
|
75
|
+
// behavior from Mongoose previous to v6 (unless already configured).
|
|
76
|
+
queryOptions.populate = queryOptions.populate.map((pop) => {
|
|
77
|
+
if (pop.strictPopulate !== undefined) return pop
|
|
78
|
+
pop.strictPopulate = false
|
|
79
|
+
return pop
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return queryOptions
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return function (req, res, next) {
|
|
87
|
+
const whitelist = ['distinct', 'limit', 'populate', 'query', 'select', 'skip', 'sort']
|
|
88
|
+
const query = {}
|
|
89
|
+
|
|
90
|
+
for (const key in req.query) {
|
|
91
|
+
if (whitelist.indexOf(key) === -1) {
|
|
92
|
+
continue
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (key === 'query') {
|
|
96
|
+
try {
|
|
97
|
+
query[key] = JSON.parse(req.query[key], jsonQueryParser)
|
|
98
|
+
} catch (e) {
|
|
99
|
+
return errorHandler(req, res, next)(new Error(`invalid_json_${key}`))
|
|
100
|
+
}
|
|
101
|
+
} else if (key === 'populate' || key === 'select' || key === 'sort') {
|
|
102
|
+
try {
|
|
103
|
+
query[key] = JSON.parse(req.query[key])
|
|
104
|
+
} catch (e) {
|
|
105
|
+
query[key] = req.query[key]
|
|
106
|
+
}
|
|
107
|
+
} else if (key === 'limit' || key === 'skip') {
|
|
108
|
+
query[key] = parseInt(req.query[key], 10)
|
|
109
|
+
|
|
110
|
+
if (`${query[key]}` !== `${req.query[key]}`) {
|
|
111
|
+
return errorHandler(req, res, next)(new Error(`invalid_${key}_value`))
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
query[key] = req.query[key]
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
req.erm = req.erm || {}
|
|
119
|
+
req.erm.query = parseQueryOptions(query)
|
|
120
|
+
|
|
121
|
+
next()
|
|
122
|
+
}
|
|
123
|
+
}
|