UNPKG

@coinbase/cdp-sdk

Version:

SDK for interacting with the Coinbase Developer Platform Wallet API

67 lines 2.17 kB
import { fromJson } from "../json.js"; import { getBinaryResponse } from "./BinaryResponse.js"; // Pins the upstream Response so undici's FinalizationRegistry can't GC it and cancel the body stream. function retainResponse(target, response) { Object.defineProperty(target, "__fern_response_ref", { value: response, enumerable: false, configurable: true, writable: false, }); } export async function getResponseBody(response, responseType) { switch (responseType) { case "binary-response": return getBinaryResponse(response); case "blob": return await response.blob(); case "arrayBuffer": return await response.arrayBuffer(); case "sse": if (response.body == null) { return { ok: false, error: { reason: "body-is-null", statusCode: response.status, }, }; } retainResponse(response.body, response); return response.body; case "streaming": if (response.body == null) { return { ok: false, error: { reason: "body-is-null", statusCode: response.status, }, }; } retainResponse(response.body, response); return response.body; case "text": return await response.text(); } // if responseType is "json" or not specified, try to parse as JSON const text = await response.text(); if (text.length > 0) { try { const responseBody = fromJson(text); return responseBody; } catch (_err) { return { ok: false, error: { reason: "non-json", statusCode: response.status, rawBody: text, }, }; } } return undefined; } //# sourceMappingURL=getResponseBody.js.map