Skip to content

Commit c86c65d

Browse files
committed
chore: refactor sdk
1 parent 92c9c15 commit c86c65d

7 files changed

Lines changed: 757 additions & 1107 deletions

File tree

packages/sdk/src/client-types.ts

Lines changed: 53 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ConfigDto, RenderedOverride } from "./types";
1+
import type { RenderedOverride } from "./types";
22

33
/**
44
* Context object for override evaluation.
@@ -52,43 +52,46 @@ export interface ReplaneSnapshot<_T extends object = object> {
5252
value: unknown;
5353
overrides: RenderedOverride[];
5454
}>;
55-
/** Default context used for override evaluation */
56-
context?: ReplaneContext;
5755
}
5856

5957
/**
60-
* The Replane client interface
58+
* Options for the Replane constructor
6159
*/
62-
export interface ReplaneClient<T extends object> {
63-
/** Get a config by its name. */
64-
get<K extends keyof T>(configName: K, options?: GetConfigOptions<T[K]>): T[K];
65-
/** Subscribe to config changes.
66-
* @param callback - A function to call when an config is changed. The callback will be called with the new config value.
67-
* @returns A function to unsubscribe from the config changes.
60+
export interface ReplaneOptions<T extends object> {
61+
/**
62+
* Optional logger (defaults to console).
63+
*/
64+
logger?: ReplaneLogger;
65+
/**
66+
* Default context for all config evaluations.
67+
* Can be overridden per-request in `client.get()`.
6868
*/
69-
subscribe(callback: (config: MapConfig<T>) => void): () => void;
70-
/** Subscribe to a specific config change.
71-
* @param configName - The name of the config to subscribe to.
72-
* @param callback - A function to call when the config is changed. The callback will be called with the new config value.
73-
* @returns A function to unsubscribe from the config changes.
69+
context?: ReplaneContext;
70+
/**
71+
* Default values to use before connection is established.
72+
* These values are used immediately and can be overwritten by server data.
73+
* @example
74+
* {
75+
* defaults: {
76+
* config1: "value1",
77+
* config2: 42,
78+
* },
79+
* }
7480
*/
75-
subscribe<K extends keyof T>(
76-
configName: K,
77-
callback: (config: MapConfig<Pick<T, K>>) => void
78-
): () => void;
81+
defaults?: {
82+
[K in keyof T]?: T[K];
83+
};
7984
/**
80-
* Get a serializable snapshot of the current client state.
81-
* Useful for SSR/hydration scenarios where you want to pass configs from server to client.
85+
* Snapshot from a server-side client's getSnapshot() call.
86+
* Used for SSR/hydration scenarios.
8287
*/
83-
getSnapshot(): ReplaneSnapshot<T>;
84-
/** Close the client and clean up resources. */
85-
close(): void;
88+
snapshot?: ReplaneSnapshot<T>;
8689
}
8790

8891
/**
89-
* Options for creating a Replane client
92+
* Options for connecting to the Replane server
9093
*/
91-
export interface ReplaneClientOptions<T extends object> {
94+
export interface ConnectOptions {
9295
/**
9396
* Base URL of the Replane instance (no trailing slash).
9497
* @example
@@ -105,186 +108,56 @@ export interface ReplaneClientOptions<T extends object> {
105108
*/
106109
sdkKey: string;
107110
/**
108-
* Custom fetch implementation (useful for tests / polyfills).
109-
*/
110-
fetchFn?: typeof fetch;
111-
/**
112-
* Optional timeout in ms for the request.
113-
* @default 2000
114-
*/
115-
requestTimeoutMs?: number;
116-
/**
117-
* Optional timeout in ms for the SDK initialization.
111+
* Optional timeout in ms for the initial connection.
118112
* @default 5000
119113
*/
120-
initializationTimeoutMs?: number;
114+
connectTimeoutMs?: number;
121115
/**
122116
* Delay between retries in ms.
123117
* @default 200
124118
*/
125119
retryDelayMs?: number;
120+
/**
121+
* Optional timeout in ms for individual requests.
122+
* @default 2000
123+
*/
124+
requestTimeoutMs?: number;
126125
/**
127126
* Timeout in ms for SSE connection inactivity.
128127
* If no events (including pings) are received within this time, the connection will be re-established.
129128
* @default 30000
130129
*/
131130
inactivityTimeoutMs?: number;
132131
/**
133-
* Optional logger (defaults to console).
134-
*/
135-
logger?: ReplaneLogger;
136-
/**
137-
* Default context for all config evaluations.
138-
* Can be overridden per-request in `client.get()`.
139-
*/
140-
context?: ReplaneContext;
141-
142-
/**
143-
* Required configs for the client.
144-
* If a config is not present, the client will throw an error during initialization.
145-
* @example
146-
* {
147-
* required: {
148-
* config1: true,
149-
* config2: true,
150-
* config3: false,
151-
* },
152-
* }
153-
*
154-
* @example
155-
* {
156-
* required: ["config1", "config2", "config3"],
157-
* }
158-
*/
159-
required?:
160-
| {
161-
[K in keyof T]: boolean;
162-
}
163-
| Array<keyof T>;
164-
165-
/**
166-
* Default values to use if the initial request to fetch configs fails or times out.
167-
* These values are used immediately while waiting for server connection.
168-
* @example
169-
* {
170-
* defaults: {
171-
* config1: "value1",
172-
* config2: 42,
173-
* },
174-
* }
132+
* Custom fetch implementation (useful for tests / polyfills).
175133
*/
176-
defaults?: {
177-
[K in keyof T]: T[K];
178-
};
179-
134+
fetchFn?: typeof fetch;
180135
/**
181136
* Agent identifier sent in User-Agent header.
182137
* Defaults to SDK identifier (e.g., "replane-js/x.y.z").
183138
*/
184139
agent?: string;
185-
186-
/**
187-
* Callback invoked when a connection error occurs during SSE streaming.
188-
* This is called on each retry attempt, useful for tracking connection health.
189-
* The SDK will automatically retry connections, so this is informational.
190-
* @param error - The error that caused the connection failure
191-
*/
192-
onConnectionError?: (error: unknown) => void;
193-
194-
/**
195-
* Callback invoked when the SSE connection is successfully established.
196-
* This is called on initial connection and on every successful reconnection.
197-
* Useful for tracking connection health and reconnection metrics.
198-
*/
199-
onConnected?: () => void;
200140
}
201141

202142
/**
203-
* Options for restoring a Replane client from a snapshot
143+
* Internal options after processing connect options
204144
*/
205-
export interface RestoreReplaneClientOptions<T extends object> {
206-
/**
207-
* Snapshot from a server-side client's getSnapshot() call.
208-
*/
209-
snapshot: ReplaneSnapshot<T>;
210-
/**
211-
* Optional connection options for live updates.
212-
* If provided, the client will connect to the Replane server for real-time config updates.
213-
* If not provided, the client will only use the snapshot data (no live updates).
214-
*/
215-
connection?: {
216-
/**
217-
* Base URL of the Replane instance (no trailing slash).
218-
*/
219-
baseUrl: string;
220-
/**
221-
* Project SDK key for authorization.
222-
*/
223-
sdkKey: string;
224-
/**
225-
* Custom fetch implementation (useful for tests / polyfills).
226-
*/
227-
fetchFn?: typeof fetch;
228-
/**
229-
* Optional timeout in ms for the request.
230-
* @default 2000
231-
*/
232-
requestTimeoutMs?: number;
233-
/**
234-
* Delay between retries in ms.
235-
* @default 200
236-
*/
237-
retryDelayMs?: number;
238-
/**
239-
* Timeout in ms for SSE connection inactivity.
240-
* @default 30000
241-
*/
242-
inactivityTimeoutMs?: number;
243-
/**
244-
* Optional logger (defaults to console).
245-
*/
246-
logger?: ReplaneLogger;
247-
/**
248-
* Agent identifier sent in User-Agent header.
249-
* Defaults to SDK identifier (e.g., "replane-js/x.y.z").
250-
*/
251-
agent?: string;
252-
/**
253-
* Callback invoked when a connection error occurs during SSE streaming.
254-
* This is called on each retry attempt, useful for tracking connection health.
255-
* The SDK will automatically retry connections, so this is informational.
256-
* @param error - The error that caused the connection failure
257-
*/
258-
onConnectionError?: (error: unknown) => void;
259-
/**
260-
* Callback invoked when the SSE connection is successfully established.
261-
* This is called on initial connection and on every successful reconnection.
262-
* Useful for tracking connection health and reconnection metrics.
263-
*/
264-
onConnected?: () => void;
265-
};
266-
/**
267-
* Override the context from the snapshot.
268-
*/
269-
context?: ReplaneContext;
270-
}
271-
272-
/**
273-
* Internal options after processing user options
274-
*/
275-
export interface ReplaneFinalOptions {
145+
export interface ConnectFinalOptions {
276146
baseUrl: string;
277-
fetchFn: typeof fetch;
278-
requestTimeoutMs: number;
279-
initializationTimeoutMs: number;
280-
inactivityTimeoutMs: number;
281147
sdkKey: string;
282-
logger: ReplaneLogger;
148+
connectTimeoutMs: number;
283149
retryDelayMs: number;
284-
context: ReplaneContext;
285-
requiredConfigs: string[];
286-
defaults: ConfigDto[];
150+
requestTimeoutMs: number;
151+
inactivityTimeoutMs: number;
152+
fetchFn: typeof fetch;
287153
agent: string;
288-
onConnectionError?: (error: unknown) => void;
289-
onConnected?: () => void;
154+
}
155+
156+
/**
157+
* Internal representation of initial configs
158+
*/
159+
export interface InitialConfig {
160+
name: string;
161+
value: unknown;
162+
overrides: RenderedOverride[];
290163
}

0 commit comments

Comments
 (0)