@bouko/ts 0.1.5 → 0.1.7
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/core/env.d.ts +11 -0
- package/dist/core/env.js +24 -0
- package/dist/core/parse.d.ts +2 -0
- package/dist/core/parse.js +14 -0
- package/dist/core/safety.d.ts +0 -0
- package/dist/core/safety.js +1 -0
- package/dist/core/types.d.ts +2 -0
- package/dist/core/types.js +5 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/types/env.d.ts +1 -0
- package/dist/types/env.js +1 -0
- package/package.json +2 -1
- /package/dist/{types.d.ts → types/generic.d.ts} +0 -0
- /package/dist/{types.js → types/generic.js} +0 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { EnvValue } from "../types/env";
|
|
2
|
+
export declare function getEnv<T extends EnvValue = string>(key: string, fallback?: T): T;
|
|
3
|
+
/**
|
|
4
|
+
* Problems:
|
|
5
|
+
*
|
|
6
|
+
* - Perfect `EnvValue`
|
|
7
|
+
* - Perfect `isNum`
|
|
8
|
+
* - Perfect `isBool`
|
|
9
|
+
* - Perfect `parseNum`
|
|
10
|
+
* - Perfect `parseBool`
|
|
11
|
+
**/
|
package/dist/core/env.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isNum, isBool } from "./types";
|
|
2
|
+
import { parseNum, parseBool } from "./parse";
|
|
3
|
+
export function getEnv(key, fallback) {
|
|
4
|
+
const value = process.env[key]?.trim();
|
|
5
|
+
if (!value || value === "") {
|
|
6
|
+
if (fallback !== undefined)
|
|
7
|
+
return fallback;
|
|
8
|
+
throw new Error(`Missing ENV: '${key}'`);
|
|
9
|
+
}
|
|
10
|
+
if (isNum(value))
|
|
11
|
+
return parseNum(value);
|
|
12
|
+
if (isBool(value))
|
|
13
|
+
return parseBool(value);
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Problems:
|
|
18
|
+
*
|
|
19
|
+
* - Perfect `EnvValue`
|
|
20
|
+
* - Perfect `isNum`
|
|
21
|
+
* - Perfect `isBool`
|
|
22
|
+
* - Perfect `parseNum`
|
|
23
|
+
* - Perfect `parseBool`
|
|
24
|
+
**/
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const parseNum = (input) => {
|
|
2
|
+
const num = Number(input.trim());
|
|
3
|
+
if (!isNaN(num))
|
|
4
|
+
return num;
|
|
5
|
+
};
|
|
6
|
+
export const parseBool = (input) => {
|
|
7
|
+
const lower = input.trim().toLowerCase();
|
|
8
|
+
if (lower === "true")
|
|
9
|
+
return true;
|
|
10
|
+
if (lower === "false")
|
|
11
|
+
return false;
|
|
12
|
+
else
|
|
13
|
+
throw new Error(`Invalid boolean: ${input}`);
|
|
14
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { JsonMap, JsonValue } from "./types";
|
|
2
|
-
export * from "./types";
|
|
1
|
+
import type { JsonMap, JsonValue } from "./types/generic";
|
|
2
|
+
export * from "./types/generic";
|
|
3
|
+
export * from "./core/env";
|
|
3
4
|
export declare const safely: <T>(action: () => T) => T | undefined;
|
|
4
5
|
export declare const safelyThru: <T>(arr: T[], action: (item: T) => void) => void;
|
|
5
6
|
export declare const isJsonMap: (value: unknown) => value is JsonMap;
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type EnvValue = string | number | boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
|
|
3
3
|
"name": "@bouko/ts",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.7",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"license": "MIT",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"author": "",
|
|
15
15
|
"description": "",
|
|
16
|
+
"engines": {},
|
|
16
17
|
|
|
17
18
|
"scripts": {
|
|
18
19
|
"build": "tsc"
|
|
File without changes
|
|
File without changes
|