🐛 Improve parse runtime env reading function
This commit is contained in:
10
packages/env/env.ts
vendored
10
packages/env/env.ts
vendored
@@ -1,14 +1,6 @@
|
||||
import { createEnv } from '@t3-oss/env-nextjs'
|
||||
import { z } from 'zod'
|
||||
|
||||
declare const window: {
|
||||
__ENV: any
|
||||
}
|
||||
|
||||
const getRuntimeVariable = (key: string) => {
|
||||
if (typeof window === 'undefined') return process.env[key]
|
||||
return window.__ENV[key]
|
||||
}
|
||||
import { getRuntimeVariable } from './getRuntimeVariable'
|
||||
|
||||
const boolean = z.enum(['true', 'false']).transform((value) => value === 'true')
|
||||
|
||||
|
||||
10
packages/env/getRuntimeVariable.ts
vendored
Normal file
10
packages/env/getRuntimeVariable.ts
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
declare const window: {
|
||||
__ENV?: any
|
||||
}
|
||||
|
||||
const isBrowser = () => Boolean(typeof window !== 'undefined' && window.__ENV)
|
||||
|
||||
export const getRuntimeVariable = (key: string) => {
|
||||
if (isBrowser()) return window.__ENV[key]
|
||||
return process.env[key]
|
||||
}
|
||||
2
packages/env/package.json
vendored
2
packages/env/package.json
vendored
@@ -15,6 +15,6 @@
|
||||
"esbuild": "^0.19.1"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "esbuild env.ts --packages=external --outfile=dist/env.mjs"
|
||||
"build": "esbuild env.ts --bundle --packages=external --format=esm --outfile=dist/env.mjs"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user