Bläddra i källkod

[fetch-utils] add fetchRedirectWithResponse (#24341)

GitOrigin-RevId: 9b1e9b02399beea87dbae0f87d1d7d897d145178
Jakob Ackermann 1 år sedan
förälder
incheckning
e99cd74cca
1 ändrade filer med 14 tillägg och 1 borttagningar
  1. 14 1
      libraries/fetch-utils/index.js

+ 14 - 1
libraries/fetch-utils/index.js

@@ -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 }
 }
 
 /**