@replanejs/sdk 0.5.12 → 0.6.2
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/index.cjs +10 -609
- package/dist/index.d.ts +201 -46
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +569 -345
- package/dist/index.js.map +1 -0
- package/package.json +6 -13
- package/LICENSE +0 -21
- package/README.md +0 -479
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,84 @@
|
|
|
1
|
-
//#region src/
|
|
1
|
+
//#region src/client-types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Base type for config objects
|
|
4
|
+
*/
|
|
2
5
|
type Configs = object;
|
|
3
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Context object for override evaluation.
|
|
8
|
+
* Keys are property names, values can be strings, numbers, booleans, null, or undefined.
|
|
9
|
+
*/
|
|
10
|
+
type ReplaneContext = Record<string, string | number | boolean | null | undefined>;
|
|
11
|
+
/**
|
|
12
|
+
* Logger interface for SDK logging
|
|
13
|
+
*/
|
|
14
|
+
interface ReplaneLogger {
|
|
15
|
+
debug(...args: unknown[]): void;
|
|
16
|
+
info(...args: unknown[]): void;
|
|
17
|
+
warn(...args: unknown[]): void;
|
|
18
|
+
error(...args: unknown[]): void;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Options for getting a config value
|
|
22
|
+
*/
|
|
23
|
+
interface GetConfigOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Context for override evaluation (merged with client-level context).
|
|
26
|
+
*/
|
|
27
|
+
context?: ReplaneContext;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Helper type for mapping configs to their names and values
|
|
31
|
+
*/
|
|
32
|
+
type MapConfig<T extends Configs> = { [K in keyof T]: {
|
|
33
|
+
name: K;
|
|
34
|
+
value: T[K];
|
|
35
|
+
} }[keyof T];
|
|
36
|
+
/**
|
|
37
|
+
* Serializable snapshot of the client state.
|
|
38
|
+
* Can be used to restore a client on the client-side from server-fetched configs.
|
|
39
|
+
*/
|
|
40
|
+
interface ReplaneSnapshot<_T extends Configs = Configs> {
|
|
41
|
+
/** Serialized config data */
|
|
42
|
+
configs: Array<{
|
|
43
|
+
name: string;
|
|
44
|
+
value: unknown;
|
|
45
|
+
overrides: Array<{
|
|
46
|
+
name: string;
|
|
47
|
+
conditions: unknown[];
|
|
48
|
+
value: unknown;
|
|
49
|
+
}>;
|
|
50
|
+
}>;
|
|
51
|
+
/** Default context used for override evaluation */
|
|
52
|
+
context?: ReplaneContext;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The Replane client interface
|
|
56
|
+
*/
|
|
57
|
+
interface ReplaneClient<T extends Configs> {
|
|
58
|
+
/** Get a config by its name. */
|
|
59
|
+
get<K extends keyof T>(configName: K, options?: GetConfigOptions): T[K];
|
|
60
|
+
/** Subscribe to config changes.
|
|
61
|
+
* @param callback - A function to call when an config is changed. The callback will be called with the new config value.
|
|
62
|
+
* @returns A function to unsubscribe from the config changes.
|
|
63
|
+
*/
|
|
64
|
+
subscribe(callback: (config: MapConfig<T>) => void): () => void;
|
|
65
|
+
/** Subscribe to a specific config change.
|
|
66
|
+
* @param configName - The name of the config to subscribe to.
|
|
67
|
+
* @param callback - A function to call when the config is changed. The callback will be called with the new config value.
|
|
68
|
+
* @returns A function to unsubscribe from the config changes.
|
|
69
|
+
*/
|
|
70
|
+
subscribe<K extends keyof T>(configName: K, callback: (config: MapConfig<Pick<T, K>>) => void): () => void;
|
|
71
|
+
/**
|
|
72
|
+
* Get a serializable snapshot of the current client state.
|
|
73
|
+
* Useful for SSR/hydration scenarios where you want to pass configs from server to client.
|
|
74
|
+
*/
|
|
75
|
+
getSnapshot(): ReplaneSnapshot<T>;
|
|
76
|
+
/** Close the client and clean up resources. */
|
|
77
|
+
close(): void;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Options for creating a Replane client
|
|
81
|
+
*/
|
|
4
82
|
interface ReplaneClientOptions<T extends Configs> {
|
|
5
83
|
/**
|
|
6
84
|
* Base URL of the Replane instance (no trailing slash).
|
|
@@ -39,7 +117,7 @@ interface ReplaneClientOptions<T extends Configs> {
|
|
|
39
117
|
/**
|
|
40
118
|
* Timeout in ms for SSE connection inactivity.
|
|
41
119
|
* If no events (including pings) are received within this time, the connection will be re-established.
|
|
42
|
-
* @default
|
|
120
|
+
* @default 30000
|
|
43
121
|
*/
|
|
44
122
|
inactivityTimeoutMs?: number;
|
|
45
123
|
/**
|
|
@@ -82,60 +160,137 @@ interface ReplaneClientOptions<T extends Configs> {
|
|
|
82
160
|
*/
|
|
83
161
|
fallbacks?: { [K in keyof T]: T[K] };
|
|
84
162
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
error(...args: unknown[]): void;
|
|
90
|
-
}
|
|
91
|
-
interface GetConfigOptions {
|
|
163
|
+
/**
|
|
164
|
+
* Options for restoring a Replane client from a snapshot
|
|
165
|
+
*/
|
|
166
|
+
interface RestoreReplaneClientOptions<T extends Configs> {
|
|
92
167
|
/**
|
|
93
|
-
*
|
|
168
|
+
* Snapshot from a server-side client's getSnapshot() call.
|
|
94
169
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
} }[keyof T];
|
|
101
|
-
interface ReplaneClient<T extends Configs> {
|
|
102
|
-
/** Get a config by its name. */
|
|
103
|
-
get<K extends keyof T>(configName: K, options?: GetConfigOptions): T[K];
|
|
104
|
-
/** Subscribe to config changes.
|
|
105
|
-
* @param callback - A function to call when an config is changed. The callback will be called with the new config value.
|
|
106
|
-
* @returns A function to unsubscribe from the config changes.
|
|
170
|
+
snapshot: ReplaneSnapshot<T>;
|
|
171
|
+
/**
|
|
172
|
+
* Optional connection options for live updates.
|
|
173
|
+
* If provided, the client will connect to the Replane server for real-time config updates.
|
|
174
|
+
* If not provided, the client will only use the snapshot data (no live updates).
|
|
107
175
|
*/
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
176
|
+
connection?: {
|
|
177
|
+
/**
|
|
178
|
+
* Base URL of the Replane instance (no trailing slash).
|
|
179
|
+
*/
|
|
180
|
+
baseUrl: string;
|
|
181
|
+
/**
|
|
182
|
+
* Project SDK key for authorization.
|
|
183
|
+
*/
|
|
184
|
+
sdkKey: string;
|
|
185
|
+
/**
|
|
186
|
+
* Custom fetch implementation (useful for tests / polyfills).
|
|
187
|
+
*/
|
|
188
|
+
fetchFn?: typeof fetch;
|
|
189
|
+
/**
|
|
190
|
+
* Optional timeout in ms for the request.
|
|
191
|
+
* @default 2000
|
|
192
|
+
*/
|
|
193
|
+
requestTimeoutMs?: number;
|
|
194
|
+
/**
|
|
195
|
+
* Delay between retries in ms.
|
|
196
|
+
* @default 200
|
|
197
|
+
*/
|
|
198
|
+
retryDelayMs?: number;
|
|
199
|
+
/**
|
|
200
|
+
* Timeout in ms for SSE connection inactivity.
|
|
201
|
+
* @default 30000
|
|
202
|
+
*/
|
|
203
|
+
inactivityTimeoutMs?: number;
|
|
204
|
+
/**
|
|
205
|
+
* Optional logger (defaults to console).
|
|
206
|
+
*/
|
|
207
|
+
logger?: ReplaneLogger;
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* Override the context from the snapshot.
|
|
113
211
|
*/
|
|
114
|
-
|
|
115
|
-
/** Close the client and clean up resources. */
|
|
116
|
-
close(): void;
|
|
117
|
-
}
|
|
118
|
-
declare class ReplaneError extends Error {
|
|
119
|
-
code: string;
|
|
120
|
-
constructor(params: {
|
|
121
|
-
message: string;
|
|
122
|
-
code: string;
|
|
123
|
-
cause?: unknown;
|
|
124
|
-
});
|
|
212
|
+
context?: ReplaneContext;
|
|
125
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Internal options after processing user options
|
|
216
|
+
*/
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region src/client.d.ts
|
|
126
219
|
/**
|
|
127
220
|
* Create a Replane client bound to an SDK key.
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```typescript
|
|
224
|
+
* const client = await createReplaneClient({
|
|
225
|
+
* sdkKey: 'your-sdk-key',
|
|
226
|
+
* baseUrl: 'https://app.replane.dev'
|
|
227
|
+
* });
|
|
228
|
+
* const value = client.get('my-config');
|
|
229
|
+
* ```
|
|
131
230
|
*/
|
|
132
231
|
declare function createReplaneClient<T extends Configs = Record<string, unknown>>(sdkOptions: ReplaneClientOptions<T>): Promise<ReplaneClient<T>>;
|
|
133
232
|
/**
|
|
134
233
|
* Create a Replane client that uses in-memory storage.
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
234
|
+
* Useful for testing or when you have static config values.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```typescript
|
|
238
|
+
* const client = createInMemoryReplaneClient({ 'my-config': 123 });
|
|
239
|
+
* const value = client.get('my-config'); // 123
|
|
240
|
+
* ```
|
|
138
241
|
*/
|
|
139
242
|
declare function createInMemoryReplaneClient<T extends Configs = Record<string, unknown>>(initialData: T): ReplaneClient<T>;
|
|
243
|
+
/**
|
|
244
|
+
* Restore a Replane client from a snapshot.
|
|
245
|
+
* This is useful for SSR/hydration scenarios where the server has already fetched configs.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* // On the server:
|
|
250
|
+
* const serverClient = await createReplaneClient({ ... });
|
|
251
|
+
* const snapshot = serverClient.getSnapshot();
|
|
252
|
+
* // Pass snapshot to client via props/serialization
|
|
253
|
+
*
|
|
254
|
+
* // On the client:
|
|
255
|
+
* const client = restoreReplaneClient({
|
|
256
|
+
* snapshot,
|
|
257
|
+
* connection: { sdkKey, baseUrl }
|
|
258
|
+
* });
|
|
259
|
+
* const value = client.get('my-config');
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
declare function restoreReplaneClient<T extends Configs = Record<string, unknown>>(options: RestoreReplaneClientOptions<T>): ReplaneClient<T>;
|
|
263
|
+
//# sourceMappingURL=client.d.ts.map
|
|
264
|
+
//#endregion
|
|
265
|
+
//#region src/error.d.ts
|
|
266
|
+
/**
|
|
267
|
+
* Error codes for ReplaneError
|
|
268
|
+
*/
|
|
269
|
+
declare enum ReplaneErrorCode {
|
|
270
|
+
NotFound = "not_found",
|
|
271
|
+
Timeout = "timeout",
|
|
272
|
+
NetworkError = "network_error",
|
|
273
|
+
AuthError = "auth_error",
|
|
274
|
+
Forbidden = "forbidden",
|
|
275
|
+
ServerError = "server_error",
|
|
276
|
+
ClientError = "client_error",
|
|
277
|
+
Closed = "closed",
|
|
278
|
+
NotInitialized = "not_initialized",
|
|
279
|
+
Unknown = "unknown",
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Custom error class for Replane SDK errors
|
|
283
|
+
*/
|
|
284
|
+
declare class ReplaneError extends Error {
|
|
285
|
+
code: string;
|
|
286
|
+
constructor(params: {
|
|
287
|
+
message: string;
|
|
288
|
+
code: string;
|
|
289
|
+
cause?: unknown;
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
//# sourceMappingURL=error.d.ts.map
|
|
293
|
+
|
|
140
294
|
//#endregion
|
|
141
|
-
export { GetConfigOptions, ReplaneClient, ReplaneClientOptions, ReplaneContext, ReplaneError, ReplaneLogger, createInMemoryReplaneClient, createReplaneClient };
|
|
295
|
+
export { type GetConfigOptions, type ReplaneClient, type ReplaneClientOptions, type ReplaneContext, ReplaneError, ReplaneErrorCode, type ReplaneLogger, type ReplaneSnapshot, type RestoreReplaneClientOptions, createInMemoryReplaneClient, createReplaneClient, restoreReplaneClient };
|
|
296
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/client-types.ts","../src/client.ts","../src/error.ts"],"sourc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