-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathopen-feature.ts
163 lines (148 loc) · 5.99 KB
/
open-feature.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { NOOP_PROVIDER, Provider } from './provider';
import {
ManageContext,
OpenFeatureCommonAPI,
EvaluationContext,
objectOrUndefined,
stringOrUndefined,
} from '@openfeature/shared';
import {
ManageTransactionContextPropagator,
NOOP_TRANSACTION_CONTEXT_PROPAGATOR,
TransactionContext,
TransactionContextPropagator,
} from './transaction-context';
import { Client, OpenFeatureClient } from './client';
// use a symbol as a key for the global singleton
const GLOBAL_OPENFEATURE_API_KEY = Symbol.for('@openfeature/js-sdk/api');
type OpenFeatureGlobal = {
[GLOBAL_OPENFEATURE_API_KEY]?: OpenFeatureAPI;
};
const _globalThis = globalThis as OpenFeatureGlobal;
export class OpenFeatureAPI
extends OpenFeatureCommonAPI<Provider>
implements ManageContext<OpenFeatureAPI>, ManageTransactionContextPropagator<OpenFeatureCommonAPI<Provider>>
{
private _transactionContextPropagator: TransactionContextPropagator = NOOP_TRANSACTION_CONTEXT_PROPAGATOR;
protected _defaultProvider: Provider = NOOP_PROVIDER;
private constructor() {
super('server');
}
/**
* Gets a singleton instance of the OpenFeature API.
* @ignore
* @returns {OpenFeatureAPI} OpenFeature API
*/
static getInstance(): OpenFeatureAPI {
const globalApi = _globalThis[GLOBAL_OPENFEATURE_API_KEY];
if (globalApi) {
return globalApi;
}
const instance = new OpenFeatureAPI();
_globalThis[GLOBAL_OPENFEATURE_API_KEY] = instance;
return instance;
}
setContext(context: EvaluationContext): this {
this._context = context;
return this;
}
getContext(): EvaluationContext {
return this._context;
}
/**
* A factory function for creating new unnamed OpenFeature clients. Clients can contain
* their own state (e.g. logger, hook, context). Multiple clients can be used
* to segment feature flag configuration.
*
* All unnamed clients use the same provider set via {@link this.setProvider setProvider}.
* @param {EvaluationContext} context Evaluation context that should be set on the client to used during flag evaluations
* @returns {Client} OpenFeature Client
*/
getClient(context?: EvaluationContext): Client;
/**
* A factory function for creating new named OpenFeature clients. Clients can contain
* their own state (e.g. logger, hook, context). Multiple clients can be used
* to segment feature flag configuration.
*
* If there is already a provider bound to this name via {@link this.setProvider setProvider}, this provider will be used.
* Otherwise, the default provider is used until a provider is assigned to that name.
* @param {string} name The name of the client
* @param {EvaluationContext} context Evaluation context that should be set on the client to used during flag evaluations
* @returns {Client} OpenFeature Client
*/
getClient(name: string, context?: EvaluationContext): Client;
/**
* A factory function for creating new named OpenFeature clients. Clients can contain
* their own state (e.g. logger, hook, context). Multiple clients can be used
* to segment feature flag configuration.
*
* If there is already a provider bound to this name via {@link this.setProvider setProvider}, this provider will be used.
* Otherwise, the default provider is used until a provider is assigned to that name.
* @param {string} name The name of the client
* @param {string} version The version of the client (only used for metadata)
* @param {EvaluationContext} context Evaluation context that should be set on the client to used during flag evaluations
* @returns {Client} OpenFeature Client
*/
getClient(name: string, version: string, context?: EvaluationContext): Client;
getClient(
nameOrContext?: string | EvaluationContext,
versionOrContext?: string | EvaluationContext,
contextOrUndefined?: EvaluationContext
): Client {
const name = stringOrUndefined(nameOrContext);
const version = stringOrUndefined(versionOrContext);
const context =
objectOrUndefined<EvaluationContext>(nameOrContext) ??
objectOrUndefined<EvaluationContext>(versionOrContext) ??
objectOrUndefined<EvaluationContext>(contextOrUndefined);
return new OpenFeatureClient(
() => this.getProviderForClient(name),
() => this.buildAndCacheEventEmitterForClient(name),
() => this._logger,
{ name, version },
context
);
}
/**
* Clears all registered providers and resets the default provider.
* @returns {Promise<void>}
*/
clearProviders(): Promise<void> {
return super.clearProvidersAndSetDefault(NOOP_PROVIDER);
}
setTransactionContextPropagator(
transactionContextPropagator: TransactionContextPropagator
): OpenFeatureCommonAPI<Provider> {
const baseMessage = 'Invalid TransactionContextPropagator, will not be set: ';
if (typeof transactionContextPropagator?.getTransactionContext !== 'function') {
this._logger.error(`${baseMessage}: getTransactionContext is not a function.`);
} else if (typeof transactionContextPropagator?.setTransactionContext !== 'function') {
this._logger.error(`${baseMessage}: setTransactionContext is not a function.`);
} else {
this._transactionContextPropagator = transactionContextPropagator;
}
return this;
}
setTransactionContext<R>(
transactionContext: TransactionContext,
callback: (...args: unknown[]) => R,
...args: unknown[]
): void {
this._transactionContextPropagator.setTransactionContext(transactionContext, callback, ...args);
}
getTransactionContext(): TransactionContext {
try {
return this._transactionContextPropagator.getTransactionContext();
} catch (err: unknown) {
const error = err as Error | undefined;
this._logger.error(`Error getting transaction context: ${error?.message}, returning empty context.`);
this._logger.error(error?.stack);
return {};
}
}
}
/**
* A singleton instance of the OpenFeature API.
* @returns {OpenFeatureAPI} OpenFeature API
*/
export const OpenFeature = OpenFeatureAPI.getInstance();