Open History to see the last commit
1import { clerkFrontendApiUrlFromPublishableKey } from "@t3tools/shared/relayAuth";
2import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
3import * as Config from "effect/Config";
4import * as Effect from "effect/Effect";
5import * as Option from "effect/Option";
6import * as Schema from "effect/Schema";
7import * as SchemaIssue from "effect/SchemaIssue";
8
9declare const __T3CODE_BUILD_RELAY_URL__: string | undefined;
10declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined;
11declare const __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: string | undefined;
12
13const CLOUD_CLI_OAUTH_REDIRECT_URI = "http://127.0.0.1:34338/callback";
14const CLOUD_CLI_OAUTH_SCOPES = ["openid", "profile", "email"] as const;
15
16function validateRelayUrl(value: string) {
17 const relayUrl = normalizeSecureRelayUrl(value);
18 return relayUrl === null
19 ? Effect.fail(
20 new Config.ConfigError(
21 new Schema.SchemaError(
22 new SchemaIssue.InvalidValue(Option.some(value), {
23 message: "Relay URL must be a secure absolute HTTPS origin.",
24 }),
25 ),
26 ),
27 )
28 : Effect.succeed(relayUrl);
29}
30
31function readBuildTimeValue(value: string | undefined): string {
32 return typeof value === "undefined" ? "" : value.trim();
33}
34
35export const buildTimeRelayUrl =
36 typeof __T3CODE_BUILD_RELAY_URL__ === "undefined"
37 ? ""
38 : (normalizeSecureRelayUrl(__T3CODE_BUILD_RELAY_URL__) ?? "");
39export const buildTimeClerkPublishableKey = readBuildTimeValue(
40 typeof __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__ === "undefined"
41 ? undefined
42 : __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__,
43);
44export const buildTimeClerkCliOAuthClientId = readBuildTimeValue(
45 typeof __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__ === "undefined"
46 ? undefined
47 : __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__,
48);
49
50export function makeRelayUrlConfig(fallback = buildTimeRelayUrl) {
51 const runtimeConfig = Config.nonEmptyString("T3CODE_RELAY_URL");
52 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
53 Config.mapOrFail(validateRelayUrl),
54 );
55}
56
57export const relayUrlConfig = makeRelayUrlConfig();
58
59function makePublicValueConfig(name: string, fallback: string) {
60 const runtimeConfig = Config.nonEmptyString(name);
61 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
62 Config.map((value) => value.trim()),
63 );
64}
65
66export interface CloudCliOAuthConfig {
67 readonly authorizationEndpoint: string;
68 readonly tokenEndpoint: string;
69 readonly clientId: string;
70 readonly redirectUri: string;
71 readonly scopes: typeof CLOUD_CLI_OAUTH_SCOPES;
72}
73
74export function makeCloudCliOAuthConfig({
75 clerkPublishableKeyFallback = buildTimeClerkPublishableKey,
76 clerkCliOAuthClientIdFallback = buildTimeClerkCliOAuthClientId,
77}: {
78 readonly clerkPublishableKeyFallback?: string;
79 readonly clerkCliOAuthClientIdFallback?: string;
80} = {}) {
81 return Config.all({
82 clerkPublishableKey: makePublicValueConfig(
83 "T3CODE_CLERK_PUBLISHABLE_KEY",
84 clerkPublishableKeyFallback,
85 ),
86 clientId: makePublicValueConfig(
87 "T3CODE_CLERK_CLI_OAUTH_CLIENT_ID",
88 clerkCliOAuthClientIdFallback,
89 ),
90 }).pipe(
91 Config.map(({ clerkPublishableKey, clientId }) => {
92 const clerkFrontendApiUrl = clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey);
93 return {
94 authorizationEndpoint: `${clerkFrontendApiUrl}/oauth/authorize`,
95 tokenEndpoint: `${clerkFrontendApiUrl}/oauth/token`,
96 clientId,
97 redirectUri: CLOUD_CLI_OAUTH_REDIRECT_URI,
98 scopes: CLOUD_CLI_OAUTH_SCOPES,
99 } satisfies CloudCliOAuthConfig;
100 }),
101 );
102}
103
104export const cloudCliOAuthConfig = makeCloudCliOAuthConfig();
105
106export const hasCloudPublicConfig = Boolean(
107 (normalizeSecureRelayUrl(process.env.T3CODE_RELAY_URL ?? "") ?? buildTimeRelayUrl) &&
108 (process.env.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() || buildTimeClerkPublishableKey) &&
109 (process.env.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || buildTimeClerkCliOAuthClientId),
110);