Skip to content

Commit f607dde

Browse files
committed
reuse logic within tanstack-start
1 parent 251a1ed commit f607dde

File tree

5 files changed

+17
-50
lines changed

5 files changed

+17
-50
lines changed

packages/shared/src/getEnvVariable.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
type CloudflareEnv = { env: Record<string, string> };
22

3-
interface Context {
4-
[key: string]: unknown;
5-
cloudflare?: CloudflareEnv;
6-
}
7-
83
const hasCloudflareProxyContext = (context: any): context is { cloudflare: CloudflareEnv } => {
94
return !!context?.cloudflare?.env;
105
};
@@ -19,7 +14,7 @@ const hasCloudflareContext = (context: any): context is CloudflareEnv => {
1914
* @param context - Optional context object that may contain environment values
2015
* @returns The environment variable value or empty string if not found
2116
*/
22-
export const getEnvVariable = (name: string, context: Context | undefined): string => {
17+
export const getEnvVariable = <T = unknown>(name: string, context?: T): string => {
2318
// Node envs
2419
if (typeof process !== 'undefined' && process.env && typeof process.env[name] === 'string') {
2520
return process.env[name];
@@ -41,8 +36,8 @@ export const getEnvVariable = (name: string, context: Context | undefined): stri
4136
}
4237

4338
// Check whether the value exists in the context object directly
44-
if (context && typeof context[name] === 'string') {
45-
return context[name];
39+
if (context && typeof context[name as keyof typeof context] === 'string') {
40+
return context[name as keyof typeof context] as string;
4641
}
4742

4843
// Cloudflare workers

packages/tanstack-start/src/server/constants.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';
2+
import { getEnvVariable } from '@clerk/shared/getEnvVariable';
23
import { getEvent } from 'vinxi/http';
34

4-
import { getEnvVariable, getPublicEnvVariables } from '../utils/env';
5+
import { getPublicEnvVariables } from '../utils/env';
56

67
export const commonEnvs = () => {
78
const event = getEvent();
@@ -21,16 +22,16 @@ export const commonEnvs = () => {
2122
TELEMETRY_DEBUG: publicEnvs.telemetryDebug,
2223

2324
// Server-only environment variables
24-
API_VERSION: getEnvVariable('CLERK_API_VERSION', 'v1', event),
25-
SECRET_KEY: getEnvVariable('CLERK_SECRET_KEY', '', event),
26-
ENCRYPTION_KEY: getEnvVariable('CLERK_ENCRYPTION_KEY', '', event),
27-
CLERK_JWT_KEY: getEnvVariable('CLERK_JWT_KEY', '', event),
28-
API_URL: getEnvVariable('CLERK_API_URL', '', event) || apiUrlFromPublishableKey(publicEnvs.publishableKey),
25+
API_VERSION: getEnvVariable('CLERK_API_VERSION', event) || 'v1',
26+
SECRET_KEY: getEnvVariable('CLERK_SECRET_KEY', event),
27+
ENCRYPTION_KEY: getEnvVariable('CLERK_ENCRYPTION_KEY', event),
28+
CLERK_JWT_KEY: getEnvVariable('CLERK_JWT_KEY', event),
29+
API_URL: getEnvVariable('CLERK_API_URL', event) || apiUrlFromPublishableKey(publicEnvs.publishableKey),
2930

3031
SDK_METADATA: {
3132
name: PACKAGE_NAME,
3233
version: PACKAGE_VERSION,
33-
environment: getEnvVariable('NODE_ENV', '', event),
34+
environment: getEnvVariable('NODE_ENV', event),
3435
},
3536
} as const;
3637
};

packages/tanstack-start/src/server/loadOptions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { createClerkRequest } from '@clerk/backend/internal';
22
import { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';
3+
import { getEnvVariable } from '@clerk/shared/getEnvVariable';
34
import { isDevelopmentFromSecretKey } from '@clerk/shared/keys';
45
import { isHttpOrHttps, isProxyUrlRelative } from '@clerk/shared/proxy';
56
import { handleValueOrFn } from '@clerk/shared/utils';
67
import { getEvent } from 'vinxi/http';
78

89
import { errorThrower } from '../utils';
9-
import { getEnvVariable, getPublicEnvVariables } from '../utils/env';
10+
import { getPublicEnvVariables } from '../utils/env';
1011
import { commonEnvs } from './constants';
1112
import type { LoaderOptions } from './types';
1213
import { patchRequest } from './utils';
@@ -18,7 +19,7 @@ export const loadOptions = (request: Request, overrides: LoaderOptions = {}) =>
1819
const secretKey = overrides.secretKey || commonEnv.SECRET_KEY;
1920
const publishableKey = overrides.publishableKey || commonEnv.PUBLISHABLE_KEY;
2021
const jwtKey = overrides.jwtKey || commonEnv.CLERK_JWT_KEY;
21-
const apiUrl = getEnvVariable('CLERK_API_URL', '', event) || apiUrlFromPublishableKey(publishableKey);
22+
const apiUrl = getEnvVariable('CLERK_API_URL', event) || apiUrlFromPublishableKey(publishableKey);
2223
const domain = handleValueOrFn(overrides.domain, new URL(request.url)) || commonEnv.DOMAIN;
2324
const isSatellite = handleValueOrFn(overrides.isSatellite, new URL(request.url)) || commonEnv.IS_SATELLITE;
2425
const relativeOrAbsoluteProxyUrl = handleValueOrFn(overrides?.proxyUrl, clerkRequest.clerkUrl, commonEnv.PROXY_URL);

packages/tanstack-start/src/server/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { RequestState } from '@clerk/backend/internal';
22
import { debugRequestState } from '@clerk/backend/internal';
3+
import { getEnvVariable } from '@clerk/shared/getEnvVariable';
34
import { isTruthy } from '@clerk/shared/underscore';
45

5-
import { getEnvVariable } from '../../utils/env';
66
import type { AdditionalStateOptions } from '../types';
77

88
/**

packages/tanstack-start/src/utils/env.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,10 @@
1+
import { getEnvVariable } from '@clerk/shared/getEnvVariable';
12
import { isTruthy } from '@clerk/shared/underscore';
23
import type { HTTPEvent } from 'vinxi/http';
34

4-
/**
5-
*
6-
* Utility function to get env variables.
7-
*
8-
* @param name env variable name
9-
* @param defaultVaue default value to return if the env variable is not set
10-
* @param event - H3Event object for accessing runtime environment variables
11-
* @returns string
12-
*
13-
* @internal
14-
*/
15-
export const getEnvVariable = (name: string, defaultValue: string = '', event?: HTTPEvent) => {
16-
// Cloudflare context check
17-
const cfValue = event?.context?.cloudflare?.env[name];
18-
if (cfValue) {
19-
return cfValue;
20-
}
21-
22-
// Node envs
23-
if (typeof process !== 'undefined' && process.env && typeof process.env[name] === 'string') {
24-
return process.env[name];
25-
}
26-
27-
// Vite specific envs
28-
if (typeof import.meta !== 'undefined' && import.meta.env && typeof import.meta.env[name] === 'string') {
29-
return import.meta.env[name];
30-
}
31-
32-
return defaultValue;
33-
};
34-
355
export const getPublicEnvVariables = (event?: HTTPEvent) => {
366
const getValue = (name: string): string => {
37-
return getEnvVariable(`VITE_${name}`, '', event) || getEnvVariable(name, '', event);
7+
return getEnvVariable(`VITE_${name}`, event) || getEnvVariable(name, event);
388
};
399

4010
return {

0 commit comments

Comments
 (0)