@openfeature/js-sdk
Advanced tools
Comparing version 0.5.0-experimental-fd144bb13457c29102e60f2075243f52b1ce6d0b to 0.5.0
@@ -240,3 +240,3 @@ "use strict"; | ||
const allHooksReversed = [...allHooks].reverse(); | ||
const mergedContext = __spreadValues(__spreadValues(__spreadValues({}, OpenFeature.getContext()), this._context), invocationContext); | ||
const mergedContext = __spreadValues(__spreadValues(__spreadValues(__spreadValues({}, OpenFeature.getContext()), OpenFeature.getTransactionContext()), this._context), invocationContext); | ||
const hookContext = { | ||
@@ -362,2 +362,13 @@ flagKey, | ||
// src/no-op-transaction-context-propagator.ts | ||
var NoopTransactionContextPropagator = class { | ||
getTransactionContext() { | ||
return {}; | ||
} | ||
setTransactionContext(_, callback) { | ||
callback(); | ||
} | ||
}; | ||
var NOOP_TRANSACTION_CONTEXT_PROPAGATOR = new NoopTransactionContextPropagator(); | ||
// src/open-feature.ts | ||
@@ -369,2 +380,3 @@ var GLOBAL_OPENFEATURE_API_KEY = Symbol.for("@openfeature/js.api"); | ||
this._provider = NOOP_PROVIDER; | ||
this._transactionContextPropagator = NOOP_TRANSACTION_CONTEXT_PROPAGATOR; | ||
this._context = {}; | ||
@@ -420,2 +432,26 @@ this._hooks = []; | ||
} | ||
setTransactionContextPropagator(transactionContextPropagator) { | ||
const baseMessage = "Invalid TransactionContextPropagator, will not be set: "; | ||
if (typeof (transactionContextPropagator == null ? void 0 : transactionContextPropagator.getTransactionContext) !== "function") { | ||
this._logger.error(`${baseMessage}: getTransactionContext is not a function.`); | ||
} else if (typeof (transactionContextPropagator == null ? void 0 : transactionContextPropagator.setTransactionContext) !== "function") { | ||
this._logger.error(`${baseMessage}: setTransactionContext is not a function.`); | ||
} else { | ||
this._transactionContextPropagator = transactionContextPropagator; | ||
} | ||
return this; | ||
} | ||
setTransactionContext(transactionContext, callback, ...args) { | ||
this._transactionContextPropagator.setTransactionContext(transactionContext, callback, ...args); | ||
} | ||
getTransactionContext() { | ||
try { | ||
return this._transactionContextPropagator.getTransactionContext(); | ||
} catch (err) { | ||
const error = err; | ||
this._logger.error(`Error getting transaction context: ${error == null ? void 0 : error.message}, returning empty context.`); | ||
this._logger.error(error == null ? void 0 : error.stack); | ||
return {}; | ||
} | ||
} | ||
}; | ||
@@ -422,0 +458,0 @@ var OpenFeature = OpenFeatureAPI.getInstance(); |
@@ -208,3 +208,3 @@ var __defProp = Object.defineProperty; | ||
const allHooksReversed = [...allHooks].reverse(); | ||
const mergedContext = __spreadValues(__spreadValues(__spreadValues({}, OpenFeature.getContext()), this._context), invocationContext); | ||
const mergedContext = __spreadValues(__spreadValues(__spreadValues(__spreadValues({}, OpenFeature.getContext()), OpenFeature.getTransactionContext()), this._context), invocationContext); | ||
const hookContext = { | ||
@@ -330,2 +330,13 @@ flagKey, | ||
// src/no-op-transaction-context-propagator.ts | ||
var NoopTransactionContextPropagator = class { | ||
getTransactionContext() { | ||
return {}; | ||
} | ||
setTransactionContext(_, callback) { | ||
callback(); | ||
} | ||
}; | ||
var NOOP_TRANSACTION_CONTEXT_PROPAGATOR = new NoopTransactionContextPropagator(); | ||
// src/open-feature.ts | ||
@@ -337,2 +348,3 @@ var GLOBAL_OPENFEATURE_API_KEY = Symbol.for("@openfeature/js.api"); | ||
this._provider = NOOP_PROVIDER; | ||
this._transactionContextPropagator = NOOP_TRANSACTION_CONTEXT_PROPAGATOR; | ||
this._context = {}; | ||
@@ -388,2 +400,26 @@ this._hooks = []; | ||
} | ||
setTransactionContextPropagator(transactionContextPropagator) { | ||
const baseMessage = "Invalid TransactionContextPropagator, will not be set: "; | ||
if (typeof (transactionContextPropagator == null ? void 0 : transactionContextPropagator.getTransactionContext) !== "function") { | ||
this._logger.error(`${baseMessage}: getTransactionContext is not a function.`); | ||
} else if (typeof (transactionContextPropagator == null ? void 0 : transactionContextPropagator.setTransactionContext) !== "function") { | ||
this._logger.error(`${baseMessage}: setTransactionContext is not a function.`); | ||
} else { | ||
this._transactionContextPropagator = transactionContextPropagator; | ||
} | ||
return this; | ||
} | ||
setTransactionContext(transactionContext, callback, ...args) { | ||
this._transactionContextPropagator.setTransactionContext(transactionContext, callback, ...args); | ||
} | ||
getTransactionContext() { | ||
try { | ||
return this._transactionContextPropagator.getTransactionContext(); | ||
} catch (err) { | ||
const error = err; | ||
this._logger.error(`Error getting transaction context: ${error == null ? void 0 : error.message}, returning empty context.`); | ||
this._logger.error(error == null ? void 0 : error.stack); | ||
return {}; | ||
} | ||
} | ||
}; | ||
@@ -390,0 +426,0 @@ var OpenFeature = OpenFeatureAPI.getInstance(); |
@@ -1,4 +0,5 @@ | ||
import { Client, EvaluationContext, FlagValue, GlobalApi, Hook, Logger, Provider, ProviderMetadata } from './types'; | ||
import { Client, EvaluationContext, FlagValue, GlobalApi, Hook, Logger, Provider, ProviderMetadata, TransactionContext, TransactionContextPropagator } from './types'; | ||
declare class OpenFeatureAPI implements GlobalApi { | ||
private _provider; | ||
private _transactionContextPropagator; | ||
private _context; | ||
@@ -22,4 +23,7 @@ private _hooks; | ||
getContext(): EvaluationContext; | ||
setTransactionContextPropagator(transactionContextPropagator: TransactionContextPropagator): OpenFeatureAPI; | ||
setTransactionContext<R>(transactionContext: TransactionContext, callback: (...args: unknown[]) => R, ...args: unknown[]): void; | ||
getTransactionContext(): TransactionContext; | ||
} | ||
export declare const OpenFeature: OpenFeatureAPI; | ||
export {}; |
@@ -237,3 +237,3 @@ declare type PrimitiveValue = null | boolean | string | number; | ||
} | ||
export interface GlobalApi extends EvaluationLifeCycle<GlobalApi>, ManageContext<GlobalApi>, ManageLogger<GlobalApi> { | ||
export interface GlobalApi extends EvaluationLifeCycle<GlobalApi>, ManageContext<GlobalApi>, ManageLogger<GlobalApi>, ManageTransactionContextPropagator<GlobalApi> { | ||
readonly providerMetadata: ProviderMetadata; | ||
@@ -312,3 +312,3 @@ /** | ||
* @template T The type of the receiver | ||
* @param {Logger} logger The logger to to be used | ||
* @param {Logger} logger The logger to be used | ||
* @returns {T} The receiver (this object) | ||
@@ -374,2 +374,47 @@ */ | ||
} | ||
/** | ||
* Transaction context is a mechanism for adding transaction specific context that | ||
* is merged with evaluation context prior to flag evaluation. Examples of potential | ||
* transaction specific context include: a user id, user agent, or request path. | ||
*/ | ||
export declare type TransactionContext = EvaluationContext; | ||
interface ManageTransactionContextPropagator<T> extends TransactionContextPropagator { | ||
/** | ||
* EXPERIMENTAL: Transaction context propagation is experimental and subject to change. | ||
* The OpenFeature Enhancement Proposal regarding transaction context can be found [here](https://github.com/open-feature/ofep/pull/32). | ||
* | ||
* Sets a transaction context propagator on this receiver. The transaction context | ||
* propagator is responsible for persisting context for the duration of a single | ||
* transaction. | ||
* | ||
* @template T The type of the receiver | ||
* @param {TransactionContextPropagator} transactionContextPropagator The context propagator to be used | ||
* @returns {T} The receiver (this object) | ||
*/ | ||
setTransactionContextPropagator(transactionContextPropagator: TransactionContextPropagator): T; | ||
} | ||
export interface TransactionContextPropagator { | ||
/** | ||
* EXPERIMENTAL: Transaction context propagation is experimental and subject to change. | ||
* The OpenFeature Enhancement Proposal regarding transaction context can be found [here](https://github.com/open-feature/ofep/pull/32). | ||
* | ||
* Returns the currently defined transaction context using the registered transaction | ||
* context propagator. | ||
* | ||
* @returns {TransactionContext} The current transaction context | ||
*/ | ||
getTransactionContext(): TransactionContext; | ||
/** | ||
* EXPERIMENTAL: Transaction context propagation is experimental and subject to change. | ||
* The OpenFeature Enhancement Proposal regarding transaction context can be found [here](https://github.com/open-feature/ofep/pull/32). | ||
* | ||
* Sets the transaction context using the registered transaction context propagator. | ||
* | ||
* @template R The return value of the callback | ||
* @param {TransactionContext} transactionContext The transaction specific context | ||
* @param {(...args: unknown[]) => R} callback Callback function used to set the transaction context on the stack | ||
* @param {...unknown[]} args Optional arguments that are passed to the callback function | ||
*/ | ||
setTransactionContext<R>(transactionContext: TransactionContext, callback: (...args: unknown[]) => R, ...args: unknown[]): void; | ||
} | ||
export {}; |
{ | ||
"name": "@openfeature/js-sdk", | ||
"version": "0.5.0-experimental-fd144bb13457c29102e60f2075243f52b1ce6d0b", | ||
"version": "0.5.0", | ||
"description": "OpenFeature SDK for JavaScript", | ||
@@ -61,2 +61,2 @@ "main": "./dist/cjs/index.js", | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
171034
23
1586