Skip to content

Commit 91f5fb4

Browse files
committed
chore: avoid exposing Replane client private fields
1 parent ae5683f commit 91f5fb4

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

packages/sdk/src/client.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ import { evaluateOverrides } from "./evaluation";
1515
import { Deferred } from "./utils";
1616
import { DEFAULT_AGENT } from "./version";
1717

18+
interface ReplaneHandle<T extends object> {
19+
_replane: ReplaneImpl<T>;
20+
}
21+
22+
function asReplaneHandle<T extends object>(replane: Replane<T>): ReplaneHandle<T> {
23+
return replane as unknown as ReplaneHandle<T>;
24+
}
25+
1826
/**
1927
* The Replane client for managing dynamic configuration.
2028
*
@@ -54,6 +62,34 @@ import { DEFAULT_AGENT } from "./version";
5462
* ```
5563
*/
5664
export class Replane<T extends object = Record<string, unknown>> {
65+
constructor(options: ReplaneOptions<T> = {}) {
66+
asReplaneHandle(this)._replane = new ReplaneImpl<T>(options);
67+
}
68+
connect(options: ConnectOptions): Promise<void> {
69+
return asReplaneHandle(this)._replane.connect(options);
70+
}
71+
disconnect(): void {
72+
asReplaneHandle(this)._replane.disconnect();
73+
}
74+
get<K extends keyof T>(configName: K, options?: GetConfigOptions<T[K]>): T[K] {
75+
return asReplaneHandle(this)._replane.get(configName, options);
76+
}
77+
subscribe<K extends keyof T>(
78+
configName: K,
79+
callback: (config: { name: K; value: T[K] }) => void
80+
): () => void {
81+
return asReplaneHandle(this)._replane.subscribe(configName, callback);
82+
}
83+
getSnapshot(): ReplaneSnapshot<T> {
84+
return asReplaneHandle(this)._replane.getSnapshot();
85+
}
86+
}
87+
88+
// we declare ReplaneImpl separately to avoid exposing private properties
89+
// otherwise function with the following signature
90+
// function f(client: Replane<T>): void;
91+
// would expect client to have all private properties of Replane
92+
class ReplaneImpl<T extends object = Record<string, unknown>> {
5793
private configs: Map<string, ConfigDto>;
5894
private context: ReplaneContext;
5995
private logger: ReplaneLogger;

0 commit comments

Comments
 (0)