-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an OpenFeature client provider for the React SDK to manage pricing-driven feature flags.
- Loading branch information
Showing
4 changed files
with
185 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { | ||
ClientProviderStatus, | ||
Hook, | ||
JsonValue, | ||
OpenFeatureEventEmitter, | ||
Provider, | ||
ResolutionDetails, | ||
} from '@openfeature/react-sdk'; | ||
import { evaluatePricing } from "../index"; | ||
import { ExtendedFeatureStatus } from 'pricing4ts'; | ||
|
||
export class ReactPricingDrivenFeaturesProvider implements Provider { | ||
readonly metadata = { | ||
name: 'pricing-driven-features', | ||
description: 'An Open Feature provider that enables features based on pricing information', | ||
}; | ||
|
||
readonly runsOn = 'client'; | ||
|
||
events = new OpenFeatureEventEmitter(); | ||
hooks?: Hook[] | undefined; | ||
status?: ClientProviderStatus | undefined; | ||
|
||
private pricingUrl: string | undefined; | ||
private pricingYaml: string | undefined; | ||
private evaluation: Record<string, ExtendedFeatureStatus> = {}; | ||
|
||
onContextChange(oldContext: any, newContext: any): Promise<void> | void { | ||
this.evaluation = evaluatePricing( | ||
this.pricingYaml!, | ||
newContext.subscription, | ||
newContext.userContext! | ||
); | ||
} | ||
|
||
resolveBooleanEvaluation(flagKey: string, defaultValue: boolean): ResolutionDetails<boolean> { | ||
try { | ||
return { | ||
value: this._evaluateFeature(flagKey).value.eval as boolean, | ||
}; | ||
} catch (error) { | ||
console.error('Error occurred during evaluation. ERROR: ', (error as Error).message); | ||
return { | ||
value: defaultValue, | ||
}; | ||
} | ||
} | ||
|
||
resolveStringEvaluation(flagKey: string, defaultValue: string): ResolutionDetails<string> { | ||
try { | ||
const result = this._evaluateFeature(flagKey); | ||
return { | ||
value: result.value.eval.toString(), | ||
}; | ||
} catch (error) { | ||
console.error('Error occurred during evaluation. ERROR: ', (error as Error).message); | ||
return { | ||
value: defaultValue, | ||
}; | ||
} | ||
} | ||
|
||
resolveNumberEvaluation(flagKey: string, defaultValue: number): ResolutionDetails<number> { | ||
try { | ||
const result = this._evaluateFeature(flagKey); | ||
return { | ||
value: result.value.eval ? 1 : 0, | ||
}; | ||
} catch (error) { | ||
console.error('Error occurred during evaluation. ERROR: ', (error as Error).message); | ||
return { | ||
value: defaultValue, | ||
}; | ||
} | ||
} | ||
|
||
resolveObjectEvaluation<T extends JsonValue>( | ||
flagKey: string, | ||
defaultValue: T | ||
): ResolutionDetails<T> { | ||
try { | ||
return this._evaluateFeature(flagKey) as unknown as ResolutionDetails<T>; | ||
} catch (error) { | ||
console.error('Error occurred during evaluation. ERROR: ', (error as Error).message); | ||
return { | ||
value: defaultValue, | ||
} as ResolutionDetails<T>; | ||
} | ||
} | ||
|
||
initialize?(context?: any): Promise<void> { | ||
if (context.pricingUrl) { | ||
this.pricingUrl = context.pricingUrl; | ||
if (!context.subscription) { | ||
return Promise.reject( | ||
"Subscription not provided in context. Use 'subscription' to provide one. It's value must be a list comprised of at least one name of plan/add-on." | ||
); | ||
} | ||
if (!context.userContext) { | ||
return Promise.reject( | ||
"User context not provided in context. Use 'userContext' to provide one. It's value must be a JSON object with at least the key 'user', with a string value identifying the user." | ||
); | ||
} | ||
return fetch(this.pricingUrl!) | ||
.then(response => response.text()) | ||
.then(yaml => { | ||
this.pricingYaml = yaml; | ||
this.evaluation = evaluatePricing( | ||
this.pricingYaml, | ||
context.subscription, | ||
context.userContext! | ||
); | ||
console.log("PricingDrivenFeaturesProvider initialized with context: ", context); | ||
}); | ||
} else { | ||
return Promise.reject( | ||
"Pricing URL not provided in context. Use 'pricingUrl' to provide one." | ||
); | ||
} | ||
} | ||
|
||
private _evaluateFeature(flagKey: string): ResolutionDetails<any> { | ||
if (Object.keys(this.evaluation).length === 0) { | ||
return { | ||
value: { | ||
eval: false, | ||
used: null, | ||
limit: null, | ||
error: { | ||
code: 'PROVIDER_NOT_READY', | ||
message: 'Pricing not yet loaded into the provider', | ||
}, | ||
}, | ||
}; | ||
} else { | ||
return { | ||
value: this.evaluation[flagKey], | ||
}; | ||
} | ||
} | ||
} | ||
|