Open History to see the last commit
1import { CONNECT_OAUTH_SCOPES, DEFAULT_HOSTED_APP_URL } from "@t3tools/shared/connectAuth";
2import { clerkFrontendApiUrlFromPublishableKey } from "@t3tools/shared/relayAuth";
3import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
4import * as Config from "effect/Config";
5import * as ConfigProvider from "effect/ConfigProvider";
6import * as Effect from "effect/Effect";
7import * as Option from "effect/Option";
8import * as Schema from "effect/Schema";
9import * as SchemaIssue from "effect/SchemaIssue";
10
11declare const __T3CODE_BUILD_RELAY_URL__: string | undefined;
12declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined;
13declare const __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: string | undefined;
14declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: string | undefined;
15declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: string | undefined;
16declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: string | undefined;
17
18const CLOUD_CLI_OAUTH_REDIRECT_URI = "http://127.0.0.1:34338/callback";
19const CLOUD_CLI_OAUTH_SCOPES = CONNECT_OAUTH_SCOPES;
20
21function validateRelayUrl(value: string) {
22 const relayUrl = normalizeSecureRelayUrl(value);
23 return relayUrl === null
24 ? Effect.fail(
25 new Config.ConfigError(
26 new Schema.SchemaError(
27 new SchemaIssue.InvalidValue(Option.some(value), {
28 message: "Relay URL must be a secure absolute HTTPS origin.",
29 }),
30 ),
31 ),
32 )
33 : Effect.succeed(relayUrl);
34}
35
36function readBuildTimeValue(value: string | undefined): string {
37 return typeof value === "undefined" ? "" : value.trim();
38}
39
40function normalizeSecureUrl(value: string): string | null {
41 try {
42 const url = new URL(value);
43 return url.protocol === "https:" ? url.toString() : null;
44 } catch {
45 return null;
46 }
47}
48
49export const buildTimeRelayUrl =
50 typeof __T3CODE_BUILD_RELAY_URL__ === "undefined"
51 ? ""
52 : (normalizeSecureRelayUrl(__T3CODE_BUILD_RELAY_URL__) ?? "");
53export const buildTimeClerkPublishableKey = readBuildTimeValue(
54 typeof __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__ === "undefined"
55 ? undefined
56 : __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__,
57);
58export const buildTimeClerkCliOAuthClientId = readBuildTimeValue(
59 typeof __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__ === "undefined"
60 ? undefined
61 : __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__,
62);
63export const buildTimeRelayClientTracing = {
64 tracesUrl: readBuildTimeValue(
65 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__ === "undefined"
66 ? undefined
67 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__,
68 ),
69 tracesDataset: readBuildTimeValue(
70 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__ === "undefined"
71 ? undefined
72 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__,
73 ),
74 tracesToken: readBuildTimeValue(
75 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__ === "undefined"
76 ? undefined
77 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__,
78 ),
79} as const;
80
81export function resolveRelayClientTracingConfig(
82 env: Readonly<Record<string, string | undefined>> = process.env,
83 fallback = buildTimeRelayClientTracing,
84) {
85 const tracesUrl = env.T3CODE_RELAY_CLIENT_OTLP_TRACES_URL?.trim() || fallback.tracesUrl;
86 const tracesDataset =
87 env.T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET?.trim() || fallback.tracesDataset;
88 const tracesToken = env.T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN?.trim() || fallback.tracesToken;
89 const normalizedTracesUrl = normalizeSecureUrl(tracesUrl);
90 return normalizedTracesUrl && tracesDataset && tracesToken
91 ? { tracesUrl: normalizedTracesUrl, tracesDataset, tracesToken }
92 : null;
93}
94
95export function makeRelayUrlConfig(fallback = buildTimeRelayUrl) {
96 const runtimeConfig = Config.nonEmptyString("T3CODE_RELAY_URL");
97 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
98 Config.mapOrFail(validateRelayUrl),
99 );
100}
101
102export const relayUrlConfig = makeRelayUrlConfig();
103
104/**
105 * Hosted app origin used for out-of-band OAuth on headless
106 * machines. Overridable so staging/nightly builds can point their CLIs at a
107 * matching hosted deployment.
108 */
109export const hostedAppUrlConfig = makePublicValueConfig(
110 "T3CODE_HOSTED_APP_URL",
111 DEFAULT_HOSTED_APP_URL,
112).pipe(Config.mapOrFail(validateHostedAppUrl));
113
114function validateHostedAppUrl(value: string) {
115 try {
116 const url = new URL(value);
117 const isLoopbackHttp =
118 url.protocol === "http:" &&
119 (url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]");
120 if (
121 (url.protocol !== "https:" && !isLoopbackHttp) ||
122 url.pathname !== "/" ||
123 url.search !== "" ||
124 url.hash !== ""
125 ) {
126 throw new Error("invalid hosted app origin");
127 }
128 return Effect.succeed(url.origin);
129 } catch {
130 return Effect.fail(
131 new Config.ConfigError(
132 new Schema.SchemaError(
133 new SchemaIssue.InvalidValue(Option.some(value), {
134 message: "Hosted app URL must be an absolute HTTPS origin (or HTTP loopback origin).",
135 }),
136 ),
137 ),
138 );
139 }
140}
141
142function makePublicValueConfig(name: string, fallback: string) {
143 const runtimeConfig = Config.nonEmptyString(name);
144 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
145 Config.map((value) => value.trim()),
146 );
147}
148
149export interface CloudCliOAuthConfig {
150 readonly authorizationEndpoint: string;
151 readonly tokenEndpoint: string;
152 readonly clientId: string;
153 readonly redirectUri: string;
154 readonly scopes: typeof CLOUD_CLI_OAUTH_SCOPES;
155}
156
157export function makeCloudCliOAuthConfig({
158 clerkPublishableKeyFallback = buildTimeClerkPublishableKey,
159 clerkCliOAuthClientIdFallback = buildTimeClerkCliOAuthClientId,
160}: {
161 readonly clerkPublishableKeyFallback?: string;
162 readonly clerkCliOAuthClientIdFallback?: string;
163} = {}) {
164 return Config.all({
165 clerkPublishableKey: makePublicValueConfig(
166 "T3CODE_CLERK_PUBLISHABLE_KEY",
167 clerkPublishableKeyFallback,
168 ),
169 clientId: makePublicValueConfig(
170 "T3CODE_CLERK_CLI_OAUTH_CLIENT_ID",
171 clerkCliOAuthClientIdFallback,
172 ),
173 }).pipe(
174 Config.mapOrFail(({ clerkPublishableKey, clientId }) =>
175 Effect.try({
176 try: () => clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey),
177 catch: (cause) =>
178 new Config.ConfigError(
179 new ConfigProvider.SourceError({
180 message: "Failed to derive Clerk Frontend API URL from the publishable key.",
181 cause,
182 }),
183 ),
184 }).pipe(
185 Effect.map(
186 (clerkFrontendApiUrl) =>
187 ({
188 authorizationEndpoint: `${clerkFrontendApiUrl}/oauth/authorize`,
189 tokenEndpoint: `${clerkFrontendApiUrl}/oauth/token`,
190 clientId,
191 redirectUri: CLOUD_CLI_OAUTH_REDIRECT_URI,
192 scopes: CLOUD_CLI_OAUTH_SCOPES,
193 }) satisfies CloudCliOAuthConfig,
194 ),
195 ),
196 ),
197 );
198}
199
200export const cloudCliOAuthConfig = makeCloudCliOAuthConfig();
201
202export const hasCloudPublicConfig = Boolean(
203 (normalizeSecureRelayUrl(process.env.T3CODE_RELAY_URL ?? "") ?? buildTimeRelayUrl) &&
204 (process.env.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() || buildTimeClerkPublishableKey) &&
205 (process.env.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || buildTimeClerkCliOAuthClientId),
206);