Skip to content

Commit daefb60

Browse files
authored
feat: fix typedoc and export common types (#81)
* fix: import types where appropriate. fixed default import on index. * fix: export common types and improve cloudflare types. * fix: minor comment update --------- Co-authored-by: Yusinto Ngadiman <[email protected]>
1 parent add0c63 commit daefb60

File tree

7 files changed

+40
-24
lines changed

7 files changed

+40
-24
lines changed

packages/sdk/cloudflare/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ yarn add -D @launchdarkly/cloudflare-server-sdk
2525
Initialize the ldClient with the [Cloudflare KV namespace](https://developers.cloudflare.com/workers/runtime-apis/kv#kv-bindings) and your client side sdk key:
2626

2727
```typescript
28-
import ldInit from '@launchdarkly/cloudflare-server-sdk';
28+
import { init } from '@launchdarkly/cloudflare-server-sdk';
2929

30-
const ldClient = ldInit(KV_NAMESPACE, 'YOUR CLIENT-SIDE SDK KEY');
30+
const ldClient = init(KV_NAMESPACE, 'YOUR CLIENT-SIDE SDK KEY');
3131
```
3232

3333
To learn more, head straight to the [complete reference guide for this SDK](https://docs.launchdarkly.com/sdk/server-side/cloudflare).

packages/sdk/cloudflare/src/createLDClient/CloudflareImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { KVNamespace } from '@cloudflare/workers-types';
1+
import type { KVNamespace } from '@cloudflare/workers-types';
22
import { EventEmitter } from 'node:events';
33
import { LDClientImpl, LDOptions } from '@launchdarkly/js-server-sdk-common';
44
import CloudflarePlatform from '../platform';

packages/sdk/cloudflare/src/createLDClient/createOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { KVNamespace } from '@cloudflare/workers-types';
1+
import type { KVNamespace } from '@cloudflare/workers-types';
22
import { BasicLogger, LDLogger, LDOptions, SafeLogger } from '@launchdarkly/js-server-sdk-common';
33
import { version } from '../../package.json';
44
import createFeatureStore from './createFeatureStore';

packages/sdk/cloudflare/src/createLDClient/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { KVNamespace } from '@cloudflare/workers-types';
2-
import { LDOptions } from '@launchdarkly/js-server-sdk-common';
1+
import type { KVNamespace } from '@cloudflare/workers-types';
2+
import type { LDOptions } from '@launchdarkly/js-server-sdk-common';
33
import CloudflareImpl from './CloudflareImpl';
44

55
const createLDClient = (kvNamespace: KVNamespace, sdkKey: string, options: LDOptions = {}) =>

packages/sdk/cloudflare/src/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { KVNamespace } from '@cloudflare/workers-types';
1+
import type { KVNamespace } from '@cloudflare/workers-types';
22
import { Miniflare } from 'miniflare';
3-
import init, { LDClientCloudflare } from './index';
3+
import { init, LDClient } from './index';
44
import * as allFlagsSegments from './utils/testData.json';
55

66
const mf = new Miniflare({
@@ -17,7 +17,7 @@ const rootEnvKey = `LD-Env-${sdkKey}`;
1717

1818
describe('worker', () => {
1919
let kv: KVNamespace;
20-
let ldClient: LDClientCloudflare;
20+
let ldClient: LDClient;
2121

2222
beforeAll(async () => {
2323
kv = (await mf.getKVNamespace(namespace)) as unknown as KVNamespace;

packages/sdk/cloudflare/src/index.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,50 @@
88
*
99
* @packageDocumentation
1010
*/
11-
import { KVNamespace } from '@cloudflare/workers-types';
12-
import {
13-
LDClient,
11+
import type { KVNamespace } from '@cloudflare/workers-types';
12+
import type {
13+
LDClient as LDClientCommon,
1414
LDFlagsState,
1515
LDFlagsStateOptions,
16-
LDOptions,
16+
LDOptions as LDOptionsCommon,
1717
LDContext,
1818
LDEvaluationDetail,
1919
LDFlagValue,
2020
} from '@launchdarkly/js-server-sdk-common';
2121
import createLDClient from './createLDClient';
2222

23-
export type LDClientCloudflare = Pick<
24-
LDClient,
25-
'variation' | 'variationDetail' | 'allFlagsState' | 'waitForInitialization'
26-
>;
23+
export * from '@launchdarkly/js-server-sdk-common';
2724

2825
/**
29-
* Creates an instance of the LaunchDarkly client.
26+
* The Cloudflare SDK only supports these functions:
27+
* - waitForInitialization
28+
* - variation
29+
* - variationDetail
30+
* - allFlagsState
31+
*/
32+
export type LDClient = Pick<
33+
Omit<LDClientCommon, 'waitForInitialization'>,
34+
'variation' | 'variationDetail' | 'allFlagsState'
35+
> & {
36+
waitForInitialization: () => Promise<LDClient>;
37+
};
38+
39+
/**
40+
* The Cloudflare SDK only supports these options:
41+
* - logger
42+
* - featureStore
43+
*/
44+
export type LDOptions = Pick<LDOptionsCommon, 'logger' | 'featureStore'>;
45+
46+
/**
47+
* Creates an instance of the Cloudflare LaunchDarkly client.
3048
*
3149
* Applications should instantiate a single instance for the lifetime of the worker.
3250
* The client will begin attempting to connect to the configured Cloudflare KV as
3351
* soon as it is created. To determine when it is ready to use, call {@link LDClient.waitForInitialization}.
3452
*
3553
* **Important:** Do **not** try to instantiate `LDClient` with its constructor
36-
* (`new LDClient()/new LDClientImpl()/new LDClientCloudflare()`); the SDK does not currently support
54+
* (`new LDClient()/new LDClientImpl()/new LDClient()`); the SDK does not currently support
3755
* this.
3856
*
3957
* @param kvNamespace
@@ -47,11 +65,11 @@ export type LDClientCloudflare = Pick<
4765
* @return
4866
* The new {@link LDClient} instance.
4967
*/
50-
const init = (
68+
export const init = (
5169
kvNamespace: KVNamespace,
5270
sdkKey: string,
5371
options: LDOptions = {}
54-
): LDClientCloudflare => {
72+
): LDClient => {
5573
const client = createLDClient(kvNamespace, sdkKey, options);
5674
return {
5775
variation(
@@ -82,5 +100,3 @@ const init = (
82100
},
83101
};
84102
};
85-
86-
export default init;

packages/sdk/cloudflare/src/utils/mockKV.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { KVNamespace } from '@cloudflare/workers-types';
1+
import type { KVNamespace } from '@cloudflare/workers-types';
22

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

0 commit comments

Comments
 (0)