dev-env-toolkit 0.1.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.
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Represents a validation error for an environment variable
3
+ */
4
+ interface EnvValidationError {
5
+ key: string;
6
+ message: string;
7
+ value?: string;
8
+ expected?: string;
9
+ }
10
+ /**
11
+ * Result of parsing environment variables
12
+ */
13
+ type EnvParseResult<T> = {
14
+ success: true;
15
+ data: T;
16
+ } | {
17
+ success: false;
18
+ errors: EnvValidationError[];
19
+ };
20
+ /**
21
+ * Options for the env parser
22
+ */
23
+ interface EnvParseOptions {
24
+ /** Path to .env file (optional) */
25
+ path?: string;
26
+ /** Whether to throw on validation errors (default: true) */
27
+ throwOnError?: boolean;
28
+ /** Custom environment object (default: process.env) */
29
+ env?: Record<string, string | undefined>;
30
+ }
31
+ /**
32
+ * Base validator interface
33
+ */
34
+ interface Validator<T> {
35
+ _type: T;
36
+ _optional: boolean;
37
+ _default?: T;
38
+ parse(value: string | undefined, key: string): T;
39
+ optional(): Validator<T | undefined>;
40
+ default(value: T): Validator<T>;
41
+ }
42
+ /**
43
+ * String validator with additional methods
44
+ */
45
+ interface StringValidator extends Validator<string> {
46
+ min(length: number): StringValidator;
47
+ max(length: number): StringValidator;
48
+ url(): StringValidator;
49
+ email(): StringValidator;
50
+ regex(pattern: RegExp): StringValidator;
51
+ }
52
+ /**
53
+ * Number validator with additional methods
54
+ */
55
+ interface NumberValidator extends Validator<number> {
56
+ min(value: number): NumberValidator;
57
+ max(value: number): NumberValidator;
58
+ int(): NumberValidator;
59
+ positive(): NumberValidator;
60
+ port(): NumberValidator;
61
+ }
62
+ /**
63
+ * Boolean validator
64
+ */
65
+ interface BooleanValidator extends Validator<boolean> {
66
+ }
67
+ /**
68
+ * Enum validator
69
+ */
70
+ interface EnumValidator<T extends string> extends Validator<T> {
71
+ }
72
+ /**
73
+ * Array validator with additional methods
74
+ */
75
+ interface ArrayValidator extends Validator<string[]> {
76
+ separator(sep: string): ArrayValidator;
77
+ min(length: number): ArrayValidator;
78
+ max(length: number): ArrayValidator;
79
+ }
80
+ /**
81
+ * Schema definition type - maps keys to validators
82
+ */
83
+ type EnvSchema = Record<string, Validator<unknown>>;
84
+ /**
85
+ * Infer the output type from a schema
86
+ */
87
+ type InferEnv<T extends EnvSchema> = {
88
+ [K in keyof T]: T[K]['_optional'] extends true ? T[K]['_type'] | undefined : T[K]['_type'];
89
+ };
90
+
91
+ /**
92
+ * Parse a .env file and return key-value pairs
93
+ */
94
+ declare function parseEnvFile(filePath: string): Record<string, string>;
95
+ /**
96
+ * Load environment variables from a .env file into the current environment
97
+ */
98
+ declare function loadEnvFile(filePath?: string, options?: {
99
+ override?: boolean;
100
+ }): Record<string, string>;
101
+
102
+ /**
103
+ * Custom error class for environment validation errors
104
+ */
105
+ declare class EnvValidationException extends Error {
106
+ readonly errors: EnvValidationError[];
107
+ constructor(errors: EnvValidationError[]);
108
+ }
109
+ /**
110
+ * Custom error for parsing errors
111
+ */
112
+ declare class EnvParseError extends Error {
113
+ readonly key: string;
114
+ readonly value?: string;
115
+ constructor(key: string, message: string, value?: string);
116
+ }
117
+
118
+ /**
119
+ * Create a type-safe environment configuration
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * import { env } from 'dev-env-toolkit';
124
+ *
125
+ * const config = env.create({
126
+ * PORT: env.number().default(3000),
127
+ * DATABASE_URL: env.string(),
128
+ * NODE_ENV: env.enum(['development', 'production', 'test']),
129
+ * DEBUG: env.boolean().default(false),
130
+ * });
131
+ *
132
+ * // Fully typed!
133
+ * console.log(config.PORT); // number
134
+ * console.log(config.DEBUG); // boolean
135
+ * ```
136
+ */
137
+ declare function create<T extends EnvSchema>(schema: T, options?: EnvParseOptions): InferEnv<T>;
138
+ /**
139
+ * Safely parse environment variables without throwing
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * const result = env.safeParse({
144
+ * PORT: env.number(),
145
+ * });
146
+ *
147
+ * if (result.success) {
148
+ * console.log(result.data.PORT);
149
+ * } else {
150
+ * console.error(result.errors);
151
+ * }
152
+ * ```
153
+ */
154
+ declare function safeParse<T extends EnvSchema>(schema: T, options?: Omit<EnvParseOptions, 'throwOnError'>): EnvParseResult<InferEnv<T>>;
155
+ /**
156
+ * The main env object with all utilities
157
+ */
158
+ declare const env: {
159
+ create: typeof create;
160
+ safeParse: typeof safeParse;
161
+ string: () => StringValidator;
162
+ number: () => NumberValidator;
163
+ boolean: () => BooleanValidator;
164
+ enum: <T extends string>(values: readonly T[]) => EnumValidator<T>;
165
+ array: () => ArrayValidator;
166
+ load: typeof loadEnvFile;
167
+ parse: typeof parseEnvFile;
168
+ };
169
+
170
+ export { type ArrayValidator, type BooleanValidator, type EnumValidator, EnvParseError, type EnvParseOptions, type EnvParseResult, type EnvSchema, type EnvValidationError, EnvValidationException, type InferEnv, type NumberValidator, type StringValidator, type Validator, env as default, env };
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Represents a validation error for an environment variable
3
+ */
4
+ interface EnvValidationError {
5
+ key: string;
6
+ message: string;
7
+ value?: string;
8
+ expected?: string;
9
+ }
10
+ /**
11
+ * Result of parsing environment variables
12
+ */
13
+ type EnvParseResult<T> = {
14
+ success: true;
15
+ data: T;
16
+ } | {
17
+ success: false;
18
+ errors: EnvValidationError[];
19
+ };
20
+ /**
21
+ * Options for the env parser
22
+ */
23
+ interface EnvParseOptions {
24
+ /** Path to .env file (optional) */
25
+ path?: string;
26
+ /** Whether to throw on validation errors (default: true) */
27
+ throwOnError?: boolean;
28
+ /** Custom environment object (default: process.env) */
29
+ env?: Record<string, string | undefined>;
30
+ }
31
+ /**
32
+ * Base validator interface
33
+ */
34
+ interface Validator<T> {
35
+ _type: T;
36
+ _optional: boolean;
37
+ _default?: T;
38
+ parse(value: string | undefined, key: string): T;
39
+ optional(): Validator<T | undefined>;
40
+ default(value: T): Validator<T>;
41
+ }
42
+ /**
43
+ * String validator with additional methods
44
+ */
45
+ interface StringValidator extends Validator<string> {
46
+ min(length: number): StringValidator;
47
+ max(length: number): StringValidator;
48
+ url(): StringValidator;
49
+ email(): StringValidator;
50
+ regex(pattern: RegExp): StringValidator;
51
+ }
52
+ /**
53
+ * Number validator with additional methods
54
+ */
55
+ interface NumberValidator extends Validator<number> {
56
+ min(value: number): NumberValidator;
57
+ max(value: number): NumberValidator;
58
+ int(): NumberValidator;
59
+ positive(): NumberValidator;
60
+ port(): NumberValidator;
61
+ }
62
+ /**
63
+ * Boolean validator
64
+ */
65
+ interface BooleanValidator extends Validator<boolean> {
66
+ }
67
+ /**
68
+ * Enum validator
69
+ */
70
+ interface EnumValidator<T extends string> extends Validator<T> {
71
+ }
72
+ /**
73
+ * Array validator with additional methods
74
+ */
75
+ interface ArrayValidator extends Validator<string[]> {
76
+ separator(sep: string): ArrayValidator;
77
+ min(length: number): ArrayValidator;
78
+ max(length: number): ArrayValidator;
79
+ }
80
+ /**
81
+ * Schema definition type - maps keys to validators
82
+ */
83
+ type EnvSchema = Record<string, Validator<unknown>>;
84
+ /**
85
+ * Infer the output type from a schema
86
+ */
87
+ type InferEnv<T extends EnvSchema> = {
88
+ [K in keyof T]: T[K]['_optional'] extends true ? T[K]['_type'] | undefined : T[K]['_type'];
89
+ };
90
+
91
+ /**
92
+ * Parse a .env file and return key-value pairs
93
+ */
94
+ declare function parseEnvFile(filePath: string): Record<string, string>;
95
+ /**
96
+ * Load environment variables from a .env file into the current environment
97
+ */
98
+ declare function loadEnvFile(filePath?: string, options?: {
99
+ override?: boolean;
100
+ }): Record<string, string>;
101
+
102
+ /**
103
+ * Custom error class for environment validation errors
104
+ */
105
+ declare class EnvValidationException extends Error {
106
+ readonly errors: EnvValidationError[];
107
+ constructor(errors: EnvValidationError[]);
108
+ }
109
+ /**
110
+ * Custom error for parsing errors
111
+ */
112
+ declare class EnvParseError extends Error {
113
+ readonly key: string;
114
+ readonly value?: string;
115
+ constructor(key: string, message: string, value?: string);
116
+ }
117
+
118
+ /**
119
+ * Create a type-safe environment configuration
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * import { env } from 'dev-env-toolkit';
124
+ *
125
+ * const config = env.create({
126
+ * PORT: env.number().default(3000),
127
+ * DATABASE_URL: env.string(),
128
+ * NODE_ENV: env.enum(['development', 'production', 'test']),
129
+ * DEBUG: env.boolean().default(false),
130
+ * });
131
+ *
132
+ * // Fully typed!
133
+ * console.log(config.PORT); // number
134
+ * console.log(config.DEBUG); // boolean
135
+ * ```
136
+ */
137
+ declare function create<T extends EnvSchema>(schema: T, options?: EnvParseOptions): InferEnv<T>;
138
+ /**
139
+ * Safely parse environment variables without throwing
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * const result = env.safeParse({
144
+ * PORT: env.number(),
145
+ * });
146
+ *
147
+ * if (result.success) {
148
+ * console.log(result.data.PORT);
149
+ * } else {
150
+ * console.error(result.errors);
151
+ * }
152
+ * ```
153
+ */
154
+ declare function safeParse<T extends EnvSchema>(schema: T, options?: Omit<EnvParseOptions, 'throwOnError'>): EnvParseResult<InferEnv<T>>;
155
+ /**
156
+ * The main env object with all utilities
157
+ */
158
+ declare const env: {
159
+ create: typeof create;
160
+ safeParse: typeof safeParse;
161
+ string: () => StringValidator;
162
+ number: () => NumberValidator;
163
+ boolean: () => BooleanValidator;
164
+ enum: <T extends string>(values: readonly T[]) => EnumValidator<T>;
165
+ array: () => ArrayValidator;
166
+ load: typeof loadEnvFile;
167
+ parse: typeof parseEnvFile;
168
+ };
169
+
170
+ export { type ArrayValidator, type BooleanValidator, type EnumValidator, EnvParseError, type EnvParseOptions, type EnvParseResult, type EnvSchema, type EnvValidationError, EnvValidationException, type InferEnv, type NumberValidator, type StringValidator, type Validator, env as default, env };