{
  "type": "module",
  "source": "doc/api/api-redirecthandler.md",
  "modules": [
    {
      "textRaw": "RedirectHandler",
      "name": "redirecthandler",
      "introduced_in": "v4.0.0",
      "type": "module",
      "stability": 2,
      "stabilityText": "Stable",
      "desc": "<p>A dispatch handler that follows HTTP redirects on behalf of another handler. It\nwraps a downstream <code>handler</code> and a <code>dispatch</code> function: when a redirectable\nresponse is received it transparently re-dispatches the request to the new\nlocation, forwarding the lifecycle callbacks to the wrapped <code>handler</code> only once a\nfinal (non-redirect) response is reached.</p>\n<p>Redirects are followed for the <code>300</code>, <code>301</code>, <code>302</code>, <code>303</code>, <code>307</code>, and <code>308</code>\nstatus codes, up to <code>maxRedirections</code> hops. Following the relevant HTTP\nspecifications, the method is downgraded to <code>GET</code> for <code>301</code>/<code>302</code> <code>POST</code>\nresponses and for <code>303</code> responses with any method other than <code>HEAD</code>, request\nbodies that have already been consumed are not replayed, and headers that refer\nto the original URL (such as <code>host</code>) are stripped on each hop, with additional\ncredential headers removed on cross-origin redirects.</p>\n<pre><code class=\"language-mjs\">import { RedirectHandler } from 'undici'\n</code></pre>\n<pre><code class=\"language-cjs\">const { RedirectHandler } = require('undici')\n</code></pre>\n<p>Most users do not construct <code>RedirectHandler</code> directly. The <code>maxRedirections</code>\noption on <a href=\"Dispatcher.html#class-dispatcher\"><code>Dispatcher</code></a> methods and the higher-level clients use it\ninternally.</p>",
      "classes": [
        {
          "textRaw": "Class: `RedirectHandler`",
          "name": "RedirectHandler",
          "type": "class",
          "meta": {
            "added": [
              "v4.0.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>Extends: <a href=\"Dispatcher.md#class-dispatcher\"><code>&#x3C;DispatchHandler></code></a></li>\n</ul>\n<p>Implements the <a href=\"Dispatcher.html#parameter-dispatchhandler\"><code>DispatchHandler</code></a> interface, delegating every callback to the\nwrapped <code>handler</code> while intercepting redirect responses.</p>",
          "signatures": [
            {
              "textRaw": "`new RedirectHandler(dispatch, maxRedirections, opts, handler)`",
              "name": "RedirectHandler",
              "type": "ctor",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "params": [
                {
                  "textRaw": "`dispatch` {Function} The dispatch function used to issue the original request and every subsequent redirect. Called as `dispatch(options, handler)`.",
                  "name": "dispatch",
                  "type": "Function",
                  "desc": "The dispatch function used to issue the original request and every subsequent redirect. Called as `dispatch(options, handler)`."
                },
                {
                  "textRaw": "`maxRedirections` {number|null} The maximum number of redirects to follow. Must be a non-negative integer or `null`. When `null` or `0`, no redirects are followed and redirect responses are passed through to the wrapped `handler`.",
                  "name": "maxRedirections",
                  "type": "number|null",
                  "desc": "The maximum number of redirects to follow. Must be a non-negative integer or `null`. When `null` or `0`, no redirects are followed and redirect responses are passed through to the wrapped `handler`."
                },
                {
                  "textRaw": "`opts` {DispatchOptions} The dispatch options for the request. In addition to the standard dispatch options, the following redirect-specific fields are recognized:",
                  "name": "opts",
                  "type": "DispatchOptions",
                  "desc": "The dispatch options for the request. In addition to the standard dispatch options, the following redirect-specific fields are recognized:",
                  "options": [
                    {
                      "textRaw": "`throwOnMaxRedirect` {boolean} When `true`, an error is thrown once `maxRedirections` is reached instead of returning the last redirect response. **Default:** `false`.",
                      "name": "throwOnMaxRedirect",
                      "type": "boolean",
                      "default": "`false`",
                      "desc": "When `true`, an error is thrown once `maxRedirections` is reached instead of returning the last redirect response."
                    },
                    {
                      "textRaw": "`stripHeadersOnRedirect` {string}[] Header names to remove from the request on every redirect hop. **Default:** `null`.",
                      "name": "stripHeadersOnRedirect",
                      "type": "string",
                      "default": "`null`",
                      "desc": "[] Header names to remove from the request on every redirect hop."
                    },
                    {
                      "textRaw": "`stripHeadersOnCrossOriginRedirect` {string}[] Additional header names to remove from the request when a redirect points to a different origin. **Default:** `null`.",
                      "name": "stripHeadersOnCrossOriginRedirect",
                      "type": "string",
                      "default": "`null`",
                      "desc": "[] Additional header names to remove from the request when a redirect points to a different origin."
                    }
                  ]
                },
                {
                  "textRaw": "`handler` {DispatchHandler} The downstream handler that receives the lifecycle callbacks for the final response.",
                  "name": "handler",
                  "type": "DispatchHandler",
                  "desc": "The downstream handler that receives the lifecycle callbacks for the final response."
                }
              ],
              "desc": "<p>Throws an <a href=\"Errors.html#class-invalidargumenterror\"><code>InvalidArgumentError</code></a> when <code>maxRedirections</code> is not a non-negative\ninteger, when <code>throwOnMaxRedirect</code> is not a boolean, or when either\n<code>stripHeadersOnRedirect</code> or <code>stripHeadersOnCrossOriginRedirect</code> is not an array of\nheader-name strings.</p>\n<pre><code class=\"language-mjs\">import { Client, RedirectHandler } from 'undici'\n\nconst client = new Client('http://example.com')\nconst dispatch = client.dispatch.bind(client)\n\nconst handler = new RedirectHandler(dispatch, 5, {\n  method: 'GET',\n  path: '/',\n  origin: 'http://example.com',\n}, {\n  onRequestStart () {},\n  onResponseStart (controller, statusCode, headers) {\n    console.log(statusCode)\n  },\n  onResponseData (controller, chunk) {},\n  onResponseEnd () {},\n})\n\ndispatch({ method: 'GET', path: '/', origin: 'http://example.com' }, handler)\n</code></pre>"
            }
          ],
          "classMethods": [
            {
              "textRaw": "Static method: `RedirectHandler.buildDispatch(dispatcher, maxRedirections)`",
              "name": "buildDispatch",
              "type": "classMethod",
              "meta": {
                "added": [
                  "v7.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`dispatcher` {Dispatcher} The dispatcher whose `dispatch` method is wrapped.",
                      "name": "dispatcher",
                      "type": "Dispatcher",
                      "desc": "The dispatcher whose `dispatch` method is wrapped."
                    },
                    {
                      "textRaw": "`maxRedirections` {number|null} The maximum number of redirects to follow. Must be a non-negative integer or `null`.",
                      "name": "maxRedirections",
                      "type": "number|null",
                      "desc": "The maximum number of redirects to follow. Must be a non-negative integer or `null`."
                    }
                  ],
                  "return": {
                    "textRaw": "Returns: {Function} A `dispatch(options, handler)` function that wraps each call in a `RedirectHandler` before delegating to `dispatcher`.",
                    "name": "return",
                    "type": "Function",
                    "desc": "A `dispatch(options, handler)` function that wraps each call in a `RedirectHandler` before delegating to `dispatcher`."
                  }
                }
              ],
              "desc": "<p>Builds a redirect-aware dispatch function from an existing dispatcher. The\nreturned function has the same shape as <code>dispatcher.dispatch</code> but follows\nredirects automatically. Throws an <a href=\"Errors.html#class-invalidargumenterror\"><code>InvalidArgumentError</code></a> when\n<code>maxRedirections</code> is not a non-negative integer.</p>\n<pre><code class=\"language-mjs\">import { Client, RedirectHandler } from 'undici'\n\nconst client = new Client('http://example.com')\nconst dispatch = RedirectHandler.buildDispatch(client, 5)\n\ndispatch({ method: 'GET', path: '/', origin: 'http://example.com' }, {\n  onRequestStart () {},\n  onResponseStart (controller, statusCode) {\n    console.log(statusCode)\n  },\n  onResponseData () {},\n  onResponseEnd () {},\n})\n</code></pre>"
            }
          ],
          "methods": [
            {
              "textRaw": "`redirectHandler.onRequestStart(controller, context)`",
              "name": "onRequestStart",
              "type": "method",
              "meta": {
                "added": [
                  "v7.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`controller` {DispatchController} The controller for the in-flight request.",
                      "name": "controller",
                      "type": "DispatchController",
                      "desc": "The controller for the in-flight request."
                    },
                    {
                      "textRaw": "`context` {Object} The dispatch context.",
                      "name": "context",
                      "type": "Object",
                      "desc": "The dispatch context."
                    }
                  ]
                }
              ],
              "desc": "<p>Forwards the request-start event to the wrapped <code>handler</code>, augmenting <code>context</code>\nwith the current redirect <code>history</code>.</p>"
            },
            {
              "textRaw": "`redirectHandler.onRequestUpgrade(controller, statusCode, headers, socket)`",
              "name": "onRequestUpgrade",
              "type": "method",
              "meta": {
                "added": [
                  "v7.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`controller` {DispatchController} The controller for the in-flight request.",
                      "name": "controller",
                      "type": "DispatchController",
                      "desc": "The controller for the in-flight request."
                    },
                    {
                      "textRaw": "`statusCode` {number} The HTTP status code of the upgrade response.",
                      "name": "statusCode",
                      "type": "number",
                      "desc": "The HTTP status code of the upgrade response."
                    },
                    {
                      "textRaw": "`headers` {Object} The response headers.",
                      "name": "headers",
                      "type": "Object",
                      "desc": "The response headers."
                    },
                    {
                      "textRaw": "`socket` {Duplex} The upgraded socket.",
                      "name": "socket",
                      "type": "Duplex",
                      "desc": "The upgraded socket."
                    }
                  ]
                }
              ],
              "desc": "<p>Forwards a connection upgrade to the wrapped <code>handler</code>.</p>"
            },
            {
              "textRaw": "`redirectHandler.onResponseStart(controller, statusCode, headers, statusMessage)`",
              "name": "onResponseStart",
              "type": "method",
              "meta": {
                "added": [
                  "v7.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`controller` {DispatchController} The controller for the in-flight request.",
                      "name": "controller",
                      "type": "DispatchController",
                      "desc": "The controller for the in-flight request."
                    },
                    {
                      "textRaw": "`statusCode` {number} The HTTP status code of the response.",
                      "name": "statusCode",
                      "type": "number",
                      "desc": "The HTTP status code of the response."
                    },
                    {
                      "textRaw": "`headers` {Object} The response headers.",
                      "name": "headers",
                      "type": "Object",
                      "desc": "The response headers."
                    },
                    {
                      "textRaw": "`statusMessage` {string} The HTTP status message.",
                      "name": "statusMessage",
                      "type": "string",
                      "desc": "The HTTP status message."
                    }
                  ]
                }
              ],
              "desc": "<p>Inspects the response headers to decide whether to follow a redirect. When the\nstatus code is redirectable, the <code>maxRedirections</code> limit has not been reached, and\nthe request body has not been consumed, the <code>location</code> property is set, the method\nand headers are adjusted for the next hop, and the response is not forwarded.\nOtherwise the event is forwarded to the wrapped <code>handler</code>.</p>\n<p>Throws when <code>throwOnMaxRedirect</code> is <code>true</code> and the number of recorded redirects\nhas reached <code>maxRedirections</code>, and throws an <a href=\"Errors.html#class-invalidargumenterror\"><code>InvalidArgumentError</code></a> if a\nredirect loop is detected (for example when a <a href=\"Client.html#class-client\"><code>Client</code></a> or <a href=\"Pool.html#class-pool\"><code>Pool</code></a> is used\nfor a cross-origin redirect; use an <a href=\"Agent.html#class-agent\"><code>Agent</code></a> instead).</p>"
            },
            {
              "textRaw": "`redirectHandler.onResponseData(controller, chunk)`",
              "name": "onResponseData",
              "type": "method",
              "meta": {
                "added": [
                  "v7.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`controller` {DispatchController} The controller for the in-flight request.",
                      "name": "controller",
                      "type": "DispatchController",
                      "desc": "The controller for the in-flight request."
                    },
                    {
                      "textRaw": "`chunk` {Buffer} A chunk of the response body.",
                      "name": "chunk",
                      "type": "Buffer",
                      "desc": "A chunk of the response body."
                    }
                  ]
                }
              ],
              "desc": "<p>Forwards body data to the wrapped <code>handler</code>. While a redirect is pending\n(<code>location</code> is set), the <code>3xx</code> response body is ignored.</p>"
            },
            {
              "textRaw": "`redirectHandler.onResponseEnd(controller, trailers)`",
              "name": "onResponseEnd",
              "type": "method",
              "meta": {
                "added": [
                  "v7.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`controller` {DispatchController} The controller for the in-flight request.",
                      "name": "controller",
                      "type": "DispatchController",
                      "desc": "The controller for the in-flight request."
                    },
                    {
                      "textRaw": "`trailers` {Object} The response trailers.",
                      "name": "trailers",
                      "type": "Object",
                      "desc": "The response trailers."
                    }
                  ]
                }
              ],
              "desc": "<p>Completes the response. When a redirect is pending, the request is re-dispatched\nto the new <code>location</code>; otherwise the end event is forwarded to the wrapped\n<code>handler</code>.</p>"
            },
            {
              "textRaw": "`redirectHandler.onResponseError(controller, error)`",
              "name": "onResponseError",
              "type": "method",
              "meta": {
                "added": [
                  "v7.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`controller` {DispatchController} The controller for the in-flight request.",
                      "name": "controller",
                      "type": "DispatchController",
                      "desc": "The controller for the in-flight request."
                    },
                    {
                      "textRaw": "`error` {Error} The error that occurred.",
                      "name": "error",
                      "type": "Error",
                      "desc": "The error that occurred."
                    }
                  ]
                }
              ],
              "desc": "<p>Forwards the error to the wrapped <code>handler</code>.</p>"
            }
          ],
          "properties": [
            {
              "textRaw": "Type: {string|null}",
              "name": "location",
              "type": "string|null",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "desc": "<p>The <code>Location</code> header value of the redirect currently being followed, or <code>null</code>\nwhen no redirect is pending and the response is being passed through to the\nwrapped <code>handler</code>.</p>"
            },
            {
              "textRaw": "Type: {DispatchOptions}",
              "name": "opts",
              "type": "DispatchOptions",
              "meta": {
                "added": [
                  "v7.12.0"
                ],
                "changes": []
              },
              "desc": "<p>A copy of the dispatch options for the next request. It excludes the redirect\ncontrol fields (<code>maxRedirections</code>, <code>stripHeadersOnRedirect</code>, and\n<code>stripHeadersOnCrossOriginRedirect</code>) and is mutated on each hop to reflect the\nupdated method, path, origin, and headers.</p>"
            },
            {
              "textRaw": "Type: {number|null}",
              "name": "maxRedirections",
              "type": "number|null",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "desc": "<p>The maximum number of redirects this handler will follow.</p>"
            },
            {
              "textRaw": "Type: {DispatchHandler}",
              "name": "handler",
              "type": "DispatchHandler",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "desc": "<p>The wrapped downstream handler that receives the lifecycle callbacks for the final\nresponse.</p>"
            },
            {
              "textRaw": "Type: {URL}[]",
              "name": "history",
              "type": "URL",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "desc": "<p>The ordered list of URLs visited while following redirects. It is used to enforce\n<code>maxRedirections</code> and to detect redirect loops.</p>",
              "shortDesc": "[]"
            }
          ]
        }
      ]
    }
  ]
}