Co-authored-by: Claude Fable 5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: codex <[email protected]>
84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
import { relayClerkTokenOptions } from "@t3tools/shared/relayAuth";
|
|
import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
|
|
import * as Schema from "effect/Schema";
|
|
|
|
export class CloudPublicConfigMissingError extends Schema.TaggedErrorClass<CloudPublicConfigMissingError>()(
|
|
"CloudPublicConfigMissingError",
|
|
{
|
|
key: Schema.Literal("T3CODE_CLERK_JWT_TEMPLATE"),
|
|
},
|
|
) {
|
|
override get message(): string {
|
|
return `${this.key} is not configured.`;
|
|
}
|
|
}
|
|
|
|
export interface CloudPublicConfig {
|
|
readonly clerkPublishableKey: string | null;
|
|
readonly clerkJwtTemplate: string | null;
|
|
readonly relayUrl: string | null;
|
|
readonly relayTracing: {
|
|
readonly tracesUrl: string | null;
|
|
readonly tracesDataset: string | null;
|
|
readonly tracesToken: string | null;
|
|
};
|
|
}
|
|
|
|
export function trimNonEmpty(value: string | undefined): string | null {
|
|
return value?.trim() || null;
|
|
}
|
|
|
|
function normalizeSecureUrl(value: string): string | null {
|
|
try {
|
|
const url = new URL(value);
|
|
return url.protocol === "https:" ? url.toString() : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function resolveCloudPublicConfig(): CloudPublicConfig {
|
|
return {
|
|
clerkPublishableKey: trimNonEmpty(
|
|
import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined,
|
|
),
|
|
clerkJwtTemplate: trimNonEmpty(import.meta.env.VITE_CLERK_JWT_TEMPLATE as string | undefined),
|
|
relayUrl: normalizeSecureRelayUrl(
|
|
(import.meta.env.VITE_T3CODE_RELAY_URL as string | undefined) ?? "",
|
|
),
|
|
relayTracing: {
|
|
tracesUrl: normalizeSecureUrl(
|
|
(import.meta.env.VITE_RELAY_OTLP_TRACES_URL as string | undefined) ?? "",
|
|
),
|
|
tracesDataset: trimNonEmpty(
|
|
import.meta.env.VITE_RELAY_OTLP_TRACES_DATASET as string | undefined,
|
|
),
|
|
tracesToken: trimNonEmpty(import.meta.env.VITE_RELAY_OTLP_TRACES_TOKEN as string | undefined),
|
|
},
|
|
};
|
|
}
|
|
|
|
export function resolveRelayTracingConfig() {
|
|
const { relayTracing } = resolveCloudPublicConfig();
|
|
return relayTracing.tracesUrl && relayTracing.tracesDataset && relayTracing.tracesToken
|
|
? {
|
|
tracesUrl: relayTracing.tracesUrl,
|
|
tracesDataset: relayTracing.tracesDataset,
|
|
tracesToken: relayTracing.tracesToken,
|
|
}
|
|
: null;
|
|
}
|
|
|
|
export function hasCloudPublicConfig(): boolean {
|
|
const config = resolveCloudPublicConfig();
|
|
return Boolean(config.clerkPublishableKey && config.clerkJwtTemplate && config.relayUrl);
|
|
}
|
|
|
|
export function resolveRelayClerkTokenOptions() {
|
|
const { clerkJwtTemplate } = resolveCloudPublicConfig();
|
|
if (!clerkJwtTemplate) {
|
|
throw new CloudPublicConfigMissingError({ key: "T3CODE_CLERK_JWT_TEMPLATE" });
|
|
}
|
|
return relayClerkTokenOptions(clerkJwtTemplate);
|
|
}
|