@togglerino/sdk 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +42 -7
- package/dist/index.mjs +42 -7
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -159,6 +159,11 @@ declare class Togglerino {
|
|
|
159
159
|
* Fetch all flags from the server evaluation endpoint.
|
|
160
160
|
*/
|
|
161
161
|
private fetchFlags;
|
|
162
|
+
/**
|
|
163
|
+
* Fetch a single flag from the server evaluation endpoint.
|
|
164
|
+
* Used to re-evaluate after an SSE change notification.
|
|
165
|
+
*/
|
|
166
|
+
private fetchSingleFlag;
|
|
162
167
|
/**
|
|
163
168
|
* Calculate the next retry delay using exponential backoff.
|
|
164
169
|
* Sequence: 1s, 2s, 4s, 8s, 16s, 30s (capped).
|
|
@@ -180,7 +185,7 @@ declare class Togglerino {
|
|
|
180
185
|
* Read and parse SSE events from a ReadableStream.
|
|
181
186
|
* SSE format:
|
|
182
187
|
* event: flag_update
|
|
183
|
-
* data: {"flagKey":"dark-mode"
|
|
188
|
+
* data: {"flagKey":"dark-mode"}
|
|
184
189
|
*
|
|
185
190
|
*/
|
|
186
191
|
private processSSEStream;
|
package/dist/index.d.ts
CHANGED
|
@@ -159,6 +159,11 @@ declare class Togglerino {
|
|
|
159
159
|
* Fetch all flags from the server evaluation endpoint.
|
|
160
160
|
*/
|
|
161
161
|
private fetchFlags;
|
|
162
|
+
/**
|
|
163
|
+
* Fetch a single flag from the server evaluation endpoint.
|
|
164
|
+
* Used to re-evaluate after an SSE change notification.
|
|
165
|
+
*/
|
|
166
|
+
private fetchSingleFlag;
|
|
162
167
|
/**
|
|
163
168
|
* Calculate the next retry delay using exponential backoff.
|
|
164
169
|
* Sequence: 1s, 2s, 4s, 8s, 16s, 30s (capped).
|
|
@@ -180,7 +185,7 @@ declare class Togglerino {
|
|
|
180
185
|
* Read and parse SSE events from a ReadableStream.
|
|
181
186
|
* SSE format:
|
|
182
187
|
* event: flag_update
|
|
183
|
-
* data: {"flagKey":"dark-mode"
|
|
188
|
+
* data: {"flagKey":"dark-mode"}
|
|
184
189
|
*
|
|
185
190
|
*/
|
|
186
191
|
private processSSEStream;
|
package/dist/index.js
CHANGED
|
@@ -232,6 +232,46 @@ var Togglerino = class {
|
|
|
232
232
|
throw error;
|
|
233
233
|
}
|
|
234
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* Fetch a single flag from the server evaluation endpoint.
|
|
237
|
+
* Used to re-evaluate after an SSE change notification.
|
|
238
|
+
*/
|
|
239
|
+
async fetchSingleFlag(flagKey) {
|
|
240
|
+
const url = `${this.config.serverUrl}/api/v1/evaluate/${encodeURIComponent(flagKey)}`;
|
|
241
|
+
const body = {
|
|
242
|
+
context: {
|
|
243
|
+
user_id: this.config.context.userId ?? "",
|
|
244
|
+
attributes: this.config.context.attributes ?? {}
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
try {
|
|
248
|
+
const response = await fetch(url, {
|
|
249
|
+
method: "POST",
|
|
250
|
+
headers: {
|
|
251
|
+
"Content-Type": "application/json",
|
|
252
|
+
Authorization: `Bearer ${this.config.sdkKey}`
|
|
253
|
+
},
|
|
254
|
+
body: JSON.stringify(body)
|
|
255
|
+
});
|
|
256
|
+
if (!response.ok) {
|
|
257
|
+
throw new Error(
|
|
258
|
+
`Togglerino: single flag evaluation failed with status ${response.status}`
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
const result = await response.json();
|
|
262
|
+
const old = this.flags.get(flagKey);
|
|
263
|
+
this.flags.set(flagKey, result);
|
|
264
|
+
if (!old || JSON.stringify(old.value) !== JSON.stringify(result.value)) {
|
|
265
|
+
this.emit("change", {
|
|
266
|
+
flagKey,
|
|
267
|
+
value: result.value,
|
|
268
|
+
variant: result.variant
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
} catch (error) {
|
|
272
|
+
this.emit("error", error);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
235
275
|
// ---------------------------------------------------------------------------
|
|
236
276
|
// Internal: SSE streaming (fetch-based with ReadableStream)
|
|
237
277
|
// ---------------------------------------------------------------------------
|
|
@@ -307,7 +347,7 @@ var Togglerino = class {
|
|
|
307
347
|
* Read and parse SSE events from a ReadableStream.
|
|
308
348
|
* SSE format:
|
|
309
349
|
* event: flag_update
|
|
310
|
-
* data: {"flagKey":"dark-mode"
|
|
350
|
+
* data: {"flagKey":"dark-mode"}
|
|
311
351
|
*
|
|
312
352
|
*/
|
|
313
353
|
async processSSEStream(reader, decoder) {
|
|
@@ -349,13 +389,8 @@ var Togglerino = class {
|
|
|
349
389
|
if (eventType !== "flag_update") return;
|
|
350
390
|
try {
|
|
351
391
|
const event = JSON.parse(data);
|
|
352
|
-
|
|
353
|
-
this.flags.set(event.flagKey, {
|
|
354
|
-
value: event.value,
|
|
355
|
-
variant: event.variant,
|
|
356
|
-
reason: existing?.reason ?? "stream_update"
|
|
392
|
+
this.fetchSingleFlag(event.flagKey).catch(() => {
|
|
357
393
|
});
|
|
358
|
-
this.emit("change", event);
|
|
359
394
|
} catch {
|
|
360
395
|
}
|
|
361
396
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -206,6 +206,46 @@ var Togglerino = class {
|
|
|
206
206
|
throw error;
|
|
207
207
|
}
|
|
208
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Fetch a single flag from the server evaluation endpoint.
|
|
211
|
+
* Used to re-evaluate after an SSE change notification.
|
|
212
|
+
*/
|
|
213
|
+
async fetchSingleFlag(flagKey) {
|
|
214
|
+
const url = `${this.config.serverUrl}/api/v1/evaluate/${encodeURIComponent(flagKey)}`;
|
|
215
|
+
const body = {
|
|
216
|
+
context: {
|
|
217
|
+
user_id: this.config.context.userId ?? "",
|
|
218
|
+
attributes: this.config.context.attributes ?? {}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
try {
|
|
222
|
+
const response = await fetch(url, {
|
|
223
|
+
method: "POST",
|
|
224
|
+
headers: {
|
|
225
|
+
"Content-Type": "application/json",
|
|
226
|
+
Authorization: `Bearer ${this.config.sdkKey}`
|
|
227
|
+
},
|
|
228
|
+
body: JSON.stringify(body)
|
|
229
|
+
});
|
|
230
|
+
if (!response.ok) {
|
|
231
|
+
throw new Error(
|
|
232
|
+
`Togglerino: single flag evaluation failed with status ${response.status}`
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
const result = await response.json();
|
|
236
|
+
const old = this.flags.get(flagKey);
|
|
237
|
+
this.flags.set(flagKey, result);
|
|
238
|
+
if (!old || JSON.stringify(old.value) !== JSON.stringify(result.value)) {
|
|
239
|
+
this.emit("change", {
|
|
240
|
+
flagKey,
|
|
241
|
+
value: result.value,
|
|
242
|
+
variant: result.variant
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
} catch (error) {
|
|
246
|
+
this.emit("error", error);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
209
249
|
// ---------------------------------------------------------------------------
|
|
210
250
|
// Internal: SSE streaming (fetch-based with ReadableStream)
|
|
211
251
|
// ---------------------------------------------------------------------------
|
|
@@ -281,7 +321,7 @@ var Togglerino = class {
|
|
|
281
321
|
* Read and parse SSE events from a ReadableStream.
|
|
282
322
|
* SSE format:
|
|
283
323
|
* event: flag_update
|
|
284
|
-
* data: {"flagKey":"dark-mode"
|
|
324
|
+
* data: {"flagKey":"dark-mode"}
|
|
285
325
|
*
|
|
286
326
|
*/
|
|
287
327
|
async processSSEStream(reader, decoder) {
|
|
@@ -323,13 +363,8 @@ var Togglerino = class {
|
|
|
323
363
|
if (eventType !== "flag_update") return;
|
|
324
364
|
try {
|
|
325
365
|
const event = JSON.parse(data);
|
|
326
|
-
|
|
327
|
-
this.flags.set(event.flagKey, {
|
|
328
|
-
value: event.value,
|
|
329
|
-
variant: event.variant,
|
|
330
|
-
reason: existing?.reason ?? "stream_update"
|
|
366
|
+
this.fetchSingleFlag(event.flagKey).catch(() => {
|
|
331
367
|
});
|
|
332
|
-
this.emit("change", event);
|
|
333
368
|
} catch {
|
|
334
369
|
}
|
|
335
370
|
}
|