@valentino.chiola/safe-vars
v1.0.4
Published
Wrapper for dotenv.config() that parses the result and sets the values in process.env
Downloads
6
Readme
Welcome to @valentino.chiola/safe-vars
, a wrapper for dotenv.config()
that parses the result and sets the values in process.env
Installation
To install use one of the following commands:
npm i @valentino.chiola/safe-vars
pnpm i @valentino.chiola/safe-vars
yarn add @valentino.chiola/safe-vars
Basic usage
import safeVars, { z } from "@valentino.chiola/safe-vars";
const schema = z.object({
VARIABLE1: z.string(),
VARIABLE2: z.string(),
BOOLEAN: z.coerce.boolean()
});
safeVars(schema)
console.log(process.env)
If in your .env
file you have declared VARIABLE1
and VARIABLE2
, it won't throw an error.
Note: any extra variables in your
.env
, that are not present in theschema
, will be removed. Note: if theBOOLEAN
variable is not defined,zod
will cast it tofalse
.
Options
inject
: If true, the variables will be injected in theprocess.env
object.Default: true
import safeVars, { z } from "@valentino.chiola/safe-vars";
const schema = z.object({
VARIABLE1: z.string(),
});
export const env = safeVars(schema, { inject: false })
console.log(env.VARIABLE1) // VARIABLE 1
console.log(process.env.VARIABLE1) // undefined
log
: If true, the variables will be logged in the console.Default: false
. Also can be an object with anonly
property which is an array of the variables that you want to log.Warning
: This option should be used for debugging purposes only.
import safeVars, { z } from "@valentino.chiola/safe-vars";
const schema = z.object({
VARIABLE1: z.string(),
});
safeVars(schema, { log: true })
or:
import safeVars, { z } from "@valentino.chiola/safe-vars";
const schema = z.object({
VARIABLE1: z.string(),
VARIABLE2: z.string(),
SUPER_SECRET: z.string()
});
safeVars(schema, {
log: {
only: ["VARIABLE1", "VARIABLE2"],
},
});
throw
: If false, it won't throw an error if the variables are not defined in the.env
file,but all the variables would be optional
.Default: true
import safeVars, { z } from "@valentino.chiola/safe-vars";
const schema = z.object({
VARIABLE1: z.string(),
});
safeVars(schema, { throw: false });
The type of env.VARIABLE1
will be string | undefined
onSuccess
: Callback function to be executed if all environment variables are successfully validated.
import safeVars, { z } from "@valentino.chiola/safe-vars";
const schema = z.object({
VARIABLE1: z.string(),
});
safeVars(schema, {
onSuccess: (env) => console.log("🚀 Envs loaded!"),
});
onError
: Callback function to be executed if there are errors in the validation.
import safeVars, { z } from "@valentino.chiola/safe-vars";
const schema = z.object({
VARIABLE1: z.string(),
});
safeVars(schema, {
onError: (errors) => console.error("❌ Error loading envs", errors),
});
dotenv
: An object that represents the options for thedotenv
package.path
: You can use thepath
key to determine witch.env
file should be parsed. This is useful for testing.
import safeVars, { z } from "@valentino.chiola/safe-vars"; const schema = z.object({ VARIABLE1: z.string(), }); safeVars(schema, { dotenv: { path: ".env.test", } });
Typed Envs
The are two ways to have your envs typed.
declare module
:
import safeVars, { z } from "@valentino.chiola/safe-vars";
const schema = z.object({
VARIABLE1: z.string(),
});
safeVars(schema);
declare global {
module NodeJS {
interface ProcessEnv extends z.infer<typeof schema> {}
}
}
return value
:
Instead of using process.env
you can use the safeVars
's return value.
const schema = z.object({
VARIABLE1: z.string(),
});
export const env = safeVars(schema, { inject: false });
The type of env.VARIABLE1
will be string
Contribution
Contributions are welcome! To contribute:
- Fork this repository.
- Create a branch with a meaningful description.
- Make the desired changes.
- Update the documentation if necessary.
- Open a Pull Request to the main branch.
If you find an issue or have a suggestion to improve the project, feel free to open an issue.
License
This project is licensed under the MIT License.