| 1 | import { relayClerkTokenOptions } from "@t3tools/shared/relayAuth"; |
| 2 | import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | export class CloudPublicConfigMissingError extends Schema.TaggedError<CloudPublicConfigMissingError>()( |
| 6 | "CloudPublicConfigMissingError", |
| 7 | { |
| 8 | key: Schema.Literal("T3CODE_CLERK_JWT_TEMPLATE"), |
| 9 | }, |
| 10 | ) { |
| 11 | override get message(): string { |
| 12 | return `${this.key} is not configured.`; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | export interface CloudPublicConfig { |
| 17 | readonly clerkPublishableKey: string | null; |
| 18 | readonly clerkJwtTemplate: string | null; |
| 19 | readonly relayUrl: string | null; |
| 20 | } |
| 21 | |
| 22 | export function trimNonEmpty(value: string | undefined): string | null { |
| 23 | return value?.trim() || null; |
| 24 | } |
| 25 | |
| 26 | export function resolveCloudPublicConfig(): CloudPublicConfig { |
| 27 | return { |
| 28 | clerkPublishableKey: trimNonEmpty( |
| 29 | import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined, |
| 30 | ), |
| 31 | clerkJwtTemplate: trimNonEmpty(import.meta.env.VITE_CLERK_JWT_TEMPLATE as string | undefined), |
| 32 | relayUrl: normalizeSecureRelayUrl( |
| 33 | (import.meta.env.VITE_T3CODE_RELAY_URL as string | undefined) ?? "", |
| 34 | ), |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | export function hasCloudPublicConfig(): boolean { |
| 39 | const config = resolveCloudPublicConfig(); |
| 40 | return Boolean(config.clerkPublishableKey && config.clerkJwtTemplate && config.relayUrl); |
| 41 | } |
| 42 | |
| 43 | export function resolveRelayClerkTokenOptions() { |
| 44 | const { clerkJwtTemplate } = resolveCloudPublicConfig(); |
| 45 | if (!clerkJwtTemplate) { |
| 46 | throw new CloudPublicConfigMissingError({ key: "T3CODE_CLERK_JWT_TEMPLATE" }); |
| 47 | } |
| 48 | return relayClerkTokenOptions(clerkJwtTemplate); |
| 49 | } |