auth0
Version:
Auth0 Node.js SDK for the Management API v2.
43 lines (42 loc) • 2.26 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { fromJson } from "../json.mjs";
import { getResponseBody } from "./getResponseBody.mjs";
export function getErrorResponseBody(response) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
let contentType = (_a = response.headers.get("Content-Type")) === null || _a === void 0 ? void 0 : _a.toLowerCase();
if (contentType == null || contentType.length === 0) {
return getResponseBody(response);
}
if (contentType.indexOf(";") !== -1) {
contentType = (_c = (_b = contentType.split(";")[0]) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : "";
}
switch (contentType) {
case "application/hal+json":
case "application/json":
case "application/ld+json":
case "application/problem+json":
case "application/vnd.api+json":
case "text/json": {
const text = yield response.text();
return text.length > 0 ? fromJson(text) : undefined;
}
default:
if (contentType.startsWith("application/vnd.") && contentType.endsWith("+json")) {
const text = yield response.text();
return text.length > 0 ? fromJson(text) : undefined;
}
// Fallback to plain text if content type is not recognized
// Even if no body is present, the response will be an empty string
return yield response.text();
}
});
}