Skip to content

Commit 5f9b565

Browse files
committed
refactor: update flag terminology and improve client logic
- Renamed `flagKey` to `key` in the feedback form options for consistency. - Refactored the `updateFlags` function in the `Toolbar` component to use `Object.entries` for better readability and updated flag handling. - Simplified the `Logo` component's SVG attributes for improved clarity. - Removed deprecated `getFeature` references in tests and adjusted related logic to align with the new flag-based approach. - Updated mock handlers to reflect the new endpoint structure for user requests.
1 parent c29b292 commit 5f9b565

8 files changed

Lines changed: 140 additions & 479 deletions

File tree

packages/browser-sdk/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ export class ReflagClient {
665665
// to prevent the same click from closing it.
666666
setTimeout(() => {
667667
feedbackLib.openFeedbackForm({
668-
flagKey: options.flagKey,
668+
key: options.flagKey,
669669
title: options.title,
670670
position: options.position || this.requestFeedbackOptions.position,
671671
translations:

packages/browser-sdk/src/toolbar/Toolbar.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,12 @@ export default function Toolbar({
3838
const updateFlags = useCallback(() => {
3939
const rawFlags = reflagClient.getFlags();
4040
setFlags(
41-
Object.values(rawFlags).map(
42-
(flag) =>
41+
Object.entries(rawFlags).map(
42+
([key, value]) =>
4343
({
44-
flagKey: flag.key,
45-
override: flag.valueOverride,
46-
value: flag.config
47-
? {
48-
key: flag.config.key,
49-
payload: flag.config.payload,
50-
}
51-
: flag.isEnabled,
44+
flagKey: key,
45+
override: reflagClient.getFlagOverride(key),
46+
value,
5247
}) satisfies FlagItem,
5348
),
5449
);

packages/browser-sdk/src/ui/icons/Logo.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@ export const Logo: FunctionComponent<h.JSX.SVGAttributes<SVGSVGElement>> = (
1010
{...props}
1111
>
1212
<path
13-
clipRule="evenodd"
14-
d="M149.608 33.1691C114.541 33.1691 88.1957 34.0727 68.6978 35.2674C45.9877 36.6589 31.2447 53.6165 32.7413 77.1477C33.9788 96.6049 36.0938 122.855 39.6451 157.89C44.0755 201.599 48.0467 231.615 51.1923 251.815C53.8983 269.193 66.5685 280.724 83.8252 281.651C99.4409 282.49 120.822 283.137 149.608 283.137C178.991 283.137 200.659 282.463 216.355 281.599C232.793 280.694 244.886 269.593 247.467 252.738C249.007 242.679 250.794 230.104 252.816 214.471C254.076 204.729 255.427 193.799 256.866 181.551C257.741 174.108 258.648 166.178 259.588 157.731C263.785 119.994 266.075 92.6244 267.274 72.9772C268.564 51.838 255.492 36.9197 234.632 35.5304C214.779 34.208 187.208 33.1691 149.608 33.1691ZM66.7035 2.65939C86.9741 1.41736 113.994 0.5 149.608 0.5C187.792 0.5 216.098 1.55452 236.8 2.93336C275.829 5.53288 302.222 36.1432 299.852 74.9692C298.613 95.2768 296.268 123.202 292.026 161.346C287.025 206.312 282.898 236.995 279.729 257.688C274.896 289.249 250.57 312.434 218.147 314.218C201.752 315.121 179.476 315.806 149.608 315.806C120.346 315.806 98.3726 315.149 82.0761 314.273C49.1273 312.503 23.9794 289.192 18.9427 256.846C15.6873 235.941 11.6471 205.329 7.17277 161.187C3.58873 125.829 1.43655 99.164 0.168272 79.2232C-2.42412 38.4637 25.1723 5.20409 66.7035 2.65939Z"
15-
fill="currentColor"
16-
fillRule="evenodd"
17-
/>
18-
<path
19-
clipRule="evenodd"
20-
d="M150.869 165.962C150.29 165.209 149.718 164.45 149.156 163.684C144.318 157.097 140.113 150.016 136.63 142.531C132.143 132.889 136.224 124.122 143.367 120.398C151.531 116.142 162.346 120.398 166.226 128.738C169.442 135.653 173.494 142.1 178.25 147.949C193.08 166.186 214.765 178.615 239.375 181.302C242.678 181.662 246.035 181.848 249.434 181.848C251.936 181.848 254.415 181.747 256.866 181.551C260.357 181.848 256.071 214.471 252.816 214.471C251.692 214.501 250.565 214.517 249.434 214.517C245.891 214.517 242.383 214.368 238.915 214.078C203.102 211.075 171.603 192.885 150.869 165.962Z"
21-
fill="currentColor"
22-
fillRule="evenodd"
13+
fill-rule="evenodd"
14+
clip-rule="evenodd"
15+
d="M12.4167 0.5C13.015 0.5 13.5 0.985025 13.5 1.58333V12.4167C13.5 13.015 13.015 13.5 12.4167 13.5H1.58333C0.985025 13.5 0.5 13.015 0.5 12.4167V1.58333C0.5 0.985025 0.985025 0.5 1.58333 0.5H12.4167ZM1.58333 1.58333V12.4167L12.4167 1.58333H1.58333Z"
16+
fill="white"
2317
/>
2418
</svg>
2519
);

packages/browser-sdk/test/client.test.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { ReflagClient } from "../src/client";
44
import { FlagsClient } from "../src/flag/flags";
55
import { HttpClient } from "../src/httpClient";
66

7-
import { flagsResult } from "./mocks/handlers";
8-
97
describe("ReflagClient", () => {
108
let client: ReflagClient;
119
const httpClientPost = vi.spyOn(HttpClient.prototype as any, "post");
@@ -65,18 +63,6 @@ describe("ReflagClient", () => {
6563
});
6664
});
6765

68-
describe("getFeature (deprecated)", () => {
69-
it("takes overrides into account", async () => {
70-
await client.initialize();
71-
72-
expect(flagsResult["flagA"].isEnabled).toBe(true);
73-
expect(client.getFeature("flagA").isEnabled).toBe(true);
74-
75-
client.getFeature("flagA").setIsEnabledOverride(false);
76-
expect(client.getFeature("flagA").isEnabled).toBe(false);
77-
});
78-
});
79-
8066
describe("getFlag", () => {
8167
beforeEach(async () => {
8268
await client.initialize();
@@ -143,8 +129,6 @@ describe("ReflagClient", () => {
143129
const userHook = vi.fn();
144130
const companyHook = vi.fn();
145131
const checkHook = vi.fn();
146-
const checkHookIsEnabled = vi.fn();
147-
const checkHookConfig = vi.fn();
148132
const flagsUpdated = vi.fn();
149133

150134
beforeEach(async () => {
@@ -154,16 +138,12 @@ describe("ReflagClient", () => {
154138
client.on("user", userHook);
155139
client.on("company", companyHook);
156140
client.on("check", checkHook);
157-
client.on("configCheck", checkHookConfig);
158-
client.on("enabledCheck", checkHookIsEnabled);
159141
client.on("flagsUpdated", flagsUpdated);
160142

161143
trackHook.mockReset();
162144
userHook.mockReset();
163145
companyHook.mockReset();
164146
checkHook.mockReset();
165-
checkHookIsEnabled.mockReset();
166-
checkHookConfig.mockReset();
167147
flagsUpdated.mockReset();
168148
});
169149

@@ -172,8 +152,6 @@ describe("ReflagClient", () => {
172152
client.off("user", userHook);
173153
client.off("company", companyHook);
174154
client.off("check", checkHook);
175-
client.off("configCheck", checkHookConfig);
176-
client.off("enabledCheck", checkHookIsEnabled);
177155
client.off("flagsUpdated", flagsUpdated);
178156
});
179157

@@ -246,23 +224,19 @@ describe("ReflagClient", () => {
246224
version: 1,
247225
};
248226

249-
expect(checkHookIsEnabled).toHaveBeenCalledWith(checkEvent);
250227
expect(checkHook).toHaveBeenCalledWith(checkEvent);
251228

252229
// Remove hooks
253230
client.off("check", checkHook);
254-
client.off("enabledCheck", checkHookIsEnabled);
255231

256232
// Reset mocks
257233
checkHook.mockReset();
258-
checkHookIsEnabled.mockReset();
259234

260235
// Trigger events again
261236
client.getFlag("flagA");
262237

263238
// Ensure hooks are not called
264239
expect(checkHook).not.toHaveBeenCalled();
265-
expect(checkHookIsEnabled).not.toHaveBeenCalled();
266240
});
267241

268242
it("check (config)", async () => {
@@ -284,20 +258,16 @@ describe("ReflagClient", () => {
284258
};
285259

286260
expect(checkHook).toHaveBeenCalledWith(checkEvent);
287-
expect(checkHookConfig).toHaveBeenCalledWith(checkEvent);
288261

289262
client.off("check", checkHook);
290-
client.off("configCheck", checkHookConfig);
291263

292264
// Reset mocks
293265
checkHook.mockReset();
294-
checkHookConfig.mockReset();
295266

296267
client.getFlag("flagB");
297268

298269
// Ensure hooks are not called
299270
expect(checkHook).not.toHaveBeenCalled();
300-
expect(checkHookConfig).not.toHaveBeenCalled();
301271
});
302272

303273
it("flagsUpdated", async () => {

packages/browser-sdk/test/hooksManager.test.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,6 @@ describe("HookManager", () => {
2525
expect(callback).toHaveBeenCalledWith(checkEvent);
2626
});
2727

28-
it("should add and trigger `enabledCheck` hooks", () => {
29-
const callback = vi.fn();
30-
hookManager.addHook("enabledCheck", callback);
31-
32-
const checkEvent: CheckEvent = {
33-
action: "check-is-enabled",
34-
key: "test-key",
35-
value: true,
36-
};
37-
hookManager.trigger("enabledCheck", checkEvent);
38-
39-
expect(callback).toHaveBeenCalledWith(checkEvent);
40-
});
41-
42-
it("should add and trigger `configCheck` hooks", () => {
43-
const callback = vi.fn();
44-
hookManager.addHook("configCheck", callback);
45-
46-
const checkEvent: CheckEvent = {
47-
action: "check-config",
48-
key: "test-key",
49-
value: { key: "key", payload: "payload" },
50-
};
51-
hookManager.trigger("configCheck", checkEvent);
52-
53-
expect(callback).toHaveBeenCalledWith(checkEvent);
54-
});
55-
56-
it("should add and trigger `featuresUpdated` hooks (deprecated)", () => {
57-
const callback = vi.fn();
58-
hookManager.addHook("featuresUpdated", callback);
59-
60-
const flags: RawFlags = {
61-
/* mock RawFlags data */
62-
};
63-
hookManager.trigger("flagsUpdated", flags);
64-
65-
expect(callback).toHaveBeenCalledWith(flags);
66-
});
67-
6828
it("should add and trigger `flagsUpdated` hooks", () => {
6929
const callback = vi.fn();
7030
hookManager.addHook("flagsUpdated", callback);

packages/browser-sdk/test/init.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("init", () => {
4040

4141
server.use(
4242
http.get(
43-
"https://example.com/flags/evaluated",
43+
"https://example.com/features/evaluated",
4444
({ request }: { request: StrictRequest<DefaultBodyType> }) => {
4545
usedSpecialHost = true;
4646
return getFlags({ request });

packages/browser-sdk/test/mocks/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function getFlags({
7979
}
8080

8181
export const handlers = [
82-
http.post("https://front.bucket.co/user", async ({ request }) => {
82+
http.post("https://front.reflag.co/user", async ({ request }) => {
8383
if (!checkRequest(request)) return invalidReqResponse;
8484

8585
const data = await request.json();

0 commit comments

Comments
 (0)