@matthew-hre/env 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,3 +3,69 @@
3
3
  Type safe environment variable validation for NextJS projects.
4
4
 
5
5
  > This package is mostly for my own projects, and I wouldn't recommend using it yourself in its current state. It may be improved in the future. May.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @matthew-hre/env
11
+ ```
12
+
13
+ ### env.ts
14
+
15
+ First, create an `env.ts` file in your project (e.g. in the `lib` folder):
16
+
17
+ ```ts
18
+ // lib/env.ts
19
+ import { z } from "zod";
20
+ import { loadEnv } from "@matthew-hre/env";
21
+
22
+ const schema = z.object({
23
+ NODE_ENV: z.string(),
24
+ NEXT_PUBLIC_API_URL: z.url(),
25
+ });
26
+
27
+ // optionally, export the schema for use elsewhere
28
+ export type EnvSchema = z.infer<typeof schema>;
29
+
30
+ export const env = loadEnv(schema);
31
+ ```
32
+
33
+ ### next.config.js
34
+
35
+ Then, import and use the `env` object in your `next.config.js`:
36
+
37
+ ```js
38
+ // next.config.js
39
+ import type { NextConfig } from "next";
40
+ import { env } from "src/lib/env"; // or wherever your env.ts file is
41
+
42
+ const nextConfig: NextConfig = {
43
+ ...
44
+ };
45
+
46
+ export default nextConfig;
47
+ ```
48
+
49
+ ## Usage
50
+
51
+ You can now use the `env` object anywhere in your NextJS project:
52
+
53
+ ```ts
54
+ import { env } from "src/lib/env"; // or wherever your env.ts file is
55
+
56
+ console.log(env.NEXT_PUBLIC_API_URL);
57
+ ```
58
+
59
+ This is type safe and will give you autocompletion based on your schema. Additionally, errors will be thrown at runtime if the environment variables do not match the schema.
60
+
61
+ ## Configuration
62
+
63
+ The `loadEnv` function accepts two optional parameters: `env` and `options`.
64
+
65
+ - `env`: An object representing the environment variables to validate. Defaults to `process.env`.
66
+ - `options`: An object containing options to customize the behavior of the `loadEnv` function.
67
+ - `exitOnError`: If set to `true`, the process will exit with a non-zero status code if the environment variables are invalid. Defaults to `true`.
68
+
69
+ ## License
70
+
71
+ Matthew Hrehirchuk © 2025. Licensed under the MIT License.
package/dist/index.cjs CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -23,22 +33,32 @@ __export(index_exports, {
23
33
  loadEnv: () => loadEnv
24
34
  });
25
35
  module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/core/load-env.ts
26
38
  var import_zod = require("zod");
27
- function loadEnv(schema, env = process.env) {
39
+ var import_picocolors = __toESM(require("picocolors"), 1);
40
+ var defaultOptions = {
41
+ exitOnError: true
42
+ };
43
+ function loadEnv(schema, env = process.env, options = defaultOptions) {
28
44
  try {
29
45
  return schema.parse(env);
30
46
  } catch (err) {
31
47
  if (err instanceof import_zod.ZodError) {
32
- let message = "Invalid environment variables:\n";
48
+ let message = import_picocolors.default.red("Invalid environment variables:\n");
33
49
  err.issues.forEach((issue) => {
34
- message += ` - ${String(issue.path[0])} (${issue.message})
50
+ const name = String(issue.path[0]);
51
+ message += ` - ${import_picocolors.default.bold(name)} ${import_picocolors.default.dim("(" + issue.message + ")")}
35
52
  `;
36
53
  });
37
54
  console.error(message);
38
55
  } else {
39
56
  console.error("Unexpected error while parsing env:", err);
40
57
  }
41
- process.exit(1);
58
+ if (options.exitOnError) {
59
+ process.exit(1);
60
+ }
61
+ throw err;
42
62
  }
43
63
  }
44
64
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.d.cts CHANGED
@@ -1,5 +1,8 @@
1
1
  import { ZodRawShape, ZodObject, z } from 'zod';
2
2
 
3
- declare function loadEnv<T extends ZodRawShape>(schema: ZodObject<T>, env?: Record<string, string | undefined>): z.output<typeof schema>;
3
+ interface LoadEnvOptions {
4
+ exitOnError?: boolean;
5
+ }
6
+ declare function loadEnv<T extends ZodRawShape>(schema: ZodObject<T>, env?: Record<string, string | undefined>, options?: LoadEnvOptions): z.output<typeof schema>;
4
7
 
5
8
  export { loadEnv };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import { ZodRawShape, ZodObject, z } from 'zod';
2
2
 
3
- declare function loadEnv<T extends ZodRawShape>(schema: ZodObject<T>, env?: Record<string, string | undefined>): z.output<typeof schema>;
3
+ interface LoadEnvOptions {
4
+ exitOnError?: boolean;
5
+ }
6
+ declare function loadEnv<T extends ZodRawShape>(schema: ZodObject<T>, env?: Record<string, string | undefined>, options?: LoadEnvOptions): z.output<typeof schema>;
4
7
 
5
8
  export { loadEnv };
package/dist/index.js CHANGED
@@ -1,20 +1,28 @@
1
- // src/index.ts
1
+ // src/core/load-env.ts
2
2
  import { ZodError } from "zod";
3
- function loadEnv(schema, env = process.env) {
3
+ import pc from "picocolors";
4
+ var defaultOptions = {
5
+ exitOnError: true
6
+ };
7
+ function loadEnv(schema, env = process.env, options = defaultOptions) {
4
8
  try {
5
9
  return schema.parse(env);
6
10
  } catch (err) {
7
11
  if (err instanceof ZodError) {
8
- let message = "Invalid environment variables:\n";
12
+ let message = pc.red("Invalid environment variables:\n");
9
13
  err.issues.forEach((issue) => {
10
- message += ` - ${String(issue.path[0])} (${issue.message})
14
+ const name = String(issue.path[0]);
15
+ message += ` - ${pc.bold(name)} ${pc.dim("(" + issue.message + ")")}
11
16
  `;
12
17
  });
13
18
  console.error(message);
14
19
  } else {
15
20
  console.error("Unexpected error while parsing env:", err);
16
21
  }
17
- process.exit(1);
22
+ if (options.exitOnError) {
23
+ process.exit(1);
24
+ }
25
+ throw err;
18
26
  }
19
27
  }
20
28
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matthew-hre/env",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Type safe environment variable validation for NextJS projects",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -10,7 +10,8 @@
10
10
  ],
11
11
  "scripts": {
12
12
  "build": "tsup src/index.ts --format esm,cjs --dts",
13
- "dev": "tsup src/index.ts --watch --dts"
13
+ "dev": "tsup src/index.ts --watch --dts",
14
+ "test": "vitest"
14
15
  },
15
16
  "keywords": [
16
17
  "nextjs",
@@ -22,12 +23,14 @@
22
23
  "author": "Matthew Hrehirchuk",
23
24
  "license": "MIT",
24
25
  "dependencies": {
26
+ "picocolors": "^1.1.1",
25
27
  "zod": "^4.1.9"
26
28
  },
27
29
  "devDependencies": {
28
30
  "@types/node": "^24.5.2",
29
31
  "tsup": "^8.5.0",
30
- "typescript": "^5.9.2"
32
+ "typescript": "^5.9.2",
33
+ "vitest": "^3.2.4"
31
34
  },
32
35
  "publishConfig": {
33
36
  "access": "public"