Skip to content

Commit b79a84a

Browse files
AndyBitzdferber90
andauthored
[flags] Add defaultValue to decide call (#107)
* [flags] Add `defaultValue` to `decide` call * Update .changeset/popular-eels-heal.md --------- Co-authored-by: Dominik Ferber <[email protected]>
1 parent 290c37e commit b79a84a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

.changeset/popular-eels-heal.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'flags': patch
3+
---
4+
5+
Pass the `defaultValue` to the adapter's decide function

packages/flags/src/next/index.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -531,4 +531,19 @@ describe('adapters', () => {
531531
);
532532
expect(f).toHaveProperty('origin', 'fake-origin#adapter-flag');
533533
});
534+
535+
it("should pass the defaultValue to the adapter's decide function", async () => {
536+
const outerValue = Math.random();
537+
538+
const exampleFlag = flag<number>({
539+
key: 'example-flag',
540+
defaultValue: outerValue,
541+
adapter: {
542+
decide: ({ defaultValue }) => defaultValue || -1,
543+
origin: (key) => `fake-origin#${key}`,
544+
},
545+
});
546+
547+
expect(await exampleFlag()).toBe(outerValue);
548+
});
534549
});

packages/flags/src/next/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ function getRun<ValueType, EntitiesType>(
283283
// Also fall back to defaultValue when the decide function returns undefined or throws an error.
284284
const decisionPromise = (async () => {
285285
return decide({
286+
// @ts-expect-error TypeScript will not be able to process `getPrecomputed` when added to `Decide`. It is, however, part of the `Adapter` type
287+
defaultValue: definition.defaultValue,
286288
headers: readonlyHeaders,
287289
cookies: readonlyCookies,
288290
entities,

0 commit comments

Comments
 (0)