diff --git a/packages/sdk/cloudflare/README.md b/packages/sdk/cloudflare/README.md index d86454cadb..55355ac987 100644 --- a/packages/sdk/cloudflare/README.md +++ b/packages/sdk/cloudflare/README.md @@ -25,9 +25,9 @@ yarn add -D @launchdarkly/cloudflare-server-sdk Initialize the ldClient with the [Cloudflare KV namespace](https://developers.cloudflare.com/workers/runtime-apis/kv#kv-bindings) and your client side sdk key: ```typescript -import ldInit from '@launchdarkly/cloudflare-server-sdk'; +import { init } from '@launchdarkly/cloudflare-server-sdk'; -const ldClient = ldInit(KV_NAMESPACE, 'YOUR CLIENT-SIDE SDK KEY'); +const ldClient = init(KV_NAMESPACE, 'YOUR CLIENT-SIDE SDK KEY'); ``` To learn more, head straight to the [complete reference guide for this SDK](https://docs.launchdarkly.com/sdk/server-side/cloudflare). diff --git a/packages/sdk/cloudflare/src/createLDClient/CloudflareImpl.ts b/packages/sdk/cloudflare/src/createLDClient/CloudflareImpl.ts index dc02b6d175..7698c36bc3 100644 --- a/packages/sdk/cloudflare/src/createLDClient/CloudflareImpl.ts +++ b/packages/sdk/cloudflare/src/createLDClient/CloudflareImpl.ts @@ -1,4 +1,4 @@ -import { KVNamespace } from '@cloudflare/workers-types'; +import type { KVNamespace } from '@cloudflare/workers-types'; import { EventEmitter } from 'node:events'; import { LDClientImpl, LDOptions } from '@launchdarkly/js-server-sdk-common'; import CloudflarePlatform from '../platform'; diff --git a/packages/sdk/cloudflare/src/createLDClient/createOptions.ts b/packages/sdk/cloudflare/src/createLDClient/createOptions.ts index 40ee37a0ed..80bec9ac18 100644 --- a/packages/sdk/cloudflare/src/createLDClient/createOptions.ts +++ b/packages/sdk/cloudflare/src/createLDClient/createOptions.ts @@ -1,4 +1,4 @@ -import { KVNamespace } from '@cloudflare/workers-types'; +import type { KVNamespace } from '@cloudflare/workers-types'; import { BasicLogger, LDLogger, LDOptions, SafeLogger } from '@launchdarkly/js-server-sdk-common'; import { version } from '../../package.json'; import createFeatureStore from './createFeatureStore'; diff --git a/packages/sdk/cloudflare/src/createLDClient/index.ts b/packages/sdk/cloudflare/src/createLDClient/index.ts index e4420a356f..01c3add0f8 100644 --- a/packages/sdk/cloudflare/src/createLDClient/index.ts +++ b/packages/sdk/cloudflare/src/createLDClient/index.ts @@ -1,5 +1,5 @@ -import { KVNamespace } from '@cloudflare/workers-types'; -import { LDOptions } from '@launchdarkly/js-server-sdk-common'; +import type { KVNamespace } from '@cloudflare/workers-types'; +import type { LDOptions } from '@launchdarkly/js-server-sdk-common'; import CloudflareImpl from './CloudflareImpl'; const createLDClient = (kvNamespace: KVNamespace, sdkKey: string, options: LDOptions = {}) => diff --git a/packages/sdk/cloudflare/src/index.test.ts b/packages/sdk/cloudflare/src/index.test.ts index b53bd7b8d0..a59a9b1c0e 100644 --- a/packages/sdk/cloudflare/src/index.test.ts +++ b/packages/sdk/cloudflare/src/index.test.ts @@ -1,6 +1,6 @@ -import { KVNamespace } from '@cloudflare/workers-types'; +import type { KVNamespace } from '@cloudflare/workers-types'; import { Miniflare } from 'miniflare'; -import init, { LDClientCloudflare } from './index'; +import { init, LDClient } from './index'; import * as allFlagsSegments from './utils/testData.json'; const mf = new Miniflare({ @@ -17,7 +17,7 @@ const rootEnvKey = `LD-Env-${sdkKey}`; describe('worker', () => { let kv: KVNamespace; - let ldClient: LDClientCloudflare; + let ldClient: LDClient; beforeAll(async () => { kv = (await mf.getKVNamespace(namespace)) as unknown as KVNamespace; diff --git a/packages/sdk/cloudflare/src/index.ts b/packages/sdk/cloudflare/src/index.ts index 80e875118d..bfff8b9370 100644 --- a/packages/sdk/cloudflare/src/index.ts +++ b/packages/sdk/cloudflare/src/index.ts @@ -8,32 +8,50 @@ * * @packageDocumentation */ -import { KVNamespace } from '@cloudflare/workers-types'; -import { - LDClient, +import type { KVNamespace } from '@cloudflare/workers-types'; +import type { + LDClient as LDClientCommon, LDFlagsState, LDFlagsStateOptions, - LDOptions, + LDOptions as LDOptionsCommon, LDContext, LDEvaluationDetail, LDFlagValue, } from '@launchdarkly/js-server-sdk-common'; import createLDClient from './createLDClient'; -export type LDClientCloudflare = Pick< - LDClient, - 'variation' | 'variationDetail' | 'allFlagsState' | 'waitForInitialization' ->; +export * from '@launchdarkly/js-server-sdk-common'; /** - * Creates an instance of the LaunchDarkly client. + * The Cloudflare SDK only supports these functions: + * - waitForInitialization + * - variation + * - variationDetail + * - allFlagsState + */ +export type LDClient = Pick< + Omit, + 'variation' | 'variationDetail' | 'allFlagsState' +> & { + waitForInitialization: () => Promise; +}; + +/** + * The Cloudflare SDK only supports these options: + * - logger + * - featureStore + */ +export type LDOptions = Pick; + +/** + * Creates an instance of the Cloudflare LaunchDarkly client. * * Applications should instantiate a single instance for the lifetime of the worker. * The client will begin attempting to connect to the configured Cloudflare KV as * soon as it is created. To determine when it is ready to use, call {@link LDClient.waitForInitialization}. * * **Important:** Do **not** try to instantiate `LDClient` with its constructor - * (`new LDClient()/new LDClientImpl()/new LDClientCloudflare()`); the SDK does not currently support + * (`new LDClient()/new LDClientImpl()/new LDClient()`); the SDK does not currently support * this. * * @param kvNamespace @@ -47,11 +65,11 @@ export type LDClientCloudflare = Pick< * @return * The new {@link LDClient} instance. */ -const init = ( +export const init = ( kvNamespace: KVNamespace, sdkKey: string, options: LDOptions = {} -): LDClientCloudflare => { +): LDClient => { const client = createLDClient(kvNamespace, sdkKey, options); return { variation( @@ -82,5 +100,3 @@ const init = ( }, }; }; - -export default init; diff --git a/packages/sdk/cloudflare/src/utils/mockKV.ts b/packages/sdk/cloudflare/src/utils/mockKV.ts index 241e37f890..c6bb1d6212 100644 --- a/packages/sdk/cloudflare/src/utils/mockKV.ts +++ b/packages/sdk/cloudflare/src/utils/mockKV.ts @@ -1,4 +1,4 @@ -import { KVNamespace } from '@cloudflare/workers-types'; +import type { KVNamespace } from '@cloudflare/workers-types'; const mockKV: KVNamespace = { get: jest.fn(),