Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/sdk/cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/cloudflare/src/createLDClient/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {}) =>
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/cloudflare/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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;
Expand Down
44 changes: 30 additions & 14 deletions packages/sdk/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a secondary problem with the reduced types, because their comments may reference other types that it doesn't know about. Which is possibly we can ignore that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I missed a line so you can ignore the deleted comment.)

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<LDClientCommon, 'waitForInitialization'>,
'variation' | 'variationDetail' | 'allFlagsState'
> & {
waitForInitialization: () => Promise<LDClient>;
};

/**
* The Cloudflare SDK only supports these options:
* - logger
* - featureStore
*/
export type LDOptions = Pick<LDOptionsCommon, 'logger' | 'featureStore'>;

/**
* 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
Expand All @@ -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(
Expand Down Expand Up @@ -82,5 +100,3 @@ const init = (
},
};
};

export default init;
2 changes: 1 addition & 1 deletion packages/sdk/cloudflare/src/utils/mockKV.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KVNamespace } from '@cloudflare/workers-types';
import type { KVNamespace } from '@cloudflare/workers-types';

const mockKV: KVNamespace<string> = {
get: jest.fn(),
Expand Down