@vyriy/config 0.1.9

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vyriy contributors
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,68 @@
1
+ # @vyriy/config
2
+
3
+ Environment config parsing utility for Vyriy projects.
4
+
5
+ ## Purpose
6
+
7
+ This package provides a small helper for reading environment variables and parsing them into useful runtime values.
8
+
9
+ ## Install
10
+
11
+ With npm:
12
+
13
+ ```bash
14
+ npm install @vyriy/config
15
+ ```
16
+
17
+ With Yarn:
18
+
19
+ ```bash
20
+ yarn add @vyriy/config
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```ts
26
+ import { getConfig, Parser } from '@vyriy/config';
27
+
28
+ const port = getConfig('PORT', 3000);
29
+ const features = getConfig<string[]>('FEATURES', [], 'auto');
30
+ const payload = getConfig<{ ok: boolean }>('PAYLOAD', null, 'json');
31
+ const timeout = getConfig('TIMEOUT', null, Parser.duration);
32
+ ```
33
+
34
+ ## Exports
35
+
36
+ The package exposes:
37
+
38
+ ```ts
39
+ import { getConfig, Parser, auto } from '@vyriy/config';
40
+ import { parseDuration } from '@vyriy/config/duration';
41
+ ```
42
+
43
+ ## API
44
+
45
+ - `getConfig(envName, defaultValue?, parser = auto)` reads an environment variable and parses it.
46
+ - if the env is missing and a non-null default is provided, the default is returned unchanged.
47
+ - if the env is missing and no default is provided, `getConfig` throws.
48
+ - `getConfig(envName, null, parser)` lets you skip the default and pass a parser explicitly.
49
+
50
+ Built-in parser names:
51
+
52
+ - `auto`
53
+ - `string`
54
+ - `number`
55
+ - `int`
56
+ - `boolean`
57
+ - `csv`
58
+ - `json`
59
+ - `duration`
60
+
61
+ You can also pass parser functions directly:
62
+
63
+ ```ts
64
+ import { getConfig, Parser } from '@vyriy/config';
65
+
66
+ const port = getConfig('PORT', null, Parser.int);
67
+ const enabled = getConfig('ENABLED', null, (value) => Parser.boolean(value, { extendedBooleans: true }));
68
+ ```
package/auto.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { AutoEnvValue, AutoOptions } from './types.js';
2
+ export declare const auto: <T extends AutoEnvValue = string>(value: string, options?: AutoOptions) => T;
package/auto.js ADDED
@@ -0,0 +1,35 @@
1
+ import { FALSE_VALUES, DEFAULT_AUTO_OPTIONS, TRUE_VALUES } from './constants.js';
2
+ import { splitCsv } from './csv.js';
3
+ import { tryJsonParse } from './json.js';
4
+ import { parseNumber } from './number.js';
5
+ export const auto = (value, options = {}) => {
6
+ const resolvedOptions = { ...DEFAULT_AUTO_OPTIONS, ...options };
7
+ const trimmed = value.trim();
8
+ const normalized = trimmed.toLowerCase();
9
+ if (resolvedOptions.nullish && normalized === 'null') {
10
+ return null;
11
+ }
12
+ if (resolvedOptions.nullish && normalized === 'undefined') {
13
+ return undefined;
14
+ }
15
+ if (TRUE_VALUES.has(normalized) && (normalized === 'true' || resolvedOptions.extendedBooleans)) {
16
+ return true;
17
+ }
18
+ if (FALSE_VALUES.has(normalized) && (normalized === 'false' || resolvedOptions.extendedBooleans)) {
19
+ return false;
20
+ }
21
+ const parsedNumber = parseNumber(trimmed);
22
+ if (parsedNumber !== undefined) {
23
+ return parsedNumber;
24
+ }
25
+ if (resolvedOptions.json) {
26
+ const parsed = tryJsonParse(trimmed);
27
+ if (parsed !== undefined) {
28
+ return parsed;
29
+ }
30
+ }
31
+ if (resolvedOptions.csvArrays && trimmed.includes(',')) {
32
+ return splitCsv(trimmed);
33
+ }
34
+ return trimmed;
35
+ };
package/boolean.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { ConfigParserWithOptions, StrictOptions } from './types.js';
2
+ export declare const boolean: ConfigParserWithOptions<boolean, StrictOptions>;
package/boolean.js ADDED
@@ -0,0 +1,19 @@
1
+ import { FALSE_VALUES, TRUE_VALUES } from './constants.js';
2
+ const parseBoolean = (value, options = {}) => {
3
+ const normalized = value.trim().toLowerCase();
4
+ const { extendedBooleans = true } = options;
5
+ if (normalized === 'true') {
6
+ return true;
7
+ }
8
+ if (normalized === 'false') {
9
+ return false;
10
+ }
11
+ if (extendedBooleans && TRUE_VALUES.has(normalized)) {
12
+ return true;
13
+ }
14
+ if (extendedBooleans && FALSE_VALUES.has(normalized)) {
15
+ return false;
16
+ }
17
+ throw new TypeError(`Expected boolean, got: "${value}"`);
18
+ };
19
+ export const boolean = (value, options = {}) => parseBoolean(value, options);
package/config.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { GetConfig } from './types.js';
2
+ export declare const getConfig: GetConfig;
package/config.js ADDED
@@ -0,0 +1,14 @@
1
+ import { existsEnv, getEnv } from '@vyriy/env';
2
+ import { auto, parsers } from './parser.js';
3
+ const NO_DEFAULT = Symbol('NO_DEFAULT');
4
+ const isParserName = (value) => typeof value === 'string';
5
+ const resolveParser = (parser = auto) => isParserName(parser) ? parsers[parser] : parser;
6
+ export const getConfig = (envName, defaultValue = NO_DEFAULT, parser = auto) => {
7
+ if (!existsEnv(envName)) {
8
+ if (defaultValue !== NO_DEFAULT && defaultValue !== null) {
9
+ return defaultValue;
10
+ }
11
+ throw new Error(`Environment variable ${envName} is not defined!`);
12
+ }
13
+ return resolveParser(parser)(getEnv(envName));
14
+ };
package/constants.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { AutoOptions } from './types.js';
2
+ export declare const DEFAULT_AUTO_OPTIONS: Required<AutoOptions>;
3
+ export declare const TRUE_VALUES: Set<string>;
4
+ export declare const FALSE_VALUES: Set<string>;
package/constants.js ADDED
@@ -0,0 +1,18 @@
1
+ export const DEFAULT_AUTO_OPTIONS = {
2
+ extendedBooleans: true,
3
+ csvArrays: true,
4
+ json: true,
5
+ nullish: true,
6
+ };
7
+ export const TRUE_VALUES = new Set([
8
+ 'true',
9
+ '1',
10
+ 'yes',
11
+ 'on',
12
+ ]);
13
+ export const FALSE_VALUES = new Set([
14
+ 'false',
15
+ '0',
16
+ 'no',
17
+ 'off',
18
+ ]);
package/csv.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const splitCsv: (value: string) => string[];
2
+ export declare const csv: (value: string) => string[];
package/csv.js ADDED
@@ -0,0 +1,5 @@
1
+ export const splitCsv = (value) => value
2
+ .split(',')
3
+ .map((entry) => entry.trim())
4
+ .filter(Boolean);
5
+ export const csv = (value) => splitCsv(value);
package/duration.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const parseDuration: (value: string) => number;
package/duration.js ADDED
@@ -0,0 +1,15 @@
1
+ const DURATION_UNITS = {
2
+ ms: 1,
3
+ s: 1000,
4
+ m: 60_000,
5
+ h: 3_600_000,
6
+ d: 86_400_000,
7
+ };
8
+ export const parseDuration = (value) => {
9
+ const match = /^(\d+)(ms|s|m|h|d)$/.exec(value.trim().toLowerCase());
10
+ if (!match) {
11
+ throw new Error(`Invalid duration: "${value}"`);
12
+ }
13
+ const [, amount, unit] = match;
14
+ return Number(amount) * DURATION_UNITS[unit];
15
+ };
package/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './config.js';
2
+ export * from './parser.js';
3
+ export * from './duration.js';
4
+ export type * from './types.js';
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './config.js';
2
+ export * from './parser.js';
3
+ export * from './duration.js';
package/json.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const tryJsonParse: (value: string) => Record<string, unknown> | unknown[] | undefined;
2
+ export declare const json: <T = unknown>(value: string) => T;
package/json.js ADDED
@@ -0,0 +1,18 @@
1
+ export const tryJsonParse = (value) => {
2
+ if (!((value.startsWith('{') && value.endsWith('}')) || (value.startsWith('[') && value.endsWith(']')))) {
3
+ return undefined;
4
+ }
5
+ try {
6
+ return JSON.parse(value);
7
+ }
8
+ catch {
9
+ return undefined;
10
+ }
11
+ };
12
+ export const json = (value) => {
13
+ const parsed = tryJsonParse(value.trim());
14
+ if (parsed === undefined) {
15
+ throw new TypeError(`Expected JSON (object/array), got: "${value}"`);
16
+ }
17
+ return parsed;
18
+ };
package/number.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare const parseNumber: (value: string) => number | undefined;
2
+ export declare const number: (value: string) => number;
3
+ export declare const int: (value: string) => number;
package/number.js ADDED
@@ -0,0 +1,19 @@
1
+ const isNumericString = (value) => value !== '' && !Number.isNaN(Number(value));
2
+ export const parseNumber = (value) => {
3
+ const trimmed = value.trim();
4
+ return isNumericString(trimmed) ? Number(trimmed) : undefined;
5
+ };
6
+ export const number = (value) => {
7
+ const parsed = parseNumber(value);
8
+ if (parsed === undefined) {
9
+ throw new TypeError(`Expected number, got: "${value}"`);
10
+ }
11
+ return parsed;
12
+ };
13
+ export const int = (value) => {
14
+ const parsed = number(value);
15
+ if (!Number.isInteger(parsed)) {
16
+ throw new TypeError(`Expected integer, got: ${parsed}`);
17
+ }
18
+ return parsed;
19
+ };
package/package.json ADDED
@@ -0,0 +1,129 @@
1
+ {
2
+ "name": "@vyriy/config",
3
+ "version": "0.1.9",
4
+ "description": "Environment config parsing utility for Vyriy projects",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "dependencies": {
8
+ "@vyriy/env": "0.1.9"
9
+ },
10
+ "license": "MIT",
11
+ "types": "./index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./index.d.ts",
15
+ "import": "./index.js",
16
+ "default": "./index.js"
17
+ },
18
+ "./auto": {
19
+ "types": "./auto.d.ts",
20
+ "import": "./auto.js",
21
+ "default": "./auto.js"
22
+ },
23
+ "./auto.js": {
24
+ "types": "./auto.d.ts",
25
+ "import": "./auto.js",
26
+ "default": "./auto.js"
27
+ },
28
+ "./boolean": {
29
+ "types": "./boolean.d.ts",
30
+ "import": "./boolean.js",
31
+ "default": "./boolean.js"
32
+ },
33
+ "./boolean.js": {
34
+ "types": "./boolean.d.ts",
35
+ "import": "./boolean.js",
36
+ "default": "./boolean.js"
37
+ },
38
+ "./config": {
39
+ "types": "./config.d.ts",
40
+ "import": "./config.js",
41
+ "default": "./config.js"
42
+ },
43
+ "./config.js": {
44
+ "types": "./config.d.ts",
45
+ "import": "./config.js",
46
+ "default": "./config.js"
47
+ },
48
+ "./constants": {
49
+ "types": "./constants.d.ts",
50
+ "import": "./constants.js",
51
+ "default": "./constants.js"
52
+ },
53
+ "./constants.js": {
54
+ "types": "./constants.d.ts",
55
+ "import": "./constants.js",
56
+ "default": "./constants.js"
57
+ },
58
+ "./csv": {
59
+ "types": "./csv.d.ts",
60
+ "import": "./csv.js",
61
+ "default": "./csv.js"
62
+ },
63
+ "./csv.js": {
64
+ "types": "./csv.d.ts",
65
+ "import": "./csv.js",
66
+ "default": "./csv.js"
67
+ },
68
+ "./duration": {
69
+ "types": "./duration.d.ts",
70
+ "import": "./duration.js",
71
+ "default": "./duration.js"
72
+ },
73
+ "./duration.js": {
74
+ "types": "./duration.d.ts",
75
+ "import": "./duration.js",
76
+ "default": "./duration.js"
77
+ },
78
+ "./index": {
79
+ "types": "./index.d.ts",
80
+ "import": "./index.js",
81
+ "default": "./index.js"
82
+ },
83
+ "./index.js": {
84
+ "types": "./index.d.ts",
85
+ "import": "./index.js",
86
+ "default": "./index.js"
87
+ },
88
+ "./json": {
89
+ "types": "./json.d.ts",
90
+ "import": "./json.js",
91
+ "default": "./json.js"
92
+ },
93
+ "./json.js": {
94
+ "types": "./json.d.ts",
95
+ "import": "./json.js",
96
+ "default": "./json.js"
97
+ },
98
+ "./number": {
99
+ "types": "./number.d.ts",
100
+ "import": "./number.js",
101
+ "default": "./number.js"
102
+ },
103
+ "./number.js": {
104
+ "types": "./number.d.ts",
105
+ "import": "./number.js",
106
+ "default": "./number.js"
107
+ },
108
+ "./parser": {
109
+ "types": "./parser.d.ts",
110
+ "import": "./parser.js",
111
+ "default": "./parser.js"
112
+ },
113
+ "./parser.js": {
114
+ "types": "./parser.d.ts",
115
+ "import": "./parser.js",
116
+ "default": "./parser.js"
117
+ },
118
+ "./string": {
119
+ "types": "./string.d.ts",
120
+ "import": "./string.js",
121
+ "default": "./string.js"
122
+ },
123
+ "./string.js": {
124
+ "types": "./string.d.ts",
125
+ "import": "./string.js",
126
+ "default": "./string.js"
127
+ }
128
+ }
129
+ }
package/parser.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { ConfigParsers } from './types.js';
2
+ export { auto } from './auto.js';
3
+ export { boolean } from './boolean.js';
4
+ export { csv } from './csv.js';
5
+ export { parseDuration as duration } from './duration.js';
6
+ export { int, number } from './number.js';
7
+ export { json } from './json.js';
8
+ export { string } from './string.js';
9
+ export declare const parsers: ConfigParsers;
10
+ export declare const Parser: ConfigParsers;
package/parser.js ADDED
@@ -0,0 +1,25 @@
1
+ import { auto } from './auto.js';
2
+ import { boolean } from './boolean.js';
3
+ import { csv } from './csv.js';
4
+ import { parseDuration as duration } from './duration.js';
5
+ import { int, number } from './number.js';
6
+ import { json } from './json.js';
7
+ import { string } from './string.js';
8
+ export { auto } from './auto.js';
9
+ export { boolean } from './boolean.js';
10
+ export { csv } from './csv.js';
11
+ export { parseDuration as duration } from './duration.js';
12
+ export { int, number } from './number.js';
13
+ export { json } from './json.js';
14
+ export { string } from './string.js';
15
+ export const parsers = {
16
+ auto,
17
+ string,
18
+ number,
19
+ int,
20
+ boolean,
21
+ csv,
22
+ json,
23
+ duration,
24
+ };
25
+ export const Parser = parsers;
package/string.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const string: (value: string) => string;
package/string.js ADDED
@@ -0,0 +1 @@
1
+ export const string = (value) => value;
package/types.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ export type AutoEnvValue = string | number | boolean | null | undefined | Record<string, unknown> | unknown[];
2
+ export type AutoOptions = {
3
+ extendedBooleans?: boolean;
4
+ csvArrays?: boolean;
5
+ json?: boolean;
6
+ nullish?: boolean;
7
+ };
8
+ export type StrictOptions = {
9
+ extendedBooleans?: boolean;
10
+ };
11
+ export type ConfigParser<T = unknown> = (value: string) => T;
12
+ export type ConfigParserWithOptions<T, TOptions> = (value: string, options?: TOptions) => T;
13
+ export type ConfigParsers = {
14
+ auto: <T extends AutoEnvValue = string>(value: string, options?: AutoOptions) => T;
15
+ string: (value: string) => string;
16
+ number: (value: string) => number;
17
+ int: (value: string) => number;
18
+ boolean: (value: string, options?: StrictOptions) => boolean;
19
+ csv: (value: string) => string[];
20
+ json: <T = unknown>(value: string) => T;
21
+ duration: (value: string) => number;
22
+ };
23
+ export type ConfigParserName = keyof ConfigParsers;
24
+ export type ConfigParserLike<T = unknown> = ConfigParserName | ConfigParser<T>;
25
+ export type GetConfig = {
26
+ <T extends AutoEnvValue = string>(envName: string): T | never;
27
+ <T>(envName: string, defaultValue: T, parser?: ConfigParserLike<T>): T;
28
+ <T>(envName: string, defaultValue: null, parser: ConfigParserLike<T>): T | never;
29
+ };