Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DO NOT MERGE: feat: add the ability to send events using the Vercel Edge SDK #217

4 changes: 2 additions & 2 deletions packages/sdk/vercel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@launchdarkly/vercel-server-sdk",
"version": "1.1.0",
"version": "1.2.0-alpha.0",
"description": "LaunchDarkly Server-Side SDK for Vercel Edge",
"homepage": "https://github.com/launchdarkly/js-core/tree/main/packages/sdk/vercel",
"repository": {
Expand Down Expand Up @@ -37,7 +37,7 @@
"check": "yarn prettier && yarn lint && yarn build && yarn test && yarn doc"
},
"dependencies": {
"@launchdarkly/js-server-sdk-common-edge": "1.0.9",
"@launchdarkly/js-server-sdk-common-edge": "1.1.0-alpha.1",
"@vercel/edge-config": "^0.1.8",
"crypto-js": "^4.1.1"
},
Expand Down
16 changes: 10 additions & 6 deletions packages/sdk/vercel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ export type { LDClient };
* (`new LDClient()/new LDClientImpl()/new LDClient()`); the SDK does not currently support
* this.
*
* @param sdkKey
* The client side SDK key. This is only used to query the edgeConfig above,
* @param clientSideID
* The client-side ID. This is only used to query the edgeConfig above,
yusinto marked this conversation as resolved.
Show resolved Hide resolved
* not to connect with LaunchDarkly servers.
* @param edgeConfig
* The Vercel Edge Config client configured for LaunchDarkly.
* @param options
* Optional configuration settings. The only supported option is logger.
* Optional configuration settings.
* @return
* The new {@link LDClient} instance.
*/
export const init = (sdkKey: string, edgeConfig: EdgeConfigClient, options: LDOptions = {}) => {
export const init = (
clientSideID: string,
edgeConfig: EdgeConfigClient,
options: LDOptions = {},
) => {
const logger = options.logger ?? BasicLogger.get();

// vercel does not support string gets so we have to stringify it
Expand All @@ -57,8 +61,8 @@ export const init = (sdkKey: string, edgeConfig: EdgeConfigClient, options: LDOp
},
};

return initEdge(sdkKey, createPlatformInfo(), {
featureStore: new EdgeFeatureStore(edgeProvider, sdkKey, 'Vercel', logger),
return initEdge(clientSideID, createPlatformInfo(), {
featureStore: new EdgeFeatureStore(edgeProvider, clientSideID, 'Vercel', logger),
logger,
...options,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/sdk-server-edge/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@launchdarkly/js-server-sdk-common-edge",
"version": "1.0.9",
"version": "1.1.0-alpha.1",
"homepage": "https://github.com/launchdarkly/js-core/tree/main/packages/shared/sdk-server-edge",
"repository": {
"type": "git",
Expand Down
5 changes: 2 additions & 3 deletions packages/shared/sdk-server-edge/src/api/LDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import createOptions from './createOptions';
export class LDClient extends LDClientImpl {
emitter: EventEmitter;

// sdkKey is only used to query featureStore, not to initialize with LD servers
constructor(sdkKey: string, platformInfo: Info, options: LDOptions) {
constructor(serverSideKey: string, platformInfo: Info, options: LDOptions) {
const em = new EventEmitter();
const platform = new EdgePlatform(platformInfo);
super('n/a', platform, createOptions(options), createCallbacks(em));
super(serverSideKey, platform, createOptions(options), createCallbacks(em));
this.emitter = em;
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/shared/sdk-server-edge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export type { LDClient, LDOptions, EdgeProvider };
*
* This is an internal API to be used directly only by LaunchDarkly Edge SDKs.
*/
export const init = (sdkKey: string, platformInfo: Info, options: LDOptionsInternal) => {
export const init = (clientSideID: string, platformInfo: Info, options: LDOptionsInternal) => {
// this throws if options are invalid
validateOptions(sdkKey, options);
validateOptions(clientSideID, options);

return new LDClient(sdkKey, platformInfo, options);
const { serverSideKey, ...allOtherOptions } = options;
return new LDClient(serverSideKey || 'n/a', platformInfo, allOtherOptions);
};
13 changes: 11 additions & 2 deletions packages/shared/sdk-server-edge/src/utils/validateOptions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { LDOptions as LDOptionsCommon } from '@launchdarkly/js-server-sdk-common';

type EdgeOptions = {
// The LaunchDarkly server-side key. This option is required if sendEvents is enabled.
serverSideKey?: string;
};
yusinto marked this conversation as resolved.
Show resolved Hide resolved

/**
* The Launchdarkly Edge SDKs configuration options. Only logger is officially
* supported. sendEvents is unsupported and is only included as a beta
* preview.
*/
export type LDOptions = Pick<LDOptionsCommon, 'logger' | 'sendEvents'>;
export type LDOptions = EdgeOptions & Pick<LDOptionsCommon, 'logger' | 'sendEvents' | 'eventsUri'>;

/**
* The internal options include featureStore because that's how the LDClient
Expand All @@ -14,7 +19,7 @@ export type LDOptions = Pick<LDOptionsCommon, 'logger' | 'sendEvents'>;
export type LDOptionsInternal = LDOptions & Pick<LDOptionsCommon, 'featureStore'>;

const validateOptions = (sdkKey: string, options: LDOptionsInternal) => {
const { featureStore, logger, sendEvents, ...rest } = options;
const { featureStore, logger, sendEvents, serverSideKey, eventsUri, ...rest } = options;
if (!sdkKey) {
throw new Error('You must configure the client with a client key');
}
Expand All @@ -27,6 +32,10 @@ const validateOptions = (sdkKey: string, options: LDOptionsInternal) => {
throw new Error('You must configure the client with a logger');
}

if (sendEvents && !serverSideKey) {
throw new Error('You must provide the serverSideKey to enable events');
}

if (JSON.stringify(rest) !== '{}') {
throw new Error(`Invalid configuration: ${Object.keys(rest).toString()} not supported`);
}
Expand Down
Loading