UNPKG

cloudflare

Version:

The official TypeScript library for the Cloudflare API

39 lines • 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultParseResponse = defaultParseResponse; const log_1 = require("./utils/log.js"); async function defaultParseResponse(client, props) { const { response, requestLogID, retryOfRequestLogID, startTime } = props; const body = await (async () => { // fetch refuses to read the body when the status code is 204. if (response.status === 204) { return null; } if (props.options.__binaryResponse) { return response; } const contentType = response.headers.get('content-type'); const mediaType = contentType?.split(';')[0]?.trim(); const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json'); if (isJSON) { const contentLength = response.headers.get('content-length'); if (contentLength === '0') { // if there is no content we can't do anything return undefined; } const json = await response.json(); return json; } const text = await response.text(); return text; })(); (0, log_1.loggerFor)(client).debug(`[${requestLogID}] response parsed`, (0, log_1.formatRequestDetails)({ retryOfRequestLogID, url: response.url, status: response.status, body, durationMs: Date.now() - startTime, })); return body; } //# sourceMappingURL=parse.js.map