76 lines
2.6 KiB
TypeScript
Raw Normal View History

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 }> =
[KcContextExtended] extends [never]
? KcContextBase
: AndByDiscriminatingKey<
"pageId",
KcContextExtended & KcContextBase.Common,
KcContextBase
>;
export function getKcContext<
KcContextExtended extends { pageId: string } = never,
>(params?: {
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
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 = {};
deepAssign({
"target": kcContext,
"source":
kcContextDefaultMock !== undefined
? kcContextDefaultMock
: { "pageId": mockPageId, ...kcContextCommonMock },
});
if (partialKcContextCustomMock !== undefined) {
deepAssign({
"target": kcContext,
"source": partialKcContextCustomMock,
});
}
return { kcContext };
}
return {
"kcContext":
typeof window === "undefined"
? undefined
: (window as any)[ftlValuesGlobalName],
};
2021-06-23 08:16:51 +02:00
}