You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Evaluate with user context - overrides apply automatically
68
+
const enabled =billingEnabled.getValue({
69
+
userId: "user-123",
70
+
plan: "premium",
71
+
region: "us-east",
72
+
});
68
73
69
74
// When done, clean up resources
75
+
featureFlag.close();
76
+
passwordReqs.close();
70
77
billingEnabled.close();
71
78
72
-
// Or, if you don't need the client anymore
79
+
// Or close all watchers at once
73
80
client.close();
74
81
```
75
82
76
83
## API
77
84
78
85
### `createReplaneClient(options)`
79
86
80
-
Returns an object: `{ getConfigValue, watchConfigValue, close }`.
87
+
Returns an object: `{ watchConfig, close }`.
81
88
82
-
`close()` stops all active watchers created by this client and marks the client as closed. After calling it, any subsequent call to `getConfigValue` or `watchConfigValue` will throw. It is safe to call multiple times (no‑op after the first call).
89
+
`close()` stops all active watchers created by this client and marks the client as closed. After calling it, any subsequent call to `watchConfig` will throw. It is safe to call multiple times (no‑op after the first call).
83
90
84
91
#### Options
85
92
86
93
-`baseUrl` (string) – API origin (no trailing slash needed).
87
94
-`apiKey` (string) – API key for authorization. Required. **Note:** Each API key is tied to a specific project and can only access configs from that project. To access configs from multiple projects, create multiple API keys and initialize separate client instances.
88
-
-`fetchFn` (function) – custom fetch (e.g. `undici.fetch` or mocked fetch in tests).
95
+
-`context` (object) – default context for all config evaluations. Can be overridden per-request in `watcher.getValue()`. Optional.
96
+
-`fetchFn` (function) – custom fetch (e.g. `undici.fetch` or mocked fetch in tests). Optional.
89
97
-`timeoutMs` (number) – abort the request after N ms. Default: 2000.
90
98
-`retries` (number) – number of retry attempts on failures (5xx or network errors). Default: 2.
91
-
-`retryDelayMs` (number) – base delay between retries in ms (a small jitter is applied). Default: 100.
92
-
93
-
### `client.getConfigValue(name, overrides?)`
94
-
95
-
Parameters:
99
+
-`retryDelayMs` (number) – base delay between retries in ms (a small jitter is applied). Default: 200.
- Overrides: same semantics as in `createReplaneClient`.
102
+
### `client.watchConfig(name, options?)`
99
103
100
-
Returns: a promise resolving to the parsed JSON value.
101
-
102
-
Errors: throws on non-2xx responses (including 404 for missing configs), network errors, or invalid JSON. Catch `ReplaneError` to handle failures.
103
-
104
-
Retry behavior:
104
+
Creates a lightweight watcher that receives realtime updates for the config value via Server-Sent Events (SSE). Useful for long‑lived processes wanting instant updates without manually refetching.
105
105
106
-
- Transient failures (5xx responses or network errors) are retried up to `retries` times with a base delay of `retryDelayMs` between attempts.
107
-
- You can override these per call via the `overrides` argument.
106
+
Parameters:
108
107
109
-
### `client.watchConfigValue(name, overrides?)`
108
+
-`name` (string) – config name to watch.
109
+
-`options` (object) – optional configuration:
110
+
-`context` (object) – context merged with client-level context for override evaluation.
110
111
111
-
Creates a lightweight watcher that receives realtime updates for the config value via Server-Sent Events (SSE). Useful for long‑lived processes wanting instant updates without manually refetching.
112
+
Returns a promise resolving to an object: `{ getValue(context?): T, close(): void }`.
112
113
113
-
Returns a promise resolving to an object: `{ get(): T, close(): void }`.
114
-
115
-
-`get()` – returns the most recent value.
116
-
-`close()` – stops watching for updates. Further calls to `get()` after `close()` throw.
114
+
-`getValue(context?)` – returns the current value with override evaluation based on provided context (merged with client and watcher contexts). The value is always up-to-date thanks to realtime SSE updates.
115
+
-`close()` – stops watching for updates. Further calls to `getValue()` after `close()` throw.
117
116
118
117
Notes:
119
118
120
119
- The initial fetch must succeed (it will throw on errors).
121
120
- Subsequent updates are pushed from the server in realtime via SSE.
121
+
- Values are automatically refreshed every 60 seconds as a fallback.
if (billingEnabled.getValue({ userId: "user-123", plan: "premium" })) {
141
+
// ...
142
+
}
143
+
136
144
// Later, when you no longer need updates:
137
145
billingEnabled.close();
138
146
```
@@ -145,13 +153,13 @@ Parameters:
145
153
146
154
-`initialData` (object) – map of config name to value.
147
155
148
-
Returns the same client shape as `createReplaneClient` (`{ getConfigValue, watchConfigValue, close }`).
156
+
Returns the same client shape as `createReplaneClient` (`{ watchConfig, close }`).
149
157
150
158
Notes:
151
159
152
-
-`getConfigValue(name)` resolves to the value from `initialData`.
160
+
-`watchConfig(name)` resolves to a watcher with the value from `initialData`.
153
161
- If a name is missing, it throws a `ReplaneError` (`Config not found: <name>`).
154
-
-`watchConfigValue` works as usual, but uses periodic refresh (every 60s) instead of SSE since there's no server connection (values remain whatever is in-memory).
162
+
-Watchers work as usual but don't receive SSE updates (values remain whatever is in-memory).
155
163
156
164
Example:
157
165
@@ -160,13 +168,19 @@ import { createInMemoryReplaneClient } from "replane-sdk";
`getConfigValue` throws on non‑2xx HTTP responses (including 404), network errors, and invalid JSON. `watchConfigValue` uses `getConfigValue` for its initial fetch; handle errors accordingly with try/catch when creating a watcher. A `ReplaneError` is thrown for HTTP failures; other errors may be thrown for network/parse issues.
197
+
`watchConfig` throws on non‑2xx HTTP responses (including 404), network errors, and invalid JSON during the initial fetch. Handle errors with try/catch when creating a watcher. A `ReplaneError` is thrown for HTTP failures; other errors may be thrown for network/parse issues.
198
+
199
+
After the initial fetch succeeds, subsequent SSE update errors are logged but don't throw (the watcher continues to work with the last known value).
0 commit comments