@revenexx/app-sdk 1.4.0 → 1.5.0

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.
@@ -164,6 +164,9 @@ var DEFAULT_GATEWAY_ENDPOINT = "https://api.revenexx.com";
164
164
  var DEFAULT_SETTINGS_TTL_MS = 6e4;
165
165
  var DEFAULT_SETTINGS_ERROR_TTL_MS = 5e3;
166
166
  var DEFAULT_SETTINGS_TIMEOUT_MS = 2e3;
167
+ function stringList(value) {
168
+ return Array.isArray(value) ? value.filter((k) => typeof k === "string") : [];
169
+ }
167
170
  function headerOf(source, name) {
168
171
  if (!source) return "";
169
172
  if (typeof source.header === "function") {
@@ -217,6 +220,7 @@ function createSettings(options = {}) {
217
220
  market,
218
221
  settings: mergeSettings(defaults, void 0),
219
222
  masked: [],
223
+ stored: [],
220
224
  resolved: false,
221
225
  error
222
226
  });
@@ -250,10 +254,81 @@ function createSettings(options = {}) {
250
254
  tenant,
251
255
  market,
252
256
  settings: mergeSettings(defaults, body.settings),
253
- masked: Array.isArray(body.masked) ? body.masked.filter((k) => typeof k === "string") : [],
257
+ masked: stringList(body.masked),
258
+ stored: stringList(body.stored),
254
259
  resolved: true
255
260
  };
256
261
  }
262
+ const forgetTenant = (tenant, app) => {
263
+ const prefix = `${tenant}\0${app}\0`;
264
+ for (const key of [...cache.keys()]) if (key.startsWith(prefix)) cache.delete(key);
265
+ for (const key of [...lastGood.keys()]) if (key.startsWith(prefix)) lastGood.delete(key);
266
+ };
267
+ async function set(source, values, writeOptions = {}) {
268
+ const app = appName();
269
+ const tenant = (source?.tenant ?? "") || headerOf(source, "x-revenexx-tenant");
270
+ const refused = (status, error) => ({
271
+ ok: false,
272
+ status,
273
+ written: [],
274
+ kept: [],
275
+ ignored: [],
276
+ error
277
+ });
278
+ if (!app) return refused(0, "no app name (pass `app` or set REVENEXX_APP_NAME)");
279
+ if (!tenant) return refused(0, "no tenant on the request");
280
+ const appContext = headerOf(source, "x-revenexx-app-context");
281
+ if (!appContext) {
282
+ return refused(0, "no app context on the request \u2014 settings are written from an HTTP route, not an event or schedule run");
283
+ }
284
+ if (!values || typeof values !== "object" || Array.isArray(values)) return refused(0, "values must be an object");
285
+ const f = options.fetchImpl ?? globalThis.fetch;
286
+ if (typeof f !== "function") return refused(0, "settings: no fetch available (Node >=18 or pass fetchImpl)");
287
+ const token = (source?.contextToken ?? "") || headerOf(source, "x-revenexx-context");
288
+ const headers = {
289
+ Accept: "application/json",
290
+ "Content-Type": "application/json",
291
+ "X-Revenexx-Tenant": tenant,
292
+ "X-Revenexx-App-Context": appContext
293
+ };
294
+ if (token) headers.Authorization = `Bearer ${token}`;
295
+ try {
296
+ const signal = timeoutSignal(timeoutMs);
297
+ const res = await f(`${endpoint}/v1/settings/apps/${encodeURIComponent(app)}`, {
298
+ method: "PATCH",
299
+ headers,
300
+ body: JSON.stringify({ data: values, if_absent: writeOptions.ifAbsent === true }),
301
+ ...signal ? { signal } : {}
302
+ });
303
+ const text = await res.text();
304
+ let body = {};
305
+ try {
306
+ body = text ? JSON.parse(text) : {};
307
+ } catch {
308
+ body = {};
309
+ }
310
+ if (!res.ok) {
311
+ const reason = typeof body.error === "string" ? body.error : typeof body.message === "string" ? body.message : "";
312
+ const err = new Error(`settings: write ${res.status} for ${app}${reason ? ` \u2014 ${reason}` : ""}`);
313
+ err.status = res.status;
314
+ report(err, { app, tenant, market: null });
315
+ return refused(res.status, err.message);
316
+ }
317
+ const result = {
318
+ ok: true,
319
+ status: res.status,
320
+ written: stringList(body.written),
321
+ kept: stringList(body.kept),
322
+ ignored: stringList(body.ignored)
323
+ };
324
+ if (result.written.length > 0) forgetTenant(tenant, app);
325
+ return result;
326
+ } catch (err) {
327
+ const error = err instanceof Error ? err : new Error(String(err));
328
+ report(error, { app, tenant, market: null });
329
+ return refused(0, error.message);
330
+ }
331
+ }
257
332
  async function load(source, readOptions = {}) {
258
333
  const app = appName();
259
334
  const tenant = (source?.tenant ?? "") || headerOf(source, "x-revenexx-tenant");
@@ -283,6 +358,7 @@ function createSettings(options = {}) {
283
358
  }
284
359
  return {
285
360
  load,
361
+ set,
286
362
  async get(source, readOptions) {
287
363
  return (await load(source, readOptions)).settings;
288
364
  },
@@ -668,6 +744,7 @@ function createApp(options = {}) {
668
744
  // resolves for the market this call is for — no argument needed.
669
745
  settings: (readOptions) => settings.get(c, readOptions),
670
746
  settingsSnapshot: (readOptions) => settings.load(c, readOptions),
747
+ setSettings: (values, writeOptions) => settings.set(c, values, writeOptions),
671
748
  now,
672
749
  clock
673
750
  };