express-version-route 1.2.16 → 1.2.17
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/.changeset/consistent-array-dispatch.md +5 -0
- package/README.md +16 -0
- package/index.js +18 -8
- package/package.json +1 -1
- package/test/array-handlers.test.js +121 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
---
|
|
2
|
+
"express-version-route": patch
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Run middleware arrays for maximum-version and default routes as well as exact matches. Forward middleware errors and route-control signals to the outer next callback instead of continuing through the remaining array handlers.
|
package/README.md
CHANGED
|
@@ -33,6 +33,22 @@ Then, on the route which you wish to version, call the `route` function of this
|
|
|
33
33
|
router.get('/test', versionRouter.route(routesMap))
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
Map values may also be arrays of middleware functions. The handlers run in order
|
|
37
|
+
when each calls `next()`, including when the route is selected by `useMaxVersion`
|
|
38
|
+
or the `'default'` fallback. A handler that does not call `next()` ends the chain;
|
|
39
|
+
`next(err)`, `next('route')` and `next('router')` leave the array through Express's
|
|
40
|
+
outer `next` callback.
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
routesMap.set('default', [
|
|
44
|
+
(req, res, next) => {
|
|
45
|
+
res.locals.apiVersion = 'default'
|
|
46
|
+
next()
|
|
47
|
+
},
|
|
48
|
+
(req, res) => res.json({ version: res.locals.apiVersion })
|
|
49
|
+
])
|
|
50
|
+
```
|
|
51
|
+
|
|
36
52
|
If no route matches the version requested by a client then the next middleware in the chain will be called.
|
|
37
53
|
To set a route fallback incase no version matches set a 'default' key on the routes map, for example:
|
|
38
54
|
|
package/index.js
CHANGED
|
@@ -3,6 +3,21 @@
|
|
|
3
3
|
const semver = require('semver')
|
|
4
4
|
const { RouteVersionUnmatchedError } = require('./errors')
|
|
5
5
|
|
|
6
|
+
function dispatchRoute (route, req, res, next) {
|
|
7
|
+
if (!Array.isArray(route)) {
|
|
8
|
+
return route(req, res, next)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return route.reduceRight((nextHandler, handler) => {
|
|
12
|
+
return err => {
|
|
13
|
+
if (err) {
|
|
14
|
+
return next(err)
|
|
15
|
+
}
|
|
16
|
+
return handler(req, res, nextHandler)
|
|
17
|
+
}
|
|
18
|
+
}, next)()
|
|
19
|
+
}
|
|
20
|
+
|
|
6
21
|
class versionRouter {
|
|
7
22
|
static route (versionsMap = new Map(), options = new Map()) {
|
|
8
23
|
return (req, res, next) => {
|
|
@@ -10,12 +25,7 @@ class versionRouter {
|
|
|
10
25
|
for (const [versionKey, versionRouter] of versionsMap) {
|
|
11
26
|
versionArray.push(versionKey)
|
|
12
27
|
if (this.checkVersionMatch(req.version, versionKey)) {
|
|
13
|
-
|
|
14
|
-
return versionRouter.reduceRight((nextHandler, handler) => {
|
|
15
|
-
return () => handler(req, res, nextHandler)
|
|
16
|
-
}, next)()
|
|
17
|
-
}
|
|
18
|
-
return versionRouter(req, res, next)
|
|
28
|
+
return dispatchRoute(versionRouter, req, res, next)
|
|
19
29
|
}
|
|
20
30
|
}
|
|
21
31
|
|
|
@@ -24,7 +34,7 @@ class versionRouter {
|
|
|
24
34
|
if (maxVersion) {
|
|
25
35
|
for (const [versionKey, versionRouter] of versionsMap) {
|
|
26
36
|
if (this.checkVersionMatch(maxVersion, versionKey)) {
|
|
27
|
-
return versionRouter
|
|
37
|
+
return dispatchRoute(versionRouter, req, res, next)
|
|
28
38
|
}
|
|
29
39
|
}
|
|
30
40
|
}
|
|
@@ -32,7 +42,7 @@ class versionRouter {
|
|
|
32
42
|
|
|
33
43
|
const defaultRoute = this.getDefaultRoute(versionsMap)
|
|
34
44
|
if (defaultRoute) {
|
|
35
|
-
return defaultRoute
|
|
45
|
+
return dispatchRoute(defaultRoute, req, res, next)
|
|
36
46
|
}
|
|
37
47
|
|
|
38
48
|
return next(new RouteVersionUnmatchedError(`${req.version} doesn't match any versions`))
|
package/package.json
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('ava')
|
|
4
|
+
const versionRouter = require('../index')
|
|
5
|
+
|
|
6
|
+
const selections = [
|
|
7
|
+
{ name: 'exact match', version: '1.2.3', key: '1.2.3', options: {} },
|
|
8
|
+
{ name: 'maximum version', version: '1.2', key: '1.2.3', options: { useMaxVersion: true } },
|
|
9
|
+
{ name: 'default route', version: '3.0.0', key: 'default', options: {} },
|
|
10
|
+
{ name: 'default after no maximum', version: '3.0', key: 'default', options: { useMaxVersion: true } }
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
function routeWithHandlers (selection, handlers) {
|
|
14
|
+
const routes = new Map([
|
|
15
|
+
['1.2.0', () => { throw new Error('The older route must not run') }],
|
|
16
|
+
[selection.key, handlers]
|
|
17
|
+
])
|
|
18
|
+
return versionRouter.route(routes, selection.options)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
for (const selection of selections) {
|
|
22
|
+
test(`${selection.name}: runs array handlers in order with the same request and response`, t => {
|
|
23
|
+
const req = { version: selection.version }
|
|
24
|
+
const res = { steps: [] }
|
|
25
|
+
const middleware = routeWithHandlers(selection, [
|
|
26
|
+
(request, response, next) => {
|
|
27
|
+
t.is(request, req)
|
|
28
|
+
t.is(response, res)
|
|
29
|
+
response.steps.push('first')
|
|
30
|
+
return next()
|
|
31
|
+
},
|
|
32
|
+
(request, response) => {
|
|
33
|
+
t.is(request, req)
|
|
34
|
+
t.is(response, res)
|
|
35
|
+
response.steps.push('second')
|
|
36
|
+
return response
|
|
37
|
+
}
|
|
38
|
+
])
|
|
39
|
+
|
|
40
|
+
t.is(middleware(req, res, () => t.fail('The response ended the chain')), res)
|
|
41
|
+
t.deepEqual(res.steps, ['first', 'second'])
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test(`${selection.name}: stops when a handler does not call next`, t => {
|
|
45
|
+
const middleware = routeWithHandlers(selection, [
|
|
46
|
+
() => 'response',
|
|
47
|
+
() => t.fail('The following handler must not run')
|
|
48
|
+
])
|
|
49
|
+
|
|
50
|
+
t.is(middleware({ version: selection.version }, {}, () => t.fail('Must not continue')), 'response')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test(`${selection.name}: continues after the final handler`, t => {
|
|
54
|
+
const middleware = routeWithHandlers(selection, [
|
|
55
|
+
(req, res, next) => next(),
|
|
56
|
+
(req, res, next) => next()
|
|
57
|
+
])
|
|
58
|
+
let calls = 0
|
|
59
|
+
|
|
60
|
+
const result = middleware({ version: selection.version }, {}, err => {
|
|
61
|
+
t.is(err, undefined)
|
|
62
|
+
calls++
|
|
63
|
+
return 'continued'
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
t.is(result, 'continued')
|
|
67
|
+
t.is(calls, 1)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test(`${selection.name}: an empty array continues to the outer middleware`, t => {
|
|
71
|
+
const middleware = routeWithHandlers(selection, [])
|
|
72
|
+
let calls = 0
|
|
73
|
+
|
|
74
|
+
const result = middleware({ version: selection.version }, {}, () => {
|
|
75
|
+
calls++
|
|
76
|
+
return 'continued'
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
t.is(result, 'continued')
|
|
80
|
+
t.is(calls, 1)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
for (const signal of [new Error('handler failed'), 'route', 'router']) {
|
|
84
|
+
test(`${selection.name}: forwards next(${String(signal)}) without running later handlers`, t => {
|
|
85
|
+
const middleware = routeWithHandlers(selection, [
|
|
86
|
+
(req, res, next) => next(signal),
|
|
87
|
+
() => t.fail('The signal must leave the array')
|
|
88
|
+
])
|
|
89
|
+
let calls = 0
|
|
90
|
+
|
|
91
|
+
const result = middleware({ version: selection.version }, {}, err => {
|
|
92
|
+
t.is(err, signal)
|
|
93
|
+
calls++
|
|
94
|
+
return 'forwarded'
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
t.is(result, 'forwarded')
|
|
98
|
+
t.is(calls, 1)
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
test(`${selection.name}: waits for an asynchronous next call`, async t => {
|
|
103
|
+
const steps = []
|
|
104
|
+
const middleware = routeWithHandlers(selection, [
|
|
105
|
+
(req, res, next) => {
|
|
106
|
+
steps.push('first')
|
|
107
|
+
setImmediate(next)
|
|
108
|
+
},
|
|
109
|
+
(req, res, next) => {
|
|
110
|
+
steps.push('second')
|
|
111
|
+
next()
|
|
112
|
+
}
|
|
113
|
+
])
|
|
114
|
+
|
|
115
|
+
await new Promise(resolve => {
|
|
116
|
+
middleware({ version: selection.version }, {}, resolve)
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
t.deepEqual(steps, ['first', 'second'])
|
|
120
|
+
})
|
|
121
|
+
}
|