Skip to content

Commit ba07fbf

Browse files
yusintokinyoklion
andauthored
fix: Improvements and fixes from docs review. (#362)
These are discovered after the docs PR review: * Client sdk default baseUri should be clientsdk.launchdarkly instead of sdk.launchdarkly. * Add getConnectionMode. * Remove identify hash arg. * Change rn default logging to info. * Add debug config option. * Add comments about autoEnv applicationInfo. * Remove LDProvider context prop. --------- Co-authored-by: Ryan Lamb <[email protected]>
1 parent d97ce82 commit ba07fbf

File tree

15 files changed

+53
-66
lines changed

15 files changed

+53
-66
lines changed

packages/sdk/react-native/example/App.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import {
88

99
import Welcome from './src/welcome';
1010

11-
const featureClient = new ReactNativeLDClient(MOBILE_KEY, AutoEnvAttributes.Enabled);
11+
const featureClient = new ReactNativeLDClient(MOBILE_KEY, AutoEnvAttributes.Enabled, {
12+
debug: true,
13+
applicationInfo: {
14+
id: 'ld-rn-test-app',
15+
version: '0.0.1',
16+
},
17+
});
1218

1319
const App = () => {
1420
return (
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"extends": "expo/tsconfig.base",
33
"compilerOptions": {
4-
"jsx": "react",
4+
"jsx": "react-jsx",
55
"strict": true,
6-
"typeRoots": ["./types"]
6+
"typeRoots": ["./types"],
77
},
8-
"exclude": ["e2e"]
8+
"exclude": ["e2e"],
99
}

packages/sdk/react-native/src/ReactNativeLDClient.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('ReactNativeLDClient', () => {
1616
diagnosticEventPath: '/mobile/events/diagnostic',
1717
events: 'https://events.launchdarkly.com',
1818
includeAuthorizationHeader: true,
19-
polling: 'https://sdk.launchdarkly.com',
19+
polling: 'https://clientsdk.launchdarkly.com',
2020
streaming: 'https://clientstream.launchdarkly.com',
2121
});
2222
});

packages/sdk/react-native/src/ReactNativeLDClient.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ export default class ReactNativeLDClient extends LDClientImpl {
3535
* @param options {@link LDOptions} to initialize the client with.
3636
*/
3737
constructor(sdkKey: string, autoEnvAttributes: AutoEnvAttributes, options: LDOptions = {}) {
38+
const { logger: customLogger, debug } = options;
3839
const logger =
39-
options.logger ??
40+
customLogger ??
4041
new BasicLogger({
41-
level: 'debug',
42+
level: debug ? 'debug' : 'info',
4243
// eslint-disable-next-line no-console
4344
destination: console.log,
4445
});

packages/sdk/react-native/src/platform/autoEnv.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ import type { LDApplication, LDDevice } from '@launchdarkly/js-client-sdk-common
55
import locale from './locale';
66

77
export const ldApplication: LDApplication = {
8-
// key is populated by client common sdk
8+
// key is populated by common/client-sdk
99
key: '',
1010
envAttributesVersion: '1.0',
1111

12-
// TODO: populate application ID, name, version, versionName
12+
locale,
13+
14+
// These are not automatically generated. Use LDOptions.applicationInfo
15+
// to specify these values.
1316
id: '',
1417
name: '',
1518
version: '',
1619
versionName: '',
17-
locale,
1820
};
1921

2022
export const ldDevice: LDDevice = {
21-
// key is populated by client common sdk
23+
// key is populated by common/client-sdk
2224
key: '',
2325
envAttributesVersion: '1.0',
2426
manufacturer: Platform.select({

packages/sdk/react-native/src/provider/LDProvider.test.tsx

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const TestApp = () => {
2323
};
2424
describe('LDProvider', () => {
2525
let ldc: ReactNativeLDClient;
26-
let context: LDContext;
2726
const mockSetupListeners = setupListeners as jest.Mock;
2827

2928
beforeEach(() => {
@@ -51,7 +50,6 @@ describe('LDProvider', () => {
5150
setState({ client });
5251
});
5352
ldc = new ReactNativeLDClient('mobile-key', AutoEnvAttributes.Enabled);
54-
context = { kind: 'user', key: 'test-user-key-1' };
5553
});
5654

5755
afterEach(() => {
@@ -69,31 +67,4 @@ describe('LDProvider', () => {
6967
expect(getByText(/mobilekey mobile-key/i)).toBeTruthy();
7068
expect(getByText(/context undefined/i)).toBeTruthy();
7169
});
72-
73-
test('specified context is identified', async () => {
74-
const { getByText } = render(
75-
<LDProvider client={ldc} context={context}>
76-
<TestApp />
77-
</LDProvider>,
78-
);
79-
80-
expect(mockSetupListeners).toHaveBeenCalledWith(ldc, expect.any(Function));
81-
expect(ldc.identify).toHaveBeenCalledWith(context);
82-
expect(ldc.getContext()).toEqual(context);
83-
expect(getByText(/context defined/i)).toBeTruthy();
84-
});
85-
86-
test('identify errors are caught', async () => {
87-
(ldc.identify as jest.Mock).mockImplementation(() =>
88-
Promise.reject(new Error('faking error when identifying')),
89-
);
90-
render(
91-
<LDProvider client={ldc} context={context}>
92-
<TestApp />
93-
</LDProvider>,
94-
);
95-
await jest.runAllTimersAsync();
96-
97-
expect(ldc.logger.debug).toHaveBeenCalledWith(expect.stringMatching(/identify error/));
98-
});
9970
});

packages/sdk/react-native/src/provider/LDProvider.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import React, { PropsWithChildren, useEffect, useState } from 'react';
22

3-
import { type LDContext } from '@launchdarkly/js-client-sdk-common';
4-
53
import ReactNativeLDClient from '../ReactNativeLDClient';
64
import { Provider, ReactContext } from './reactContext';
75
import setupListeners from './setupListeners';
86
import useAppState from './useAppState';
97

108
type LDProps = {
119
client: ReactNativeLDClient;
12-
context?: LDContext;
1310
};
1411

1512
/**
@@ -18,25 +15,15 @@ type LDProps = {
1815
*
1916
* @param client The ReactNativeLDClient object. Initialize this object separately
2017
* and then set this prop when declaring the LDProvider.
21-
* @param context Optional. The LDContext object. If set, the LDProvider will
22-
* `identify` this context on application mount. If not set, default flag values
23-
* will be returned. You can run `identify` at a later time.
2418
* @param children
19+
*
2520
* @constructor
2621
*/
27-
const LDProvider = ({ client, context, children }: PropsWithChildren<LDProps>) => {
22+
const LDProvider = ({ client, children }: PropsWithChildren<LDProps>) => {
2823
const [state, setState] = useState<ReactContext>({ client });
2924

3025
useEffect(() => {
3126
setupListeners(client, setState);
32-
33-
if (context) {
34-
client
35-
.identify(context)
36-
.catch((e: any) =>
37-
client.logger.debug(`LaunchDarkly React Native Sdk identify error: ${e}`),
38-
);
39-
}
4027
}, []);
4128

4229
useAppState(client);

packages/shared/sdk-client/src/LDClientImpl.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('sdk-client object', () => {
6969

7070
expect(ldc.config).toMatchObject({
7171
allAttributesPrivate: false,
72-
baseUri: 'https://sdk.launchdarkly.com',
72+
baseUri: 'https://clientsdk.launchdarkly.com',
7373
capacity: 100,
7474
diagnosticOptOut: false,
7575
diagnosticRecordingInterval: 900,
@@ -87,7 +87,7 @@ describe('sdk-client object', () => {
8787
sendLDHeaders: true,
8888
serviceEndpoints: {
8989
events: 'https://events.launchdarkly.com',
90-
polling: 'https://sdk.launchdarkly.com',
90+
polling: 'https://clientsdk.launchdarkly.com',
9191
streaming: 'https://clientstream.launchdarkly.com',
9292
},
9393
streamInitialReconnectDelay: 1,

packages/shared/sdk-client/src/LDClientImpl.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export default class LDClientImpl implements LDClient {
7878
this.emitter = new LDEmitter();
7979
}
8080

81+
/**
82+
* Sets the SDK connection mode.
83+
*
84+
* @param mode - One of supported {@link ConnectionMode}. Default is 'streaming'.
85+
*/
8186
async setConnectionMode(mode: ConnectionMode): Promise<void> {
8287
if (this.config.connectionMode === mode) {
8388
this.logger.debug(`setConnectionMode ignored. Mode is already '${mode}'.`);
@@ -108,6 +113,13 @@ export default class LDClientImpl implements LDClient {
108113
return Promise.resolve();
109114
}
110115

116+
/**
117+
* Gets the SDK connection mode.
118+
*/
119+
getConnectionMode(): ConnectionMode {
120+
return this.config.connectionMode;
121+
}
122+
111123
isOffline() {
112124
return this.config.connectionMode === 'offline';
113125
}
@@ -270,7 +282,7 @@ export default class LDClientImpl implements LDClient {
270282
return f ? JSON.parse(f) : undefined;
271283
}
272284

273-
async identify(pristineContext: LDContext, _hash?: string): Promise<void> {
285+
async identify(pristineContext: LDContext): Promise<void> {
274286
let context = await ensureKey(pristineContext, this.platform);
275287

276288
if (this.autoEnvAttributes === AutoEnvAttributes.Enabled) {

packages/shared/sdk-client/src/api/LDClient.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,10 @@ export interface LDClient {
100100
*
101101
* @param context
102102
* The LDContext object.
103-
* @param hash
104-
* The signed context key if you are using [Secure Mode](https://docs.launchdarkly.com/sdk/features/secure-mode#configuring-secure-mode-in-the-javascript-client-side-sdk).
105103
* @returns
106104
* A Promise which resolves when the flag values for the specified context are available.
107105
*/
108-
identify(context: LDContext, hash?: string): Promise<void>;
106+
identify(context: LDContext): Promise<void>;
109107

110108
/**
111109
* Determines the json variation of a feature flag.

0 commit comments

Comments
 (0)