-
Notifications
You must be signed in to change notification settings - Fork 374
chore(shared,react-router,tanstack-start): Create shared environment variable retrieval function #4985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(shared,react-router,tanstack-start): Create shared environment variable retrieval function #4985
Changes from all commits
23db855
f756662
029dce0
7f808fa
f1ce19b
9f486ca
a876fa7
2a1bd48
d85c375
78804b9
a7cb5a9
585ff10
b8f584a
ca4aafa
fa68dae
0cb3adb
58c1fdd
df3533b
5152f78
7b44219
d09261d
2ea0a3e
f8305f6
15b7417
8431042
1f6bff3
db1427e
01ba102
71ff504
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
"@clerk/shared": minor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the introduction of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems fine 👍 |
||
--- | ||
|
||
Introduce unified environment variable handling across all supported platforms | ||
|
||
Usage: | ||
|
||
```ts | ||
import { getEnvVariable } from '@clerk/shared/getEnvVariable' | ||
|
||
const publishableKey = getEnvVariable('CLERK_PUBLISHABLE_KEY') | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@clerk/tanstack-start': patch | ||
'@clerk/react-router': patch | ||
--- | ||
|
||
Internal changes to use new `getEnvVariable` utility from `@clerk/shared` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,7 @@ | |
"object", | ||
"oauth", | ||
"web3", | ||
"getEnvVariable", | ||
"pathMatcher" | ||
], | ||
"scripts": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
type CloudflareEnv = { env: Record<string, string> }; | ||
|
||
const hasCloudflareProxyContext = (context: any): context is { cloudflare: CloudflareEnv } => { | ||
return !!context?.cloudflare?.env; | ||
}; | ||
|
||
const hasCloudflareContext = (context: any): context is CloudflareEnv => { | ||
return !!context?.env; | ||
}; | ||
|
||
/** | ||
* Retrieves an environment variable across runtime environments. | ||
* @param name - The environment variable name to retrieve | ||
* @param context - Optional context object that may contain environment values | ||
* @returns The environment variable value or empty string if not found | ||
*/ | ||
export const getEnvVariable = (name: string, context?: Record<string, any>): string => { | ||
// Node envs | ||
if (typeof process !== 'undefined' && process.env && typeof process.env[name] === 'string') { | ||
return process.env[name]; | ||
} | ||
|
||
// Vite specific | ||
if (typeof import.meta !== 'undefined' && import.meta.env && typeof import.meta.env[name] === 'string') { | ||
return import.meta.env[name]; | ||
} | ||
|
||
if (hasCloudflareProxyContext(context)) { | ||
return context.cloudflare.env[name] || ''; | ||
} | ||
|
||
// Cloudflare | ||
if (hasCloudflareContext(context)) { | ||
return context.env[name] || ''; | ||
} | ||
|
||
// Check whether the value exists in the context object directly | ||
if (context && typeof context[name] === 'string') { | ||
return context[name]; | ||
} | ||
|
||
// Cloudflare workers | ||
try { | ||
return globalThis[name as keyof typeof globalThis]; | ||
} catch { | ||
// This will raise an error in Cloudflare Pages | ||
} | ||
|
||
return ''; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ export default defineConfig(overrideOptions => { | |
minify: false, | ||
sourcemap: true, | ||
dts: true, | ||
target: 'es2020', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To support |
||
external: ['react', 'react-dom'], | ||
esbuildPlugins: [WebWorkerMinifyPlugin as any], | ||
define: { | ||
|
Uh oh!
There was an error while loading. Please reload this page.