abi
abi2y ago

Safety measures (statically, not runtime) for libraries that require environment variables?

Suppose I have a Deno library which expects certain environment variables to have been configured, otherwise a crash will happen. Does anyone know of a good way to somehow ensure at "compile-time" that whatever Deno application is using the library has configured these environment variables?
10 Replies
𝕒𝕣𝕤𝕙
but env vars will be read at runtime?
abi
abiOP2y ago
@arshxcx yes, i know, it's a weird question, not sure why i'm even asking... i figured maybe i've missed something, maybe one could do some cool stuff using dotenv, maybe something involving weird type inference? i don't even know man.
𝕒𝕣𝕤𝕙
is your dev environment the same as your deployment environment?
abi
abiOP2y ago
nah, not at all
𝕒𝕣𝕤𝕙
so you only want safety for the dev env
abi
abiOP2y ago
well, i guess it's technically only possible for dev env... unless i'm missing something. right now, i use a zod schema which gets fed Deno.env.toObject(), so it will crash at startup. i'm just thinking maybe there's something more clever.
𝕒𝕣𝕤𝕙
well yeah its only possible for dev env, the alternative would be a type system that can predict the future if you want the put in the effort, you could have a script that periodically writes Deno.env to a .ts file and exports it as an object with as const you could dynamically import the file and you would have the exact types with exact values
abi
abiOP2y ago
yeah, currently i'm experimenting with importing the environment configuration as a JSON import...
𝕒𝕣𝕤𝕙
yeah json works if you dont need to look at the values, json imports just give you keys
abi
abiOP2y ago
ah, good point i'll try your idea, thanks for your input

Did you find this page helpful?