1import { relayClerkTokenOptions } from "@t3tools/shared/relayAuth";
2import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
3import * as Schema from "effect/Schema";
4
5export 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
16export interface CloudPublicConfig {
17 readonly clerkPublishableKey: string | null;
18 readonly clerkJwtTemplate: string | null;
19 readonly relayUrl: string | null;
20}
21
22export function trimNonEmpty(value: string | undefined): string | null {
23 return value?.trim() || null;
24}
25
26export 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
38export function hasCloudPublicConfig(): boolean {
39 const config = resolveCloudPublicConfig();
40 return Boolean(config.clerkPublishableKey && config.clerkJwtTemplate && config.relayUrl);
41}
42
43export function resolveRelayClerkTokenOptions() {
44 const { clerkJwtTemplate } = resolveCloudPublicConfig();
45 if (!clerkJwtTemplate) {
46 throw new CloudPublicConfigMissingError({ key: "T3CODE_CLERK_JWT_TEMPLATE" });
47 }
48 return relayClerkTokenOptions(clerkJwtTemplate);
49}