-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
d11224c
commit 0848ab7
Showing
2 changed files
with
77 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |