keycloak_theme/src/lib/kcContext.ts

152 lines
4.5 KiB
TypeScript
Raw Normal View History

2021-02-23 15:32:37 +01:00
2021-03-02 12:17:24 +01:00
import { ftlValuesGlobalName } from "../bin/build-keycloak-theme/ftlValuesGlobalName";
2021-02-23 15:32:37 +01:00
import type { generateFtlFilesCodeFactory } from "../bin/build-keycloak-theme/generateFtl";
import { id } from "evt/tools/typeSafety/id";
2021-03-04 18:15:48 +01:00
import type { KcLanguageTag } from "./i18n/KcLanguageTag";
2021-03-04 21:14:54 +01:00
import { doExtends } from "evt/tools/typeSafety/doExtends";
2021-03-06 14:42:56 +01:00
import type { MessageKey } from "./i18n/useKcTranslation";
2021-02-23 15:32:37 +01:00
2021-03-06 14:42:56 +01:00
type ExtractAfterStartingWith<Prefix extends string, StrEnum> =
StrEnum extends `${Prefix}${infer U}` ? U : never;
2021-02-23 15:32:37 +01:00
2021-03-06 14:42:56 +01:00
const x: "33" | "44" = null as any;;
const y: `foo.${typeof x}` = `foo.${x}` as const;
y;
export type KcContext = KcContext.Login | KcContext.Register | KcContext.Info;
2021-03-04 21:14:54 +01:00
export declare namespace KcContext {
export type Template = {
url: {
loginAction: string;
resourcesPath: string;
resourcesCommonPath: string;
loginRestartFlowUrl: string;
2021-03-02 22:48:36 +01:00
loginUrl: string;
2021-03-04 21:14:54 +01:00
};
realm: {
2021-03-04 18:15:48 +01:00
displayName?: string;
2021-03-04 21:14:54 +01:00
displayNameHtml?: string;
internationalizationEnabled: boolean;
password: boolean;
registrationEmailAsUsername: boolean;
};
/** Undefined if !realm.internationalizationEnabled */
locale?: {
supported: {
url: string;
2021-03-04 21:14:54 +01:00
languageTag: KcLanguageTag;
/** Is determined by languageTag. Ex: languageTag === "en" => label === "English"
* or getLanguageLabel(languageTag) === label
*/
//label: LanguageLabel;
}[];
//NOTE: We do not expose this because the language is managed
//client side. We use this value however to set the default.
//current: LanguageLabel;
},
auth?: {
showUsername: boolean;
showResetCredentials: boolean;
showTryAnotherWayLink: boolean;
attemptedUsername?: boolean;
};
scripts: string[];
message?: {
type: "success" | "warning" | "error" | "info";
summary: string;
};
isAppInitiatedAction: boolean;
};
export type Login = Template & {
pageBasename: "login.ftl";
url: {
loginResetCredentialsUrl: string;
registrationUrl: string;
};
realm: {
loginWithEmailAllowed: boolean;
rememberMe: boolean;
resetPasswordAllowed: boolean;
2021-03-04 23:24:43 +01:00
registrationAllowed: boolean;
2021-03-04 21:14:54 +01:00
};
auth: {
selectedCredential?: string;
};
registrationDisabled: boolean;
login: {
2021-03-04 18:15:48 +01:00
username?: string;
2021-03-04 21:14:54 +01:00
rememberMe: boolean;
};
usernameEditDisabled: boolean;
social: {
displayInfo: boolean;
providers?: {
loginUrl: string;
alias: string;
providerId: string;
displayName: string;
}[]
};
};
export type Register = Template & {
pageBasename: "register.ftl";
url: {
registrationAction: string;
};
messagesPerField: {
printIfExists<T>(
key:
"userLabel" |
"username" |
"email" |
"firstName" |
"lastName" |
"password" |
"password-confirm",
x: T
): T | undefined;
};
register: {
formData: {
firstName?: string;
displayName?: string;
lastName?: string;
email?: string;
username?: string;
}
};
passwordRequired: boolean;
recaptchaRequired: boolean;
2021-03-05 14:50:46 +01:00
/** undefined if !recaptchaRequired */
recaptchaSiteKey?: string;
2021-03-04 18:15:48 +01:00
};
2021-03-04 21:14:54 +01:00
2021-03-06 14:42:56 +01:00
export type Info = Template & {
pageBasename: "info.ftl";
messageHeader?: string;
requiredActions?: ExtractAfterStartingWith<"requiredAction.",MessageKey>[];
skipLink: boolean;
pageRedirectUri?: string;
actionUri?: string;
client: {
baseUrl?: string;
}
};
2021-03-04 21:14:54 +01:00
}
{
type T = KcContext["pageBasename"];
type U = Parameters<ReturnType<typeof generateFtlFilesCodeFactory>["generateFtlFilesCode"]>[0]["pageBasename"];
doExtends<T, U>();
doExtends<U, T>();
}
2021-02-23 15:32:37 +01:00
2021-03-02 23:48:31 +01:00
export const kcContext = id<KcContext | undefined>((window as any)[ftlValuesGlobalName]);