Skip to content

Commit cd821cb

Browse files
committed
fix: only enable feature if all conditions pass
1 parent 54fbdb3 commit cd821cb

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ To get started, add a `#### Conditions` subheading to the feature issue and list
203203
#### Conditions
204204

205205
- [ ] Only if admin
206-
207206
```
208207

209208
Now define how the condition is evaluated during runtime.
@@ -213,7 +212,7 @@ Now define how the condition is evaluated during runtime.
213212

214213
The `requestToContext` is a good place to extract information from the request object that is needed to evaluate the conditions. For example, you can extract cookies or headers from the request object to determine if a user is signed in.
215214

216-
>❗ Important: The `requestToContext` function is only called when communicating with the `NextFlag` API over HTTP. If you are using the `NextFlag` directly in a server-side component, you must build the context object yourself and pass it to the `isFeatureEnabled` method directly.
215+
> ❗ Important: The `requestToContext` function is only called when communicating with the `NextFlag` API over HTTP. If you are using the `NextFlag` directly in a server-side component, you must build the context object yourself and pass it to the `isFeatureEnabled` method directly.
217216
218217
```ts
219218
// src/app/api/next-flag/index.ts
@@ -450,4 +449,3 @@ If you don't want to use a Webhook simply omit the `NEXT_FLAG_WEBHOOK_SECRET` fr
450449
| `POST` | `(req: NextRequest) => Promise<NextResponse<{ error: string; }> or NextResponse<{ success: boolean; }>>` |
451450

452451
<!-- TSDOC_END -->
453-
````

src/NextFlag.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,23 @@ export class NextFlag {
256256
return;
257257
}
258258

259-
await Promise.all(
259+
const computedConditions = await Promise.all(
260260
conditionKeys.map(async (conditionKey) => {
261261
const condition = conditionsForFeature[conditionKey];
262262
if (!condition.enabled) {
263263
enabledFeaturesWithConditions.push(feature);
264-
return null;
264+
return true;
265265
}
266266
const conditionFn = conditionsForPath[conditionKey];
267267
const result =
268268
(await conditionFn?.(options.context || {})) || false;
269-
if (result) {
270-
enabledFeaturesWithConditions.push(feature);
271-
}
272-
return null;
269+
return result;
273270
})
274271
);
272+
273+
if (computedConditions.every((x) => x === true)) {
274+
enabledFeaturesWithConditions.push(feature);
275+
}
275276
})
276277
);
277278

0 commit comments

Comments
 (0)