@configdirector/react-native-sdk 0.1.0
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/LICENSE +21 -0
- package/README.md +39 -0
- package/dist/configdirector-react-native.d.mts +309 -0
- package/dist/configdirector-react-native.mjs +1507 -0
- package/package.json +118 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ConfigDirector
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is furnished
|
|
10
|
+
to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# ConfigDirector React Native SDK
|
|
2
|
+
|
|
3
|
+
## Getting started
|
|
4
|
+
|
|
5
|
+
### 1. Install
|
|
6
|
+
|
|
7
|
+
Install from NPM:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install --save @configdirector/react-native-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { createClient } from "@configdirector/react-native-sdk";
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## Requirements
|
|
24
|
+
|
|
25
|
+
- React Native **0.71 or later** (requires `ReadableStream` support)
|
|
26
|
+
- **Hermes** is strongly recommended. JavaScriptCore (JSC) is supported via a built-in UTF-8 fallback, but Hermes is the default engine in RN 0.70+ and provides better performance.
|
|
27
|
+
|
|
28
|
+
## Expo
|
|
29
|
+
|
|
30
|
+
This SDK is compatible with both **Expo managed workflow** and **bare workflow**.
|
|
31
|
+
|
|
32
|
+
- In the **managed workflow**, Hermes is the default engine — no additional setup needed.
|
|
33
|
+
- In the **bare workflow**, ensure Hermes is enabled in your `Podfile` (iOS) and `gradle.properties` (Android), which is the default in Expo SDK 47+.
|
|
34
|
+
|
|
35
|
+
Expo SDK 48 (RN 0.71) or later is required.
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
MIT
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { Component, PropsWithChildren } from "react";
|
|
2
|
+
import * as _$react_jsx_runtime0 from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region ../js-client-core/src/Emitter.d.ts
|
|
5
|
+
type EventType = string | symbol;
|
|
6
|
+
type EventsRecord = Record<EventType, any>;
|
|
7
|
+
type Handler<TEventsRecord extends EventsRecord, TKey extends keyof TEventsRecord> = (payload: TEventsRecord[TKey]) => void;
|
|
8
|
+
interface EventProvider<TEventsRecord extends EventsRecord> {
|
|
9
|
+
on<TName extends keyof TEventsRecord>(name: TName, handler: Handler<TEventsRecord, TName>): void;
|
|
10
|
+
off<TName extends keyof TEventsRecord>(name: TName, handler?: Handler<TEventsRecord, TName>): void;
|
|
11
|
+
clear(): void;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region ../shared/src/types.d.ts
|
|
15
|
+
type ConfigEnumLikeType = {
|
|
16
|
+
[key: string]: string | number;
|
|
17
|
+
};
|
|
18
|
+
type ConfigValueType = string | number | boolean | object | ConfigEnumLikeType;
|
|
19
|
+
type ConfigDirectorLoggingLevel = "debug" | "info" | "warn" | "error" | "off";
|
|
20
|
+
interface ConfigDirectorLogger {
|
|
21
|
+
debug(message: string, ...args: any): void;
|
|
22
|
+
info(message: string, ...args: any): void;
|
|
23
|
+
warn(message: string, ...args: any): void;
|
|
24
|
+
error(message: string, ...args: any): void;
|
|
25
|
+
}
|
|
26
|
+
interface ConfigDirectorLogMessageDecorator {
|
|
27
|
+
decorateMessage(message: string): string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The user's context to be sent to ConfigDirector. This context will be used for targeting
|
|
31
|
+
* rules evaluation.
|
|
32
|
+
*/
|
|
33
|
+
type ConfigDirectorContext = {
|
|
34
|
+
/**
|
|
35
|
+
* The user's identifier. This should be a value that uniquely identifies an application
|
|
36
|
+
* user.
|
|
37
|
+
* In the case of anonymous users, you could generate a UUID or alternatively not provide
|
|
38
|
+
* the {@link id} and the SDK will generate a random UUID. However, keep in mind that this
|
|
39
|
+
* value is used for segmenting users in percentage rollouts, and changes to the {@link id}
|
|
40
|
+
* could result in the user being assigned to a different percentile.
|
|
41
|
+
*/
|
|
42
|
+
id?: string;
|
|
43
|
+
/**
|
|
44
|
+
* The user's display name. This will be shown in the ConfigDirector dashboard and may be
|
|
45
|
+
* used for targeting rules.
|
|
46
|
+
*/
|
|
47
|
+
name?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Any arbitrary traits for the current user. They will be shown in the ConfigDirector
|
|
50
|
+
* dashboard and may be used for targeting rules.
|
|
51
|
+
*/
|
|
52
|
+
traits?: {
|
|
53
|
+
[key: string]: unknown;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Metadata about your application. It is recommended you include these values when configuring
|
|
58
|
+
* a ConfigDirector client so that you can use them in targeting rules.
|
|
59
|
+
*/
|
|
60
|
+
type ConfigDirectorMetaContext = {
|
|
61
|
+
appVersion?: string;
|
|
62
|
+
appName?: string;
|
|
63
|
+
};
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region ../js-client-core/src/types.d.ts
|
|
66
|
+
/**
|
|
67
|
+
* Configuration options for the {@link ConfigDirectorClient}
|
|
68
|
+
*/
|
|
69
|
+
type ConfigDirectorClientOptions = {
|
|
70
|
+
/**
|
|
71
|
+
* Application metadata that remains constant through the lifetime of the connection
|
|
72
|
+
*/
|
|
73
|
+
metadata?: ConfigDirectorMetaContext;
|
|
74
|
+
/**
|
|
75
|
+
* Connection options
|
|
76
|
+
*/
|
|
77
|
+
connection?: {
|
|
78
|
+
/**
|
|
79
|
+
* Whether to open a streaming connection or use a one-time pull of configuration state.
|
|
80
|
+
* If set to true, the streaming connection will remain open and receive updates whenever
|
|
81
|
+
* config state is updated on the ConfigDirector dashboard.
|
|
82
|
+
* When set to false, there will be an initial request to retrieve config state during
|
|
83
|
+
* initialization, and an additional request whenever {@link ConfigDirectorClient.updateContext}
|
|
84
|
+
* is called. But not updates will be received after those requests.
|
|
85
|
+
*
|
|
86
|
+
* Defaults to true (streaming connection)
|
|
87
|
+
*/
|
|
88
|
+
streaming?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* The timeout, in milliseconds, to be used in initialization and when updating the context.
|
|
91
|
+
* If streaming is enabled, the operation (initialization or context update) may still succeed
|
|
92
|
+
* after it times out if no unrecoverable errors are encountered (like an invalid SDK key).
|
|
93
|
+
* If streaming is disabled, if the operation times out, it will not be retried.
|
|
94
|
+
*/
|
|
95
|
+
timeout?: number;
|
|
96
|
+
/**
|
|
97
|
+
* The base URL to the ConfigDirector SDK server. To be used only when needing to route through a
|
|
98
|
+
* proxy to connect to the ConfigDirector SDK server. Please refer to the docs on how to configure
|
|
99
|
+
* a proxy for the client SDK.
|
|
100
|
+
*/
|
|
101
|
+
url?: string;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* A logger that implements {@link ConfigDirectorLogger}. It defaults to the ConfigDirector console
|
|
105
|
+
* logger set to 'warn' level.
|
|
106
|
+
*
|
|
107
|
+
* The log level of the default logger can be adjusted by creating a default logger with the desired
|
|
108
|
+
* level and providing it in this property:
|
|
109
|
+
* @example
|
|
110
|
+
* import { createClient, createConsoleLogger } from "@configdirector/client-sdk";
|
|
111
|
+
* const client = createClient(
|
|
112
|
+
* "YOUR-SDK-KEY",
|
|
113
|
+
* { logger: createConsoleLogger("debug") },
|
|
114
|
+
* );
|
|
115
|
+
*/
|
|
116
|
+
logger?: ConfigDirectorLogger;
|
|
117
|
+
};
|
|
118
|
+
type ClientConnectAction = "initialization" | "context update" | "network resume";
|
|
119
|
+
type ClientEvents = {
|
|
120
|
+
configsUpdated: {
|
|
121
|
+
keys: string[];
|
|
122
|
+
};
|
|
123
|
+
clientReady: {
|
|
124
|
+
action: ClientConnectAction;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
type WatchHandler<T extends ConfigValueType> = (message: T) => void;
|
|
128
|
+
/**
|
|
129
|
+
* The ConfigDirector SDK client object.
|
|
130
|
+
*
|
|
131
|
+
* Applications should create a single instance of `ConfigDirectorClient`, and call
|
|
132
|
+
* {@link initialize} during application initialization.
|
|
133
|
+
*
|
|
134
|
+
* After initialization, to update the user's context, so that targeting rules are evaluated
|
|
135
|
+
* with the updated context, call {@link updateContext}.
|
|
136
|
+
*/
|
|
137
|
+
interface ConfigDirectorClient extends EventProvider<ClientEvents> {
|
|
138
|
+
/**
|
|
139
|
+
* Initializes the connection to ConfigDirector to retrieve config evaluations. Until
|
|
140
|
+
* initialization is successful, all flags will return their default value provided to
|
|
141
|
+
* {@link watch} or {@link getValue}.
|
|
142
|
+
*
|
|
143
|
+
* If the connection fails or is interrupted with a transient error (network error,
|
|
144
|
+
* internal server error, etc) the client will continue to attempt to connect. However,
|
|
145
|
+
* if the connection fails with a persistent error, like an invalid SDK key, the client will
|
|
146
|
+
* not attempt to re-connect and an error will be logged to the console or the provided
|
|
147
|
+
* logger.
|
|
148
|
+
*
|
|
149
|
+
* @param context The current user's context to be used for evaluating targeting rules (optional).
|
|
150
|
+
*/
|
|
151
|
+
initialize(context?: ConfigDirectorContext): Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* Updates the user's context and re-evaluates all config and flag values based on the new context.
|
|
154
|
+
*
|
|
155
|
+
* @param context The current user's context to be used for evaluating targeting rules (required).
|
|
156
|
+
*/
|
|
157
|
+
updateContext(context: ConfigDirectorContext): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Returns whether or not the client is ready after calling {@link initialize} or {@link updateContext}
|
|
160
|
+
*
|
|
161
|
+
* The definition of ready is that the connection to the server was successful, and config state
|
|
162
|
+
* was received.
|
|
163
|
+
*/
|
|
164
|
+
get isReady(): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Evaluates a config and returns its value based on the current context and targeting rules
|
|
167
|
+
*
|
|
168
|
+
* @returns The evaluated config value, or the `defaultValue` if the config state was unavailable
|
|
169
|
+
* @param configKey The config key to evaluate
|
|
170
|
+
* @param defaultValue The default value to be returned if the config state is unavailable. For
|
|
171
|
+
* example, if the client cannot connect to the server due to network conditions, or if getValue
|
|
172
|
+
* is called before initialization is done.
|
|
173
|
+
*/
|
|
174
|
+
getValue<T extends ConfigValueType>(configKey: string, defaultValue: T): T;
|
|
175
|
+
/**
|
|
176
|
+
* Watches for changes to a a config evaluation value. Whenever the config value changes, the
|
|
177
|
+
* provided callback function will be called with the new value. Changes can happen due to updates
|
|
178
|
+
* to the config in the ConfigDirector dashboard, or if the context is updated via {@link updateContext}.
|
|
179
|
+
*
|
|
180
|
+
* @returns An 'unwatch' function that can be called to remove the subscriber
|
|
181
|
+
*
|
|
182
|
+
* @param configKey The config key to watch
|
|
183
|
+
* @param defaultValue The default value to be referenced if the config state is unavailable
|
|
184
|
+
* @param callback The callback function to be called whenever the config value is updated
|
|
185
|
+
*/
|
|
186
|
+
watch<T extends ConfigValueType>(configKey: string, defaultValue: T, callback: WatchHandler<T>): () => void;
|
|
187
|
+
/**
|
|
188
|
+
* Removes a particular subscriber to the given `configKey`, or all subscribers if no callback
|
|
189
|
+
* is provided.
|
|
190
|
+
*
|
|
191
|
+
* @param configKey The config key to remove subscribers from
|
|
192
|
+
* @param callback The subscriber to be removed. If not provided, all subscribers are removed for
|
|
193
|
+
* the given `configKey`.
|
|
194
|
+
*/
|
|
195
|
+
unwatch<T extends ConfigValueType>(configKey: string, callback?: WatchHandler<T>): void;
|
|
196
|
+
/**
|
|
197
|
+
* Removes all subscribers from all config keys
|
|
198
|
+
*/
|
|
199
|
+
unwatchAll(): void;
|
|
200
|
+
/**
|
|
201
|
+
* Pauses the network connection without clearing event handlers, watch handlers, or config
|
|
202
|
+
* state. Intended for mobile environments where the OS may kill background connections (e.g.
|
|
203
|
+
* when the app is backgrounded on iOS/Android).
|
|
204
|
+
*
|
|
205
|
+
* Call {@link resumeNetwork} to re-establish the connection.
|
|
206
|
+
*/
|
|
207
|
+
pauseNetwork(): void;
|
|
208
|
+
/**
|
|
209
|
+
* Resumes a connection that was paused via {@link pauseNetwork}, using the last context that
|
|
210
|
+
* was provided to {@link initialize} or {@link updateContext}.
|
|
211
|
+
*/
|
|
212
|
+
resumeNetwork(): Promise<void>;
|
|
213
|
+
/**
|
|
214
|
+
* Disposes of the client. All connections are closed, and all event and config key subscribers
|
|
215
|
+
* are removed.
|
|
216
|
+
*
|
|
217
|
+
* Intended to be called when your application shuts down
|
|
218
|
+
*/
|
|
219
|
+
dispose(): void;
|
|
220
|
+
}
|
|
221
|
+
//#endregion
|
|
222
|
+
//#region ../shared/src/logger.d.ts
|
|
223
|
+
declare class DefaultConsoleLogger implements ConfigDirectorLogger {
|
|
224
|
+
private readonly level;
|
|
225
|
+
private readonly decorator;
|
|
226
|
+
private readonly dateFormatter;
|
|
227
|
+
constructor(level: ConfigDirectorLoggingLevel, decorator: ConfigDirectorLogMessageDecorator);
|
|
228
|
+
debug(message: string, ...args: any): void;
|
|
229
|
+
info(message: string, ...args: any): void;
|
|
230
|
+
warn(message: string, ...args: any): void;
|
|
231
|
+
error(message: string, ...args: any): void;
|
|
232
|
+
private log;
|
|
233
|
+
}
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/types.d.ts
|
|
236
|
+
type ClientStatus = "loading" | "ready" | "default";
|
|
237
|
+
interface ConfigDirectorContextData {
|
|
238
|
+
client?: ConfigDirectorClient;
|
|
239
|
+
updatedAt?: Date;
|
|
240
|
+
status: ClientStatus;
|
|
241
|
+
}
|
|
242
|
+
type ConfigDirectorProviderState = ConfigDirectorContextData;
|
|
243
|
+
/** Minimal connectivity state shape consumed from @react-native-community/netinfo */
|
|
244
|
+
type NetInfoState = {
|
|
245
|
+
isConnected: boolean | null;
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* A subscription function with the same signature as `NetInfo.addEventListener` from
|
|
249
|
+
* `@react-native-community/netinfo`. Pass this prop to enable immediate reconnection
|
|
250
|
+
* when the device regains network connectivity instead of waiting for the next
|
|
251
|
+
* exponential-backoff retry.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* import NetInfo from '@react-native-community/netinfo';
|
|
255
|
+
* <ConfigDirectorProvider netInfoSubscribe={NetInfo.addEventListener} ... />
|
|
256
|
+
*/
|
|
257
|
+
type NetInfoSubscribe = (callback: (state: NetInfoState) => void) => () => void;
|
|
258
|
+
type ConfigDirectorProviderOptions = {
|
|
259
|
+
sdkKey: string;
|
|
260
|
+
appName?: string;
|
|
261
|
+
appVersion?: string;
|
|
262
|
+
url?: string;
|
|
263
|
+
timeout?: number;
|
|
264
|
+
context?: ConfigDirectorContext;
|
|
265
|
+
logger?: ConfigDirectorLogger;
|
|
266
|
+
netInfoSubscribe?: NetInfoSubscribe;
|
|
267
|
+
};
|
|
268
|
+
//#endregion
|
|
269
|
+
//#region src/provider.d.ts
|
|
270
|
+
declare class ConfigDirectorProvider extends Component<PropsWithChildren<ConfigDirectorProviderOptions>, ConfigDirectorProviderState> {
|
|
271
|
+
private appStateSubscription;
|
|
272
|
+
private netInfoUnsubscribe;
|
|
273
|
+
private wasOffline;
|
|
274
|
+
constructor(props: ConfigDirectorProviderOptions);
|
|
275
|
+
componentDidMount(): Promise<void>;
|
|
276
|
+
componentDidUpdate(prevProps: PropsWithChildren<ConfigDirectorProviderOptions>): Promise<void>;
|
|
277
|
+
private handleAppStateChange;
|
|
278
|
+
private handleConnectivityChange;
|
|
279
|
+
private reconnect;
|
|
280
|
+
componentWillUnmount(): void;
|
|
281
|
+
render(): _$react_jsx_runtime0.JSX.Element;
|
|
282
|
+
}
|
|
283
|
+
//#endregion
|
|
284
|
+
//#region src/hooks.d.ts
|
|
285
|
+
declare const useConfigValue: <T extends ConfigValueType>(key: string, defaultValue: T) => {
|
|
286
|
+
value: T;
|
|
287
|
+
readyStatus: ClientStatus;
|
|
288
|
+
loading: boolean;
|
|
289
|
+
};
|
|
290
|
+
declare const useConfigDirectorContext: () => {
|
|
291
|
+
updateContext: (context: ConfigDirectorContext) => Promise<void>;
|
|
292
|
+
};
|
|
293
|
+
declare const useConfigDirectorClient: () => {
|
|
294
|
+
client: ConfigDirectorClient;
|
|
295
|
+
};
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/client.d.ts
|
|
298
|
+
declare const createClient: (clientSdkKey: string, clientOptions?: ConfigDirectorClientOptions) => ConfigDirectorClient;
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/errors.d.ts
|
|
301
|
+
declare class ConfigDirectorReactContextError extends Error {
|
|
302
|
+
readonly name: string;
|
|
303
|
+
constructor(message: string);
|
|
304
|
+
}
|
|
305
|
+
//#endregion
|
|
306
|
+
//#region src/logger.d.ts
|
|
307
|
+
declare const createConsoleLogger: (level?: ConfigDirectorLoggingLevel) => DefaultConsoleLogger;
|
|
308
|
+
//#endregion
|
|
309
|
+
export { type ClientStatus, type ConfigDirectorClient, type ConfigDirectorClientOptions, type ConfigDirectorContext, type ConfigDirectorLogger, type ConfigDirectorLoggingLevel, ConfigDirectorProvider, type ConfigDirectorProviderOptions, ConfigDirectorReactContextError, type NetInfoSubscribe, createClient, createConsoleLogger, useConfigDirectorClient, useConfigDirectorContext, useConfigValue };
|