@catbee/utils 2.0.0-next.0 → 2.0.0-next.1
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 +52 -16
- package/array/index.cjs +180 -71
- package/array/index.d.ts +293 -1
- package/array/index.mjs +171 -72
- package/async/index.cjs +92 -36
- package/async/index.d.ts +275 -1
- package/async/index.mjs +92 -36
- package/cache/index.cjs +1 -1
- package/cache/index.d.ts +155 -1
- package/cache/index.mjs +2 -2
- package/config/index.cjs +78 -64
- package/config/index.d.ts +64 -2
- package/config/index.mjs +76 -64
- package/context-store/index.d.ts +192 -1
- package/crypto/index.d.ts +163 -1
- package/date/index.cjs +46 -1
- package/date/index.d.ts +190 -1
- package/date/index.mjs +45 -2
- package/decorators/index.cjs +1156 -18
- package/decorators/index.d.ts +684 -1
- package/decorators/index.mjs +1156 -18
- package/dir/index.cjs +4 -3
- package/dir/index.d.ts +195 -1
- package/dir/index.mjs +4 -3
- package/env/index.cjs +10 -26
- package/env/index.d.ts +379 -1
- package/env/index.mjs +10 -26
- package/exception/index.d.ts +232 -1
- package/fs/index.cjs +70 -36
- package/fs/index.d.ts +205 -1
- package/fs/index.mjs +64 -34
- package/http-status-codes/index.d.ts +267 -1
- package/id/index.d.ts +37 -1
- package/index.cjs +3 -3
- package/index.d.ts +1 -1
- package/index.mjs +1 -1
- package/logger/index.cjs +11 -11
- package/logger/index.d.ts +189 -1
- package/logger/index.mjs +12 -12
- package/middleware/index.d.ts +103 -1
- package/obj/index.cjs +150 -162
- package/obj/index.d.ts +136 -1
- package/obj/index.mjs +150 -162
- package/package.json +11 -11
- package/performance/index.cjs +2 -2
- package/performance/index.d.ts +138 -1
- package/performance/index.mjs +2 -2
- package/request/index.cjs +1 -1
- package/request/index.d.ts +241 -2
- package/request/index.mjs +1 -1
- package/response/index.d.ts +318 -2
- package/server/index.cjs +27 -23
- package/server/index.d.ts +785 -4
- package/server/index.mjs +28 -23
- package/stream/index.d.ts +90 -1
- package/string/index.d.ts +102 -1
- package/type/index.cjs +1 -1
- package/type/index.d.ts +107 -1
- package/type/index.mjs +1 -1
- package/types/index.d.ts +774 -4
- package/url/index.cjs +2 -4
- package/url/index.d.ts +142 -1
- package/url/index.mjs +2 -4
- package/{validate → validation}/index.cjs +89 -42
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +32 -23
- package/{validate → validation}/index.mjs +85 -42
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/decorators.utils.d.ts +0 -705
- package/dir/dir.utils.d.ts +0 -216
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/obj.utils.d.ts +0 -156
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/dir/dir.utils.d.ts
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The MIT License
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
-
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
* in the Software without restriction, including without limitation the rights
|
|
9
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
* furnished to do so, subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
* copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
* SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import fs from 'fs';
|
|
26
|
-
/**
|
|
27
|
-
* Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
|
|
28
|
-
*
|
|
29
|
-
* @param {string} dirPath - The directory path to ensure.
|
|
30
|
-
* @returns {Promise<void>} Resolves when the directory exists.
|
|
31
|
-
* @throws {Error} If directory cannot be created.
|
|
32
|
-
*/
|
|
33
|
-
export declare function ensureDir(dirPath: string): Promise<void>;
|
|
34
|
-
/**
|
|
35
|
-
* Recursively lists all files in a directory.
|
|
36
|
-
*
|
|
37
|
-
* @param {string} dirPath - The base directory.
|
|
38
|
-
* @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
|
|
39
|
-
* @returns {Promise<string[]>} Array of absolute file paths.
|
|
40
|
-
* @throws {Error} If the directory cannot be read.
|
|
41
|
-
*/
|
|
42
|
-
export declare function listFiles(dirPath: string, recursive?: boolean): Promise<string[]>;
|
|
43
|
-
/**
|
|
44
|
-
* Deletes a directory and all its contents recursively (like `rm -rf`).
|
|
45
|
-
*
|
|
46
|
-
* @param {string} dirPath - Directory to delete.
|
|
47
|
-
* @returns {Promise<void>} Resolves when deletion is complete.
|
|
48
|
-
* @throws {Error} If deletion fails.
|
|
49
|
-
*/
|
|
50
|
-
export declare function deleteDirRecursive(dirPath: string): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
* Checks whether a given path is a directory.
|
|
53
|
-
*
|
|
54
|
-
* @param {string} pathStr - Path to check.
|
|
55
|
-
* @returns {Promise<boolean>} True if the path is a directory, else false.
|
|
56
|
-
*/
|
|
57
|
-
export declare function isDirectory(pathStr: string): Promise<boolean>;
|
|
58
|
-
/**
|
|
59
|
-
* Recursively copies a directory and all its contents to a destination.
|
|
60
|
-
*
|
|
61
|
-
* @param {string} src - Source directory path.
|
|
62
|
-
* @param {string} dest - Destination directory path.
|
|
63
|
-
* @returns {Promise<void>} Resolves when copy is complete.
|
|
64
|
-
* @throws {Error} If source does not exist or copy fails.
|
|
65
|
-
*/
|
|
66
|
-
export declare function copyDir(src: string, dest: string): Promise<void>;
|
|
67
|
-
/**
|
|
68
|
-
* Moves a directory to a new location by copying and deleting the original.
|
|
69
|
-
*
|
|
70
|
-
* @param {string} src - Source directory path.
|
|
71
|
-
* @param {string} dest - Destination directory path.
|
|
72
|
-
* @returns {Promise<void>} Resolves when move is complete.
|
|
73
|
-
* @throws {Error} If copy or deletion fails.
|
|
74
|
-
*/
|
|
75
|
-
export declare function moveDir(src: string, dest: string): Promise<void>;
|
|
76
|
-
/**
|
|
77
|
-
* Empties a directory by deleting all files and subdirectories inside it.
|
|
78
|
-
*
|
|
79
|
-
* @param {string} dirPath - Path to the directory to empty.
|
|
80
|
-
* @returns {Promise<void>} Resolves when the directory has been emptied.
|
|
81
|
-
* @throws {Error} If files or subdirectories cannot be removed.
|
|
82
|
-
*/
|
|
83
|
-
export declare function emptyDir(dirPath: string): Promise<void>;
|
|
84
|
-
/**
|
|
85
|
-
* Calculates the total size (in bytes) of all files in a directory (recursive).
|
|
86
|
-
*
|
|
87
|
-
* @param {string} dirPath - Path to the directory.
|
|
88
|
-
* @returns {Promise<number>} Total size in bytes.
|
|
89
|
-
* @throws {Error} If any file stats cannot be read.
|
|
90
|
-
*/
|
|
91
|
-
export declare function getDirSize(dirPath: string): Promise<number>;
|
|
92
|
-
/**
|
|
93
|
-
* Watches a directory for file changes and calls a callback on each event.
|
|
94
|
-
*
|
|
95
|
-
* @param {string} dirPath - Directory path to watch.
|
|
96
|
-
* @param {(eventType: "rename" | "change", filename: string | null) => void} callback - Callback for each change event.
|
|
97
|
-
* @returns {() => void} A function to stop watching the directory.
|
|
98
|
-
*/
|
|
99
|
-
export declare function watchDir(dirPath: string, callback: (eventType: 'rename' | 'change', filename: string | null) => void): () => void;
|
|
100
|
-
/**
|
|
101
|
-
* Recursively finds files matching a simple pattern (supports '*' and '?').
|
|
102
|
-
*
|
|
103
|
-
* @param {string} pattern - Simple pattern to match files (e.g., '*.ts', 'src/*.js').
|
|
104
|
-
* @param {object} [options] - Options for matching files.
|
|
105
|
-
* @param {string} [options.cwd=process.cwd()] - Base directory to start searching from.
|
|
106
|
-
* @param {boolean} [options.dot=false] - Include dotfiles in matches.
|
|
107
|
-
* @returns {Promise<string[]>} Array of matched file paths.
|
|
108
|
-
*/
|
|
109
|
-
export declare function findFilesByPattern(pattern: string, options?: {
|
|
110
|
-
cwd?: string;
|
|
111
|
-
dot?: boolean;
|
|
112
|
-
}): Promise<string[]>;
|
|
113
|
-
/**
|
|
114
|
-
* Gets all subdirectories in a directory.
|
|
115
|
-
*
|
|
116
|
-
* @param {string} dirPath - The directory to search in.
|
|
117
|
-
* @param {boolean} [recursive=false] - Whether to include subdirectories recursively.
|
|
118
|
-
* @returns {Promise<string[]>} Array of absolute subdirectory paths.
|
|
119
|
-
* @throws {Error} If directory cannot be read.
|
|
120
|
-
*/
|
|
121
|
-
export declare function getSubdirectories(dirPath: string, recursive?: boolean): Promise<string[]>;
|
|
122
|
-
/**
|
|
123
|
-
* Ensures a directory exists and is empty.
|
|
124
|
-
*
|
|
125
|
-
* @param {string} dirPath - Path to the directory.
|
|
126
|
-
* @returns {Promise<void>} Resolves when the directory exists and is empty.
|
|
127
|
-
* @throws {Error} If directory cannot be created or emptied.
|
|
128
|
-
*/
|
|
129
|
-
export declare function ensureEmptyDir(dirPath: string): Promise<void>;
|
|
130
|
-
/**
|
|
131
|
-
* Creates a temporary directory with optional auto-cleanup.
|
|
132
|
-
*
|
|
133
|
-
* @param {object} [options] - Options for the temporary directory.
|
|
134
|
-
* @param {string} [options.prefix='tmp-'] - Prefix for the directory name.
|
|
135
|
-
* @param {string} [options.parentDir=os.tmpdir()] - Parent directory.
|
|
136
|
-
* @param {boolean} [options.cleanup=false] - Whether to register cleanup on process exit.
|
|
137
|
-
* @returns {Promise<{ path: string, cleanup: () => Promise<void> }>} Object with directory path and cleanup function.
|
|
138
|
-
* @throws {Error} If directory cannot be created.
|
|
139
|
-
*/
|
|
140
|
-
export declare function createTempDir(options?: {
|
|
141
|
-
prefix?: string;
|
|
142
|
-
parentDir?: string;
|
|
143
|
-
cleanup?: boolean;
|
|
144
|
-
}): Promise<{
|
|
145
|
-
path: string;
|
|
146
|
-
cleanup: () => Promise<void>;
|
|
147
|
-
}>;
|
|
148
|
-
/**
|
|
149
|
-
* Finds the newest file in a directory.
|
|
150
|
-
*
|
|
151
|
-
* @param {string} dirPath - Directory to search.
|
|
152
|
-
* @param {boolean} [recursive=false] - Whether to search subdirectories.
|
|
153
|
-
* @returns {Promise<string | null>} Path to the newest file or null if no files.
|
|
154
|
-
* @throws {Error} If directory cannot be read.
|
|
155
|
-
*/
|
|
156
|
-
export declare function findNewestFile(dirPath: string, recursive?: boolean): Promise<string | null>;
|
|
157
|
-
/**
|
|
158
|
-
* Finds the oldest file in a directory.
|
|
159
|
-
*
|
|
160
|
-
* @param {string} dirPath - Directory to search.
|
|
161
|
-
* @param {boolean} [recursive=false] - Whether to search subdirectories.
|
|
162
|
-
* @returns {Promise<string | null>} Path to the oldest file or null if no files.
|
|
163
|
-
* @throws {Error} If directory cannot be read.
|
|
164
|
-
*/
|
|
165
|
-
export declare function findOldestFile(dirPath: string, recursive?: boolean): Promise<string | null>;
|
|
166
|
-
/**
|
|
167
|
-
* Finds files or directories in a directory matching a predicate function.
|
|
168
|
-
*
|
|
169
|
-
* @param {string} dirPath - Directory to search.
|
|
170
|
-
* @param {(path: string, stat: fs.Stats) => boolean | Promise<boolean>} predicate - Function to test each path.
|
|
171
|
-
* @param {boolean} [recursive=false] - Whether to search subdirectories.
|
|
172
|
-
* @returns {Promise<string[]>} Array of matching paths.
|
|
173
|
-
* @throws {Error} If directory cannot be read.
|
|
174
|
-
*/
|
|
175
|
-
export declare function findInDir(dirPath: string, predicate: (path: string, stat: fs.Stats) => boolean | Promise<boolean>, recursive?: boolean): Promise<string[]>;
|
|
176
|
-
/**
|
|
177
|
-
* Watches a directory recursively for file changes.
|
|
178
|
-
*
|
|
179
|
-
* @param {string} dirPath - Base directory path to watch.
|
|
180
|
-
* @param {(eventType: "rename" | "change", filename: string) => void} callback - Callback for each change event.
|
|
181
|
-
* @param {boolean} [includeSubdirs=true] - Whether to watch subdirectories.
|
|
182
|
-
* @returns {Promise<() => void>} A function to stop watching the directory.
|
|
183
|
-
* @throws {Error} If directory cannot be watched.
|
|
184
|
-
*/
|
|
185
|
-
export declare function watchDirRecursive(dirPath: string, callback: (eventType: 'rename' | 'change', filename: string) => void, includeSubdirs?: boolean): Promise<() => void>;
|
|
186
|
-
/**
|
|
187
|
-
* Gets detailed directory statistics including file count, directory count, and size.
|
|
188
|
-
*
|
|
189
|
-
* @param {string} dirPath - Path to the directory.
|
|
190
|
-
* @returns {Promise<{ fileCount: number, dirCount: number, totalSize: number }>} Directory statistics.
|
|
191
|
-
* @throws {Error} If directory cannot be read.
|
|
192
|
-
*/
|
|
193
|
-
export declare function getDirStats(dirPath: string): Promise<{
|
|
194
|
-
fileCount: number;
|
|
195
|
-
dirCount: number;
|
|
196
|
-
totalSize: number;
|
|
197
|
-
}>;
|
|
198
|
-
/**
|
|
199
|
-
* Walks through a directory hierarchy, calling a visitor function for each entry.
|
|
200
|
-
*
|
|
201
|
-
* @param {string} dirPath - Starting directory path.
|
|
202
|
-
* @param {object} options - Options for walking the directory.
|
|
203
|
-
* @param {(entry: { path: string, name: string, isDirectory: boolean, stats: fs.Stats }) => boolean | void | Promise<boolean | void>} options.visitorFn -
|
|
204
|
-
* Function called for each file/directory. Return false to skip a directory.
|
|
205
|
-
* @param {'pre' | 'post'} [options.traversalOrder='pre'] - Whether to visit directories before or after their contents.
|
|
206
|
-
* @throws {Error} If directory cannot be read.
|
|
207
|
-
*/
|
|
208
|
-
export declare function walkDir(dirPath: string, options: {
|
|
209
|
-
visitorFn: (entry: {
|
|
210
|
-
path: string;
|
|
211
|
-
name: string;
|
|
212
|
-
isDirectory: boolean;
|
|
213
|
-
stats: fs.Stats;
|
|
214
|
-
}) => boolean | void | Promise<boolean | void>;
|
|
215
|
-
traversalOrder?: 'pre' | 'post';
|
|
216
|
-
}): Promise<void>;
|
package/env/env.utils.d.ts
DELETED
|
@@ -1,400 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The MIT License
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
-
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
* in the Software without restriction, including without limitation the rights
|
|
9
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
* furnished to do so, subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
* copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
* SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Enum representing valid application environments.
|
|
27
|
-
*/
|
|
28
|
-
export declare enum Environment {
|
|
29
|
-
PRODUCTION = "production",
|
|
30
|
-
DEVELOPMENT = "development",
|
|
31
|
-
STAGING = "staging",
|
|
32
|
-
TESTING = "testing"
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Options for validating URL environment variables
|
|
36
|
-
*/
|
|
37
|
-
export interface UrlOptions {
|
|
38
|
-
/** List of allowed protocols (e.g., ['http', 'https']) */
|
|
39
|
-
protocols?: string[];
|
|
40
|
-
/** Whether a top-level domain is required (default: true) */
|
|
41
|
-
requireTld?: boolean;
|
|
42
|
-
/** Whether to allow IP addresses (default: true) */
|
|
43
|
-
allowIp?: boolean;
|
|
44
|
-
/** Whether to allow localhost (default: true) */
|
|
45
|
-
allowLocalhost?: boolean;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Options for validating path environment variables
|
|
49
|
-
*/
|
|
50
|
-
export interface PathOptions {
|
|
51
|
-
/** Whether the path must exist on the filesystem (default: false) */
|
|
52
|
-
mustExist?: boolean;
|
|
53
|
-
/** Whether to convert relative paths to absolute (default: true) */
|
|
54
|
-
makeAbsolute?: boolean;
|
|
55
|
-
/** List of allowed file extensions */
|
|
56
|
-
allowedExtensions?: string[];
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Utility class for accessing and managing environment variables.
|
|
60
|
-
* Provides typed getters with fallback defaults and validation.
|
|
61
|
-
*/
|
|
62
|
-
export declare class Env {
|
|
63
|
-
private static cache;
|
|
64
|
-
/**
|
|
65
|
-
* Checks if the current NODE_ENV is 'development'.
|
|
66
|
-
*
|
|
67
|
-
* @returns {boolean} `true` if NODE_ENV is 'development', else `false`.
|
|
68
|
-
*/
|
|
69
|
-
static isDev(): boolean;
|
|
70
|
-
/**
|
|
71
|
-
* Checks if the current NODE_ENV is 'production'.
|
|
72
|
-
*
|
|
73
|
-
* @returns {boolean} `true` if NODE_ENV is 'production', else `false`.
|
|
74
|
-
*/
|
|
75
|
-
static isProd(): boolean;
|
|
76
|
-
/**
|
|
77
|
-
* Checks if the current NODE_ENV is 'testing'.
|
|
78
|
-
*
|
|
79
|
-
* @returns {boolean} `true` if NODE_ENV is 'testing', else `false`.
|
|
80
|
-
*/
|
|
81
|
-
static isTest(): boolean;
|
|
82
|
-
/**
|
|
83
|
-
* Checks if the current NODE_ENV is 'staging'.
|
|
84
|
-
*
|
|
85
|
-
* @returns {boolean} `true` if NODE_ENV is 'staging', else `false`.
|
|
86
|
-
*/
|
|
87
|
-
static isStaging(): boolean;
|
|
88
|
-
/**
|
|
89
|
-
* Sets an environment variable (only affects runtime memory).
|
|
90
|
-
*
|
|
91
|
-
* @param {string} key - The environment variable key.
|
|
92
|
-
* @param {string} value - The value to set.
|
|
93
|
-
*/
|
|
94
|
-
static set(key: string, value: string): void;
|
|
95
|
-
/**
|
|
96
|
-
* Returns all environment variables as an object.
|
|
97
|
-
*
|
|
98
|
-
* @returns {object} The current `process.env` object.
|
|
99
|
-
*/
|
|
100
|
-
static getAll(): object;
|
|
101
|
-
/**
|
|
102
|
-
* Retrieves a string environment variable with a fallback default.
|
|
103
|
-
* Supports variable expansion with ${VAR_NAME} syntax.
|
|
104
|
-
*
|
|
105
|
-
* @param {string} key - The environment variable key.
|
|
106
|
-
* @param {string} [defaultValue] - Value to return if the key is missing.
|
|
107
|
-
* @returns {string} The env value or the fallback.
|
|
108
|
-
*
|
|
109
|
-
* @example
|
|
110
|
-
* // If DATABASE_URL is "postgres://localhost:5432/${DB_NAME}"
|
|
111
|
-
* // and DB_NAME is "myapp"
|
|
112
|
-
* const url = Env.get('DATABASE_URL', ''); // "postgres://localhost:5432/myapp"
|
|
113
|
-
*/
|
|
114
|
-
static get(key: string, defaultValue: string): string;
|
|
115
|
-
/**
|
|
116
|
-
* Retrieves a string environment variable and throws if it's missing.
|
|
117
|
-
*
|
|
118
|
-
* @param {string} key - The environment variable key.
|
|
119
|
-
* @returns {string} The environment variable's value.
|
|
120
|
-
* @throws {Error} If the variable is not defined.
|
|
121
|
-
*/
|
|
122
|
-
static getRequired(key: string): string;
|
|
123
|
-
/**
|
|
124
|
-
* Retrieves a value using a default generating function if the key doesn't exist.
|
|
125
|
-
* Useful for expensive default calculations.
|
|
126
|
-
*
|
|
127
|
-
* @param {string} key - The environment variable key.
|
|
128
|
-
* @param {() => string} defaultFn - Function that generates default value.
|
|
129
|
-
* @returns {string} The environment value or generated default.
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* const hostname = Env.getWithDefault('HOSTNAME', () => {
|
|
133
|
-
* // Only called if HOSTNAME is not set
|
|
134
|
-
* return require('os').hostname();
|
|
135
|
-
* });
|
|
136
|
-
*/
|
|
137
|
-
static getWithDefault(key: string, defaultFn: () => string): string;
|
|
138
|
-
/**
|
|
139
|
-
* Retrieves an environment variable as a number, or returns a default.
|
|
140
|
-
*
|
|
141
|
-
* @param {string} key - The environment variable key.
|
|
142
|
-
* @param {number} defaultValue - Fallback number if key is not present.
|
|
143
|
-
* @returns {number} Parsed numeric value or default.
|
|
144
|
-
* @throws {Error} If the value is not a valid number.
|
|
145
|
-
*/
|
|
146
|
-
static getNumber(key: string, defaultValue: number): number;
|
|
147
|
-
/**
|
|
148
|
-
* Retrieves a required environment variable as a number.
|
|
149
|
-
*
|
|
150
|
-
* @param {string} key - The environment variable key.
|
|
151
|
-
* @returns {number} Parsed number.
|
|
152
|
-
* @throws {Error} If the value is missing or not a number.
|
|
153
|
-
*/
|
|
154
|
-
static getNumberRequired(key: string): number;
|
|
155
|
-
/**
|
|
156
|
-
* Retrieves an integer environment variable and validates it.
|
|
157
|
-
*
|
|
158
|
-
* @param {string} key - The environment variable key.
|
|
159
|
-
* @param {number} defaultValue - Fallback number if key is not present.
|
|
160
|
-
* @param {object} [options] - Validation options.
|
|
161
|
-
* @param {number} [options.min] - Minimum allowed value.
|
|
162
|
-
* @param {number} [options.max] - Maximum allowed value.
|
|
163
|
-
* @returns {number} The parsed integer.
|
|
164
|
-
*
|
|
165
|
-
* @example
|
|
166
|
-
* // Require PORT to be between 1000 and 9999
|
|
167
|
-
* const port = Env.getInteger('PORT', 3000, { min: 1000, max: 9999 });
|
|
168
|
-
*/
|
|
169
|
-
static getInteger(key: string, defaultValue: number, options?: {
|
|
170
|
-
min?: number;
|
|
171
|
-
max?: number;
|
|
172
|
-
}): number;
|
|
173
|
-
/**
|
|
174
|
-
* Retrieves an environment variable as a boolean.
|
|
175
|
-
* Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.
|
|
176
|
-
*
|
|
177
|
-
* @param {string} key - The environment variable key.
|
|
178
|
-
* @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.
|
|
179
|
-
* @returns {boolean} Parsed boolean.
|
|
180
|
-
* @throws {Error} If the value is not a recognized boolean string.
|
|
181
|
-
*
|
|
182
|
-
* @example
|
|
183
|
-
* // If DEBUG=yes
|
|
184
|
-
* const isDebug = Env.getBoolean('DEBUG', false); // true
|
|
185
|
-
*/
|
|
186
|
-
static getBoolean(key: string, defaultValue?: boolean): boolean;
|
|
187
|
-
/**
|
|
188
|
-
* Retrieves a required environment variable as a boolean.
|
|
189
|
-
*
|
|
190
|
-
* @param {string} key - The environment variable key.
|
|
191
|
-
* @returns {boolean} Parsed boolean value.
|
|
192
|
-
* @throws {Error} If missing or invalid.
|
|
193
|
-
*/
|
|
194
|
-
static getBooleanRequired(key: string): boolean;
|
|
195
|
-
/**
|
|
196
|
-
* Parses a stringified JSON object from an environment variable.
|
|
197
|
-
*
|
|
198
|
-
* @typeParam T - The type to parse as (defaults to `object`).
|
|
199
|
-
* @param {string} key - The environment variable key.
|
|
200
|
-
* @param {T} defaultValue - Value to return if key is missing.
|
|
201
|
-
* @returns {T} Parsed object or default.
|
|
202
|
-
* @throws {Error} If the value is not valid JSON.
|
|
203
|
-
*
|
|
204
|
-
* @example
|
|
205
|
-
* // If CONFIG='{"debug":true,"api":{"url":"https://api.example.com"}}'
|
|
206
|
-
* const config = Env.getJSON('CONFIG', { debug: false });
|
|
207
|
-
* // { debug: true, api: { url: "https://api.example.com" }}
|
|
208
|
-
*/
|
|
209
|
-
static getJSON<T extends object = object>(key: string, defaultValue: T): T;
|
|
210
|
-
/**
|
|
211
|
-
* Parses a comma-separated string as an array.
|
|
212
|
-
*
|
|
213
|
-
* @typeParam T - The item type (optional, defaults to string).
|
|
214
|
-
* @param {string} key - The environment variable key.
|
|
215
|
-
* @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.
|
|
216
|
-
* @param {string} [splitter=','] - Delimiter to split on.
|
|
217
|
-
* @param {(item: string) => T} [transform] - Optional function to transform each item.
|
|
218
|
-
* @returns {T[]} An array of items.
|
|
219
|
-
*
|
|
220
|
-
* @example
|
|
221
|
-
* // If ALLOWED_IPS=127.0.0.1,192.168.1.1,10.0.0.1
|
|
222
|
-
* const ips = Env.getArray('ALLOWED_IPS');
|
|
223
|
-
* // ["127.0.0.1", "192.168.1.1", "10.0.0.1"]
|
|
224
|
-
*
|
|
225
|
-
* // With transformation function
|
|
226
|
-
* const ports = Env.getArray('PORTS', [], ',', (p) => parseInt(p, 10));
|
|
227
|
-
*/
|
|
228
|
-
static getArray<T = string>(key: string, defaultValue?: T[], splitter?: string, transform?: (item: string) => T): T[];
|
|
229
|
-
/**
|
|
230
|
-
* Parses a comma-separated list of numbers.
|
|
231
|
-
*
|
|
232
|
-
* @param {string} key - The environment variable key.
|
|
233
|
-
* @param {number[]} [defaultValue=[]] - Default value if not present.
|
|
234
|
-
* @param {string} [splitter=','] - Delimiter to split on.
|
|
235
|
-
* @returns {number[]} Array of parsed numbers.
|
|
236
|
-
*
|
|
237
|
-
* @example
|
|
238
|
-
* // If ALLOWED_PORTS=80,443,3000,8080
|
|
239
|
-
* const ports = Env.getNumberArray('ALLOWED_PORTS');
|
|
240
|
-
* // [80, 443, 3000, 8080]
|
|
241
|
-
*/
|
|
242
|
-
static getNumberArray(key: string, defaultValue?: number[], splitter?: string): number[];
|
|
243
|
-
/**
|
|
244
|
-
* Retrieves an enum-like environment variable value, validating against allowed values.
|
|
245
|
-
*
|
|
246
|
-
* @typeParam T - The allowed value type (string literal types).
|
|
247
|
-
* @param {string} key - The environment variable key.
|
|
248
|
-
* @param {T} [defaultValue] - Optional fallback value.
|
|
249
|
-
* @param {T[]} allowedValues - Array of accepted string values.
|
|
250
|
-
* @returns {T} The validated environment value.
|
|
251
|
-
* @throws {Error} If missing or invalid.
|
|
252
|
-
*
|
|
253
|
-
* @example
|
|
254
|
-
* // If LOG_LEVEL=debug
|
|
255
|
-
* const level = Env.getEnum('LOG_LEVEL', 'info', ['debug', 'info', 'warn', 'error'] as const);
|
|
256
|
-
* // 'debug' (typed as 'debug' | 'info' | 'warn' | 'error')
|
|
257
|
-
*/
|
|
258
|
-
static getEnum<T extends string>(key: string, defaultValue: T, allowedValues: readonly T[]): T;
|
|
259
|
-
/**
|
|
260
|
-
* Retrieves an enum-like numeric environment variable.
|
|
261
|
-
*
|
|
262
|
-
* @param {string} key - The environment variable key.
|
|
263
|
-
* @param {number} defaultValue - Default value if not present.
|
|
264
|
-
* @param {number[]} allowedValues - Array of accepted values.
|
|
265
|
-
* @returns {number} The validated value.
|
|
266
|
-
*
|
|
267
|
-
* @example
|
|
268
|
-
* // If NODE_VERSION=16
|
|
269
|
-
* const version = Env.getNumberEnum('NODE_VERSION', 16, [14, 16, 18]);
|
|
270
|
-
*/
|
|
271
|
-
static getNumberEnum(key: string, defaultValue: number, allowedValues: number[]): number;
|
|
272
|
-
/**
|
|
273
|
-
* Retrieves a URL environment variable and validates it.
|
|
274
|
-
*
|
|
275
|
-
* @param {string} key - The environment variable key.
|
|
276
|
-
* @param {string} [defaultValue] - Optional fallback value.
|
|
277
|
-
* @param {UrlOptions} [options] - Validation options.
|
|
278
|
-
* @returns {string} The validated URL.
|
|
279
|
-
* @throws {Error} If URL is invalid.
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* // Validate API URL requires HTTPS
|
|
283
|
-
* const apiUrl = Env.getUrl('API_URL', 'https://api.example.com', {
|
|
284
|
-
* protocols: ['https'],
|
|
285
|
-
* requireTld: true,
|
|
286
|
-
* allowIp: false
|
|
287
|
-
* });
|
|
288
|
-
*/
|
|
289
|
-
static getUrl(key: string, defaultValue: string, options?: UrlOptions): string;
|
|
290
|
-
/**
|
|
291
|
-
* Retrieves an email environment variable and validates it.
|
|
292
|
-
*
|
|
293
|
-
* @param {string} key - The environment variable key.
|
|
294
|
-
* @param {string} [defaultValue] - Optional fallback value.
|
|
295
|
-
* @returns {string} The validated email address.
|
|
296
|
-
* @throws {Error} If email is invalid.
|
|
297
|
-
*
|
|
298
|
-
* @example
|
|
299
|
-
* const supportEmail = Env.getEmail('SUPPORT_EMAIL', 'support@example.com');
|
|
300
|
-
*/
|
|
301
|
-
static getEmail(key: string, defaultValue: string): string;
|
|
302
|
-
/**
|
|
303
|
-
* Retrieves a path environment variable and validates it exists.
|
|
304
|
-
*
|
|
305
|
-
* @param {string} key - The environment variable key.
|
|
306
|
-
* @param {string} [defaultValue] - Optional fallback value.
|
|
307
|
-
* @param {PathOptions} [options] - Validation options.
|
|
308
|
-
* @returns {string} The validated path.
|
|
309
|
-
* @throws {Error} If path is invalid.
|
|
310
|
-
*
|
|
311
|
-
* @example
|
|
312
|
-
* // Require that the path exists and is a .json file
|
|
313
|
-
* const configPath = Env.getPath('CONFIG_PATH', './config.json', {
|
|
314
|
-
* mustExist: true,
|
|
315
|
-
* allowedExtensions: ['.json', '.yaml']
|
|
316
|
-
* });
|
|
317
|
-
*/
|
|
318
|
-
static getPath(key: string, defaultValue: string, options?: PathOptions): string;
|
|
319
|
-
/**
|
|
320
|
-
* Retrieves a port environment variable and validates it.
|
|
321
|
-
*
|
|
322
|
-
* @param {string} key - The environment variable key.
|
|
323
|
-
* @param {number} [defaultValue] - Optional fallback value.
|
|
324
|
-
* @returns {number} The validated port number.
|
|
325
|
-
* @throws {Error} If port is invalid.
|
|
326
|
-
*
|
|
327
|
-
* @example
|
|
328
|
-
* const serverPort = Env.getPort('PORT', 3000);
|
|
329
|
-
*/
|
|
330
|
-
static getPort(key: string, defaultValue: number): number;
|
|
331
|
-
/**
|
|
332
|
-
* Retrieves an ISO date string and converts it to a Date object.
|
|
333
|
-
*
|
|
334
|
-
* @param {string} key - The environment variable key.
|
|
335
|
-
* @param {string|Date} [defaultValue] - Optional fallback value.
|
|
336
|
-
* @returns {Date} The parsed Date object.
|
|
337
|
-
*
|
|
338
|
-
* @example
|
|
339
|
-
* // If EXPIRY_DATE=2023-12-31T23:59:59Z
|
|
340
|
-
* const expiryDate = Env.getDate('EXPIRY_DATE', new Date());
|
|
341
|
-
*/
|
|
342
|
-
static getDate(key: string, defaultValue?: string | Date): Date;
|
|
343
|
-
/**
|
|
344
|
-
* Retrieves a duration string and converts it to milliseconds.
|
|
345
|
-
* Supports formats like "1d", "2h", "30m", "45s", "100ms" or combinations like "1h30m".
|
|
346
|
-
*
|
|
347
|
-
* @param {string} key - The environment variable key.
|
|
348
|
-
* @param {string|number} [defaultValue='0'] - Optional fallback value.
|
|
349
|
-
* @returns {number} The duration in milliseconds.
|
|
350
|
-
* @throws {Error} If duration format is invalid.
|
|
351
|
-
*
|
|
352
|
-
* @example
|
|
353
|
-
* // If CACHE_TTL=2h30m
|
|
354
|
-
* const cacheTtlMs = Env.getDuration('CACHE_TTL', '1h');
|
|
355
|
-
* // 9000000 (2.5 hours in milliseconds)
|
|
356
|
-
*/
|
|
357
|
-
static getDuration(key: string, defaultValue?: string | number): number;
|
|
358
|
-
/**
|
|
359
|
-
* Gets all environment variables with sensitive values masked for safe logging.
|
|
360
|
-
*
|
|
361
|
-
* @param {string[]} [sensitiveKeys=['password', 'secret', 'key', 'token', 'auth']] - Keys to mask.
|
|
362
|
-
* @returns {Record<string, string>} Environment variables with sensitive values masked.
|
|
363
|
-
*
|
|
364
|
-
* @example
|
|
365
|
-
* console.log(Env.getSafeEnv(['password', 'secret', 'key']));
|
|
366
|
-
* // { DATABASE_URL: "postgres://...", API_KEY: "******", ... }
|
|
367
|
-
*/
|
|
368
|
-
static getSafeEnv(sensitiveKeys?: string[]): Record<string, string>;
|
|
369
|
-
/**
|
|
370
|
-
* Loads environment variables from a .env file.
|
|
371
|
-
* Does not override existing variables.
|
|
372
|
-
*
|
|
373
|
-
* @param {string} [path='.env'] - Path to the .env file.
|
|
374
|
-
* @returns {Record<string, string>} Loaded environment variables.
|
|
375
|
-
*
|
|
376
|
-
* @example
|
|
377
|
-
* // Load variables from .env.development
|
|
378
|
-
* Env.loadFromFile('.env.development');
|
|
379
|
-
*/
|
|
380
|
-
static loadFromFile(path?: string): Record<string, string>;
|
|
381
|
-
/**
|
|
382
|
-
* Checks whether the specified environment variable exists.
|
|
383
|
-
*
|
|
384
|
-
* @param {string} key - The environment variable key.
|
|
385
|
-
* @returns {boolean} `true` if the variable is defined, otherwise `false`.
|
|
386
|
-
*/
|
|
387
|
-
static has(key: string): boolean;
|
|
388
|
-
/**
|
|
389
|
-
* Deletes the given environment variable (useful in tests).
|
|
390
|
-
*
|
|
391
|
-
* @param {string} key - The environment variable key to delete.
|
|
392
|
-
* @returns {void}
|
|
393
|
-
*/
|
|
394
|
-
static delete(key: string): void;
|
|
395
|
-
/**
|
|
396
|
-
* Clears the internal cache of parsed environment values.
|
|
397
|
-
* Useful for testing or when environment variables might change.
|
|
398
|
-
*/
|
|
399
|
-
static clearCache(): void;
|
|
400
|
-
}
|