|
1 | 1 | 'use strict'
|
2 |
| - |
3 |
| -const { InvalidArgumentError } = require('../core/errors') |
4 |
| -const Dispatcher = require('../dispatcher/dispatcher') |
5 | 2 | const RedirectHandler = require('../handler/redirect-handler')
|
6 | 3 |
|
7 |
| -class RedirectDispatcher extends Dispatcher { |
8 |
| - #opts |
9 |
| - #dispatcher |
10 |
| - |
11 |
| - constructor (dispatcher, opts) { |
12 |
| - super() |
13 |
| - |
14 |
| - this.#dispatcher = dispatcher |
15 |
| - this.#opts = opts |
16 |
| - } |
17 |
| - |
18 |
| - dispatch (opts, handler) { |
19 |
| - return this.#dispatcher.dispatch( |
20 |
| - opts, |
21 |
| - new RedirectHandler(this.#dispatcher, opts, this.#opts, handler) |
22 |
| - ) |
23 |
| - } |
24 |
| - |
25 |
| - close (...args) { |
26 |
| - return this.#dispatcher.close(...args) |
27 |
| - } |
| 4 | +module.exports = opts => { |
| 5 | + const globalMaxRedirections = opts?.maxRedirections |
| 6 | + return dispatcher => { |
| 7 | + const dispatch = dispatcher.dispatch.bind(dispatcher) |
28 | 8 |
|
29 |
| - destroy (...args) { |
30 |
| - return this.#dispatcher.destroy(...args) |
31 |
| - } |
32 |
| -} |
| 9 | + return function redirectInterceptor (opts, handler) { |
| 10 | + const { maxRedirections = globalMaxRedirections } = opts |
33 | 11 |
|
34 |
| -module.exports = opts => { |
35 |
| - if (opts?.maxRedirections == null || opts?.maxRedirections === 0) { |
36 |
| - return null |
37 |
| - } |
| 12 | + if (!maxRedirections) { |
| 13 | + return dispatch(opts, handler) |
| 14 | + } |
38 | 15 |
|
39 |
| - if (!Number.isInteger(opts.maxRedirections) || opts.maxRedirections < 0) { |
40 |
| - throw new InvalidArgumentError('maxRedirections must be a positive number') |
| 16 | + const redirectHandler = new RedirectHandler(dispatch, maxRedirections, opts, handler) |
| 17 | + opts = { ...opts, maxRedirections: 0 } // Stop sub dispatcher from also redirecting. |
| 18 | + return dispatch(opts, redirectHandler) |
| 19 | + } |
41 | 20 | }
|
42 |
| - |
43 |
| - return dispatcher => new RedirectDispatcher(dispatcher, opts) |
44 | 21 | }
|
0 commit comments