Skip to content

Commit

Permalink
feat: Scaffold browser client. (#579)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kinyoklion authored Sep 13, 2024
1 parent d11224c commit 0848ab7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
43 changes: 43 additions & 0 deletions packages/sdk/browser/src/BrowserClient.ts
Original file line number Diff line number Diff line change
@@ -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<CommonClient, 'setConnectionMode'>;

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)}`;
}
}
42 changes: 34 additions & 8 deletions packages/sdk/browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 0848ab7

Please sign in to comment.