|
| 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 | +} |
0 commit comments