Skip to content

Commit 357943e

Browse files
authored
wrap the sdk in a thicker client (#10)
1 parent ec1fa6a commit 357943e

File tree

4 files changed

+51
-28
lines changed

4 files changed

+51
-28
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,12 @@ import { LaunchDarkly } from "@convex-dev/launchdarkly";
116116
import { components } from "./_generated/api";
117117
import { query } from "./_generated/server";
118118

119+
const launchdarkly = new LaunchDarkly(components.launchdarkly);
120+
119121
export const myQuery = query({
120122
args: {},
121123
handler: async ({ ctx }) => {
122-
const launchdarkly = new LaunchDarkly(components.launchdarkly, ctx);
124+
const launchdarkly = ld.sdk(ctx);
123125
const isFlagOn = await launchdarkly.boolVariation(
124126
"my-flag",
125127
{ key: "myUser" },
@@ -202,16 +204,20 @@ Once configured, you may initialize `LaunchDarkly` with the appropriate componen
202204
```typescript
203205
import { LaunchDarkly } from "@convex-dev/launchdarkly";
204206

207+
const ldFirst = new LaunchDarkly(components.first, {
208+
LAUNCHDARKLY_SDK_KEY: process.env.LAUNCHDARKLY_SDK_KEY_FIRST!,
209+
});
210+
211+
const ldSecond = new LaunchDarkly(components.second, {
212+
LAUNCHDARKLY_SDK_KEY: process.env.LAUNCHDARKLY_SDK_KEY_SECOND!,
213+
});
214+
205215
export const myQuery = query({
206216
args: {},
207217
handler: async ({ ctx }) => {
208-
const launchdarklyFirst = new LaunchDarkly(components.first, ctx, {
209-
LAUNCHDARKLY_SDK_KEY: process.env.LAUNCHDARKLY_SDK_KEY_FIRST!
210-
});
218+
const launchdarklyFirst = ldFirst.sdk(ctx);
211219

212-
const launchdarklySecond = new LaunchDarkly(components.second, ctx, {
213-
LAUNCHDARKLY_SDK_KEY: process.env.LAUNCHDARKLY_SDK_KEY_SECOND!
214-
});
220+
const launchdarklySecond = ldSecond.sdk(ctx);
215221

216222
...
217223
},

example/convex/example.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { LaunchDarkly } from "@convex-dev/launchdarkly";
22
import { mutation, query } from "./_generated/server";
33
import { components } from "./_generated/api";
44

5+
const launchdarkly = new LaunchDarkly(components.launchdarkly);
6+
57
export const listFruits = query({
68
handler: async (ctx) => {
7-
const launchdarkly = new LaunchDarkly(components.launchdarkly, ctx);
9+
const ld= = launchdarkly.sdk(ctx);
810
const user = { key: "myUserId" };
911

10-
const showFruits = await launchdarkly.boolVariation(
12+
const showFruits = await ld.boolVariation(
1113
"can-show-fruits",
1214
user,
1315
true
@@ -21,11 +23,11 @@ export const listFruits = query({
2123

2224
export const buyFruit = mutation({
2325
handler: async (ctx) => {
24-
const launchdarkly = new LaunchDarkly(components.launchdarkly, ctx);
26+
const ld = launchdarkly.sdk(ctx);
2527

2628
const user = { key: "myUserId" };
2729

28-
const showFruits = await launchdarkly.boolVariation(
30+
const showFruits = await ld.boolVariation(
2931
"can-buy-fruits",
3032
user,
3133
false
@@ -37,7 +39,7 @@ export const buyFruit = mutation({
3739
};
3840
}
3941

40-
launchdarkly.track("buy-fruit", user);
42+
ld.track("buy-fruit", user);
4143
},
4244
});
4345

@@ -47,9 +49,9 @@ export const initialized = query({
4749
if (!process.env.LAUNCHDARKLY_SDK_KEY) {
4850
return false;
4951
}
50-
const launchdarkly = new LaunchDarkly(components.launchdarkly, ctx);
52+
const ld = launchdarkly.sdk(ctx);
5153
return (
52-
(await launchdarkly.allFlagsState({ key: "any" })).allValues()[
54+
(await ld.allFlagsState({ key: "any" })).allValues()[
5355
"can-buy-fruits"
5456
] !== undefined
5557
);

src/sdk/LaunchDarkly.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@ describe("LaunchDarkly", () => {
1212
new LaunchDarkly(
1313
// @ts-expect-error It's ok
1414
api,
15-
{},
1615
{
1716
LAUNCHDARKLY_SDK_KEY: "test-key",
1817
}
19-
);
18+
// @ts-expect-error It's ok
19+
).sdk({});
2020

2121
expect(setTimeoutSpy).not.toHaveBeenCalled();
2222
expect(setIntervalSpy).not.toHaveBeenCalled();
2323
});
2424

2525
test("should throw an error if LAUNCHDARKLY_SDK_KEY is not provided", async () => {
26-
await expect(
27-
// @ts-expect-error It's ok
28-
() => new LaunchDarkly(api, {}, {})
29-
).toThrow(new Error("LAUNCHDARKLY_SDK_KEY is required"));
26+
// @ts-expect-error It's ok
27+
await expect(() => new LaunchDarkly(api, {}).sdk({})).toThrow(
28+
new Error("LAUNCHDARKLY_SDK_KEY is required")
29+
);
3030
});
3131

3232
test("should not throw an error if the env var is set", () => {
3333
vi.stubEnv("LAUNCHDARKLY_SDK_KEY", "test-key");
3434

3535
expect(() => {
3636
// @ts-expect-error It's ok
37-
new LaunchDarkly(api, {}, {});
37+
new LaunchDarkly(api, {}).sdk({});
3838
}).not.toThrow();
3939

4040
vi.unstubAllEnvs();
@@ -44,11 +44,11 @@ describe("LaunchDarkly", () => {
4444
const ld = new LaunchDarkly(
4545
// @ts-expect-error It's ok
4646
api,
47-
{},
4847
{
4948
LAUNCHDARKLY_SDK_KEY: "test-key",
5049
}
51-
);
50+
// @ts-expect-error It's ok
51+
).sdk({});
5252

5353
// @ts-expect-error We are testing internal state
5454
expect(ld.eventProcessor).not.toBeInstanceOf(EventProcessor);
@@ -58,11 +58,11 @@ describe("LaunchDarkly", () => {
5858
const ld = new LaunchDarkly(
5959
// @ts-expect-error It's ok
6060
api,
61-
{ runMutation: () => {} },
6261
{
6362
LAUNCHDARKLY_SDK_KEY: "test-key",
6463
}
65-
);
64+
// @ts-expect-error It's ok
65+
).sdk({ runMutation: () => {} });
6666

6767
// @ts-expect-error We are testing internal state
6868
expect(ld.eventProcessor).toBeInstanceOf(EventProcessor);
@@ -72,12 +72,12 @@ describe("LaunchDarkly", () => {
7272
const ld = new LaunchDarkly(
7373
// @ts-expect-error It's ok
7474
api,
75-
{ runMutation: () => {} },
7675
{
7776
LAUNCHDARKLY_SDK_KEY: "test-key",
7877
sendEvents: false,
7978
}
80-
);
79+
// @ts-expect-error It's ok
80+
).sdk({ runMutation: () => {} });
8181

8282
// @ts-expect-error We are testing internal state
8383
expect(ld.eventProcessor).not.toBeInstanceOf(EventProcessor);

src/sdk/LaunchDarkly.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,22 @@ import {
1717
import { RunMutationCtx, RunQueryCtx } from "../component/types";
1818
import { ComponentApi } from "./useApi";
1919

20-
export class LaunchDarkly extends LDClientImpl {
20+
export class LaunchDarkly {
21+
constructor(
22+
private component: ComponentApi,
23+
private options?: {
24+
application?: LDOptions["application"];
25+
sendEvents?: boolean;
26+
LAUNCHDARKLY_SDK_KEY?: string;
27+
} & EventProcessorOptions
28+
) {}
29+
30+
sdk(ctx: RunQueryCtx | RunMutationCtx) {
31+
return new LDClient(this.component, ctx, this.options);
32+
}
33+
}
34+
35+
class LDClient extends LDClientImpl {
2136
constructor(
2237
component: ComponentApi,
2338
ctx: RunQueryCtx | RunMutationCtx,

0 commit comments

Comments
 (0)