import { WidgetT } from "@/shared/schema"; export class S3WidgetKeyGenerator { private prefix: string = ""; private owner: string = ""; private widgetId: string = ""; public withPrefix(prefix: string): S3WidgetKeyGenerator { this.prefix = prefix; return this; } public withOwner(owner: string): S3WidgetKeyGenerator { this.owner = owner; return this; } public withWidgetId(widgetId: string): S3WidgetKeyGenerator { this.widgetId = widgetId; return this; } public build(): string { const parts = [ this.prefix, this.owner.replaceAll(" ", "-").toLowerCase(), this.widgetId, ]; if (parts.some((part) => !part || !part.match(/^[a-zA-Z0-9!\-_.*'()]+$/))) { throw new Error("Invalid Key Specification"); } return parts.join("/"); } public fromWidget(widget: WidgetT) { const keyBuilder = new S3WidgetKeyGenerator(); return keyBuilder .withPrefix(this.prefix) .withOwner(widget.owner) .withWidgetId(widget.id) .build(); } }