-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
174 lines (149 loc) · 4.17 KB
/
index.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
164
165
166
167
168
169
170
171
172
173
174
import init, { Engine } from '../dist/flipt_engine_wasm.js';
import wasm from '../dist/flipt_engine_wasm_bg.wasm';
import {
BatchResult,
BooleanResult,
EngineOpts,
EvaluationRequest,
IFetcher,
VariantResult
} from './models.js';
export class FliptEvaluationClient {
private engine: Engine;
private fetcher: IFetcher;
private etag?: string;
private constructor(engine: Engine, fetcher: IFetcher) {
this.engine = engine;
this.fetcher = fetcher;
}
/**
* Initialize the client
* @param namespace - optional namespace to evaluate flags
* @param engine_opts - optional engine options
* @returns {Promise<FliptEvaluationClient>}
*/
static async init(
namespace: string = 'default',
engine_opts: EngineOpts = {
url: 'http://localhost:8080',
reference: ''
}
): Promise<FliptEvaluationClient> {
await init(await wasm());
let url = engine_opts.url ?? 'http://localhost:8080';
// trim trailing slash
url = url.replace(/\/$/, '');
url = `${url}/internal/v1/evaluation/snapshot/namespace/${namespace}`;
if (engine_opts.reference) {
url = `${url}?ref=${engine_opts.reference}`;
}
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('x-flipt-accept-server-version', '1.47.0');
if (engine_opts.authentication) {
if ('client_token' in engine_opts.authentication) {
headers.append(
'Authorization',
`Bearer ${engine_opts.authentication.client_token}`
);
} else if ('jwt_token' in engine_opts.authentication) {
headers.append(
'Authorization',
`JWT ${engine_opts.authentication.jwt_token}`
);
}
}
let fetcher = engine_opts.fetcher;
if (!fetcher) {
fetcher = async (opts?: { etag?: string }) => {
if (opts && opts.etag) {
headers.append('If-None-Match', opts.etag);
}
const resp = await fetch(url, {
method: 'GET',
headers
});
// check for 304 status code
if (resp.status === 304) {
return resp;
}
// ok only checks for range 200-299
if (!resp.ok) {
throw new Error('Failed to fetch data');
}
return resp;
};
}
// should be no etag on first fetch
const resp = await fetcher();
if (!resp) {
throw new Error('Failed to fetch data');
}
const data = await resp.json();
const engine = new Engine(namespace, data);
return new FliptEvaluationClient(engine, fetcher);
}
/**
* Refresh the flags snapshot
* @returns void
*/
public async refresh() {
const opts = { etag: this.etag };
const resp = await this.fetcher(opts);
if (resp.status === 304) {
let etag = resp.headers.get('etag');
if (etag) {
this.etag = etag;
}
return;
}
const data = await resp.json();
this.engine.snapshot(data);
}
/**
* Evaluate a variant flag
* @param flag_key - flag key to evaluate
* @param entity_id - entity id to evaluate
* @param context - optional evaluation context
* @returns {VariantResult}
*/
public evaluateVariant(
flag_key: string,
entity_id: string,
context: {}
): VariantResult {
const evaluation_request: EvaluationRequest = {
flag_key,
entity_id,
context
};
return this.engine.evaluate_variant(evaluation_request) as VariantResult;
}
/**
* Evaluate a boolean flag
* @param flag_key - flag key to evaluate
* @param entity_id - entity id to evaluate
* @param context - optional evaluation context
* @returns {BooleanResult}
*/
public evaluateBoolean(
flag_key: string,
entity_id: string,
context: {}
): BooleanResult {
const evaluation_request: EvaluationRequest = {
flag_key,
entity_id,
context
};
return this.engine.evaluate_boolean(evaluation_request) as BooleanResult;
}
/**
* Evaluate a batch of flag requests
* @param requests evaluation requests
* @returns {BatchResult}
*/
public evaluateBatch(requests: EvaluationRequest[]): BatchResult {
return this.engine.evaluate_batch(requests);
}
}