env.macro
Problem
Sometimes you need to use env variables in JS. For example, to pass API keys for third-party services, and sometimes you need those keys only in production, but not in developement and you want to have check that key was passed in production build.
We can solve this problem with macros easily.
Basic usage
import env from "env.macro";
const MY_VAR = e("MY_VAR");
Will be converted to (assuming process.env.MY_VAR === "abc"
)
const MY_VAR = "abc";
Type checking and type conversion
import env from "env.macro";
const MY_VAR = e("MY_VAR", x => {
const result = parseInt(x, 10);
if (isNaN(result)) {
throw new Error("Integer expected");
}
return result;
});
Credits
Code boilerpalte based on pveyes/raw.macro.
License
MIT