@matthew-hre/env 0.3.1 → 0.3.2
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 +2 -2
- package/dist/index.cjs +10 -7
- package/dist/index.js +10 -7
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ For NextJS projects, you can separate client and server environment variables fo
|
|
|
19
19
|
```ts
|
|
20
20
|
// lib/env.ts
|
|
21
21
|
import { loadEnv } from "@matthew-hre/env";
|
|
22
|
-
import
|
|
22
|
+
import * as z from "zod";
|
|
23
23
|
|
|
24
24
|
const schema = {
|
|
25
25
|
server: z.object({
|
|
@@ -47,7 +47,7 @@ For simpler projects or backward compatibility, you can still use the original s
|
|
|
47
47
|
```ts
|
|
48
48
|
import { loadEnv } from "@matthew-hre/env";
|
|
49
49
|
// lib/env.ts
|
|
50
|
-
import
|
|
50
|
+
import * as z from "zod";
|
|
51
51
|
|
|
52
52
|
const schema = z.object({
|
|
53
53
|
NODE_ENV: z.string(),
|
package/dist/index.cjs
CHANGED
|
@@ -34,9 +34,6 @@ __export(index_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(index_exports);
|
|
36
36
|
|
|
37
|
-
// src/core/load-env.ts
|
|
38
|
-
var import_zod = require("zod");
|
|
39
|
-
|
|
40
37
|
// src/core/error-handling.ts
|
|
41
38
|
var import_picocolors = __toESM(require("picocolors"), 1);
|
|
42
39
|
function handleClientServerErrors(errors, options) {
|
|
@@ -68,6 +65,12 @@ function handleSingleSchemaError(error) {
|
|
|
68
65
|
}
|
|
69
66
|
|
|
70
67
|
// src/core/load-env.ts
|
|
68
|
+
function isZodError(err) {
|
|
69
|
+
return err !== null && typeof err === "object" && "issues" in err && Array.isArray(err.issues);
|
|
70
|
+
}
|
|
71
|
+
function isZodObject(schema) {
|
|
72
|
+
return schema !== null && typeof schema === "object" && "parse" in schema;
|
|
73
|
+
}
|
|
71
74
|
var defaultOptions = {
|
|
72
75
|
exitOnError: true
|
|
73
76
|
};
|
|
@@ -79,7 +82,7 @@ function loadEnv(schema, env = process.env, options = defaultOptions) {
|
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
84
|
function isClientServerSchema(schema) {
|
|
82
|
-
return typeof schema === "object" && schema !== null && "server" in schema && "client" in schema && schema.server
|
|
85
|
+
return typeof schema === "object" && schema !== null && "server" in schema && "client" in schema && isZodObject(schema.server) && isZodObject(schema.client);
|
|
83
86
|
}
|
|
84
87
|
function parseClientServerSchema(schema, env, options) {
|
|
85
88
|
const errors = [];
|
|
@@ -90,7 +93,7 @@ function parseClientServerSchema(schema, env, options) {
|
|
|
90
93
|
try {
|
|
91
94
|
serverEnv = schema.server.parse(env);
|
|
92
95
|
} catch (err) {
|
|
93
|
-
if (err
|
|
96
|
+
if (isZodError(err)) {
|
|
94
97
|
errors.push({ context: "server", error: err });
|
|
95
98
|
} else {
|
|
96
99
|
throw err;
|
|
@@ -109,7 +112,7 @@ function parseClientServerSchema(schema, env, options) {
|
|
|
109
112
|
try {
|
|
110
113
|
clientEnv = schema.client.parse(clientEnv_variables);
|
|
111
114
|
} catch (err) {
|
|
112
|
-
if (err
|
|
115
|
+
if (isZodError(err)) {
|
|
113
116
|
errors.push({ context: "client", error: err });
|
|
114
117
|
} else {
|
|
115
118
|
throw err;
|
|
@@ -124,7 +127,7 @@ function parseSingleSchema(schema, env, options) {
|
|
|
124
127
|
try {
|
|
125
128
|
return schema.parse(env);
|
|
126
129
|
} catch (err) {
|
|
127
|
-
if (err
|
|
130
|
+
if (isZodError(err)) {
|
|
128
131
|
handleSingleSchemaError(err);
|
|
129
132
|
} else {
|
|
130
133
|
console.error("Unexpected error while parsing env:", err);
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// src/core/load-env.ts
|
|
2
|
-
import { ZodError, ZodObject } from "zod";
|
|
3
|
-
|
|
4
1
|
// src/core/error-handling.ts
|
|
5
2
|
import pc from "picocolors";
|
|
6
3
|
function handleClientServerErrors(errors, options) {
|
|
@@ -32,6 +29,12 @@ function handleSingleSchemaError(error) {
|
|
|
32
29
|
}
|
|
33
30
|
|
|
34
31
|
// src/core/load-env.ts
|
|
32
|
+
function isZodError(err) {
|
|
33
|
+
return err !== null && typeof err === "object" && "issues" in err && Array.isArray(err.issues);
|
|
34
|
+
}
|
|
35
|
+
function isZodObject(schema) {
|
|
36
|
+
return schema !== null && typeof schema === "object" && "parse" in schema;
|
|
37
|
+
}
|
|
35
38
|
var defaultOptions = {
|
|
36
39
|
exitOnError: true
|
|
37
40
|
};
|
|
@@ -43,7 +46,7 @@ function loadEnv(schema, env = process.env, options = defaultOptions) {
|
|
|
43
46
|
}
|
|
44
47
|
}
|
|
45
48
|
function isClientServerSchema(schema) {
|
|
46
|
-
return typeof schema === "object" && schema !== null && "server" in schema && "client" in schema && schema.server
|
|
49
|
+
return typeof schema === "object" && schema !== null && "server" in schema && "client" in schema && isZodObject(schema.server) && isZodObject(schema.client);
|
|
47
50
|
}
|
|
48
51
|
function parseClientServerSchema(schema, env, options) {
|
|
49
52
|
const errors = [];
|
|
@@ -54,7 +57,7 @@ function parseClientServerSchema(schema, env, options) {
|
|
|
54
57
|
try {
|
|
55
58
|
serverEnv = schema.server.parse(env);
|
|
56
59
|
} catch (err) {
|
|
57
|
-
if (err
|
|
60
|
+
if (isZodError(err)) {
|
|
58
61
|
errors.push({ context: "server", error: err });
|
|
59
62
|
} else {
|
|
60
63
|
throw err;
|
|
@@ -73,7 +76,7 @@ function parseClientServerSchema(schema, env, options) {
|
|
|
73
76
|
try {
|
|
74
77
|
clientEnv = schema.client.parse(clientEnv_variables);
|
|
75
78
|
} catch (err) {
|
|
76
|
-
if (err
|
|
79
|
+
if (isZodError(err)) {
|
|
77
80
|
errors.push({ context: "client", error: err });
|
|
78
81
|
} else {
|
|
79
82
|
throw err;
|
|
@@ -88,7 +91,7 @@ function parseSingleSchema(schema, env, options) {
|
|
|
88
91
|
try {
|
|
89
92
|
return schema.parse(env);
|
|
90
93
|
} catch (err) {
|
|
91
|
-
if (err
|
|
94
|
+
if (isZodError(err)) {
|
|
92
95
|
handleSingleSchemaError(err);
|
|
93
96
|
} else {
|
|
94
97
|
console.error("Unexpected error while parsing env:", err);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matthew-hre/env",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.2",
|
|
5
5
|
"description": "Type safe environment variable validation for NextJS projects",
|
|
6
6
|
"author": "Matthew Hrehirchuk",
|
|
7
7
|
"license": "MIT",
|
|
@@ -24,9 +24,11 @@
|
|
|
24
24
|
"lint": "eslint .",
|
|
25
25
|
"lint:fix": "eslint ."
|
|
26
26
|
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"zod": "^4.0.0"
|
|
29
|
+
},
|
|
27
30
|
"dependencies": {
|
|
28
|
-
"picocolors": "^1.1.1"
|
|
29
|
-
"zod": "^4.1.9"
|
|
31
|
+
"picocolors": "^1.1.1"
|
|
30
32
|
},
|
|
31
33
|
"devDependencies": {
|
|
32
34
|
"@antfu/eslint-config": "^5.4.1",
|
|
@@ -39,7 +41,8 @@
|
|
|
39
41
|
"tsup": "^8.5.0",
|
|
40
42
|
"typescript": "^5.9.2",
|
|
41
43
|
"typescript-eslint": "^8.44.0",
|
|
42
|
-
"vitest": "^3.2.4"
|
|
44
|
+
"vitest": "^3.2.4",
|
|
45
|
+
"zod": "^4.1.9"
|
|
43
46
|
},
|
|
44
47
|
"publishConfig": {
|
|
45
48
|
"access": "public"
|