siko 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.
- package/package.json +10 -0
- package/src/README.md +27 -0
- package/src/index.js +17 -0
package/package.json
ADDED
package/src/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# siko
|
|
2
|
+
|
|
3
|
+
Tiny, safe config loader.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
```bash
|
|
7
|
+
npm install siko
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
import { siko } from "siko";
|
|
15
|
+
|
|
16
|
+
const config = siko({
|
|
17
|
+
PORT: {
|
|
18
|
+
default: 3000,
|
|
19
|
+
parse: Number
|
|
20
|
+
},
|
|
21
|
+
NODE_ENV: {
|
|
22
|
+
default: "development"
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
console.log(config.PORT);
|
|
27
|
+
```
|
package/src/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function siko(schema, env = process.env) {
|
|
2
|
+
const result = {};
|
|
3
|
+
|
|
4
|
+
for (const key in schema) {
|
|
5
|
+
const value = env[key] ?? schema[key].default;
|
|
6
|
+
|
|
7
|
+
if (value === undefined) {
|
|
8
|
+
throw new Error(`Missing required config: ${key}`);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
result[key] = schema[key].parse
|
|
12
|
+
? schema[key].parse(value)
|
|
13
|
+
: value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return Object.freeze(result);
|
|
17
|
+
}
|