kfg 0.0.1 → 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Drylian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,15 +1,120 @@
1
- # test
1
+ # K(con)fg - Simple, Type-Safe Configuration Management
2
2
 
3
- To install dependencies:
3
+ [![npm version](https://badge.fury.io/js/kfg.svg)](https://badge.fury.io/js/kfg)
4
+ [![Documentation](https://github.com/drylian/kfg/actions/workflows/docs.yml/badge.svg)](https://kfg.drylian.com/)
5
+
6
+ Kfg is a robust and 100% type-safe configuration management system for Node.js and Bun applications. It provides a structured way to define, validate, and access environment variables and other configuration sources with the power of TypeScript.
7
+
8
+ - ✅ **Fully Typed**: Autocomplete and type safety for all your configurations.
9
+ - ✅ **Flexible Drivers**: Load configurations from `.env` files, JSON, or create your own driver.
10
+ - ✅ **Built-in Validation**: Define rules and formats (email, url, etc.) directly in the schema.
11
+ - ✅ **Smart Defaults**: Define defaults that are applied automatically.
12
+ - ✅ **Nested Structures**: Organize your configurations logically.
13
+ - ✅ **File-based Configuration**: Manage configurations across multiple files with `Kfg`.
14
+
15
+ - ✅ **Relations**: Create one-to-one and one-to-many relations between configurations.
16
+
17
+ ---
18
+
19
+ ## 📖 Documentation
20
+
21
+ - **[Full Usage Guide](https://kfg.drylian.com/)**: Learn how to install and use Kfg.
22
+
23
+ ## Installation
4
24
 
5
25
  ```bash
6
- bun install
26
+ npm install kfg
27
+ # or
28
+ yarn add kfg
29
+ # or
30
+ bun add kfg
7
31
  ```
8
32
 
9
- To run:
33
+ ## Quick Example
10
34
 
11
- ```bash
12
- bun run index.ts
35
+ **1. Define your schema (`schema.ts`):**
36
+
37
+ ```typescript
38
+ import { c } from "kfg";
39
+
40
+ export const AppSchema = {
41
+ server: {
42
+ host: c.string({ default: "0.0.0.0" }),
43
+ port: c.number({ default: 3000 }),
44
+ },
45
+ database: {
46
+ url: c.string({ prop: "DATABASE_URL" }), // Reads from the DATABASE_URL environment variable
47
+ },
48
+ };
49
+ ```
50
+
51
+ **2. Create and load your instance (`config.ts`):**
52
+
53
+ ```typescript
54
+ import { Kfg } from "kfg";
55
+ import { envDriver } from "kfg/drivers";
56
+ import { AppSchema } from "./schema";
57
+
58
+ const config = new Kfg(envDriver, AppSchema);
59
+ config.load(); // Loads values from .env and process.env
60
+
61
+ export default config;
62
+ ```
63
+
64
+ **3. Use it anywhere (`index.ts`):**
65
+
66
+ ```typescript
67
+ import config from "./config";
68
+
69
+ const port = config.get("server.port"); // Inferred as `number`
70
+ const dbUrl = config.get("database.url"); // Inferred as `string`
71
+
72
+ console.log(`Server running on port ${port}`);
73
+
74
+ // Type Error! TypeScript prevents incorrect assignments.
75
+ // config.set("server.port", "not-a-number");
76
+ ```
77
+
78
+ ## File-based Configuration with `Kfg`
79
+
80
+ `Kfg` allows you to manage configurations across multiple files, which is useful for managing configurations for different users, tenants, or environments.
81
+
82
+ **1. Define your schemas:**
83
+
84
+ ```typescript
85
+ import { Kfg, c, cfs, jsonDriver } from "kfg";
86
+
87
+ const inventory = new Kfg(jsonDriver, {
88
+ items: c.array(c.string()),
89
+ });
90
+
91
+ const user = new Kfg(jsonDriver, {
92
+ name: c.string(),
93
+ inventory_ids: cfs.many(inventory),
94
+ });
13
95
  ```
14
96
 
15
- This project was created using `bun init` in bun v1.3.1. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
97
+ **2. Initialize `Kfg`:**
98
+
99
+ ```typescript
100
+ inventory.init((id) => `resources/inventory/${id}.json`);
101
+ user.init((id) => `resources/users/${id}.json`);
102
+ ```
103
+
104
+ **3. Use it:**
105
+
106
+ ```typescript
107
+ const user1 = user.file("user-1");
108
+ user1.set("name", "Alice");
109
+
110
+ const inv1 = inventory.file("inv-1");
111
+ inv1.set("items", ["sword", "shield"]);
112
+
113
+ user1.set("inventory_ids", [inv1]);
114
+
115
+ const user1Inventories = await user1.getMany("inventory_ids");
116
+ ```
117
+
118
+ ## License
119
+
120
+ MIT