UNPKG

@clickup/rest-client

Version:

A syntax sugar tool around Node fetch() API, tailored to work with TypeScript and response validators

50 lines 2.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("util"); const sortBy_1 = __importDefault(require("lodash/sortBy")); const ellipsis_1 = __importDefault(require("./ellipsis")); function inspectPossibleJSON(headers, text, maxOutputLen) { const MAX_LEN_TO_TRY_PARSE = 1024 * 1024; if (typeof text === "string" && text.length > MAX_LEN_TO_TRY_PARSE) { // Don't even try to JSON-parse if the text is too long. return (0, ellipsis_1.default)(text, maxOutputLen); } if (text instanceof Buffer) { return `<Buffer: ${text.length} bytes>`; } if (!text || typeof text === "string") { if (!(headers.get("content-type") || "").match(/json/)) { return (0, ellipsis_1.default)(text, maxOutputLen); } try { const json = JSON.parse(text); if (json && typeof json === "object" && !(json instanceof Array)) { // Move error/errors fields on top for better logging. This is a poor // man's approach: of course not all APIs return error/errors fields at // all, but it's hard to reorder at any other layer of abstraction. reorderObjectProps(json, (k) => k === "error" || k === "errors" ? "" : k); } return (0, ellipsis_1.default)((0, util_1.inspect)(json, { depth: 20, compact: true }), maxOutputLen); } catch (e) { return (0, ellipsis_1.default)(text, maxOutputLen); } } return "<Stream>"; } exports.default = inspectPossibleJSON; /** * In-place-reorders keys in a given object. The important part is to do it * in-place to e.g. be able to alter some @Memoized values. */ function reorderObjectProps(obj, ranker) { const entries = Object.entries(obj); for (const k in obj) { delete obj[k]; } Object.assign(obj, Object.fromEntries((0, sortBy_1.default)(entries, ([k, v]) => ranker(k, v)))); } //# sourceMappingURL=inspectPossibleJSON.js.map