Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions packages/openfeature-node-provider/example/app.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import express from "express";
import "./bucket";
import { EvaluationContext, OpenFeature } from "@openfeature/server-sdk";
import provider from "./bucket";
import provider, { CreateTodosConfig } from "./bucket";

// In the following, we assume that targetingKey is a unique identifier for the user
// In the following, we assume that targetingKey is a unique identifier for the user.
type Context = EvaluationContext & {
targetingKey: string;
companyId: string;
};

// Augment the Express types to include the some context property on the `res.locals` object
// Augment the Express types to include the some context property on the `res.locals` object.
declare global {
namespace Express {
interface Locals {
Expand All @@ -36,6 +36,7 @@ const todos = ["Buy milk", "Walk the dog"];
app.get("/", (_req, res) => {
const ofClient = OpenFeature.getClient();
ofClient.track("front-page-viewed", res.locals.context);

res.json({ message: "Ready to manage some TODOs!" });
});

Expand All @@ -46,7 +47,7 @@ app.get("/todos", async (req, res) => {
// and that the indexing for feature name below is type-checked at compile time.
const ofClient = OpenFeature.getClient();
const isEnabled = await ofClient.getBooleanValue(
"show-todo",
"show-todos",
false,
res.locals.context,
);
Expand Down Expand Up @@ -75,10 +76,23 @@ app.post("/todos", async (req, res) => {
res.locals.context,
);

// Check if the user has the "create-todos" feature enabled
// Check if the user has the "create-todos" feature enabled.
if (isEnabled) {
// Get the configuration for the "create-todos" feature.
// We expect the configuration to be a JSON object with a `maxLength` property.
const config = await ofClient.getObjectValue<CreateTodosConfig>(
"create-todos",
{ maxLength: 100 },
res.locals.context,
);

// Check if the todo is too long.
if (todo.length > config.maxLength) {
return res.status(400).json({ error: "Todo is too long" });
}

// Track the feature usage
ofClient.track("create-todo", res.locals.context);
ofClient.track("create-todos", res.locals.context);
todos.push(todo);

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

const ofClient = OpenFeature.getClient();
const isEnabled = await ofClient.getBooleanValue(
"delete-todo",
"delete-todos",
false,
res.locals.context,
);

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

ofClient.track("delete-todo", res.locals.context);
ofClient.track("delete-todos", res.locals.context);
return res.json({});
}

Expand Down
20 changes: 19 additions & 1 deletion packages/openfeature-node-provider/example/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ if (!process.env.BUCKET_SECRET_KEY) {
throw new Error("BUCKET_SECRET_KEY is required");
}

export type CreateTodosConfig = {
maxLength: number;
};

const provider = new BucketNodeProvider({
secretKey: process.env.BUCKET_SECRET_KEY!,
fallbackFeatures: ["show-todos"],
fallbackFeatures: {
"show-todos": {
isEnabled: true,
},
"create-todos": {
isEnabled: true,
config: {
key: "default",
payload: {
maxLength: 100,
},
},
},
},
logger: console,
});

OpenFeature.setProvider(provider);

export default provider;