express-version-route 1.2.15 → 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.
@@ -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.
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
4
+ cd "$repo_root"
5
+ if [[ -f .npm-version ]]; then
6
+ npm_version="$(cat .npm-version)"
7
+ else
8
+ npm_version="$(node -p "require('./package.json').packageManager.slice(4)")"
9
+ fi
10
+ [[ "$npm_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "Expected an exact npm version" >&2; exit 1; }
11
+ config=(--min-release-age=30)
12
+ if [[ -f .npmrc ]]; then
13
+ config+=(--userconfig "$repo_root/.npmrc")
14
+ fi
15
+ npm install --global --ignore-scripts "${config[@]}" "npm@$npm_version"
16
+ [[ "$(npm --version)" == "$npm_version" ]]
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
4
+ cd "$repo_root"
5
+ package_manager="$(node -p "require('./package.json').packageManager")"
6
+ [[ "$package_manager" =~ ^yarn@[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "Expected an exact Yarn version" >&2; exit 1; }
7
+ config=(--min-release-age=30)
8
+ if [[ -f .npmrc ]]; then
9
+ config+=(--userconfig "$repo_root/.npmrc")
10
+ fi
11
+ # Use an isolated prefix rather than replacing runner-provided binaries.
12
+ yarn_prefix="${RUNNER_TEMP:-${TMPDIR:-/tmp}}/pinned-yarn"
13
+ npm install --global --prefix "$yarn_prefix" --ignore-scripts "${config[@]}" "$package_manager"
14
+ export PATH="$yarn_prefix/bin:$PATH"
15
+ if [[ -n "${GITHUB_PATH:-}" ]]; then
16
+ echo "$yarn_prefix/bin" >> "$GITHUB_PATH"
17
+ fi
18
+ [[ "$(yarn --version)" == "${package_manager#yarn@}" ]]
@@ -19,6 +19,10 @@ jobs:
19
19
  - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
20
20
  with:
21
21
  node-version: ${{ matrix.node }}
22
+ - name: Set up pinned npm
23
+ run: bash "${{ github.workspace }}/.github/scripts/setup-npm.sh"
24
+ - name: Set up pinned Yarn
25
+ run: bash "${{ github.workspace }}/.github/scripts/setup-yarn.sh"
22
26
  - name: install dependencies
23
27
  run: yarn install --ignore-engines
24
28
  - name: run tests
@@ -48,6 +52,10 @@ jobs:
48
52
  - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
49
53
  with:
50
54
  node-version: '24.x'
55
+ - name: Set up pinned npm
56
+ run: bash "${{ github.workspace }}/.github/scripts/setup-npm.sh"
57
+ - name: Set up pinned Yarn
58
+ run: bash "${{ github.workspace }}/.github/scripts/setup-yarn.sh"
51
59
  - name: install dependencies
52
60
  run: yarn install --ignore-engines
53
61
  - name: release
package/.npm-version ADDED
@@ -0,0 +1 @@
1
+ 11.21.0
package/CONTRIBUTING.md CHANGED
@@ -72,3 +72,15 @@ If any of the above doesn't work on a fresh checkout, that's a bug, please open
72
72
  If you are an AI coding agent or automated bot opening a PR, please add 🤖🤖🤖 to the end of the PR title. This helps maintainers triage agent-authored contributions and is a baseline expectation for this repo — PRs without the marker that turn out to be automated may be closed without review.
73
73
 
74
74
  The same applies to issues: prefix the title with 🤖🤖🤖 if it was opened by an agent on your behalf.
75
+ ## Package-manager pin
76
+
77
+ CI uses Yarn Classic, pinned by `package.json#packageManager`.
78
+ The auxiliary npm version used for tooling is pinned in `.npm-version`.
79
+ Run `bash .github/scripts/setup-npm.sh` and then
80
+ `bash .github/scripts/setup-yarn.sh` to install those exact versions.
81
+ The Yarn setup writes its bin directory to `GITHUB_PATH` in CI; locally add
82
+ `${TMPDIR:-/tmp}/pinned-yarn/bin` to your `PATH` before running Yarn.
83
+
84
+ Upgrade the pins in a maintenance PR, choose releases at least 30 days old,
85
+ and run the project checks. The bootstrap keeps `.npmrc` policies enabled.
86
+ Periodic upgrade automation is not configured.
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
- if (Array.isArray(versionRouter)) {
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(req, res, next)
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(req, res, next)
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-version-route",
3
- "version": "1.2.15",
3
+ "version": "1.2.17",
4
4
  "description": "provides middleware to load controllers based on api versions",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -140,5 +140,6 @@
140
140
  "engines": {
141
141
  "node": ">=24.0.0",
142
142
  "npm": ">=11.10.0"
143
- }
143
+ },
144
+ "packageManager": "yarn@1.22.22"
144
145
  }
@@ -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
+ }