Skip to content

Commit 79f3d9c

Browse files
authored
feat(openfeature-node-provider): improve flag resolution and context handling (#318)
- Enhanced flag resolution methods with more robust error handling - Added support for more context attributes in context translator - Improved type checking and resolution for different flag types - Updated README with more detailed usage and resolution method explanations - Bumped node-sdk dependency to 1.6.0-alpha.3
1 parent 8c79bd8 commit 79f3d9c

5 files changed

Lines changed: 463 additions & 137 deletions

File tree

packages/openfeature-node-provider/README.md

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@ The official OpenFeature Node.js provider for [Bucket](https://bucket.co) featur
44

55
## Installation
66

7-
```
8-
$ npm install @bucketco/openfeature-node-provider
7+
```shell
8+
npm install @bucketco/openfeature-node-provider
99
```
1010

11-
#### Required peer dependencies
11+
### Required peer dependencies
1212

1313
The OpenFeature SDK is required as peer dependency.
14-
1514
The minimum required version of `@openfeature/server-sdk` currently is `1.13.5`.
16-
1715
The minimum required version of `@bucketco/node-sdk` currently is `2.0.0`.
1816

19-
```
20-
$ npm install @openfeature/server-sdk @bucketco/node-sdk
17+
```shell
18+
npm install @openfeature/server-sdk @bucketco/node-sdk
2119
```
2220

2321
## Usage
2422

2523
The provider uses the [Bucket Node.js SDK](https://docs.bucket.co/quickstart/supported-languages-frameworks/node.js-sdk).
26-
2724
The available options can be found in the [Bucket Node.js SDK](https://github.com/bucketco/bucket-javascript-sdk/tree/main/packages/node-sdk#initialization-options).
2825

2926
### Example using the default configuration
@@ -56,6 +53,59 @@ const enterpriseFeatureEnabled = await client.getBooleanValue(
5653
);
5754
```
5855

56+
## Feature resolution methods
57+
58+
The Bucket OpenFeature Provider implements the OpenFeature evaluation interface for different value types. Each method handles the resolution of feature flags according to the OpenFeature specification.
59+
60+
### Common behavior
61+
62+
All resolution methods share these behaviors:
63+
64+
- Return default value with `PROVIDER_NOT_READY` if client is not initialized,
65+
- Return default value with `FLAG_NOT_FOUND` if flag doesn't exist,
66+
- Return default value with `ERROR` if there was a type mismatch,
67+
- Return evaluated value with `TARGETING_MATCH` on successful resolution.
68+
69+
### Type-Specific Methods
70+
71+
#### Boolean Resolution
72+
73+
```ts
74+
client.getBooleanValue("my-flag", false);
75+
```
76+
77+
Returns the feature's enabled state. This is the most common use case for feature flags.
78+
79+
#### String Resolution
80+
81+
```ts
82+
client.getStringValue("my-flag", "default");
83+
```
84+
85+
Returns the feature's remote config key (also known as "variant"). Useful for multi-variate use cases.
86+
87+
#### Number Resolution
88+
89+
```ts
90+
client.getNumberValue("my-flag", 0);
91+
```
92+
93+
Not directly supported by Bucket. Use `getObjectValue` instead for numeric configurations.
94+
95+
#### Object Resolution
96+
97+
```ts
98+
// works for any type:
99+
client.getObjectValue("my-flag", { defaultValue: true });
100+
client.getObjectValue("my-flag", "string-value");
101+
client.getObjectValue("my-flag", 199);
102+
```
103+
104+
Returns the feature's remote config payload with type validation. This is the most flexible method,
105+
allowing for complex configuration objects or simple types.
106+
107+
The object resolution performs runtime type checking between the default value and the feature payload to ensure type safety.
108+
59109
## Translating Evaluation Context
60110

61111
Bucket uses a context object of the following shape:
@@ -69,11 +119,19 @@ export type BucketContext = {
69119
/**
70120
* The user context. If the user is set, the user ID is required.
71121
*/
72-
user?: { id: string; [k: string]: any };
122+
user?: {
123+
id: string;
124+
name?: string;
125+
email?: string;
126+
avatar?: string;
127+
[k: string]: any;
128+
};
129+
73130
/**
74131
* The company context. If the company is set, the company ID is required.
75132
*/
76-
company?: { id: string; [k: string]: any };
133+
company?: { id: string; name?: string; avatar?: string; [k: string]: any };
134+
77135
/**
78136
* The other context. This is used for any additional context that is not related to user or company.
79137
*/
@@ -91,14 +149,17 @@ import { BucketNodeProvider } from "@openfeature/bucket-node-provider";
91149
const contextTranslator = (context: EvaluationContext): BucketContext => {
92150
return {
93151
user: {
94-
id: context.targetingKey,
152+
id: context.targetingKey ?? context["userId"]?.toString(),
95153
name: context["name"]?.toString(),
96154
email: context["email"]?.toString(),
155+
avatar: context["avatar"]?.toString(),
97156
country: context["country"]?.toString(),
98157
},
99158
company: {
100-
id: context["companyId"],
101-
name: context["companyName"],
159+
id: context["companyId"]?.toString(),
160+
name: context["companyName"]?.toString(),
161+
avatar: context["companyAvatar"]?.toString(),
162+
plan: context["companyPlan"]?.toString(),
102163
},
103164
};
104165
};
@@ -115,7 +176,7 @@ It's straight forward to start sending tracking events through OpenFeature.
115176

116177
Simply call the "track" method on the OpenFeature client:
117178

118-
```ts
179+
```typescript
119180
import { BucketNodeProvider } from "@bucketco/openfeature-node-provider";
120181
import { OpenFeature } from "@openfeature/server-sdk";
121182

@@ -132,8 +193,7 @@ const enterpriseFeatureEnabled = await client.track(
132193
);
133194
```
134195

135-
# License
136-
137-
MIT License
196+
## License
138197

139-
Copyright (c) 2025 Bucket ApS
198+
> MIT License
199+
> Copyright (c) 2025 Bucket ApS

packages/openfeature-node-provider/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bucketco/openfeature-node-provider",
3-
"version": "0.2.1",
3+
"version": "0.3.0-alpha.0",
44
"license": "MIT",
55
"repository": {
66
"type": "git",
@@ -44,7 +44,7 @@
4444
"vitest": "~1.6.0"
4545
},
4646
"dependencies": {
47-
"@bucketco/node-sdk": ">=1.4.2"
47+
"@bucketco/node-sdk": "1.6.0-alpha.3"
4848
},
4949
"peerDependencies": {
5050
"@openfeature/server-sdk": ">=1.16.1"

0 commit comments

Comments
 (0)