@smplkit/sdk 1.3.38 → 1.3.40
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 +148 -27
- package/dist/index.cjs +164 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +149 -42
- package/dist/index.d.ts +149 -42
- package/dist/index.js +161 -71
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ The official TypeScript SDK for [smplkit](https://www.smplkit.com) — simple ap
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
npm install smplkit
|
|
10
|
+
npm install @smplkit/sdk
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
## Requirements
|
|
@@ -17,7 +17,7 @@ npm install smplkit-sdk
|
|
|
17
17
|
## Quick Start
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
|
-
import { SmplClient } from "smplkit
|
|
20
|
+
import { SmplClient } from "@smplkit/sdk";
|
|
21
21
|
|
|
22
22
|
// Option 1: Explicit API key
|
|
23
23
|
const client = new SmplClient({ apiKey: "sk_api_..." });
|
|
@@ -32,29 +32,6 @@ const client2 = new SmplClient();
|
|
|
32
32
|
const client3 = new SmplClient();
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
```typescript
|
|
36
|
-
import { SmplClient } from "smplkit-sdk";
|
|
37
|
-
|
|
38
|
-
const client = new SmplClient({ apiKey: "sk_api_..." });
|
|
39
|
-
|
|
40
|
-
// Get a config by key
|
|
41
|
-
const config = await client.config.getByKey("user_service");
|
|
42
|
-
|
|
43
|
-
// List all configs
|
|
44
|
-
const configs = await client.config.list();
|
|
45
|
-
|
|
46
|
-
// Create a config
|
|
47
|
-
const newConfig = await client.config.create({
|
|
48
|
-
name: "My Service",
|
|
49
|
-
key: "my_service",
|
|
50
|
-
description: "Configuration for my service",
|
|
51
|
-
values: { timeout: 30, retries: 3 },
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// Delete a config
|
|
55
|
-
await client.config.delete(newConfig.id);
|
|
56
|
-
```
|
|
57
|
-
|
|
58
35
|
## Configuration
|
|
59
36
|
|
|
60
37
|
The API key is resolved using the following priority:
|
|
@@ -79,15 +56,159 @@ const client = new SmplClient({
|
|
|
79
56
|
});
|
|
80
57
|
```
|
|
81
58
|
|
|
59
|
+
## Config
|
|
60
|
+
|
|
61
|
+
### Runtime (resolve config values)
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// Resolve config values for a service
|
|
65
|
+
const config = await client.config.get("my-service");
|
|
66
|
+
console.log(config.getString("timeout"));
|
|
67
|
+
console.log(config.getNumber("retries"));
|
|
68
|
+
|
|
69
|
+
// Subscribe to live updates
|
|
70
|
+
client.config.subscribe("my-service", (config) => {
|
|
71
|
+
console.log("Config updated:", config.getString("timeout"));
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Management (CRUD)
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
// Create a config
|
|
79
|
+
const cfg = client.config.management.new("my-service", {
|
|
80
|
+
name: "My Service",
|
|
81
|
+
description: "Configuration for my service",
|
|
82
|
+
});
|
|
83
|
+
await cfg.save();
|
|
84
|
+
|
|
85
|
+
// List configs
|
|
86
|
+
const configs = await client.config.management.list();
|
|
87
|
+
|
|
88
|
+
// Get a config by id
|
|
89
|
+
const fetched = await client.config.management.get("my-service");
|
|
90
|
+
|
|
91
|
+
// Delete a config
|
|
92
|
+
await client.config.management.delete("my-service");
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Flags
|
|
96
|
+
|
|
97
|
+
### Runtime (evaluate flags)
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { SmplClient, Rule } from "@smplkit/sdk";
|
|
101
|
+
|
|
102
|
+
const client = new SmplClient({ environment: "production", service: "my-service" });
|
|
103
|
+
|
|
104
|
+
// Declare a flag
|
|
105
|
+
const checkoutFlag = client.flags.booleanFlag("checkout-v2", { default: false });
|
|
106
|
+
|
|
107
|
+
// Start the client (connects and fetches flags)
|
|
108
|
+
await client.flags.initialize();
|
|
109
|
+
|
|
110
|
+
// Evaluate with context
|
|
111
|
+
const enabled = await checkoutFlag.get({ user: { plan: "enterprise" } });
|
|
112
|
+
console.log("checkout-v2:", enabled);
|
|
113
|
+
|
|
114
|
+
client.close();
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Management (CRUD)
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// Create flags
|
|
121
|
+
const boolFlag = client.flags.management.newBooleanFlag("checkout-v2", {
|
|
122
|
+
default: false,
|
|
123
|
+
description: "Controls rollout of the new checkout experience.",
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Add targeting rules
|
|
127
|
+
boolFlag.addRule(
|
|
128
|
+
new Rule("Enable for enterprise users")
|
|
129
|
+
.environment("production")
|
|
130
|
+
.when("user.plan", "==", "enterprise")
|
|
131
|
+
.serve(true)
|
|
132
|
+
.build(),
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
// Configure environments
|
|
136
|
+
boolFlag.setEnvironmentEnabled("production", true);
|
|
137
|
+
boolFlag.setEnvironmentDefault("production", false);
|
|
138
|
+
|
|
139
|
+
await boolFlag.save();
|
|
140
|
+
|
|
141
|
+
// Other factory methods
|
|
142
|
+
const strFlag = client.flags.management.newStringFlag("banner-color", {
|
|
143
|
+
default: "red",
|
|
144
|
+
values: [{ name: "Red", value: "red" }, { name: "Blue", value: "blue" }],
|
|
145
|
+
});
|
|
146
|
+
const numFlag = client.flags.management.newNumberFlag("max-retries", { default: 3 });
|
|
147
|
+
const jsonFlag = client.flags.management.newJsonFlag("ui-theme", {
|
|
148
|
+
default: { mode: "light" },
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// List / get / delete
|
|
152
|
+
const flags = await client.flags.management.list();
|
|
153
|
+
const flag = await client.flags.management.get("checkout-v2");
|
|
154
|
+
await client.flags.management.delete("checkout-v2");
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Logging
|
|
158
|
+
|
|
159
|
+
### Runtime (live log level management)
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
const client = new SmplClient({ environment: "production", service: "my-service" });
|
|
163
|
+
|
|
164
|
+
// Register an adapter for your logging library
|
|
165
|
+
client.logging.registerAdapter(myAdapter);
|
|
166
|
+
|
|
167
|
+
// Start the logging runtime (connects and fetches log levels)
|
|
168
|
+
await client.logging.start();
|
|
169
|
+
|
|
170
|
+
client.logging.onChange((loggers) => {
|
|
171
|
+
console.log("Log levels updated:", loggers.map((l) => `${l.id}=${l.level}`));
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Management (CRUD)
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
// Create a logger
|
|
179
|
+
const logger = client.logging.management.new("sqlalchemy.engine", { managed: true });
|
|
180
|
+
logger.setLevel(LogLevel.WARN);
|
|
181
|
+
logger.setEnvironmentLevel("production", LogLevel.ERROR);
|
|
182
|
+
await logger.save();
|
|
183
|
+
|
|
184
|
+
// Create a log group
|
|
185
|
+
const group = client.logging.management.newGroup("sql", { name: "SQL Loggers" });
|
|
186
|
+
group.setLevel(LogLevel.WARN);
|
|
187
|
+
await group.save();
|
|
188
|
+
|
|
189
|
+
// Assign logger to group
|
|
190
|
+
logger.group = group.id;
|
|
191
|
+
await logger.save();
|
|
192
|
+
|
|
193
|
+
// List / get / delete
|
|
194
|
+
const loggers = await client.logging.management.list();
|
|
195
|
+
const fetched = await client.logging.management.get("Sqlalchemy.Engine");
|
|
196
|
+
await client.logging.management.delete("Sqlalchemy.Engine");
|
|
197
|
+
|
|
198
|
+
const groups = await client.logging.management.listGroups();
|
|
199
|
+
const fetchedGroup = await client.logging.management.getGroup(group.id);
|
|
200
|
+
await client.logging.management.deleteGroup(group.id);
|
|
201
|
+
```
|
|
202
|
+
|
|
82
203
|
## Error Handling
|
|
83
204
|
|
|
84
205
|
All SDK errors extend `SmplError`:
|
|
85
206
|
|
|
86
207
|
```typescript
|
|
87
|
-
import { SmplError, SmplNotFoundError } from "smplkit
|
|
208
|
+
import { SmplError, SmplNotFoundError } from "@smplkit/sdk";
|
|
88
209
|
|
|
89
210
|
try {
|
|
90
|
-
const
|
|
211
|
+
const flag = await client.flags.management.get("nonexistent");
|
|
91
212
|
} catch (err) {
|
|
92
213
|
if (err instanceof SmplNotFoundError) {
|
|
93
214
|
console.log("Not found:", err.message);
|