diff --git a/packages/tanstack-start/src/server/clerkClient.ts b/packages/tanstack-start/src/server/clerkClient.ts index b1f32bad05..e28b603cca 100644 --- a/packages/tanstack-start/src/server/clerkClient.ts +++ b/packages/tanstack-start/src/server/clerkClient.ts @@ -1,34 +1,25 @@ import { createClerkClient } from '@clerk/backend'; -import { - API_URL, - API_VERSION, - DOMAIN, - IS_SATELLITE, - PROXY_URL, - PUBLISHABLE_KEY, - SDK_METADATA, - SECRET_KEY, - TELEMETRY_DEBUG, - TELEMETRY_DISABLED, -} from './constants'; +import { commonEnvs } from './constants'; -const clerkClient: typeof createClerkClient = options => - createClerkClient({ - secretKey: SECRET_KEY, - publishableKey: PUBLISHABLE_KEY, - apiUrl: API_URL, - apiVersion: API_VERSION, +const clerkClient: typeof createClerkClient = options => { + const commonEnv = commonEnvs(); + return createClerkClient({ + secretKey: commonEnv.SECRET_KEY, + publishableKey: commonEnv.PUBLISHABLE_KEY, + apiUrl: commonEnv.API_URL, + apiVersion: commonEnv.API_VERSION, userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`, - proxyUrl: PROXY_URL, - domain: DOMAIN, - isSatellite: IS_SATELLITE, - sdkMetadata: SDK_METADATA, + proxyUrl: commonEnv.PROXY_URL, + domain: commonEnv.DOMAIN, + isSatellite: commonEnv.IS_SATELLITE, + sdkMetadata: commonEnv.SDK_METADATA, telemetry: { - disabled: TELEMETRY_DISABLED, - debug: TELEMETRY_DEBUG, + disabled: commonEnv.TELEMETRY_DISABLED, + debug: commonEnv.TELEMETRY_DEBUG, }, ...options, }); +}; export { clerkClient }; diff --git a/packages/tanstack-start/src/server/constants.ts b/packages/tanstack-start/src/server/constants.ts index cf4b2789a3..05e87615ec 100644 --- a/packages/tanstack-start/src/server/constants.ts +++ b/packages/tanstack-start/src/server/constants.ts @@ -2,24 +2,27 @@ import { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey import { getEnvVariable, getPublicEnvVariables } from '../utils/env'; -export const CLERK_JS_VERSION = getPublicEnvVariables().clerkJsVersion || ''; -export const CLERK_JS_URL = getPublicEnvVariables().clerkJsUrl || ''; -export const API_VERSION = getEnvVariable('CLERK_API_VERSION') || 'v1'; -export const SECRET_KEY = getEnvVariable('CLERK_SECRET_KEY') || ''; -export const PUBLISHABLE_KEY = getPublicEnvVariables().publishableKey || ''; -export const ENCRYPTION_KEY = getEnvVariable('CLERK_ENCRYPTION_KEY') || ''; -export const API_URL = getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(PUBLISHABLE_KEY); -export const DOMAIN = getPublicEnvVariables().domain || ''; -export const PROXY_URL = getPublicEnvVariables().proxyUrl || ''; -export const CLERK_JWT_KEY = getEnvVariable('CLERK_JWT_KEY') || ''; -export const IS_SATELLITE = getPublicEnvVariables().isSatellite || false; -export const SIGN_IN_URL = getPublicEnvVariables().signInUrl || ''; -export const SIGN_UP_URL = getPublicEnvVariables().signUpUrl || ''; -export const SDK_METADATA = { - name: PACKAGE_NAME, - version: PACKAGE_VERSION, - environment: getEnvVariable('NODE_ENV'), -}; - -export const TELEMETRY_DISABLED = getPublicEnvVariables().telemetryDisabled; -export const TELEMETRY_DEBUG = getPublicEnvVariables().telemetryDebug; +// @ts-expect-error - TODO: Improve types +export const commonEnvs = () => + ({ + CLERK_JS_VERSION: getPublicEnvVariables().clerkJsVersion || '', + CLERK_JS_URL: getPublicEnvVariables().clerkJsUrl || '', + API_VERSION: getEnvVariable('CLERK_API_VERSION') || 'v1', + SECRET_KEY: getEnvVariable('CLERK_SECRET_KEY') || '', + PUBLISHABLE_KEY: getPublicEnvVariables().publishableKey || '', + ENCRYPTION_KEY: getEnvVariable('CLERK_ENCRYPTION_KEY') || '', + API_URL: getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(commonEnvs().PUBLISHABLE_KEY), + DOMAIN: getPublicEnvVariables().domain || '', + PROXY_URL: getPublicEnvVariables().proxyUrl || '', + CLERK_JWT_KEY: getEnvVariable('CLERK_JWT_KEY') || '', + IS_SATELLITE: getPublicEnvVariables().isSatellite || false, + SIGN_IN_URL: getPublicEnvVariables().signInUrl || '', + SIGN_UP_URL: getPublicEnvVariables().signUpUrl || '', + SDK_METADATA: { + name: PACKAGE_NAME, + version: PACKAGE_VERSION, + environment: getEnvVariable('NODE_ENV'), + }, + TELEMETRY_DISABLED: getPublicEnvVariables().telemetryDisabled, + TELEMETRY_DEBUG: getPublicEnvVariables().telemetryDebug, + }) as const; diff --git a/packages/tanstack-start/src/server/loadOptions.ts b/packages/tanstack-start/src/server/loadOptions.ts index b20f392930..fc76bc9752 100644 --- a/packages/tanstack-start/src/server/loadOptions.ts +++ b/packages/tanstack-start/src/server/loadOptions.ts @@ -6,31 +6,22 @@ import { handleValueOrFn } from '@clerk/shared/utils'; import { errorThrower } from '../utils'; import { getEnvVariable, getPublicEnvVariables } from '../utils/env'; -import { - CLERK_JWT_KEY, - DOMAIN, - IS_SATELLITE, - PROXY_URL, - PUBLISHABLE_KEY, - SECRET_KEY, - SIGN_IN_URL, - SIGN_UP_URL, -} from './constants'; +import { commonEnvs } from './constants'; import type { LoaderOptions } from './types'; import { patchRequest } from './utils'; export const loadOptions = (request: Request, overrides: LoaderOptions = {}) => { const clerkRequest = createClerkRequest(patchRequest(request)); - - const secretKey = overrides.secretKey || SECRET_KEY; - const publishableKey = overrides.publishableKey || PUBLISHABLE_KEY; - const jwtKey = overrides.jwtKey || CLERK_JWT_KEY; + const commonEnv = commonEnvs(); + const secretKey = overrides.secretKey || commonEnv.SECRET_KEY; + const publishableKey = overrides.publishableKey || commonEnv.PUBLISHABLE_KEY; + const jwtKey = overrides.jwtKey || commonEnv.CLERK_JWT_KEY; const apiUrl = getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(publishableKey); - const domain = handleValueOrFn(overrides.domain, new URL(request.url)) || DOMAIN; - const isSatellite = handleValueOrFn(overrides.isSatellite, new URL(request.url)) || IS_SATELLITE; - const relativeOrAbsoluteProxyUrl = handleValueOrFn(overrides?.proxyUrl, clerkRequest.clerkUrl, PROXY_URL); - const signInUrl = overrides.signInUrl || SIGN_IN_URL; - const signUpUrl = overrides.signUpUrl || SIGN_UP_URL; + const domain = handleValueOrFn(overrides.domain, new URL(request.url)) || commonEnv.DOMAIN; + const isSatellite = handleValueOrFn(overrides.isSatellite, new URL(request.url)) || commonEnv.IS_SATELLITE; + const relativeOrAbsoluteProxyUrl = handleValueOrFn(overrides?.proxyUrl, clerkRequest.clerkUrl, commonEnv.PROXY_URL); + const signInUrl = overrides.signInUrl || commonEnv.SIGN_IN_URL; + const signUpUrl = overrides.signUpUrl || commonEnv.SIGN_UP_URL; const afterSignInUrl = overrides.afterSignInUrl || getPublicEnvVariables().afterSignInUrl; const afterSignUpUrl = overrides.afterSignUpUrl || getPublicEnvVariables().afterSignUpUrl;