11import express from "express" ;
22import "./bucket" ;
33import { 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.
77type 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.
1313declare global {
1414 namespace Express {
1515 interface Locals {
@@ -36,6 +36,7 @@ const todos = ["Buy milk", "Walk the dog"];
3636app . 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
0 commit comments