Skip to content

Commit 0848ab7

Browse files
authored
feat: Scaffold browser client. (#579)
This adds a basic client and initialization function. The SDK does function, but does not yet function like a JS SDK, but more like a mobile SDK.
1 parent d11224c commit 0848ab7

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
AutoEnvAttributes,
3+
base64UrlEncode,
4+
LDClient as CommonClient,
5+
LDClientImpl,
6+
LDContext,
7+
LDOptions,
8+
} from '@launchdarkly/js-client-sdk-common';
9+
10+
import BrowserPlatform from './platform/BrowserPlatform';
11+
12+
/**
13+
* We are not supporting dynamically setting the connection mode on the LDClient.
14+
*/
15+
export type LDClient = Omit<CommonClient, 'setConnectionMode'>;
16+
17+
export class BrowserClient extends LDClientImpl {
18+
constructor(
19+
private readonly clientSideId: string,
20+
autoEnvAttributes: AutoEnvAttributes,
21+
options: LDOptions = {},
22+
) {
23+
super(clientSideId, autoEnvAttributes, new BrowserPlatform(options), options, {
24+
analyticsEventPath: `/events/bulk/${clientSideId}`,
25+
diagnosticEventPath: `/events/diagnostic/${clientSideId}`,
26+
includeAuthorizationHeader: false,
27+
highTimeoutThreshold: 5,
28+
userAgentHeaderName: 'x-launchdarkly-user-agent',
29+
});
30+
}
31+
32+
private encodeContext(context: LDContext) {
33+
return base64UrlEncode(JSON.stringify(context), this.platform.encoding!);
34+
}
35+
36+
override createStreamUriPath(context: LDContext) {
37+
return `/eval/${this.clientSideId}/${this.encodeContext(context)}`;
38+
}
39+
40+
override createPollUriPath(context: LDContext): string {
41+
return `/sdk/evalx/${this.clientSideId}/contexts/${this.encodeContext(context)}`;
42+
}
43+
}

packages/sdk/browser/src/index.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1-
import BrowserInfo from './platform/BrowserInfo';
2-
import DefaultBrowserEventSource from './platform/DefaultBrowserEventSource';
1+
import {
2+
AutoEnvAttributes,
3+
LDContext,
4+
LDContextCommon,
5+
LDContextMeta,
6+
LDFlagSet,
7+
LDLogger,
8+
LDLogLevel,
9+
LDMultiKindContext,
10+
LDOptions,
11+
LDSingleKindContext,
12+
} from '@launchdarkly/js-client-sdk-common';
313

4-
// Temporary exports for testing in a browser.
5-
export { DefaultBrowserEventSource, BrowserInfo };
6-
export * from '@launchdarkly/js-client-sdk-common';
14+
import { BrowserClient, LDClient } from './BrowserClient';
715

8-
export function Hello() {
9-
// eslint-disable-next-line no-console
10-
console.log('HELLO');
16+
// TODO: Export and use browser specific options.
17+
export {
18+
LDClient,
19+
AutoEnvAttributes,
20+
LDOptions,
21+
LDFlagSet,
22+
LDContext,
23+
LDContextCommon,
24+
LDContextMeta,
25+
LDMultiKindContext,
26+
LDSingleKindContext,
27+
LDLogLevel,
28+
LDLogger,
29+
};
30+
31+
export function init(
32+
clientSideId: string,
33+
autoEnvAttributes: AutoEnvAttributes,
34+
options?: LDOptions,
35+
): LDClient {
36+
return new BrowserClient(clientSideId, autoEnvAttributes, options);
1137
}

0 commit comments

Comments
 (0)