UNPKG

@keycloakify/keycloak-admin-ui

Version:
117 lines 4.5 kB
import { saveAs } from "file-saver"; import { flatten } from "flat"; import { cloneDeep } from "lodash-es"; import { arrayToKeyValue, keyValueToArray, } from "./components/key-value-form/key-value-convert"; export const sortProviders = (providers) => { return [...new Map(Object.entries(providers).sort(sortProvider)).keys()]; }; const sortProvider = (a, b) => { let s1, s2; if (a[1].order !== b[1].order) { s1 = b[1].order; s2 = a[1].order; } else { s1 = a[0]; s2 = b[0]; } if (s1 < s2) { return -1; } else if (s1 > s2) { return 1; } else { return 0; } }; export const toKey = (value) => value.replace(/\s/g, "-"); export const exportClient = (client) => { const clientCopy = cloneDeep(client); delete clientCopy.id; if (clientCopy.protocolMappers) { for (let i = 0; i < clientCopy.protocolMappers.length; i++) { delete clientCopy.protocolMappers[i].id; } } saveAs(new Blob([prettyPrintJSON(clientCopy)], { type: "application/json", }), clientCopy.clientId + ".json"); }; export const toUpperCase = (name) => (name.charAt(0).toUpperCase() + name.slice(1)); const isAttributesObject = (value) => Object.values(value).filter((value) => Array.isArray(value) && value.length >= 1).length !== 0; const isAttributeArray = (value) => { if (!Array.isArray(value)) { return false; } return value.some((e) => Object.hasOwn(e, "key") && Object.hasOwn(e, "value")); }; const isEmpty = (obj) => Object.keys(obj).length === 0; export function convertAttributeNameToForm(name) { const index = name.indexOf("."); return `${name.substring(0, index)}.${beerify(name.substring(index + 1))}`; } export const beerify = (name) => name.replaceAll(".", "🍺"); export const debeerify = (name) => name.replaceAll("🍺", "."); export function convertToFormValues(obj, setValue) { Object.entries(obj).map((entry) => { const [key, value] = entry; if (key === "attributes" && isAttributesObject(value)) { setValue(key, arrayToKeyValue(value)); } else if (key === "config" || key === "attributes") { if (!isEmpty(value)) { const flattened = flatten(value, { safe: true }); const convertedValues = Object.entries(flattened).map(([key, value]) => Array.isArray(value) && value.length === 1 ? [key, value[0]] : [key, value]); convertedValues.forEach(([k, v]) => setValue(`${key}.${beerify(k)}`, v)); } else { setValue(key, undefined); } } else { setValue(key, value); } }); } export function convertFormValuesToObject(obj) { const result = {}; Object.entries(obj).map(([key, value]) => { if (isAttributeArray(value)) { result[key] = keyValueToArray(value); } else if (key === "config" || key === "attributes") { result[key] = Object.fromEntries(Object.entries(value || {}).map(([k, v]) => [debeerify(k), v])); } else { result[key] = value; } }); return result; } export const emptyFormatter = () => (data) => { return data ? data : "—"; }; export const upperCaseFormatter = () => (data) => { const value = data === null || data === void 0 ? void 0 : data.toString(); return (value ? toUpperCase(value) : undefined); }; export const alphaRegexPattern = /[^A-Za-z]/g; export const emailRegexPattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; export const KEY_PROVIDER_TYPE = "org.keycloak.keys.KeyProvider"; export const prettyPrintJSON = (value) => JSON.stringify(value, null, 2); export const addTrailingSlash = (url) => url.endsWith("/") ? url : url + "/"; export const localeToDisplayName = (locale, displayLocale) => { try { return new Intl.DisplayNames([displayLocale], { type: "language" }).of( // This is mapping old locale codes to the new locale codes for Simplified and Traditional Chinese. // Once the existing locales have been moved, this code can be removed. locale === "zh-CN" ? "zh-HANS" : locale === "zh-TW" ? "zh-HANT" : locale); } catch (_a) { return locale; } }; //# sourceMappingURL=util.js.map