[fetch-utils] add fetchRedirectWithResponse (#24341)

GitOrigin-RevId: 9b1e9b02399beea87dbae0f87d1d7d897d145178
This commit is contained in:
Jakob Ackermann 2025-03-17 15:44:31 +00:00 committed by Copybot
parent dcabf55882
commit e99cd74cca

View file

@ -95,6 +95,19 @@ async function fetchNothing(url, opts = {}) {
* @throws {RequestFailedError} if the response has a non redirect status code or missing Location header
*/
async function fetchRedirect(url, opts = {}) {
const { location } = await fetchRedirectWithResponse(url, opts)
return location
}
/**
* Make a request and extract the redirect from the response.
*
* @param {string | URL} url - request URL
* @param {object} opts - fetch options
* @return {Promise<{location: string, response: Response}>}
* @throws {RequestFailedError} if the response has a non redirect status code or missing Location header
*/
async function fetchRedirectWithResponse(url, opts = {}) {
const { fetchOpts } = parseOpts(opts)
fetchOpts.redirect = 'manual'
const response = await performRequest(url, fetchOpts)
@ -112,7 +125,7 @@ async function fetchRedirect(url, opts = {}) {
)
}
await discardResponseBody(response)
return location
return { location, response }
}
/**