lecoffre-front/src/sdk/IframeReference.ts
Debian Dev4 4c011f9ec6
All checks were successful
build-and-push-ext / build_push (push) Successful in 1m36s
ci: docker_tag=ext
2025-09-17 08:11:42 +00:00

65 lines
1.6 KiB
TypeScript

export default class IframeReference {
private static targetOrigin: string | null = null;
private static iframeUrl: string | null = null;
private static iframe: HTMLIFrameElement | null = null;
private constructor() { }
public static setTargetOrigin(targetOrigin: string): void {
if (this.targetOrigin) {
console.debug("targetOrigin is already set");
return;
}
try {
new URL(targetOrigin);
this.targetOrigin = targetOrigin;
} catch {
throw new Error(`Invalid targetOrigin: ${targetOrigin}`);
}
}
public static getTargetOrigin(): string {
if (!this.targetOrigin) {
throw new Error("targetOrigin is not set");
}
return this.targetOrigin;
}
public static setIframeUrl(iframeUrl: string): void {
if (this.iframeUrl) {
console.debug("iframeUrl is already set");
return;
}
try {
// Validate and normalize URL
const url = new URL(iframeUrl);
this.iframeUrl = url.toString();
} catch {
throw new Error(`Invalid iframeUrl: ${iframeUrl}`);
}
}
public static getIframeUrl(): string {
if (!this.iframeUrl) {
throw new Error("iframeUrl is not set");
}
return this.iframeUrl;
}
public static setIframe(iframe: HTMLIFrameElement | null): void {
if (iframe !== null && !(iframe instanceof HTMLIFrameElement)) {
throw new Error("setIframe expects an HTMLIFrameElement or null");
}
this.iframe = iframe;
}
public static getIframe(): HTMLIFrameElement {
if (!this.iframe) {
throw new Error("iframe is not set");
}
return this.iframe;
}
}