2023-04-16 02:09:26 +02:00
|
|
|
import type { KcContext, Attribute } from "./KcContext";
|
|
|
|
import { kcContextMocks, kcContextCommonMock } from "./kcContextMocks";
|
|
|
|
import type { DeepPartial } from "keycloakify/tools/DeepPartial";
|
|
|
|
import { deepAssign } from "keycloakify/tools/deepAssign";
|
2024-02-11 00:21:04 +01:00
|
|
|
import { isStorybook } from "keycloakify/lib/isStorybook";
|
2023-04-16 02:09:26 +02:00
|
|
|
import { id } from "tsafe/id";
|
|
|
|
import { exclude } from "tsafe/exclude";
|
|
|
|
import { assert } from "tsafe/assert";
|
|
|
|
import type { ExtendKcContext } from "./getKcContextFromWindow";
|
|
|
|
import { getKcContextFromWindow } from "./getKcContextFromWindow";
|
|
|
|
import { symToStr } from "tsafe/symToStr";
|
|
|
|
|
|
|
|
export function createGetKcContext<KcContextExtension extends { pageId: string } = never>(params?: {
|
|
|
|
mockData?: readonly DeepPartial<ExtendKcContext<KcContextExtension>>[];
|
2024-03-02 09:29:36 +01:00
|
|
|
mockProperties?: Record<string, string>;
|
2023-04-16 02:09:26 +02:00
|
|
|
}) {
|
2024-03-02 09:29:36 +01:00
|
|
|
const { mockData, mockProperties } = params ?? {};
|
2023-04-16 02:09:26 +02:00
|
|
|
|
2023-04-16 03:00:03 +02:00
|
|
|
function getKcContext<PageId extends ExtendKcContext<KcContextExtension>["pageId"] | undefined = undefined>(params?: {
|
2023-04-16 02:09:26 +02:00
|
|
|
mockPageId?: PageId;
|
2023-04-17 01:32:06 +02:00
|
|
|
storyPartialKcContext?: DeepPartial<Extract<ExtendKcContext<KcContextExtension>, { pageId: PageId }>>;
|
2023-04-16 02:36:15 +02:00
|
|
|
}): {
|
2023-04-16 03:00:03 +02:00
|
|
|
kcContext: PageId extends undefined
|
2023-04-16 02:36:15 +02:00
|
|
|
? ExtendKcContext<KcContextExtension> | undefined
|
|
|
|
: Extract<ExtendKcContext<KcContextExtension>, { pageId: PageId }>;
|
|
|
|
} {
|
2023-04-17 01:32:06 +02:00
|
|
|
const { mockPageId, storyPartialKcContext } = params ?? {};
|
2023-04-16 02:09:26 +02:00
|
|
|
|
|
|
|
const realKcContext = getKcContextFromWindow<KcContextExtension>();
|
|
|
|
|
|
|
|
if (mockPageId !== undefined && realKcContext === undefined) {
|
|
|
|
//TODO maybe trow if no mock fo custom page
|
|
|
|
|
2024-02-11 00:21:04 +01:00
|
|
|
warn_that_mock_is_enbaled: {
|
|
|
|
if (isStorybook) {
|
|
|
|
break warn_that_mock_is_enbaled;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`%cKeycloakify: ${symToStr({ mockPageId })} set to ${mockPageId}.`, "background: red; color: yellow; font-size: medium");
|
|
|
|
}
|
2023-04-16 02:09:26 +02:00
|
|
|
|
|
|
|
const kcContextDefaultMock = kcContextMocks.find(({ pageId }) => pageId === mockPageId);
|
|
|
|
|
|
|
|
const partialKcContextCustomMock = (() => {
|
|
|
|
const out: DeepPartial<ExtendKcContext<KcContextExtension>> = {};
|
|
|
|
|
|
|
|
const mockDataPick = mockData?.find(({ pageId }) => pageId === mockPageId);
|
|
|
|
|
|
|
|
if (mockDataPick !== undefined) {
|
|
|
|
deepAssign({
|
|
|
|
"target": out,
|
|
|
|
"source": mockDataPick
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-17 01:32:06 +02:00
|
|
|
if (storyPartialKcContext !== undefined) {
|
2023-04-16 02:09:26 +02:00
|
|
|
deepAssign({
|
|
|
|
"target": out,
|
2023-04-17 01:32:06 +02:00
|
|
|
"source": storyPartialKcContext
|
2023-04-16 02:09:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(out).length === 0 ? undefined : out;
|
|
|
|
})();
|
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2024-05-11 22:48:15 +02:00
|
|
|
if ("profile" in partialKcContextCustomMock) {
|
|
|
|
assert(kcContextDefaultMock !== undefined && "profile" in kcContextDefaultMock);
|
2023-04-16 02:09:26 +02:00
|
|
|
|
|
|
|
const { attributes } = kcContextDefaultMock.profile;
|
|
|
|
|
2024-05-11 22:48:15 +02:00
|
|
|
id<KcContext.Register>(kcContext).profile.attributes = [];
|
|
|
|
id<KcContext.Register>(kcContext).profile.attributesByName = {};
|
2023-04-16 02:09:26 +02:00
|
|
|
|
2024-05-11 22:48:15 +02:00
|
|
|
const partialAttributes = [...((partialKcContextCustomMock as DeepPartial<KcContext.Register>).profile?.attributes ?? [])].filter(
|
|
|
|
exclude(undefined)
|
|
|
|
);
|
2023-04-16 02:09:26 +02:00
|
|
|
|
|
|
|
attributes.forEach(attribute => {
|
|
|
|
const partialAttribute = partialAttributes.find(({ name }) => name === attribute.name);
|
|
|
|
|
|
|
|
const augmentedAttribute: Attribute = {} as any;
|
|
|
|
|
|
|
|
deepAssign({
|
|
|
|
"target": augmentedAttribute,
|
|
|
|
"source": attribute
|
|
|
|
});
|
|
|
|
|
|
|
|
if (partialAttribute !== undefined) {
|
|
|
|
partialAttributes.splice(partialAttributes.indexOf(partialAttribute), 1);
|
|
|
|
|
|
|
|
deepAssign({
|
|
|
|
"target": augmentedAttribute,
|
|
|
|
"source": partialAttribute
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-11 22:48:15 +02:00
|
|
|
id<KcContext.Register>(kcContext).profile.attributes.push(augmentedAttribute);
|
|
|
|
id<KcContext.Register>(kcContext).profile.attributesByName[augmentedAttribute.name] = augmentedAttribute;
|
2023-04-16 02:09:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
partialAttributes
|
|
|
|
.map(partialAttribute => ({ "validators": {}, ...partialAttribute }))
|
|
|
|
.forEach(partialAttribute => {
|
|
|
|
const { name } = partialAttribute;
|
|
|
|
|
|
|
|
assert(name !== undefined, "If you define a mock attribute it must have at least a name");
|
|
|
|
|
2024-05-11 22:48:15 +02:00
|
|
|
id<KcContext.Register>(kcContext).profile.attributes.push(partialAttribute as any);
|
|
|
|
id<KcContext.Register>(kcContext).profile.attributesByName[name] = partialAttribute as any;
|
2023-04-16 02:09:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:29:36 +01:00
|
|
|
if (mockProperties !== undefined) {
|
|
|
|
deepAssign({
|
|
|
|
"target": kcContext.properties,
|
|
|
|
"source": mockProperties
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-16 02:09:26 +02:00
|
|
|
return { kcContext };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (realKcContext === undefined) {
|
2023-04-16 02:36:15 +02:00
|
|
|
return { "kcContext": undefined as any };
|
2023-04-16 02:09:26 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 11:52:02 +02:00
|
|
|
if (realKcContext.themeType !== "login") {
|
2023-04-16 02:36:15 +02:00
|
|
|
return { "kcContext": undefined as any };
|
2023-04-16 02:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return { "kcContext": realKcContext as any };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { getKcContext };
|
|
|
|
}
|