@bouko/ts 0.2.1 → 0.2.3
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types/generic.d.ts +1 -1
- package/dist/types/generic.js +1 -1
- package/dist/utils/env.d.ts +5 -0
- package/dist/utils/env.js +23 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { JsonMap, JsonValue } from "./types/generic";
|
|
|
2
2
|
export * from "./types/generic";
|
|
3
3
|
export * from "./types/work";
|
|
4
4
|
export * from "./utils/checkers";
|
|
5
|
+
export * from "./utils/env";
|
|
5
6
|
export * from "./utils/formatters";
|
|
6
7
|
export * from "./utils/parsers";
|
|
7
8
|
export declare const safely: <T>(action: () => T) => T | undefined;
|
package/dist/index.js
CHANGED
package/dist/types/generic.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* - `JsonValue`: Union type representing a valid JSON value.
|
|
7
7
|
*
|
|
8
8
|
* They are intended to facilitate type-safe handling
|
|
9
|
-
* of generic objects (ex. forms)
|
|
9
|
+
* of generic objects (ex. forms).
|
|
10
10
|
**/
|
|
11
11
|
export declare type StringMap = Record<string, string>;
|
|
12
12
|
export declare type JsonMap = {
|
package/dist/types/generic.js
CHANGED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function getEnv(key: string): string;
|
|
2
|
+
export declare function getEnv(key: string, fallback: string): string;
|
|
3
|
+
export declare function getEnv(key: string, fallback: number): number;
|
|
4
|
+
export declare function getEnv(key: string, fallback: boolean): boolean;
|
|
5
|
+
export declare function getEnv<T>(key: string): T;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { parseStr } from "./parsers";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves and parses an environment variable.
|
|
4
|
+
*
|
|
5
|
+
* @param key - The variable name
|
|
6
|
+
* @param fallback - Backup if no value is found (optional)
|
|
7
|
+
*
|
|
8
|
+
* @returns {JsonValue} The value parsed based on whichever
|
|
9
|
+
* primitive type the string matches.
|
|
10
|
+
*
|
|
11
|
+
* @throws {Error} If the variable is not found in the
|
|
12
|
+
* environment and there is no fallback set.
|
|
13
|
+
**/
|
|
14
|
+
export function getEnv(key, fallback) {
|
|
15
|
+
const raw = process.env[key];
|
|
16
|
+
if (!raw || raw === "") {
|
|
17
|
+
if (fallback !== undefined)
|
|
18
|
+
return fallback;
|
|
19
|
+
throw new Error(`ENV Missing: '${key}'`);
|
|
20
|
+
}
|
|
21
|
+
return parseStr(raw);
|
|
22
|
+
}
|
|
23
|
+
// clean
|