Skip to content

Commit c2777c2

Browse files
committed
chore: refactor replane sdk
1 parent c86c65d commit c2777c2

33 files changed

Lines changed: 661 additions & 513 deletions

packages/admin/src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Auto-generated - do not edit manually
2-
export const VERSION = "0.8.14";
2+
export const VERSION = "0.8.20";
33
export const DEFAULT_AGENT = `replane-js-admin/${VERSION}`;

packages/next/src/index.tsx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,21 @@ export {
8686
createReplaneHook,
8787
createConfigHook,
8888
clearSuspenseCache,
89+
Replane,
90+
ReplaneError,
91+
ReplaneErrorCode,
8992
} from "@replanejs/react";
9093

9194
export type {
9295
ReplaneProviderProps,
9396
ReplaneProviderWithClientProps,
9497
ReplaneProviderWithOptionsProps,
98+
ReplaneProviderOptions,
9599
GetReplaneSnapshotOptions,
96-
} from "@replanejs/react";
97-
98-
export {
99-
createReplaneClient,
100-
createInMemoryReplaneClient,
101-
restoreReplaneClient,
102-
ReplaneError,
103-
ReplaneErrorCode,
104-
} from "@replanejs/sdk";
105-
106-
export type {
107-
ReplaneClient,
108-
ReplaneClientOptions,
109100
ReplaneSnapshot,
110101
ReplaneContext,
111102
ReplaneLogger,
103+
ReplaneOptions,
104+
ConnectOptions,
112105
GetConfigOptions,
113-
RestoreReplaneClientOptions,
114-
} from "@replanejs/sdk";
106+
} from "@replanejs/react";

packages/next/src/root.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*/
55

66
import type { ReactNode } from "react";
7-
import { getReplaneSnapshot, type ReplaneClientOptions } from "@replanejs/sdk";
8-
import { ReplaneProvider } from "@replanejs/react";
7+
import { getReplaneSnapshot } from "@replanejs/sdk";
8+
import { ReplaneProvider, type ReplaneProviderOptions } from "@replanejs/react";
99
import { DEFAULT_AGENT } from "./version";
1010

1111
/**
@@ -16,7 +16,7 @@ export interface ReplaneRootProps<T extends object> {
1616
* Options for Replane client.
1717
* Used for both server-side fetching and client-side live updates.
1818
*/
19-
options: ReplaneClientOptions<T>;
19+
options: ReplaneProviderOptions<T>;
2020
/**
2121
* React children to render inside the provider
2222
*/

packages/next/src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Auto-generated - do not edit manually
2-
export const VERSION = "0.8.14";
2+
export const VERSION = "0.8.20";
33
export const DEFAULT_AGENT = `replane-js-next/${VERSION}`;

packages/react/src/hooks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import { useCallback, useContext, useEffect, useRef, useSyncExternalStore } from "react";
44
import { ReplaneContext } from "./context";
55
import type { UntypedReplaneConfig } from "./types";
6-
import type { ReplaneClient, GetConfigOptions } from "@replanejs/sdk";
6+
import type { Replane, GetConfigOptions } from "@replanejs/sdk";
77

8-
export function useReplane<T extends object = UntypedReplaneConfig>(): ReplaneClient<T> {
8+
export function useReplane<T extends object = UntypedReplaneConfig>(): Replane<T> {
99
const context = useContext(ReplaneContext);
1010
if (!context) {
1111
throw new Error("useReplane must be used within a ReplaneProvider");
1212
}
13-
return context.client as ReplaneClient<T>;
13+
return context.client as Replane<T>;
1414
}
1515

1616
export function useConfig<T>(name: string, options?: GetConfigOptions<T>): T {
@@ -51,7 +51,7 @@ export function useConfig<T>(name: string, options?: GetConfigOptions<T>): T {
5151
* ```
5252
*/
5353
export function createReplaneHook<TConfigs extends object>() {
54-
return function useTypedReplane(): ReplaneClient<TConfigs> {
54+
return function useTypedReplane(): Replane<TConfigs> {
5555
return useReplane<TConfigs>();
5656
};
5757
}

packages/react/src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ export type {
77
ReplaneProviderProps,
88
ReplaneProviderWithClientProps,
99
ReplaneProviderWithOptionsProps,
10+
ReplaneProviderOptions,
1011
} from "./types";
1112

12-
// Re-export snapshot utilities from SDK
13-
export { getReplaneSnapshot } from "@replanejs/sdk";
14-
export type { GetReplaneSnapshotOptions } from "@replanejs/sdk";
13+
// Re-export from SDK
14+
export { Replane, getReplaneSnapshot, ReplaneError, ReplaneErrorCode } from "@replanejs/sdk";
15+
export type {
16+
ReplaneSnapshot,
17+
ReplaneContext,
18+
ReplaneLogger,
19+
ReplaneOptions,
20+
ConnectOptions,
21+
GetConfigOptions,
22+
GetReplaneSnapshotOptions,
23+
} from "@replanejs/sdk";

packages/react/src/provider.tsx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { useMemo } from "react";
4-
import { restoreReplaneClient } from "@replanejs/sdk";
4+
import { Replane } from "@replanejs/sdk";
55
import { ReplaneContext } from "./context";
66
import { useReplaneClientInternal, useReplaneClientSuspense } from "./useReplaneClient";
77
import { useStateful } from "./hooks";
@@ -27,7 +27,7 @@ function ReplaneProviderWithClient<T extends object>({
2727

2828
/**
2929
* Internal provider component for restoring client from snapshot.
30-
* Uses restoreReplaneClient which is synchronous.
30+
* Creates a Replane client synchronously and connects in background.
3131
*/
3232
function ReplaneProviderWithSnapshot<T extends object>({
3333
options,
@@ -37,22 +37,27 @@ function ReplaneProviderWithSnapshot<T extends object>({
3737
snapshot: NonNullable<ReplaneProviderWithOptionsProps<T>["snapshot"]>;
3838
}) {
3939
const client = useStateful(
40-
() =>
41-
restoreReplaneClient<T>({
40+
() => {
41+
const replane = new Replane<T>({
4242
snapshot,
43-
connection: {
44-
baseUrl: options.baseUrl,
45-
sdkKey: options.sdkKey,
46-
fetchFn: options.fetchFn,
47-
requestTimeoutMs: options.requestTimeoutMs,
48-
retryDelayMs: options.retryDelayMs,
49-
inactivityTimeoutMs: options.inactivityTimeoutMs,
50-
logger: options.logger,
51-
agent: options.agent ?? DEFAULT_AGENT,
52-
},
43+
logger: options.logger,
5344
context: options.context,
54-
}),
55-
(c) => c.close(),
45+
defaults: options.defaults,
46+
});
47+
// Start connection in background (don't await)
48+
replane.connect({
49+
baseUrl: options.baseUrl,
50+
sdkKey: options.sdkKey,
51+
fetchFn: options.fetchFn,
52+
requestTimeoutMs: options.requestTimeoutMs,
53+
retryDelayMs: options.retryDelayMs,
54+
inactivityTimeoutMs: options.inactivityTimeoutMs,
55+
connectTimeoutMs: options.connectTimeoutMs,
56+
agent: options.agent ?? DEFAULT_AGENT,
57+
});
58+
return replane;
59+
},
60+
(c) => c.disconnect(),
5661
[snapshot, options]
5762
);
5863
const value = useMemo<ReplaneContextValue<T>>(() => ({ client }), [client]);
@@ -96,13 +101,14 @@ function ReplaneProviderWithSuspense<T extends object>({
96101
}
97102

98103
/**
99-
* Provider component that makes a ReplaneClient available to the component tree.
104+
* Provider component that makes a Replane client available to the component tree.
100105
*
101-
* Can be used in three ways:
106+
* Can be used in several ways:
102107
*
103108
* 1. With a pre-created client:
104109
* ```tsx
105-
* const client = await createReplaneClient({ ... });
110+
* const client = new Replane({ defaults: { ... } });
111+
* await client.connect({ baseUrl: '...', sdkKey: '...' });
106112
* <ReplaneProvider client={client}>
107113
* <App />
108114
* </ReplaneProvider>

packages/react/src/types.ts

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,90 @@
11
import type {
2-
ReplaneClient,
3-
ReplaneClientOptions,
2+
Replane,
43
ReplaneSnapshot,
4+
ReplaneContext,
5+
ReplaneLogger,
56
} from "@replanejs/sdk";
67
import type { ReactNode } from "react";
78

89
export type UntypedReplaneConfig = Record<string, unknown>;
910

1011
export interface ReplaneContextValue<T extends object = UntypedReplaneConfig> {
11-
client: ReplaneClient<T>;
12+
client: Replane<T>;
13+
}
14+
15+
/**
16+
* Combined options for ReplaneProvider.
17+
* Includes both constructor options (context, logger, defaults) and connection options.
18+
*/
19+
export interface ReplaneProviderOptions<T extends object = UntypedReplaneConfig> {
20+
/**
21+
* Base URL of the Replane instance (no trailing slash).
22+
* @example "https://app.replane.dev"
23+
*/
24+
baseUrl: string;
25+
/**
26+
* Project SDK key for authorization.
27+
* @example "rp_XXXXXXXXX"
28+
*/
29+
sdkKey: string;
30+
/**
31+
* Default context for all config evaluations.
32+
* Can be overridden per-request in `client.get()`.
33+
*/
34+
context?: ReplaneContext;
35+
/**
36+
* Optional logger (defaults to console).
37+
*/
38+
logger?: ReplaneLogger;
39+
/**
40+
* Default values to use before connection is established.
41+
*/
42+
defaults?: { [K in keyof T]?: T[K] };
43+
/**
44+
* Optional timeout in ms for the initial connection.
45+
* @default 5000
46+
*/
47+
connectTimeoutMs?: number;
48+
/**
49+
* Delay between retries in ms.
50+
* @default 200
51+
*/
52+
retryDelayMs?: number;
53+
/**
54+
* Optional timeout in ms for individual requests.
55+
* @default 2000
56+
*/
57+
requestTimeoutMs?: number;
58+
/**
59+
* Timeout in ms for SSE connection inactivity.
60+
* @default 30000
61+
*/
62+
inactivityTimeoutMs?: number;
63+
/**
64+
* Custom fetch implementation (useful for tests / polyfills).
65+
*/
66+
fetchFn?: typeof fetch;
67+
/**
68+
* Agent identifier sent in User-Agent header.
69+
*/
70+
agent?: string;
1271
}
1372

1473
/**
1574
* Props for ReplaneProvider when using a pre-created client.
1675
*/
1776
export interface ReplaneProviderWithClientProps<T extends object = UntypedReplaneConfig> {
18-
/** Pre-created ReplaneClient instance */
19-
client: ReplaneClient<T>;
77+
/** Pre-created Replane client instance */
78+
client: Replane<T>;
2079
children: ReactNode;
2180
}
2281

2382
/**
2483
* Props for ReplaneProvider when letting it manage the client internally.
2584
*/
2685
export interface ReplaneProviderWithOptionsProps<T extends object = UntypedReplaneConfig> {
27-
/** Options to create or restore the ReplaneClient */
28-
options: ReplaneClientOptions<T>;
86+
/** Options to create or restore the Replane client */
87+
options: ReplaneProviderOptions<T>;
2988
children: ReactNode;
3089
/**
3190
* Optional snapshot from server-side rendering.

0 commit comments

Comments
 (0)