@revenexx/app-sdk 1.4.0 → 1.6.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.
package/dist/index.cjs CHANGED
@@ -113,11 +113,11 @@ function toParams(query = {}) {
113
113
  const { where = {}, select, order, limit, offset } = query;
114
114
  for (const [col, cond] of Object.entries(where)) {
115
115
  if (Array.isArray(cond)) {
116
- params.append(col, `in.(${cond.join(",")})`);
116
+ params.append(col, `in.${inList(cond)}`);
117
117
  } else if (cond && typeof cond === "object" && typeof cond.op === "string") {
118
118
  const c = cond;
119
119
  if (!OPS.has(c.op)) throw new Error(`query: unsupported operator '${c.op}'`);
120
- const v = Array.isArray(c.value) ? `(${c.value.join(",")})` : c.value;
120
+ const v = Array.isArray(c.value) ? inList(c.value) : c.value;
121
121
  params.append(col, `${c.op}.${v}`);
122
122
  } else if (cond === null) {
123
123
  params.append(col, "is.null");
@@ -131,6 +131,13 @@ function toParams(query = {}) {
131
131
  if (Number.isInteger(offset)) params.set("offset", String(offset));
132
132
  return params;
133
133
  }
134
+ function inList(values) {
135
+ return `(${values.map(quoteListValue).join(",")})`;
136
+ }
137
+ function quoteListValue(v) {
138
+ if (v === null || v === void 0) return "null";
139
+ return `"${String(v).replace(/[\\"]/g, "\\$&")}"`;
140
+ }
134
141
  function matches(row, where = {}) {
135
142
  for (const [col, cond] of Object.entries(where)) {
136
143
  const val = row[col] ?? null;
@@ -742,6 +749,9 @@ var DEFAULT_GATEWAY_ENDPOINT = "https://api.revenexx.com";
742
749
  var DEFAULT_SETTINGS_TTL_MS = 6e4;
743
750
  var DEFAULT_SETTINGS_ERROR_TTL_MS = 5e3;
744
751
  var DEFAULT_SETTINGS_TIMEOUT_MS = 2e3;
752
+ function stringList(value) {
753
+ return Array.isArray(value) ? value.filter((k) => typeof k === "string") : [];
754
+ }
745
755
  function headerOf(source, name) {
746
756
  if (!source) return "";
747
757
  if (typeof source.header === "function") {
@@ -795,6 +805,7 @@ function createSettings(options = {}) {
795
805
  market,
796
806
  settings: mergeSettings(defaults, void 0),
797
807
  masked: [],
808
+ stored: [],
798
809
  resolved: false,
799
810
  error
800
811
  });
@@ -828,10 +839,81 @@ function createSettings(options = {}) {
828
839
  tenant,
829
840
  market,
830
841
  settings: mergeSettings(defaults, body.settings),
831
- masked: Array.isArray(body.masked) ? body.masked.filter((k) => typeof k === "string") : [],
842
+ masked: stringList(body.masked),
843
+ stored: stringList(body.stored),
832
844
  resolved: true
833
845
  };
834
846
  }
847
+ const forgetTenant = (tenant, app) => {
848
+ const prefix = `${tenant}\0${app}\0`;
849
+ for (const key of [...cache.keys()]) if (key.startsWith(prefix)) cache.delete(key);
850
+ for (const key of [...lastGood.keys()]) if (key.startsWith(prefix)) lastGood.delete(key);
851
+ };
852
+ async function set(source, values, writeOptions = {}) {
853
+ const app = appName();
854
+ const tenant = (source?.tenant ?? "") || headerOf(source, "x-revenexx-tenant");
855
+ const refused = (status, error) => ({
856
+ ok: false,
857
+ status,
858
+ written: [],
859
+ kept: [],
860
+ ignored: [],
861
+ error
862
+ });
863
+ if (!app) return refused(0, "no app name (pass `app` or set REVENEXX_APP_NAME)");
864
+ if (!tenant) return refused(0, "no tenant on the request");
865
+ const appContext = headerOf(source, "x-revenexx-app-context");
866
+ if (!appContext) {
867
+ return refused(0, "no app context on the request \u2014 settings are written from an HTTP route, not an event or schedule run");
868
+ }
869
+ if (!values || typeof values !== "object" || Array.isArray(values)) return refused(0, "values must be an object");
870
+ const f = options.fetchImpl ?? globalThis.fetch;
871
+ if (typeof f !== "function") return refused(0, "settings: no fetch available (Node >=18 or pass fetchImpl)");
872
+ const token = (source?.contextToken ?? "") || headerOf(source, "x-revenexx-context");
873
+ const headers = {
874
+ Accept: "application/json",
875
+ "Content-Type": "application/json",
876
+ "X-Revenexx-Tenant": tenant,
877
+ "X-Revenexx-App-Context": appContext
878
+ };
879
+ if (token) headers.Authorization = `Bearer ${token}`;
880
+ try {
881
+ const signal = timeoutSignal(timeoutMs);
882
+ const res = await f(`${endpoint}/v1/settings/apps/${encodeURIComponent(app)}`, {
883
+ method: "PATCH",
884
+ headers,
885
+ body: JSON.stringify({ data: values, if_absent: writeOptions.ifAbsent === true }),
886
+ ...signal ? { signal } : {}
887
+ });
888
+ const text = await res.text();
889
+ let body = {};
890
+ try {
891
+ body = text ? JSON.parse(text) : {};
892
+ } catch {
893
+ body = {};
894
+ }
895
+ if (!res.ok) {
896
+ const reason = typeof body.error === "string" ? body.error : typeof body.message === "string" ? body.message : "";
897
+ const err = new Error(`settings: write ${res.status} for ${app}${reason ? ` \u2014 ${reason}` : ""}`);
898
+ err.status = res.status;
899
+ report(err, { app, tenant, market: null });
900
+ return refused(res.status, err.message);
901
+ }
902
+ const result = {
903
+ ok: true,
904
+ status: res.status,
905
+ written: stringList(body.written),
906
+ kept: stringList(body.kept),
907
+ ignored: stringList(body.ignored)
908
+ };
909
+ if (result.written.length > 0) forgetTenant(tenant, app);
910
+ return result;
911
+ } catch (err) {
912
+ const error = err instanceof Error ? err : new Error(String(err));
913
+ report(error, { app, tenant, market: null });
914
+ return refused(0, error.message);
915
+ }
916
+ }
835
917
  async function load(source, readOptions = {}) {
836
918
  const app = appName();
837
919
  const tenant = (source?.tenant ?? "") || headerOf(source, "x-revenexx-tenant");
@@ -861,6 +943,7 @@ function createSettings(options = {}) {
861
943
  }
862
944
  return {
863
945
  load,
946
+ set,
864
947
  async get(source, readOptions) {
865
948
  return (await load(source, readOptions)).settings;
866
949
  },