express-toolkit 0.7.1 → 0.8.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/package.json +10 -10
- package/src/router.js +12 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-toolkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"files": [
|
|
5
5
|
"index.js",
|
|
6
6
|
"src/"
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"test": "nyc --reporter=text ava test/*.spec.js --verbose",
|
|
17
17
|
"test-ci": "npx nyc --reporter=lcov ava test/*.spec.js --verbose",
|
|
18
18
|
"commit": "npx git-cz",
|
|
19
|
-
"pre-commit": "npm run lint &&
|
|
19
|
+
"pre-commit": "npm run lint && npm run test",
|
|
20
20
|
"changelog": "npx easy-changelog --out=./CHANGELOG.md",
|
|
21
21
|
"release": "sh scripts/release.sh"
|
|
22
22
|
},
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"express"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"express": "^4.17.
|
|
37
|
+
"express": "^4.17.3",
|
|
38
38
|
"json8-patch": "^1.0.4",
|
|
39
39
|
"mongoose": "^5.12.3",
|
|
40
40
|
"throwable-http-errors": "^2.1.0"
|
|
@@ -42,15 +42,15 @@
|
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"ava": "^3.8.1",
|
|
44
44
|
"body-parser": "^1.19.0",
|
|
45
|
-
"eslint": "^
|
|
46
|
-
"eslint-config-standard": "^
|
|
47
|
-
"eslint-plugin-import": "^2.
|
|
48
|
-
"eslint-plugin-node": "^
|
|
49
|
-
"eslint-plugin-promise": "^
|
|
45
|
+
"eslint": "^7.32.0",
|
|
46
|
+
"eslint-config-standard": "^16.0.3",
|
|
47
|
+
"eslint-plugin-import": "^2.25.4",
|
|
48
|
+
"eslint-plugin-node": "^11.1.0",
|
|
49
|
+
"eslint-plugin-promise": "^5.2.0",
|
|
50
50
|
"eslint-plugin-standard": "^4.0.1",
|
|
51
51
|
"husky": "^1.3.1",
|
|
52
|
-
"mongodb-memory-server": "^
|
|
53
|
-
"nyc": "^
|
|
52
|
+
"mongodb-memory-server": "^8.4.0",
|
|
53
|
+
"nyc": "^15.1.0",
|
|
54
54
|
"sinon": "^7.5.0",
|
|
55
55
|
"supertest": "^4.0.2"
|
|
56
56
|
}
|
package/src/router.js
CHANGED
|
@@ -48,10 +48,12 @@ const defaultEndpoints = {
|
|
|
48
48
|
* @param {Object} config
|
|
49
49
|
* @param {Object} config.controller
|
|
50
50
|
* @param {Object} [config.endpoints] Map<String,Boolean>
|
|
51
|
+
* @param {String} [config.id] The named route parameter to use as id. Defaults to "id"
|
|
51
52
|
* @param {Object} [config.options] Express router options. As described in https://expressjs.com/en/api.html#express.router
|
|
52
53
|
*/
|
|
53
54
|
function buildRouter (config) {
|
|
54
55
|
config.options = config.options || {}
|
|
56
|
+
config.id = config.id || 'id'
|
|
55
57
|
const router = express.Router(config.options)
|
|
56
58
|
if (!(config.controller instanceof Controller)) {
|
|
57
59
|
throw new Error('config.controller must be an instance of Controller')
|
|
@@ -64,7 +66,7 @@ function buildRouter (config) {
|
|
|
64
66
|
const endpointsMap = Object.assign({}, defaultEndpoints, config.endpoints)
|
|
65
67
|
|
|
66
68
|
const findByIdMiddleware = asyncMiddleware(async (req, res, next) => {
|
|
67
|
-
const resource = await config.controller.findById(req.params.id, req.query)
|
|
69
|
+
const resource = await config.controller.findById(req.params[config.id], req.query)
|
|
68
70
|
req.toSend = resource
|
|
69
71
|
return next()
|
|
70
72
|
})
|
|
@@ -76,7 +78,7 @@ function buildRouter (config) {
|
|
|
76
78
|
})
|
|
77
79
|
|
|
78
80
|
const updateByIdMiddleware = asyncMiddleware(async (req, res, next) => {
|
|
79
|
-
const resource = await config.controller.updateById(req.params.id, req.body, req.query)
|
|
81
|
+
const resource = await config.controller.updateById(req.params[config.id], req.body, req.query)
|
|
80
82
|
req.toSend = resource
|
|
81
83
|
return next()
|
|
82
84
|
})
|
|
@@ -88,7 +90,7 @@ function buildRouter (config) {
|
|
|
88
90
|
})
|
|
89
91
|
|
|
90
92
|
const deleteByIdMiddleware = asyncMiddleware(async (req, res, next) => {
|
|
91
|
-
await config.controller.deleteById(req.params.id)
|
|
93
|
+
await config.controller.deleteById(req.params[config.id])
|
|
92
94
|
req.toSend = null
|
|
93
95
|
return next()
|
|
94
96
|
})
|
|
@@ -114,13 +116,13 @@ function buildRouter (config) {
|
|
|
114
116
|
})
|
|
115
117
|
|
|
116
118
|
const patchByIdMiddleware = asyncMiddleware(async (req, res, next) => {
|
|
117
|
-
const resource = await config.controller.patchById(req.params.id, req.body)
|
|
119
|
+
const resource = await config.controller.patchById(req.params[config.id], req.body)
|
|
118
120
|
req.toSend = resource
|
|
119
121
|
return next()
|
|
120
122
|
})
|
|
121
123
|
|
|
122
124
|
const replaceByIdMiddleware = asyncMiddleware(async (req, res, next) => {
|
|
123
|
-
const resource = await config.controller.replaceById(req.params.id, req.body)
|
|
125
|
+
const resource = await config.controller.replaceById(req.params[config.id], req.body)
|
|
124
126
|
req.toSend = resource
|
|
125
127
|
return next()
|
|
126
128
|
})
|
|
@@ -138,7 +140,7 @@ function buildRouter (config) {
|
|
|
138
140
|
},
|
|
139
141
|
findById: {
|
|
140
142
|
method: 'get',
|
|
141
|
-
path:
|
|
143
|
+
path: `/:${config.id}`,
|
|
142
144
|
middleware: findByIdMiddleware
|
|
143
145
|
},
|
|
144
146
|
create: {
|
|
@@ -148,7 +150,7 @@ function buildRouter (config) {
|
|
|
148
150
|
},
|
|
149
151
|
updateById: {
|
|
150
152
|
method: 'put',
|
|
151
|
-
path:
|
|
153
|
+
path: `/:${config.id}`,
|
|
152
154
|
middleware: updateByIdMiddleware
|
|
153
155
|
},
|
|
154
156
|
updateByQuery: {
|
|
@@ -158,7 +160,7 @@ function buildRouter (config) {
|
|
|
158
160
|
},
|
|
159
161
|
deleteById: {
|
|
160
162
|
method: 'delete',
|
|
161
|
-
path:
|
|
163
|
+
path: `/:${config.id}`,
|
|
162
164
|
middleware: deleteByIdMiddleware
|
|
163
165
|
},
|
|
164
166
|
deleteByQuery: {
|
|
@@ -168,12 +170,12 @@ function buildRouter (config) {
|
|
|
168
170
|
},
|
|
169
171
|
patchById: {
|
|
170
172
|
method: 'patch',
|
|
171
|
-
path:
|
|
173
|
+
path: `/:${config.id}`,
|
|
172
174
|
middleware: patchByIdMiddleware
|
|
173
175
|
},
|
|
174
176
|
replaceById: {
|
|
175
177
|
method: 'put',
|
|
176
|
-
path:
|
|
178
|
+
path: `/:${config.id}/replace`,
|
|
177
179
|
middleware: replaceByIdMiddleware
|
|
178
180
|
}
|
|
179
181
|
|