-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(shared,react-router,tanstack-start): Create shared environment …
…variable retrieval function (#4985)
- Loading branch information
1 parent
6ce705c
commit f41081c
Showing
13 changed files
with
120 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
"@clerk/shared": minor | ||
--- | ||
|
||
Introduce unified environment variable handling across all supported platforms | ||
|
||
Usage: | ||
|
||
```ts | ||
import { getEnvVariable } from '@clerk/shared/getEnvVariable' | ||
|
||
const publishableKey = getEnvVariable('CLERK_PUBLISHABLE_KEY') | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,7 @@ | |
"object", | ||
"oauth", | ||
"web3", | ||
"getEnvVariable", | ||
"pathMatcher" | ||
], | ||
"scripts": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
wobsoriano
Author
Member
|
||
external: ['react', 'react-dom'], | ||
esbuildPlugins: [WebWorkerMinifyPlugin as any], | ||
define: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@wobsoriano This broke everyone using an es5 browser (ios 12 for instance) because of optional chaining, since version 5.52.2 of clerk-js. I can confirm that using 5.52.1 is working fine and it had no optional chaining in the built javascript.