Skip to content

Commit 8b63880

Browse files
pavkamroncohen
authored andcommitted
feat(openfeature-node-provider): enhance todo feature management with config and validation (#330)
1 parent 33acb1d commit 8b63880

2 files changed

Lines changed: 41 additions & 9 deletions

File tree

packages/openfeature-node-provider/example/app.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import express from "express";
22
import "./bucket";
33
import { EvaluationContext, OpenFeature } from "@openfeature/server-sdk";
4-
import provider from "./bucket";
4+
import provider, { CreateTodosConfig } from "./bucket";
55

6-
// In the following, we assume that targetingKey is a unique identifier for the user
6+
// In the following, we assume that targetingKey is a unique identifier for the user.
77
type Context = EvaluationContext & {
88
targetingKey: string;
99
companyId: string;
1010
};
1111

12-
// Augment the Express types to include the some context property on the `res.locals` object
12+
// Augment the Express types to include the some context property on the `res.locals` object.
1313
declare global {
1414
namespace Express {
1515
interface Locals {
@@ -36,6 +36,7 @@ const todos = ["Buy milk", "Walk the dog"];
3636
app.get("/", (_req, res) => {
3737
const ofClient = OpenFeature.getClient();
3838
ofClient.track("front-page-viewed", res.locals.context);
39+
3940
res.json({ message: "Ready to manage some TODOs!" });
4041
});
4142

@@ -46,7 +47,7 @@ app.get("/todos", async (req, res) => {
4647
// and that the indexing for feature name below is type-checked at compile time.
4748
const ofClient = OpenFeature.getClient();
4849
const isEnabled = await ofClient.getBooleanValue(
49-
"show-todo",
50+
"show-todos",
5051
false,
5152
res.locals.context,
5253
);
@@ -75,10 +76,23 @@ app.post("/todos", async (req, res) => {
7576
res.locals.context,
7677
);
7778

78-
// Check if the user has the "create-todos" feature enabled
79+
// Check if the user has the "create-todos" feature enabled.
7980
if (isEnabled) {
81+
// Get the configuration for the "create-todos" feature.
82+
// We expect the configuration to be a JSON object with a `maxLength` property.
83+
const config = await ofClient.getObjectValue<CreateTodosConfig>(
84+
"create-todos",
85+
{ maxLength: 100 },
86+
res.locals.context,
87+
);
88+
89+
// Check if the todo is too long.
90+
if (todo.length > config.maxLength) {
91+
return res.status(400).json({ error: "Todo is too long" });
92+
}
93+
8094
// Track the feature usage
81-
ofClient.track("create-todo", res.locals.context);
95+
ofClient.track("create-todos", res.locals.context);
8296
todos.push(todo);
8397

8498
return res.status(201).json({ todo });
@@ -98,15 +112,15 @@ app.delete("/todos/:idx", async (req, res) => {
98112

99113
const ofClient = OpenFeature.getClient();
100114
const isEnabled = await ofClient.getBooleanValue(
101-
"delete-todo",
115+
"delete-todos",
102116
false,
103117
res.locals.context,
104118
);
105119

106120
if (isEnabled) {
107121
todos.splice(idx, 1);
108122

109-
ofClient.track("delete-todo", res.locals.context);
123+
ofClient.track("delete-todos", res.locals.context);
110124
return res.json({});
111125
}
112126

packages/openfeature-node-provider/example/bucket.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@ if (!process.env.BUCKET_SECRET_KEY) {
55
throw new Error("BUCKET_SECRET_KEY is required");
66
}
77

8+
export type CreateTodosConfig = {
9+
maxLength: number;
10+
};
11+
812
const provider = new BucketNodeProvider({
913
secretKey: process.env.BUCKET_SECRET_KEY!,
10-
fallbackFeatures: ["show-todos"],
14+
fallbackFeatures: {
15+
"show-todos": {
16+
isEnabled: true,
17+
},
18+
"create-todos": {
19+
isEnabled: true,
20+
config: {
21+
key: "default",
22+
payload: {
23+
maxLength: 100,
24+
},
25+
},
26+
},
27+
},
1128
logger: console,
1229
});
30+
1331
OpenFeature.setProvider(provider);
1432

1533
export default provider;

0 commit comments

Comments
 (0)