-
Notifications
You must be signed in to change notification settings - Fork 27
/
web-harness-provider.ts
109 lines (92 loc) · 3.65 KB
/
web-harness-provider.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
import { Event as HarnessEvent, initialize } from '@harnessio/ff-javascript-client-sdk';
import {
EvaluationContext,
OpenFeatureEventEmitter,
JsonValue,
Logger,
Provider,
ProviderEvents,
ProviderMetadata,
ResolutionDetails,
TypeMismatchError,
ProviderStatus,
} from '@openfeature/web-sdk';
type ValueTypes = 'boolean' | 'string' | 'number' | 'object';
/**
* NOTE: This is an unofficial provider that was created for demonstration
* purposes only. The playground environment will be updated to use official
* providers once they're available.
*/
export class HarnessWebProvider implements Provider {
private _client!: ReturnType<typeof initialize>;
metadata: ProviderMetadata = {
name: 'Harness web provider',
};
private _status = ProviderStatus.NOT_READY;
get status() {
return this._status;
}
events = new OpenFeatureEventEmitter();
constructor(private apiKey: string, private logger?: Logger) {}
initialize(context: EvaluationContext): Promise<void> {
this._client = initialize(this.apiKey, { identifier: context.targetingKey || 'anon', attributes: context });
return new Promise((resolve) => {
this._client.on(HarnessEvent.READY, () => {
// mark as ready as soon as the SDK setup completes
this.addChangeHandler(this._client);
this._status = ProviderStatus.READY;
resolve();
});
});
}
onContextChange?(oldContext: EvaluationContext, newContext: EvaluationContext): Promise<void> {
const oldClient = this._client;
const client = initialize(this.apiKey, { identifier: newContext.targetingKey || 'anon', attributes: newContext });
// needed?
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return new Promise((resolve, reject) => {
client.on(HarnessEvent.READY, () => {
oldClient.close();
this.addChangeHandler(client);
this._client = client;
resolve();
});
});
}
onClose?(): Promise<void> {
return Promise.resolve(this._client.close());
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resolveBooleanEvaluation(flagKey: string, defaultValue: boolean, context: EvaluationContext, logger: Logger): ResolutionDetails<boolean> {
return this.resolve(flagKey, defaultValue, 'boolean');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resolveStringEvaluation(flagKey: string, defaultValue: string, context: EvaluationContext, logger: Logger): ResolutionDetails<string> {
return this.resolve(flagKey, defaultValue, 'string');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resolveNumberEvaluation(flagKey: string, defaultValue: number, context: EvaluationContext, logger: Logger): ResolutionDetails<number> {
return this.resolve(flagKey, defaultValue, 'number');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T, context: EvaluationContext, logger: Logger): ResolutionDetails<T> {
return this.resolve(flagKey, defaultValue, 'object');
}
resolve<T>(flagKey: string, defaultValue: T, type: ValueTypes): ResolutionDetails<T> {
// harness doesn't seem to allow '-' in flag keys
const transformedKey = flagKey.replace(/-/g, '');
const value = this._client.variation(transformedKey, defaultValue);
if (typeof value === type) {
return {
value,
} as ResolutionDetails<T>;
} else {
throw new TypeMismatchError();
}
}
addChangeHandler(client: ReturnType<typeof initialize>) {
client.on(HarnessEvent.CHANGED, () => {
this.events.emit(ProviderEvents.ConfigurationChanged);
});
}
}