arkenv 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/LICENSE +21 -0
- package/README.md +7 -0
- package/dist/index.cjs +119 -0
- package/dist/index.d.cts +34 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +86 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yam Borodetsky <https://yam.codes/>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# ArkEnv
|
|
2
|
+
|
|
3
|
+
Hello, developers! Welcome to the ArkEnv package codebase. This directory contains the core implementation of the ArkEnv library, which provides typesafe environment variable parsing and validation for Node.js.
|
|
4
|
+
|
|
5
|
+
For more information, please refer to the main [README](../../README.md) in the monorepo root.
|
|
6
|
+
|
|
7
|
+
Happy coding!
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
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
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
default: () => index_default,
|
|
34
|
+
defineEnv: () => defineEnv,
|
|
35
|
+
host: () => host,
|
|
36
|
+
port: () => port
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/define-env.ts
|
|
41
|
+
var define_env_exports = {};
|
|
42
|
+
__export(define_env_exports, {
|
|
43
|
+
defineEnv: () => defineEnv
|
|
44
|
+
});
|
|
45
|
+
var import_arktype = require("arktype");
|
|
46
|
+
|
|
47
|
+
// src/errors.ts
|
|
48
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
49
|
+
|
|
50
|
+
// src/utils.ts
|
|
51
|
+
var indent = (str, amt = 2, { dontDetectNewlines = false } = {}) => {
|
|
52
|
+
const detectNewlines = !dontDetectNewlines;
|
|
53
|
+
if (detectNewlines) {
|
|
54
|
+
return str.split("\n").map((line) => `${" ".repeat(amt)}${line}`).join("\n");
|
|
55
|
+
}
|
|
56
|
+
return `${" ".repeat(amt)}${str}`;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/errors.ts
|
|
60
|
+
var formatErrors = (errors) => Object.entries(errors.byPath).map(([path, error]) => {
|
|
61
|
+
const messageWithoutPath = error.message.startsWith(path) ? error.message.slice(path.length) : error.message;
|
|
62
|
+
const valueMatch = messageWithoutPath.match(/\(was "([^"]+)"\)/);
|
|
63
|
+
const formattedMessage = valueMatch ? messageWithoutPath.replace(
|
|
64
|
+
`(was "${valueMatch[1]}")`,
|
|
65
|
+
`(was ${import_chalk.default.cyan(`"${valueMatch[1]}"`)})`
|
|
66
|
+
) : messageWithoutPath;
|
|
67
|
+
return `${import_chalk.default.yellow(path)}${formattedMessage}`;
|
|
68
|
+
}).join("\n");
|
|
69
|
+
var ArkEnvError = class extends Error {
|
|
70
|
+
constructor(errors, message = "Errors found while validating environment variables") {
|
|
71
|
+
super(`${import_chalk.default.red(message)}
|
|
72
|
+
${indent(formatErrors(errors))}
|
|
73
|
+
`);
|
|
74
|
+
this.name = "ArkEnvError";
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/define-env.ts
|
|
79
|
+
var defineEnv = (def, env = process.env) => {
|
|
80
|
+
const schema = (0, import_arktype.type)(def);
|
|
81
|
+
const requiredEnvKeys = Object.keys(
|
|
82
|
+
def
|
|
83
|
+
);
|
|
84
|
+
const filteredEnvVars = Object.fromEntries(
|
|
85
|
+
Object.entries(env).filter(([key]) => requiredEnvKeys.includes(key))
|
|
86
|
+
);
|
|
87
|
+
const validatedEnv = schema(filteredEnvVars);
|
|
88
|
+
if (validatedEnv instanceof import_arktype.type.errors) {
|
|
89
|
+
throw new ArkEnvError(validatedEnv);
|
|
90
|
+
}
|
|
91
|
+
return validatedEnv;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// src/types.ts
|
|
95
|
+
var types_exports = {};
|
|
96
|
+
__export(types_exports, {
|
|
97
|
+
host: () => host,
|
|
98
|
+
port: () => port
|
|
99
|
+
});
|
|
100
|
+
var import_arktype2 = require("arktype");
|
|
101
|
+
var port = (0, import_arktype2.type)("string", "=>", (data, ctx) => {
|
|
102
|
+
const asNumber = Number.parseInt(data);
|
|
103
|
+
const isInteger = Number.isInteger(asNumber);
|
|
104
|
+
const isBetween = 0 <= asNumber && asNumber <= 65535;
|
|
105
|
+
if (!isInteger || !isBetween) {
|
|
106
|
+
ctx.mustBe("an integer between 0 and 65535");
|
|
107
|
+
}
|
|
108
|
+
return asNumber;
|
|
109
|
+
});
|
|
110
|
+
var host = (0, import_arktype2.type)("string.ip | 'localhost'");
|
|
111
|
+
|
|
112
|
+
// src/index.ts
|
|
113
|
+
var index_default = { ...define_env_exports, ...types_exports };
|
|
114
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
115
|
+
0 && (module.exports = {
|
|
116
|
+
defineEnv,
|
|
117
|
+
host,
|
|
118
|
+
port
|
|
119
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as arktype from 'arktype';
|
|
2
|
+
import { type, distill } from 'arktype';
|
|
3
|
+
import * as arktype_internal_methods_string_ts from 'arktype/internal/methods/string.ts';
|
|
4
|
+
import * as arktype_internal_methods_morph_ts from 'arktype/internal/methods/morph.ts';
|
|
5
|
+
import * as arktype_internal_attributes_ts from 'arktype/internal/attributes.ts';
|
|
6
|
+
|
|
7
|
+
type UserEnvironment = Record<string, string | undefined>;
|
|
8
|
+
type EnvSchema<T extends Record<string, string | undefined> = Record<string, string | undefined>> = type.validate<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Define an environment variable schema and validate it against a given environment (defaults to `process.env`)
|
|
11
|
+
* @param def - The environment variable schema
|
|
12
|
+
* @param env - The environment variables to validate, defaults to `process.env`
|
|
13
|
+
* @returns The validated environment variable schema
|
|
14
|
+
*/
|
|
15
|
+
declare const defineEnv: <const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: UserEnvironment) => distill.Out<type.infer<T>>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A `string` that can be parsed into a number between 0 and 65535
|
|
19
|
+
*/
|
|
20
|
+
declare const port: arktype_internal_methods_morph_ts.MorphType<(In: string) => arktype_internal_attributes_ts.Out<number>, {}>;
|
|
21
|
+
/**
|
|
22
|
+
* An IP address or `"localhost"`
|
|
23
|
+
*/
|
|
24
|
+
declare const host: arktype_internal_methods_string_ts.StringType<string, {}>;
|
|
25
|
+
|
|
26
|
+
declare const _default: {
|
|
27
|
+
port: arktype_internal_methods_morph_ts.MorphType<(In: string) => arktype_internal_attributes_ts.Out<number>, {}>;
|
|
28
|
+
host: arktype_internal_methods_string_ts.StringType<string, {}>;
|
|
29
|
+
defineEnv: <const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: {
|
|
30
|
+
[x: string]: string | undefined;
|
|
31
|
+
}) => arktype.distill.Out<arktype.type.infer<T>>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { type EnvSchema, _default as default, defineEnv, host, port };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as arktype from 'arktype';
|
|
2
|
+
import { type, distill } from 'arktype';
|
|
3
|
+
import * as arktype_internal_methods_string_ts from 'arktype/internal/methods/string.ts';
|
|
4
|
+
import * as arktype_internal_methods_morph_ts from 'arktype/internal/methods/morph.ts';
|
|
5
|
+
import * as arktype_internal_attributes_ts from 'arktype/internal/attributes.ts';
|
|
6
|
+
|
|
7
|
+
type UserEnvironment = Record<string, string | undefined>;
|
|
8
|
+
type EnvSchema<T extends Record<string, string | undefined> = Record<string, string | undefined>> = type.validate<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Define an environment variable schema and validate it against a given environment (defaults to `process.env`)
|
|
11
|
+
* @param def - The environment variable schema
|
|
12
|
+
* @param env - The environment variables to validate, defaults to `process.env`
|
|
13
|
+
* @returns The validated environment variable schema
|
|
14
|
+
*/
|
|
15
|
+
declare const defineEnv: <const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: UserEnvironment) => distill.Out<type.infer<T>>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A `string` that can be parsed into a number between 0 and 65535
|
|
19
|
+
*/
|
|
20
|
+
declare const port: arktype_internal_methods_morph_ts.MorphType<(In: string) => arktype_internal_attributes_ts.Out<number>, {}>;
|
|
21
|
+
/**
|
|
22
|
+
* An IP address or `"localhost"`
|
|
23
|
+
*/
|
|
24
|
+
declare const host: arktype_internal_methods_string_ts.StringType<string, {}>;
|
|
25
|
+
|
|
26
|
+
declare const _default: {
|
|
27
|
+
port: arktype_internal_methods_morph_ts.MorphType<(In: string) => arktype_internal_attributes_ts.Out<number>, {}>;
|
|
28
|
+
host: arktype_internal_methods_string_ts.StringType<string, {}>;
|
|
29
|
+
defineEnv: <const T extends Record<string, string | undefined>>(def: EnvSchema<T>, env?: {
|
|
30
|
+
[x: string]: string | undefined;
|
|
31
|
+
}) => arktype.distill.Out<arktype.type.infer<T>>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { type EnvSchema, _default as default, defineEnv, host, port };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/define-env.ts
|
|
8
|
+
var define_env_exports = {};
|
|
9
|
+
__export(define_env_exports, {
|
|
10
|
+
defineEnv: () => defineEnv
|
|
11
|
+
});
|
|
12
|
+
import { type } from "arktype";
|
|
13
|
+
|
|
14
|
+
// src/errors.ts
|
|
15
|
+
import chalk from "chalk";
|
|
16
|
+
|
|
17
|
+
// src/utils.ts
|
|
18
|
+
var indent = (str, amt = 2, { dontDetectNewlines = false } = {}) => {
|
|
19
|
+
const detectNewlines = !dontDetectNewlines;
|
|
20
|
+
if (detectNewlines) {
|
|
21
|
+
return str.split("\n").map((line) => `${" ".repeat(amt)}${line}`).join("\n");
|
|
22
|
+
}
|
|
23
|
+
return `${" ".repeat(amt)}${str}`;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// src/errors.ts
|
|
27
|
+
var formatErrors = (errors) => Object.entries(errors.byPath).map(([path, error]) => {
|
|
28
|
+
const messageWithoutPath = error.message.startsWith(path) ? error.message.slice(path.length) : error.message;
|
|
29
|
+
const valueMatch = messageWithoutPath.match(/\(was "([^"]+)"\)/);
|
|
30
|
+
const formattedMessage = valueMatch ? messageWithoutPath.replace(
|
|
31
|
+
`(was "${valueMatch[1]}")`,
|
|
32
|
+
`(was ${chalk.cyan(`"${valueMatch[1]}"`)})`
|
|
33
|
+
) : messageWithoutPath;
|
|
34
|
+
return `${chalk.yellow(path)}${formattedMessage}`;
|
|
35
|
+
}).join("\n");
|
|
36
|
+
var ArkEnvError = class extends Error {
|
|
37
|
+
constructor(errors, message = "Errors found while validating environment variables") {
|
|
38
|
+
super(`${chalk.red(message)}
|
|
39
|
+
${indent(formatErrors(errors))}
|
|
40
|
+
`);
|
|
41
|
+
this.name = "ArkEnvError";
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/define-env.ts
|
|
46
|
+
var defineEnv = (def, env = process.env) => {
|
|
47
|
+
const schema = type(def);
|
|
48
|
+
const requiredEnvKeys = Object.keys(
|
|
49
|
+
def
|
|
50
|
+
);
|
|
51
|
+
const filteredEnvVars = Object.fromEntries(
|
|
52
|
+
Object.entries(env).filter(([key]) => requiredEnvKeys.includes(key))
|
|
53
|
+
);
|
|
54
|
+
const validatedEnv = schema(filteredEnvVars);
|
|
55
|
+
if (validatedEnv instanceof type.errors) {
|
|
56
|
+
throw new ArkEnvError(validatedEnv);
|
|
57
|
+
}
|
|
58
|
+
return validatedEnv;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// src/types.ts
|
|
62
|
+
var types_exports = {};
|
|
63
|
+
__export(types_exports, {
|
|
64
|
+
host: () => host,
|
|
65
|
+
port: () => port
|
|
66
|
+
});
|
|
67
|
+
import { type as type2 } from "arktype";
|
|
68
|
+
var port = type2("string", "=>", (data, ctx) => {
|
|
69
|
+
const asNumber = Number.parseInt(data);
|
|
70
|
+
const isInteger = Number.isInteger(asNumber);
|
|
71
|
+
const isBetween = 0 <= asNumber && asNumber <= 65535;
|
|
72
|
+
if (!isInteger || !isBetween) {
|
|
73
|
+
ctx.mustBe("an integer between 0 and 65535");
|
|
74
|
+
}
|
|
75
|
+
return asNumber;
|
|
76
|
+
});
|
|
77
|
+
var host = type2("string.ip | 'localhost'");
|
|
78
|
+
|
|
79
|
+
// src/index.ts
|
|
80
|
+
var index_default = { ...define_env_exports, ...types_exports };
|
|
81
|
+
export {
|
|
82
|
+
index_default as default,
|
|
83
|
+
defineEnv,
|
|
84
|
+
host,
|
|
85
|
+
port
|
|
86
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "arkenv",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"description": "Typesafe environment variables parsing and validation with ArkType",
|
|
9
|
+
"exports": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"pnpm",
|
|
19
|
+
"arktype",
|
|
20
|
+
"environment",
|
|
21
|
+
"variables",
|
|
22
|
+
"typesafe",
|
|
23
|
+
"validation"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"homepage": "https://yam.codes/arkenv",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/yamcodes/arkenv.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": "https://github.com/yamcodes/arkenv/labels/arkenv",
|
|
32
|
+
"author": "Yam Borodetsky <yam@yam.codes>",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@ark/schema": "^0.46.0",
|
|
35
|
+
"@types/node": "22.14.0",
|
|
36
|
+
"tsup": "^8.5.0",
|
|
37
|
+
"typescript": "^5.9.2"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"arktype": "^2.0.2"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"chalk": "^5.6.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "rimraf dist && tsup",
|
|
47
|
+
"test:once": "pnpm test",
|
|
48
|
+
"typecheck": "tsc --noEmit",
|
|
49
|
+
"clean": "rimraf dist node_modules",
|
|
50
|
+
"test": "pnpm -w test --project arkenv"
|
|
51
|
+
}
|
|
52
|
+
}
|