import { parse, type ContentType } from "content-type"; import { JSONParse, JSONStringify } from "json-with-bigint"; import { isPlainObject } from "./is-plain-object.js"; import { RequestError } from "@octokit/request-error"; import type { EndpointInterface, OctokitResponse } from "@octokit/types"; /* v8 ignore next -- @preserve */ const noop = () => ""; export default async function fetchWrapper( requestOptions: ReturnType, ): Promise> { const fetch: typeof globalThis.fetch = requestOptions.request?.fetch || globalThis.fetch; if (!fetch) { throw new Error( "fetch is not set. Please pass a fetch implementation as new Octokit({ request: { fetch }}). Learn more at https://github.com/octokit/octokit.js/#fetch-missing", ); } const log = requestOptions.request?.log || console; const parseSuccessResponseBody = requestOptions.request?.parseSuccessResponseBody !== false; const body = isPlainObject(requestOptions.body) || Array.isArray(requestOptions.body) ? JSONStringify(requestOptions.body) : requestOptions.body; // Header values must be `string` const requestHeaders = Object.fromEntries( Object.entries(requestOptions.headers).map(([name, value]) => [ name, String(value), ]), ); let fetchResponse: Response; try { fetchResponse = await fetch(requestOptions.url, { method: requestOptions.method, body, redirect: requestOptions.request?.redirect, headers: requestHeaders, signal: requestOptions.request?.signal, // duplex must be set if request.body is ReadableStream or Async Iterables. // See https://fetch.spec.whatwg.org/#dom-requestinit-duplex. ...(requestOptions.body && { duplex: "half" }), }); // wrap fetch errors as RequestError if it is not a AbortError } catch (error) { let message = "Unknown Error"; /* vitest coverage bug where it thinks that there is an else */ /* v8 ignore else -- @preserve */ if (error instanceof Error) { if (error.name === "AbortError") { (error as RequestError).status = 500; throw error; } message = error.message; // undici throws a TypeError for network errors // and puts the error message in `error.cause` // https://github.com/nodejs/undici/blob/e5c9d703e63cd5ad691b8ce26e3f9a81c598f2e3/lib/fetch/index.js#L227 if (error.name === "TypeError" && "cause" in error) { /* v8 ignore else -- @preserve */ if (error.cause instanceof Error) { message = error.cause.message; } else if (typeof error.cause === "string") { message = error.cause; } } } const requestError = new RequestError(message, 500, { request: requestOptions, }); requestError.cause = error; throw requestError; } const status = fetchResponse.status; const url = fetchResponse.url; const responseHeaders = Object.fromEntries(fetchResponse.headers); const octokitResponse: OctokitResponse = { url, status, headers: responseHeaders, data: "", }; if ("deprecation" in responseHeaders) { const matches = responseHeaders.link && responseHeaders.link.match(/<([^<>]+)>; rel="deprecation"/); const deprecationLink = matches && matches.pop(); log.warn( `[@octokit/request] "${requestOptions.method} ${ requestOptions.url }" is deprecated. It is scheduled to be removed on ${responseHeaders.sunset}${ deprecationLink ? `. See ${deprecationLink}` : "" }`, ); } if (status === 204 || status === 205) { return octokitResponse; } // GitHub API returns 200 for HEAD requests if (requestOptions.method === "HEAD") { if (status < 400) { return octokitResponse; } throw new RequestError(fetchResponse.statusText, status, { response: octokitResponse, request: requestOptions, }); } if (status === 304) { octokitResponse.data = await getResponseData(fetchResponse); throw new RequestError("Not modified", status, { response: octokitResponse, request: requestOptions, }); } if (status >= 400) { octokitResponse.data = await getResponseData(fetchResponse); throw new RequestError(toErrorMessage(octokitResponse.data), status, { response: octokitResponse, request: requestOptions, }); } octokitResponse.data = parseSuccessResponseBody ? await getResponseData(fetchResponse) : fetchResponse.body; return octokitResponse; } async function getResponseData(response: Response): Promise { const contentType = response.headers.get("content-type"); if (!contentType) { return response.text().catch(noop); } const mimetype = parse(contentType); if (isJSONResponse(mimetype)) { let text = ""; try { text = await response.text(); return JSONParse(text); } catch (err) { return text; } } else if ( mimetype.type.startsWith("text/") || // `application/octet-stream` is the canonical "arbitrary binary" type // (RFC 2046) and must never be decoded as text, even when the response // carries a (misleading) `charset=utf-8` parameter — see #751. (mimetype.parameters.charset?.toLowerCase() === "utf-8" && mimetype.type !== "application/octet-stream") ) { return response.text().catch(noop); } else { return response.arrayBuffer().catch( /* v8 ignore next -- @preserve */ () => new ArrayBuffer(0), ); } } function isJSONResponse(mimetype: ContentType): boolean { return ( mimetype.type === "application/json" || mimetype.type === "application/graphql-response+json" || mimetype.type === "application/scim+json" ); } function toErrorMessage(data: unknown) { if (typeof data === "string") { return data; } if (data instanceof ArrayBuffer) { return "Unknown error"; } // Guard the object-shaped branch before using the `in` operator: a JSON error // body can be a primitive (`null`, a number, a boolean), and `"message" in // data` throws a raw TypeError on non-object values. Primitives fall through // to the generic unknown-error message below. See #819. if (typeof data === "object" && data !== null && "message" in data) { const objectData = data as Record; const suffix = "documentation_url" in objectData ? ` - ${objectData.documentation_url}` : ""; return Array.isArray(objectData.errors) ? `${objectData.message}: ${objectData.errors.map((v) => JSONStringify(v)).join(", ")}${suffix}` : `${objectData.message}${suffix}`; } return `Unknown error: ${JSONStringify(data)}`; }