diff --git a/packages/sdk/browser/src/BrowserClient.ts b/packages/sdk/browser/src/BrowserClient.ts new file mode 100644 index 0000000000..6bb99c8b1d --- /dev/null +++ b/packages/sdk/browser/src/BrowserClient.ts @@ -0,0 +1,43 @@ +import { + AutoEnvAttributes, + base64UrlEncode, + LDClient as CommonClient, + LDClientImpl, + LDContext, + LDOptions, +} from '@launchdarkly/js-client-sdk-common'; + +import BrowserPlatform from './platform/BrowserPlatform'; + +/** + * We are not supporting dynamically setting the connection mode on the LDClient. + */ +export type LDClient = Omit; + +export class BrowserClient extends LDClientImpl { + constructor( + private readonly clientSideId: string, + autoEnvAttributes: AutoEnvAttributes, + options: LDOptions = {}, + ) { + super(clientSideId, autoEnvAttributes, new BrowserPlatform(options), options, { + analyticsEventPath: `/events/bulk/${clientSideId}`, + diagnosticEventPath: `/events/diagnostic/${clientSideId}`, + includeAuthorizationHeader: false, + highTimeoutThreshold: 5, + userAgentHeaderName: 'x-launchdarkly-user-agent', + }); + } + + private encodeContext(context: LDContext) { + return base64UrlEncode(JSON.stringify(context), this.platform.encoding!); + } + + override createStreamUriPath(context: LDContext) { + return `/eval/${this.clientSideId}/${this.encodeContext(context)}`; + } + + override createPollUriPath(context: LDContext): string { + return `/sdk/evalx/${this.clientSideId}/contexts/${this.encodeContext(context)}`; + } +} diff --git a/packages/sdk/browser/src/index.ts b/packages/sdk/browser/src/index.ts index e736320369..5e25241ec9 100644 --- a/packages/sdk/browser/src/index.ts +++ b/packages/sdk/browser/src/index.ts @@ -1,11 +1,37 @@ -import BrowserInfo from './platform/BrowserInfo'; -import DefaultBrowserEventSource from './platform/DefaultBrowserEventSource'; +import { + AutoEnvAttributes, + LDContext, + LDContextCommon, + LDContextMeta, + LDFlagSet, + LDLogger, + LDLogLevel, + LDMultiKindContext, + LDOptions, + LDSingleKindContext, +} from '@launchdarkly/js-client-sdk-common'; -// Temporary exports for testing in a browser. -export { DefaultBrowserEventSource, BrowserInfo }; -export * from '@launchdarkly/js-client-sdk-common'; +import { BrowserClient, LDClient } from './BrowserClient'; -export function Hello() { - // eslint-disable-next-line no-console - console.log('HELLO'); +// TODO: Export and use browser specific options. +export { + LDClient, + AutoEnvAttributes, + LDOptions, + LDFlagSet, + LDContext, + LDContextCommon, + LDContextMeta, + LDMultiKindContext, + LDSingleKindContext, + LDLogLevel, + LDLogger, +}; + +export function init( + clientSideId: string, + autoEnvAttributes: AutoEnvAttributes, + options?: LDOptions, +): LDClient { + return new BrowserClient(clientSideId, autoEnvAttributes, options); }