express-version-route 1.2.9 → 1.2.11

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![view on npm](http://img.shields.io/npm/v/express-version-route.svg)](https://www.npmjs.org/package/express-version-route)
4
4
  [![view on npm](http://img.shields.io/npm/l/express-version-route.svg)](https://www.npmjs.org/package/express-version-route)
5
- [![Build](https://github.com/lirantal/express-version-route/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/lirantal/express-version-route/actions?query=workflow%3ACI)
5
+ [![CI](https://github.com/lirantal/express-version-route/actions/workflows/main.yml/badge.svg)](https://github.com/lirantal/express-version-route/actions/workflows/main.yml)
6
6
  [![Codecov](https://img.shields.io/codecov/c/gh/lirantal/express-version-route.svg)](https://codecov.io/gh/lirantal/express-version-route)
7
7
  [![npm module downloads](http://img.shields.io/npm/dt/express-version-route.svg)](https://www.npmjs.org/package/express-version-route)
8
8
 
package/index.js CHANGED
@@ -10,6 +10,11 @@ class versionRouter {
10
10
  for (let [versionKey, versionRouter] of versionsMap) {
11
11
  versionArray.push(versionKey)
12
12
  if (this.checkVersionMatch(req.version, versionKey)) {
13
+ if (Array.isArray(versionRouter)) {
14
+ return versionRouter.reduceRight((nextHandler, handler) => {
15
+ return () => handler(req, res, nextHandler)
16
+ }, next)()
17
+ }
13
18
  return versionRouter(req, res, next)
14
19
  }
15
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-version-route",
3
- "version": "1.2.9",
3
+ "version": "1.2.11",
4
4
  "description": "provides middleware to load controllers based on api versions",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -23,6 +23,33 @@ test('given a versioned router, match the route', t => {
23
23
  t.is(result.testVersion, v1)
24
24
  })
25
25
 
26
+ test('given a versioned router with an array of handlers, match and chain handlers', t => {
27
+ const v1 = '1.0'
28
+ const requestedVersion = '1.0.0'
29
+
30
+ const routesMap = new Map()
31
+ routesMap.set(v1, [
32
+ (req, res, next) => {
33
+ res.chain = ['first']
34
+ return next()
35
+ },
36
+ (req, res, next) => {
37
+ res.chain.push('second')
38
+ res.out = { testVersion: v1 }
39
+ return res.out
40
+ }
41
+ ])
42
+
43
+ const middleware = versionRouter.route(routesMap)
44
+ const req = {
45
+ version: requestedVersion
46
+ }
47
+
48
+ const result = middleware(req, {}, () => { })
49
+ t.is(result.testVersion, v1)
50
+ t.deepEqual(result, { testVersion: v1 })
51
+ })
52
+
26
53
  test('given a versioned router, dont match if requestVersion is not semver syntax', t => {
27
54
  const v1 = '1.0'
28
55
  const requestedVersion = '1.0'