94 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-06-23 08:16:51 +02:00
import type { KcContextBase } from "./KcContextBase";
2021-06-23 08:16:51 +02:00
import { kcContextMocks, kcContextCommonMock } from "./kcContextMocks";
import { ftlValuesGlobalName } from "../../bin/build-keycloak-theme/ftlValuesGlobalName";
import type { AndByDiscriminatingKey } from "../tools/AndByDiscriminatingKey";
import type { DeepPartial } from "../tools/DeepPartial";
import { deepAssign } from "../tools/deepAssign";
2021-06-23 08:16:51 +02:00
export type ExtendsKcContextBase<
KcContextExtended extends ({ pageId: string; } | undefined)
> =
KcContextExtended extends undefined ?
KcContextBase :
AndByDiscriminatingKey<
"pageId",
KcContextExtended & KcContextBase.Common,
KcContextBase
>;
export function getKcContext<KcContextExtended extends ({ pageId: string; } | undefined) = undefined>(
2021-06-23 08:16:51 +02:00
params?: {
mockPageId?: ExtendsKcContextBase<KcContextExtended>["pageId"];
mockData?: readonly DeepPartial<ExtendsKcContextBase<KcContextExtended>>[];
2021-06-23 08:16:51 +02:00
}
): { kcContext: ExtendsKcContextBase<KcContextExtended> | undefined; } {
2021-06-23 08:16:51 +02:00
const {
mockPageId,
mockData
} = params ?? {};
2021-06-23 08:16:51 +02:00
if (mockPageId !== undefined) {
2021-06-23 08:16:51 +02:00
//TODO maybe trow if no mock fo custom page
const kcContextDefaultMock = kcContextMocks.find(({ pageId }) => pageId === mockPageId);
const partialKcContextCustomMock = mockData?.find(({ pageId }) => pageId === mockPageId);
if (
kcContextDefaultMock === undefined &&
partialKcContextCustomMock === undefined
) {
console.warn([
`WARNING: You declared the non build in page ${mockPageId} but you didn't `,
`provide mock data needed to debug the page outside of Keycloak as you are trying to do now.`,
`Please check the documentation of the getKcContext function`
].join("\n"));
}
const kcContext: any = { "pageId": mockPageId };
deepAssign({
"target": kcContext,
"source": kcContextCommonMock
});
if (kcContextDefaultMock !== undefined) {
deepAssign({
"target": kcContext,
"source": kcContextDefaultMock
});
}
if (partialKcContextCustomMock !== undefined) {
deepAssign({
"target": kcContext,
"source": partialKcContextCustomMock
});
}
return { kcContext };
2021-06-23 08:16:51 +02:00
}
return {
"kcContext":
typeof window === "undefined" ?
undefined :
(window as any)[ftlValuesGlobalName]
};
2021-06-23 08:16:51 +02:00
}