@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
79 lines • 3.66 kB
JavaScript
const isBundleKey = (displayName) => displayName === null || displayName === void 0 ? void 0 : displayName.includes("${");
const unWrap = (key) => key.substring(2, key.length - 1);
export const label = (t, text, fallback) => (isBundleKey(text) ? t(unWrap(text)) : text) || fallback;
export const labelAttribute = (t, attribute) => label(t, attribute.displayName, attribute.name);
const ROOT_ATTRIBUTES = ["username", "firstName", "lastName", "email"];
export const isRootAttribute = (attr) => attr && ROOT_ATTRIBUTES.includes(attr);
export const fieldName = (name) => `${isRootAttribute(name) ? "" : "attributes."}${name === null || name === void 0 ? void 0 : name.replaceAll(".", "🍺")}`;
export const beerify = (name) => name.replaceAll(".", "🍺");
export const debeerify = (name) => name.replaceAll("🍺", ".");
export function setUserProfileServerError(error, setError, t) {
(error.responseData.errors !== undefined
? error.responseData.errors
: [error.responseData]).forEach((e) => {
var _a;
const params = Object.assign({}, (_a = e.params) === null || _a === void 0 ? void 0 : _a.map((p) => t(isBundleKey(p.toString()) ? unWrap(p) : p)));
setError(fieldName(e.field), {
message: t(e.errorMessage, Object.assign(Object.assign({}, params), { defaultValue: e.errorMessage || e.field })),
type: "server",
});
});
}
export function isRequiredAttribute({ required, validators, }) {
// Check if required is true or if the validators include a validation that would make the attribute implicitly required.
return required || hasRequiredValidators(validators);
}
/**
* Checks whether the given validators include a validation that would make the attribute implicitly required.
*/
function hasRequiredValidators(validators) {
// If we don't have any validators, the attribute is not required.
if (!validators) {
return false;
}
// If the 'length' validator is defined and has a minimal length greater than zero the attribute is implicitly required.
// We have to do a lot of defensive coding here, because we don't have type information for the validators.
if ("length" in validators &&
"min" in validators.length &&
typeof validators.length.min === "number") {
return validators.length.min > 0;
}
return false;
}
export function isUserProfileError(error) {
// Check if the error is an object with a 'responseData' property.
if (typeof error !== "object" ||
error === null ||
!("responseData" in error)) {
return false;
}
const { responseData } = error;
if (isFieldError(responseData)) {
return true;
}
// Check if 'responseData' is an object with an 'errors' property that is an array.
if (typeof responseData !== "object" ||
responseData === null ||
!("errors" in responseData) ||
!Array.isArray(responseData.errors)) {
return false;
}
// Check if all errors are field errors.
return responseData.errors.every(isFieldError);
}
function isFieldError(error) {
// Check if the error is an object.
if (typeof error !== "object" || error === null) {
return false;
}
// Check if the error object has a 'field' property that is a string.
if (!("field" in error) || typeof error.field !== "string") {
return false;
}
// Check if the error object has an 'errorMessage' property that is a string.
if (!("errorMessage" in error) || typeof error.errorMessage !== "string") {
return false;
}
return true;
}
//# sourceMappingURL=utils.js.map