Files
web/env/server.mjs
2024-02-07 11:58:54 +01:00

34 lines
896 B
JavaScript

// @ts-check
/**
* This file is included in `/next.config.mjs` which ensures the app isn't built with invalid env vars.
* It has to be a `.mjs`-file to be imported there.
*/
import { serverSchema, serverEnv } from './schema.mjs'
import { env as clientEnv, formatErrors } from './client.mjs'
const _serverEnv = serverSchema.safeParse(serverEnv)
if (!_serverEnv.success) {
const msg = [
'❌ Invalid environment variables [Server]:\n',
...formatErrors(_serverEnv.error.format()),
].join('')
console.error(msg)
throw new Error(msg)
}
for (let key of Object.keys(_serverEnv.data)) {
const msg = []
if (key.startsWith('NEXT_PUBLIC_')) {
msg.push(
`❌ You are exposing a server-side env-variable: ${key}\n`
)
}
if (msg.length) {
console.warn(msg.join(''))
throw new Error(msg.join(''))
}
}
export const env = { ..._serverEnv.data, ...clientEnv }