Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

publicConfig.ts

167 lines6.1 KB

1import { clerkFrontendApiUrlFromPublishableKey } from "@t3tools/shared/relayAuth";
2import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
3import * as Config from "effect/Config";
4import * as ConfigProvider from "effect/ConfigProvider";
5import * as Effect from "effect/Effect";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8import * as SchemaIssue from "effect/SchemaIssue";
9​
10declare const __T3CODE_BUILD_RELAY_URL__: string | undefined;
11declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined;
12declare const __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: string | undefined;
13declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: string | undefined;
14declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: string | undefined;
15declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: string | undefined;
16​
17const CLOUD_CLI_OAUTH_REDIRECT_URI = "http://127.0.0.1:34338/callback";
18const CLOUD_CLI_OAUTH_SCOPES = ["openid", "profile", "email"] as const;
19​
20function validateRelayUrl(value: string) {
21 const relayUrl = normalizeSecureRelayUrl(value);
22 return relayUrl === null
23 ? Effect.fail(
24 new Config.ConfigError(
25 new Schema.SchemaError(
26 new SchemaIssue.InvalidValue(Option.some(value), {
27 message: "Relay URL must be a secure absolute HTTPS origin.",
28 }),
29 ),
30 ),
31 )
32 : Effect.succeed(relayUrl);
34​
35function readBuildTimeValue(value: string | undefined): string {
36 return typeof value === "undefined" ? "" : value.trim();
38​
39function normalizeSecureUrl(value: string): string | null {
40 try {
41 const url = new URL(value);
42 return url.protocol === "https:" ? url.toString() : null;
43 } catch {
44 return null;
45 }
47​
48export const buildTimeRelayUrl =
49 typeof __T3CODE_BUILD_RELAY_URL__ === "undefined"
50 ? ""
51 : (normalizeSecureRelayUrl(__T3CODE_BUILD_RELAY_URL__) ?? "");
52export const buildTimeClerkPublishableKey = readBuildTimeValue(
53 typeof __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__ === "undefined"
54 ? undefined
55 : __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__,
56);
57export const buildTimeClerkCliOAuthClientId = readBuildTimeValue(
58 typeof __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__ === "undefined"
59 ? undefined
60 : __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__,
61);
62export const buildTimeRelayClientTracing = {
63 tracesUrl: readBuildTimeValue(
64 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__ === "undefined"
65 ? undefined
66 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__,
67 ),
68 tracesDataset: readBuildTimeValue(
69 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__ === "undefined"
70 ? undefined
71 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__,
72 ),
73 tracesToken: readBuildTimeValue(
74 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__ === "undefined"
75 ? undefined
76 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__,
77 ),
78} as const;
79​
80export function resolveRelayClientTracingConfig(
81 env: Readonly<Record<string, string | undefined>> = process.env,
82 fallback = buildTimeRelayClientTracing,
83) {
84 const tracesUrl = env.T3CODE_RELAY_CLIENT_OTLP_TRACES_URL?.trim() || fallback.tracesUrl;
85 const tracesDataset =
86 env.T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET?.trim() || fallback.tracesDataset;
87 const tracesToken = env.T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN?.trim() || fallback.tracesToken;
88 const normalizedTracesUrl = normalizeSecureUrl(tracesUrl);
89 return normalizedTracesUrl && tracesDataset && tracesToken
90 ? { tracesUrl: normalizedTracesUrl, tracesDataset, tracesToken }
91 : null;
93​
94export function makeRelayUrlConfig(fallback = buildTimeRelayUrl) {
95 const runtimeConfig = Config.nonEmptyString("T3CODE_RELAY_URL");
96 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
97 Config.mapOrFail(validateRelayUrl),
98 );
100​
101export const relayUrlConfig = makeRelayUrlConfig();
102​
103function makePublicValueConfig(name: string, fallback: string) {
104 const runtimeConfig = Config.nonEmptyString(name);
105 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
106 Config.map((value) => value.trim()),
107 );
109​
110export interface CloudCliOAuthConfig {
111 readonly authorizationEndpoint: string;
112 readonly tokenEndpoint: string;
113 readonly clientId: string;
114 readonly redirectUri: string;
115 readonly scopes: typeof CLOUD_CLI_OAUTH_SCOPES;
117​
118export function makeCloudCliOAuthConfig({
119 clerkPublishableKeyFallback = buildTimeClerkPublishableKey,
120 clerkCliOAuthClientIdFallback = buildTimeClerkCliOAuthClientId,
121}: {
122 readonly clerkPublishableKeyFallback?: string;
123 readonly clerkCliOAuthClientIdFallback?: string;
124} = {}) {
125 return Config.all({
126 clerkPublishableKey: makePublicValueConfig(
127 "T3CODE_CLERK_PUBLISHABLE_KEY",
128 clerkPublishableKeyFallback,
129 ),
130 clientId: makePublicValueConfig(
131 "T3CODE_CLERK_CLI_OAUTH_CLIENT_ID",
132 clerkCliOAuthClientIdFallback,
133 ),
134 }).pipe(
135 Config.mapOrFail(({ clerkPublishableKey, clientId }) =>
136 Effect.try({
137 try: () => clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey),
138 catch: (cause) =>
139 new Config.ConfigError(
140 new ConfigProvider.SourceError({
141 message: "Failed to derive Clerk Frontend API URL from the publishable key.",
142 cause,
143 }),
144 ),
145 }).pipe(
146 Effect.map(
147 (clerkFrontendApiUrl) =>
148 ({
149 authorizationEndpoint: `${clerkFrontendApiUrl}/oauth/authorize`,
150 tokenEndpoint: `${clerkFrontendApiUrl}/oauth/token`,
151 clientId,
152 redirectUri: CLOUD_CLI_OAUTH_REDIRECT_URI,
153 scopes: CLOUD_CLI_OAUTH_SCOPES,
154 }) satisfies CloudCliOAuthConfig,
155 ),
156 ),
157 ),
158 );
160​
161export const cloudCliOAuthConfig = makeCloudCliOAuthConfig();
162​
163export const hasCloudPublicConfig = Boolean(
164 (normalizeSecureRelayUrl(process.env.T3CODE_RELAY_URL ?? "") ?? buildTimeRelayUrl) &&
165 (process.env.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() || buildTimeClerkPublishableKey) &&
166 (process.env.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || buildTimeClerkCliOAuthClientId),
167);