2021-06-28 04:04:48 +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";
|
2021-06-28 04:04:48 +02:00
|
|
|
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
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
export type ExtendsKcContextBase<KcContextExtended extends { pageId: string }> = [KcContextExtended] extends [never]
|
|
|
|
? KcContextBase
|
|
|
|
: AndByDiscriminatingKey<"pageId", KcContextExtended & KcContextBase.Common, KcContextBase>;
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
export function getKcContext<KcContextExtended extends { pageId: string } = never>(params?: {
|
2021-10-11 21:35:40 +02:00
|
|
|
mockPageId?: ExtendsKcContextBase<KcContextExtended>["pageId"];
|
|
|
|
mockData?: readonly DeepPartial<ExtendsKcContextBase<KcContextExtended>>[];
|
|
|
|
}): { kcContext: ExtendsKcContextBase<KcContextExtended> | undefined } {
|
|
|
|
const { mockPageId, mockData } = params ?? {};
|
|
|
|
|
|
|
|
if (mockPageId !== undefined) {
|
|
|
|
//TODO maybe trow if no mock fo custom page
|
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
const kcContextDefaultMock = kcContextMocks.find(({ pageId }) => pageId === mockPageId);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
const partialKcContextCustomMock = mockData?.find(({ pageId }) => pageId === mockPageId);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
if (kcContextDefaultMock === undefined && partialKcContextCustomMock === undefined) {
|
2021-10-11 21:35:40 +02:00
|
|
|
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 = {};
|
|
|
|
|
|
|
|
deepAssign({
|
|
|
|
"target": kcContext,
|
2021-10-12 00:26:29 +02:00
|
|
|
"source": kcContextDefaultMock !== undefined ? kcContextDefaultMock : { "pageId": mockPageId, ...kcContextCommonMock },
|
2021-10-11 21:35:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (partialKcContextCustomMock !== undefined) {
|
|
|
|
deepAssign({
|
|
|
|
"target": kcContext,
|
|
|
|
"source": partialKcContextCustomMock,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return { kcContext };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-10-12 00:26:29 +02:00
|
|
|
"kcContext": typeof window === "undefined" ? undefined : (window as any)[ftlValuesGlobalName],
|
2021-10-11 21:35:40 +02:00
|
|
|
};
|
2021-06-23 08:16:51 +02:00
|
|
|
}
|