UNPKG

@keycloakify/keycloak-account-ui

Version:

<p align="center"> <img src="https://github.com/user-attachments/assets/e31c4910-7205-441c-9a35-e134b806b3a8"> </p> <p align="center"> <i>Repackaged Keycloak Account UI</i> <br> <br> <a href="https://github.com/keycloakify/keycloak-a

40 lines 1.36 kB
import { isRecord } from "../utils/isRecord"; import { CONTENT_TYPE_HEADER, CONTENT_TYPE_JSON } from "../api/constants"; export class ApiError extends Error { } export async function parseResponse(response) { const contentType = response.headers.get(CONTENT_TYPE_HEADER); const isJSON = contentType ? contentType.includes(CONTENT_TYPE_JSON) : false; if (!isJSON) { throw new Error(`Expected response to have a JSON content type, got '${contentType}' instead.`); } const data = await parseJSON(response); if (!response.ok) { throw new ApiError(getErrorMessage(data)); } return data; } async function parseJSON(response) { try { return await response.json(); } catch (error) { throw new Error("Unable to parse response as valid JSON.", { cause: error, }); } } function getErrorMessage(data) { if (!isRecord(data)) { throw new Error("Unable to retrieve error message from response."); } const errorKeys = ["error_description", "errorMessage", "error"]; for (const key of errorKeys) { const value = data[key]; if (typeof value === "string") { return value; } } throw new Error("Unable to retrieve error message from response, no matching key found."); } //# sourceMappingURL=parse-response.js.map