@auxilium/datalynk-client 0.7.0 → 0.8.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
@@ -1,25 +1,24 @@
1
- (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.utils = {}));
1
+ (function(global2, factory) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global2 = typeof globalThis !== "undefined" ? globalThis : global2 || self, factory(global2.utils = {}));
3
3
  })(this, function(exports2) {
4
4
  "use strict";var __defProp = Object.defineProperty;
5
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
6
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
7
 
8
- var rt = Object.defineProperty;
9
- var nt = (r, t, e) => t in r ? rt(r, t, { enumerable: true, configurable: true, writable: true, value: e }) : r[t] = e;
10
- var c = (r, t, e) => (nt(r, typeof t != "symbol" ? t + "" : t, e), e);
11
- function ot(r, t = false) {
12
- if (r == null)
13
- throw new Error("Cannot clean a NULL value");
8
+ var it = Object.defineProperty;
9
+ var ct = (r, t, e) => t in r ? it(r, t, { enumerable: true, configurable: true, writable: true, value: e }) : r[t] = e;
10
+ var c = (r, t, e) => ct(r, typeof t != "symbol" ? t + "" : t, e);
11
+ function ut(r, t = false) {
12
+ if (r == null) throw new Error("Cannot clean a NULL value");
14
13
  return Array.isArray(r) ? r = r.filter((e) => e != null) : Object.entries(r).forEach(([e, n]) => {
15
14
  (t && n === void 0 || !t && n == null) && delete r[e];
16
15
  }), r;
17
16
  }
18
- function St(r) {
17
+ function $t(r) {
19
18
  const t = new FormData();
20
19
  return Object.entries(r).forEach(([e, n]) => t.append(e, n)), t;
21
20
  }
22
- function T(r) {
21
+ function M(r) {
23
22
  try {
24
23
  return JSON.parse(r);
25
24
  } catch {
@@ -28,10 +27,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
28
27
  }
29
28
  class E extends Promise {
30
29
  constructor(e) {
31
- super((n, o) => e(
32
- (s) => n(s),
33
- (s) => o(s),
34
- (s) => this.progress = s
30
+ super((n, s) => e(
31
+ (o) => n(o),
32
+ (o) => s(o),
33
+ (o) => this.progress = o
35
34
  ));
36
35
  c(this, "listeners", []);
37
36
  c(this, "_progress", 0);
@@ -43,18 +42,18 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
43
42
  e != this._progress && (this._progress = e, this.listeners.forEach((n) => n(e)));
44
43
  }
45
44
  static from(e) {
46
- return e instanceof E ? e : new E((n, o) => e.then((...s) => n(...s)).catch((...s) => o(...s)));
45
+ return e instanceof E ? e : new E((n, s) => e.then((...o) => n(...o)).catch((...o) => s(...o)));
47
46
  }
48
47
  from(e) {
49
48
  const n = E.from(e);
50
- return this.onProgress((o) => n.progress = o), n;
49
+ return this.onProgress((s) => n.progress = s), n;
51
50
  }
52
51
  onProgress(e) {
53
52
  return this.listeners.push(e), this;
54
53
  }
55
54
  then(e, n) {
56
- const o = super.then(e, n);
57
- return this.from(o);
55
+ const s = super.then(e, n);
56
+ return this.from(s);
58
57
  }
59
58
  catch(e) {
60
59
  return this.from(super.catch(e));
@@ -63,7 +62,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
63
62
  return this.from(super.finally(e));
64
63
  }
65
64
  }
66
- class v {
65
+ class _ {
67
66
  constructor() {
68
67
  c(this, "listeners", {});
69
68
  }
@@ -72,17 +71,17 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
72
71
  }
73
72
  static off(t, e) {
74
73
  const n = t.toString();
75
- this.listeners[n] = (this.listeners[n] || []).filter((o) => o === e);
74
+ this.listeners[n] = (this.listeners[n] || []).filter((s) => s === e);
76
75
  }
77
76
  static on(t, e) {
78
- var o;
77
+ var s;
79
78
  const n = t.toString();
80
- return this.listeners[n] || (this.listeners[n] = []), (o = this.listeners[n]) == null || o.push(e), () => this.off(t, e);
79
+ return this.listeners[n] || (this.listeners[n] = []), (s = this.listeners[n]) == null || s.push(e), () => this.off(t, e);
81
80
  }
82
81
  static once(t, e) {
83
82
  return new Promise((n) => {
84
- const o = this.on(t, (...s) => {
85
- n(s.length == 1 ? s[0] : s), e && e(...s), o();
83
+ const s = this.on(t, (...o) => {
84
+ n(o.length == 1 ? o[0] : o), e && e(...o), s();
86
85
  });
87
86
  });
88
87
  }
@@ -98,13 +97,13 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
98
97
  }
99
98
  once(t, e) {
100
99
  return new Promise((n) => {
101
- const o = this.on(t, (...s) => {
102
- n(s.length == 1 ? s[0] : s), e && e(...s), o();
100
+ const s = this.on(t, (...o) => {
101
+ n(o.length == 1 ? o[0] : o), e && e(...o), s();
103
102
  });
104
103
  });
105
104
  }
106
105
  }
107
- c(v, "listeners", {});
106
+ c(_, "listeners", {});
108
107
  class p extends Error {
109
108
  constructor(e, n) {
110
109
  super(e);
@@ -118,8 +117,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
118
117
  this._code = e;
119
118
  }
120
119
  static from(e) {
121
- const n = Number(e.statusCode) ?? Number(e.code), o = new this(e.message || e.toString());
122
- return Object.assign(o, {
120
+ const n = Number(e.statusCode) ?? Number(e.code), s = new this(e.message || e.toString());
121
+ return Object.assign(s, {
123
122
  stack: e.stack,
124
123
  ...e,
125
124
  code: n ?? void 0
@@ -133,7 +132,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
133
132
  }
134
133
  }
135
134
  c(p, "code", 500);
136
- class Y extends p {
135
+ class W extends p {
137
136
  constructor(t = "Bad Request") {
138
137
  super(t);
139
138
  }
@@ -141,8 +140,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
141
140
  return t.constructor.code == this.code;
142
141
  }
143
142
  }
144
- c(Y, "code", 400);
145
- class H extends p {
143
+ c(W, "code", 400);
144
+ class J extends p {
146
145
  constructor(t = "Unauthorized") {
147
146
  super(t);
148
147
  }
@@ -150,8 +149,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
150
149
  return t.constructor.code == this.code;
151
150
  }
152
151
  }
153
- c(H, "code", 401);
154
- class W extends p {
152
+ c(J, "code", 401);
153
+ class z extends p {
155
154
  constructor(t = "Payment Required") {
156
155
  super(t);
157
156
  }
@@ -159,8 +158,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
159
158
  return t.constructor.code == this.code;
160
159
  }
161
160
  }
162
- c(W, "code", 402);
163
- class z extends p {
161
+ c(z, "code", 402);
162
+ class K extends p {
164
163
  constructor(t = "Forbidden") {
165
164
  super(t);
166
165
  }
@@ -168,8 +167,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
168
167
  return t.constructor.code == this.code;
169
168
  }
170
169
  }
171
- c(z, "code", 403);
172
- class K extends p {
170
+ c(K, "code", 403);
171
+ class Z extends p {
173
172
  constructor(t = "Not Found") {
174
173
  super(t);
175
174
  }
@@ -177,8 +176,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
177
176
  return t.constructor.code == this.code;
178
177
  }
179
178
  }
180
- c(K, "code", 404);
181
- class J extends p {
179
+ c(Z, "code", 404);
180
+ class V extends p {
182
181
  constructor(t = "Method Not Allowed") {
183
182
  super(t);
184
183
  }
@@ -186,8 +185,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
186
185
  return t.constructor.code == this.code;
187
186
  }
188
187
  }
189
- c(J, "code", 405);
190
- class Z extends p {
188
+ c(V, "code", 405);
189
+ class X extends p {
191
190
  constructor(t = "Not Acceptable") {
192
191
  super(t);
193
192
  }
@@ -195,8 +194,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
195
194
  return t.constructor.code == this.code;
196
195
  }
197
196
  }
198
- c(Z, "code", 406);
199
- class V extends p {
197
+ c(X, "code", 406);
198
+ class Q extends p {
200
199
  constructor(t = "Internal Server Error") {
201
200
  super(t);
202
201
  }
@@ -204,8 +203,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
204
203
  return t.constructor.code == this.code;
205
204
  }
206
205
  }
207
- c(V, "code", 500);
208
- class X extends p {
206
+ c(Q, "code", 500);
207
+ class tt extends p {
209
208
  constructor(t = "Not Implemented") {
210
209
  super(t);
211
210
  }
@@ -213,8 +212,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
213
212
  return t.constructor.code == this.code;
214
213
  }
215
214
  }
216
- c(X, "code", 501);
217
- class Q extends p {
215
+ c(tt, "code", 501);
216
+ class et extends p {
218
217
  constructor(t = "Bad Gateway") {
219
218
  super(t);
220
219
  }
@@ -222,8 +221,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
222
221
  return t.constructor.code == this.code;
223
222
  }
224
223
  }
225
- c(Q, "code", 502);
226
- class _ extends p {
224
+ c(et, "code", 502);
225
+ class rt extends p {
227
226
  constructor(t = "Service Unavailable") {
228
227
  super(t);
229
228
  }
@@ -231,8 +230,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
231
230
  return t.constructor.code == this.code;
232
231
  }
233
232
  }
234
- c(_, "code", 503);
235
- class tt extends p {
233
+ c(rt, "code", 503);
234
+ class nt extends p {
236
235
  constructor(t = "Gateway Timeout") {
237
236
  super(t);
238
237
  }
@@ -240,35 +239,34 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
240
239
  return t.constructor.code == this.code;
241
240
  }
242
241
  }
243
- c(tt, "code", 504);
244
- function kt(r, t) {
245
- if (r >= 200 && r < 300)
246
- return null;
242
+ c(nt, "code", 504);
243
+ function Yt(r, t) {
244
+ if (r >= 200 && r < 300) return null;
247
245
  switch (r) {
248
246
  case 400:
249
- return new Y(t);
247
+ return new W(t);
250
248
  case 401:
251
- return new H(t);
249
+ return new J(t);
252
250
  case 402:
253
- return new W(t);
254
- case 403:
255
251
  return new z(t);
256
- case 404:
252
+ case 403:
257
253
  return new K(t);
254
+ case 404:
255
+ return new Z(t);
258
256
  case 405:
259
- return new J(t);
257
+ return new V(t);
260
258
  case 406:
261
- return new Z(t);
259
+ return new X(t);
262
260
  case 500:
263
- return new V(t);
261
+ return new Q(t);
264
262
  case 501:
265
- return new X(t);
263
+ return new tt(t);
266
264
  case 502:
267
- return new Q(t);
265
+ return new et(t);
268
266
  case 503:
269
- return new _(t);
267
+ return new rt(t);
270
268
  case 504:
271
- return new tt(t);
269
+ return new nt(t);
272
270
  default:
273
271
  return new p(t, r);
274
272
  }
@@ -293,60 +291,58 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
293
291
  };
294
292
  }
295
293
  request(t = {}) {
296
- var o;
297
- if (!this.url && !t.url)
298
- throw new Error("URL needs to be set");
299
- let e = ((o = t.url) != null && o.startsWith("http") ? t.url : (this.url || "") + (t.url || "")).replace(/([^:]\/)\/+/g, "$1");
300
- if (t.fragment && (e.includes("#") ? e.replace(/#.*(\?|\n)/g, (s, i) => `#${t.fragment}${i}`) : e += "#" + t.fragment), t.query) {
301
- const s = Array.isArray(t.query) ? t.query : Object.keys(t.query).map((i) => ({ key: i, value: t.query[i] }));
302
- e += (e.includes("?") ? "&" : "?") + s.map((i) => `${i.key}=${i.value}`).join("&");
294
+ var s;
295
+ if (!this.url && !t.url) throw new Error("URL needs to be set");
296
+ let e = ((s = t.url) != null && s.startsWith("http") ? t.url : (this.url || "") + (t.url || "")).replace(/([^:]\/)\/+/g, "$1");
297
+ if (t.fragment && (e.includes("#") ? e.replace(/#.*(\?|\n)/g, (o, i) => `#${t.fragment}${i}`) : e += "#" + t.fragment), t.query) {
298
+ const o = Array.isArray(t.query) ? t.query : Object.keys(t.query).map((i) => ({ key: i, value: t.query[i] }));
299
+ e += (e.includes("?") ? "&" : "?") + o.map((i) => `${i.key}=${i.value}`).join("&");
303
300
  }
304
- const n = ot({
301
+ const n = ut({
305
302
  "Content-Type": t.body ? t.body instanceof FormData ? "multipart/form-data" : "application/json" : void 0,
306
303
  ...w2.headers,
307
304
  ...this.headers,
308
305
  ...t.headers
309
306
  });
310
- return typeof t.body == "object" && t.body != null && n["Content-Type"] == "application/json" && (t.body = JSON.stringify(t.body)), new E((s, i, y) => {
307
+ return typeof t.body == "object" && t.body != null && n["Content-Type"] == "application/json" && (t.body = JSON.stringify(t.body)), new E((o, i, a) => {
311
308
  fetch(e, {
312
309
  headers: n,
313
310
  method: t.method || (t.body ? "POST" : "GET"),
314
311
  body: t.body
315
312
  }).then(async (u) => {
316
- var k, U;
317
- for (let a of [...Object.values(w2.interceptors), ...Object.values(this.interceptors)])
318
- await new Promise((O) => a(u, () => O()));
319
- const R = u.headers.get("Content-Length"), L = R ? parseInt(R, 10) : 0;
320
- let M = 0;
321
- const N = (k = u.body) == null ? void 0 : k.getReader(), et = new ReadableStream({
322
- start(a) {
323
- function O() {
324
- N == null || N.read().then((B) => {
325
- if (B.done)
326
- return a.close();
327
- M += B.value.byteLength, y(M / L), a.enqueue(B.value), O();
328
- }).catch((B) => a.error(B));
313
+ var G, U;
314
+ for (let l of [...Object.values(w2.interceptors), ...Object.values(this.interceptors)])
315
+ await new Promise((R) => l(u, () => R()));
316
+ const v = u.headers.get("Content-Length"), N = v ? parseInt(v, 10) : 0;
317
+ let P = 0;
318
+ const I = (G = u.body) == null ? void 0 : G.getReader(), ot = new ReadableStream({
319
+ start(l) {
320
+ function R() {
321
+ I == null || I.read().then((O) => {
322
+ if (O.done) return l.close();
323
+ P += O.value.byteLength, a(P / N), l.enqueue(O.value), R();
324
+ }).catch((O) => l.error(O));
329
325
  }
330
- O();
326
+ R();
331
327
  }
332
328
  });
333
- if (u.data = new Response(et), t.decode == null || t.decode) {
334
- const a = (U = u.headers.get("Content-Type")) == null ? void 0 : U.toLowerCase();
335
- a != null && a.includes("form") ? u.data = await u.data.formData() : a != null && a.includes("json") ? u.data = await u.data.json() : a != null && a.includes("text") ? u.data = await u.data.text() : a != null && a.includes("application") && (u.data = await u.data.blob());
329
+ if (u.data = new Response(ot), t.decode == null || t.decode) {
330
+ const l = (U = u.headers.get("Content-Type")) == null ? void 0 : U.toLowerCase();
331
+ l != null && l.includes("form") ? u.data = await u.data.formData() : l != null && l.includes("json") ? u.data = await u.data.json() : l != null && l.includes("text") ? u.data = await u.data.text() : l != null && l.includes("application") && (u.data = await u.data.blob());
336
332
  }
337
- u.ok ? s(u) : i(u);
333
+ u.ok ? o(u) : i(u);
338
334
  });
339
335
  });
340
336
  }
341
337
  };
342
338
  c(w, "interceptors", {}), c(w, "headers", {});
343
- function Ut(r) {
339
+ function Ht(r) {
344
340
  const t = r.split(".")[1].replace(/-/g, "+").replace(/_/g, "/");
345
- return T(decodeURIComponent(atob(t).split("").map(function(e) {
341
+ return M(decodeURIComponent(atob(t).split("").map(function(e) {
346
342
  return "%" + ("00" + e.charCodeAt(0).toString(16)).slice(-2);
347
343
  }).join("")));
348
344
  }
349
- const S = {
345
+ const x = {
350
346
  CLEAR: "\x1B[0m",
351
347
  BRIGHT: "\x1B[1m",
352
348
  DIM: "\x1B[2m",
@@ -354,7 +350,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
354
350
  BLINK: "\x1B[5m",
355
351
  REVERSE: "\x1B[7m",
356
352
  HIDDEN: "\x1B[8m"
357
- }, C = {
353
+ }, j = {
358
354
  BLACK: "\x1B[30m",
359
355
  RED: "\x1B[31m",
360
356
  GREEN: "\x1B[32m",
@@ -372,60 +368,186 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
372
368
  LIGHT_CYAN: "\x1B[96m",
373
369
  WHITE: "\x1B[97m"
374
370
  };
375
- const g = class g2 extends v {
371
+ const g = class g2 extends _ {
376
372
  constructor(t) {
377
373
  super(), this.namespace = t;
378
374
  }
379
- pad(t, e, n, o = false) {
380
- const s = t.toString(), i = e - s.length;
381
- if (i <= 0)
382
- return s;
383
- const y = Array(~~(i / n.length)).fill(n).join("");
384
- return o ? s + y : y + s;
375
+ pad(t, e, n, s = false) {
376
+ const o = t.toString(), i = e - o.length;
377
+ if (i <= 0) return o;
378
+ const a = Array(~~(i / n.length)).fill(n).join("");
379
+ return s ? o + a : a + o;
385
380
  }
386
381
  format(...t) {
387
382
  const e = /* @__PURE__ */ new Date();
388
383
  return `${`${e.getFullYear()}-${e.getMonth() + 1}-${e.getDate()} ${this.pad(e.getHours().toString(), 2, "0")}:${this.pad(e.getMinutes().toString(), 2, "0")}:${this.pad(e.getSeconds().toString(), 2, "0")}.${this.pad(e.getMilliseconds().toString(), 3, "0", true)}`}${this.namespace ? ` [${this.namespace}]` : ""} ${t.join(" ")}`;
389
384
  }
390
385
  debug(...t) {
391
- if (g2.LOG_LEVEL < 4)
392
- return;
386
+ if (g2.LOG_LEVEL < 4) return;
393
387
  const e = this.format(...t);
394
- g2.emit(4, e), console.debug(C.LIGHT_GREY + e + S.CLEAR);
388
+ g2.emit(4, e), console.debug(j.LIGHT_GREY + e + x.CLEAR);
395
389
  }
396
390
  log(...t) {
397
- if (g2.LOG_LEVEL < 3)
398
- return;
391
+ if (g2.LOG_LEVEL < 3) return;
399
392
  const e = this.format(...t);
400
- g2.emit(3, e), console.log(S.CLEAR + e);
393
+ g2.emit(3, e), console.log(x.CLEAR + e);
401
394
  }
402
395
  info(...t) {
403
- if (g2.LOG_LEVEL < 2)
404
- return;
396
+ if (g2.LOG_LEVEL < 2) return;
405
397
  const e = this.format(...t);
406
- g2.emit(2, e), console.info(C.BLUE + e + S.CLEAR);
398
+ g2.emit(2, e), console.info(j.BLUE + e + x.CLEAR);
407
399
  }
408
400
  warn(...t) {
409
- if (g2.LOG_LEVEL < 1)
410
- return;
401
+ if (g2.LOG_LEVEL < 1) return;
411
402
  const e = this.format(...t);
412
- g2.emit(1, e), console.warn(C.YELLOW + e + S.CLEAR);
403
+ g2.emit(1, e), console.warn(j.YELLOW + e + x.CLEAR);
413
404
  }
414
405
  error(...t) {
415
- if (g2.LOG_LEVEL < 0)
416
- return;
406
+ if (g2.LOG_LEVEL < 0) return;
417
407
  const e = this.format(...t);
418
- g2.emit(0, e), console.error(C.RED + e + S.CLEAR);
408
+ g2.emit(0, e), console.error(j.RED + e + x.CLEAR);
419
409
  }
420
410
  };
421
411
  c(g, "LOG_LEVEL", 4);
422
- function mt(r) {
412
+ function Ot(r) {
423
413
  return new Promise((t) => setTimeout(t, r));
424
414
  }
425
- async function _t(r, t = 100) {
426
- for (; await r(); )
427
- await mt(t);
415
+ async function ie(r, t = 100) {
416
+ for (; await r(); ) await Ot(t);
428
417
  }
418
+ var $ = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}, xt = {}, S = {};
419
+ Object.defineProperty(S, "__esModule", { value: true });
420
+ S.persist = S.Persist = void 0;
421
+ class st {
422
+ /**
423
+ * @param {string} key Primary key value will be stored under
424
+ * @param {PersistOptions<T>} options Configure using {@link PersistOptions}
425
+ */
426
+ constructor(t, e = {}) {
427
+ c(this, "key");
428
+ c(this, "options");
429
+ c(this, "storage");
430
+ c(this, "watches", {});
431
+ c(this, "_value");
432
+ this.key = t, this.options = e, this.storage = e.storage || localStorage, this.load();
433
+ }
434
+ /** Current value or default if undefined */
435
+ get value() {
436
+ var t;
437
+ return this._value !== void 0 ? this._value : (t = this.options) == null ? void 0 : t.default;
438
+ }
439
+ /** Set value with proxy object wrapper to sync future changes */
440
+ set value(t) {
441
+ t == null || typeof t != "object" ? this._value = t : this._value = new Proxy(t, {
442
+ get: (e, n) => typeof e[n] == "function" ? (...o) => {
443
+ const i = e[n](...o);
444
+ return this.save(), i;
445
+ } : e[n],
446
+ set: (e, n, s) => (e[n] = s, this.save(), true)
447
+ }), this.save();
448
+ }
449
+ /** Notify listeners of change */
450
+ notify(t) {
451
+ Object.values(this.watches).forEach((e) => e(t));
452
+ }
453
+ /** Delete value from storage */
454
+ clear() {
455
+ this.storage.removeItem(this.key);
456
+ }
457
+ /** Save current value to storage */
458
+ save() {
459
+ this._value === void 0 ? this.clear() : this.storage.setItem(this.key, JSON.stringify(this._value)), this.notify(this.value);
460
+ }
461
+ /** Load value from storage */
462
+ load() {
463
+ if (this.storage[this.key] != null) {
464
+ let t = JSON.parse(this.storage.getItem(this.key));
465
+ t != null && typeof t == "object" && this.options.type && (t.__proto__ = this.options.type.prototype), this.value = t;
466
+ } else
467
+ this.value = this.options.default || void 0;
468
+ }
469
+ /**
470
+ * Callback function which is run when there are changes
471
+ *
472
+ * @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
473
+ * @returns {() => void} Function which will unsubscribe the watch/callback when called
474
+ */
475
+ watch(t) {
476
+ const e = Object.keys(this.watches).length;
477
+ return this.watches[e] = t, () => {
478
+ delete this.watches[e];
479
+ };
480
+ }
481
+ /**
482
+ * Return value as JSON string
483
+ *
484
+ * @returns {string} Stringified object as JSON
485
+ */
486
+ toString() {
487
+ return JSON.stringify(this.value);
488
+ }
489
+ /**
490
+ * Return current value
491
+ *
492
+ * @returns {T} Current value
493
+ */
494
+ valueOf() {
495
+ return this.value;
496
+ }
497
+ }
498
+ S.Persist = st;
499
+ function Bt(r) {
500
+ return (t, e) => {
501
+ const n = (r == null ? void 0 : r.key) || `${t.constructor.name}.${e.toString()}`, s = new st(n, r);
502
+ Object.defineProperty(t, e, {
503
+ get: function() {
504
+ return s.value;
505
+ },
506
+ set: function(o) {
507
+ s.value = o;
508
+ }
509
+ });
510
+ };
511
+ }
512
+ S.persist = Bt;
513
+ var L = {};
514
+ Object.defineProperty(L, "__esModule", { value: true });
515
+ L.MemoryStorage = void 0;
516
+ class At {
517
+ get length() {
518
+ return Object.keys(this).length;
519
+ }
520
+ clear() {
521
+ Object.keys(this).forEach((t) => this.removeItem(t));
522
+ }
523
+ getItem(t) {
524
+ return this[t];
525
+ }
526
+ key(t) {
527
+ return Object.keys(this)[t];
528
+ }
529
+ removeItem(t) {
530
+ delete this[t];
531
+ }
532
+ setItem(t, e) {
533
+ this[t] = e;
534
+ }
535
+ }
536
+ L.MemoryStorage = At;
537
+ (function(r) {
538
+ var t = $ && $.__createBinding || (Object.create ? function(n, s, o, i) {
539
+ i === void 0 && (i = o);
540
+ var a = Object.getOwnPropertyDescriptor(s, o);
541
+ (!a || ("get" in a ? !s.__esModule : a.writable || a.configurable)) && (a = { enumerable: true, get: function() {
542
+ return s[o];
543
+ } }), Object.defineProperty(n, i, a);
544
+ } : function(n, s, o, i) {
545
+ i === void 0 && (i = o), n[i] = s[o];
546
+ }), e = $ && $.__exportStar || function(n, s) {
547
+ for (var o in n) o !== "default" && !Object.prototype.hasOwnProperty.call(s, o) && t(s, n, o);
548
+ };
549
+ Object.defineProperty(r, "__esModule", { value: true }), e(S, r), e(L, r);
550
+ })(xt);
429
551
  var extendStatics = function(d, b) {
430
552
  extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
431
553
  d2.__proto__ = b2;
@@ -660,8 +782,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
660
782
  return setTimeout.apply(void 0, __spreadArray([handler, timeout], __read(args)));
661
783
  },
662
784
  clearTimeout: function(handle) {
663
- var delegate = timeoutProvider.delegate;
664
- return ((delegate === null || delegate === void 0 ? void 0 : delegate.clearTimeout) || clearTimeout)(handle);
785
+ return clearTimeout(handle);
665
786
  },
666
787
  delegate: void 0
667
788
  };
@@ -834,8 +955,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
834
955
  var observable = function() {
835
956
  return typeof Symbol === "function" && Symbol.observable || "@@observable";
836
957
  }();
837
- function identity(x) {
838
- return x;
958
+ function identity(x2) {
959
+ return x2;
839
960
  }
840
961
  function pipeFromArray(fns) {
841
962
  if (fns.length === 0) {
@@ -916,8 +1037,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
916
1037
  promiseCtor = getPromiseCtor(promiseCtor);
917
1038
  return new promiseCtor(function(resolve, reject) {
918
1039
  var value;
919
- _this.subscribe(function(x) {
920
- return value = x;
1040
+ _this.subscribe(function(x2) {
1041
+ return value = x2;
921
1042
  }, function(err) {
922
1043
  return reject(err);
923
1044
  }, function() {
@@ -1553,7 +1674,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1553
1674
  login(spoke, login, password, twoFactor) {
1554
1675
  return fetch(`${this.api.url}login`, {
1555
1676
  method: "POST",
1556
- body: St(ot({
1677
+ body: $t(ut({
1557
1678
  realm: spoke.trim(),
1558
1679
  login: login.trim(),
1559
1680
  password: password.trim(),
@@ -1562,7 +1683,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1562
1683
  }))
1563
1684
  }).then(async (resp) => {
1564
1685
  const data = await resp.json().catch(() => ({}));
1565
- if (!resp.ok || data["error"]) throw Object.assign(kt(resp.status, data.error) || {}, data);
1686
+ if (!resp.ok || data["error"]) throw Object.assign(Yt(resp.status, data.error) || {}, data);
1566
1687
  this.api.token = data["token"];
1567
1688
  return data;
1568
1689
  });
@@ -1575,7 +1696,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1575
1696
  loginGuest() {
1576
1697
  return fetch(`${this.api.url}guest`).then(async (resp) => {
1577
1698
  const data = await resp.json().catch(() => ({}));
1578
- if (!resp.ok || data["error"]) throw Object.assign(kt(resp.status, data.error) || {}, data);
1699
+ if (!resp.ok || data["error"]) throw Object.assign(Yt(resp.status, data.error) || {}, data);
1579
1700
  this.api.token = data["token"];
1580
1701
  return data;
1581
1702
  });
@@ -1667,11 +1788,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1667
1788
  data.append("", file, file.name);
1668
1789
  return fetch(this.url, {
1669
1790
  method: "POST",
1670
- headers: ot({ "Authorization": this.api.token ? `Bearer ${this.api.token}` : "" }),
1791
+ headers: ut({ "Authorization": this.api.token ? `Bearer ${this.api.token}` : "" }),
1671
1792
  body: data
1672
1793
  }).then(async (resp) => {
1673
1794
  const data2 = await resp.json().catch(() => ({}));
1674
- if (!resp.ok || data2["error"]) throw Object.assign(kt(resp.status, data2.error) || {}, data2);
1795
+ if (!resp.ok || data2["error"]) throw Object.assign(Yt(resp.status, data2.error) || {}, data2);
1675
1796
  return resp;
1676
1797
  });
1677
1798
  })).then(async (files2) => {
@@ -2067,7 +2188,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2067
2188
  if (!this.unsubscribe) this.unsubscribe = this.api.socket.sliceEvents(this.slice, (event) => {
2068
2189
  const ids = [...event.data.new, ...event.data.changed];
2069
2190
  new Slice(this.slice, this.api).select(ids).exec().rows().then((rows) => this.cache = [...this.cache.filter((c2) => c2.id != null && !ids.includes(c2.id)), ...rows]);
2070
- this.cache = this.cache.filter((v2) => v2.id && !event.data.lost.includes(v2.id));
2191
+ this.cache = this.cache.filter((v) => v.id && !event.data.lost.includes(v.id));
2071
2192
  });
2072
2193
  return this.cache$;
2073
2194
  } else if (this.unsubscribe) {
@@ -2132,22 +2253,20 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2132
2253
  __publicField(this, "listeners", []);
2133
2254
  __publicField(this, "reconnect", false);
2134
2255
  __publicField(this, "socket");
2256
+ __publicField(this, "_connected", false);
2135
2257
  this.api = api;
2136
2258
  this.options = options;
2259
+ const url = new URL(this.api.url.replace("http", "ws"));
2260
+ url.port = "9390";
2137
2261
  this.options = {
2138
- url: this.api.url.replace("http", "ws") + ":3000",
2262
+ url: url.origin,
2139
2263
  ...this.options
2140
2264
  };
2141
2265
  if (this.options.url !== false)
2142
2266
  api.token$.pipe(distinctUntilChanged()).subscribe(() => this.connect());
2143
2267
  }
2144
- /**
2145
- * Is socket connected to Datalynk
2146
- * @return {boolean} true if connected, false if offline
2147
- */
2148
- get open() {
2149
- var _a;
2150
- return ((_a = this.socket) == null ? void 0 : _a.readyState) == 1;
2268
+ get connected() {
2269
+ return this._connected;
2151
2270
  }
2152
2271
  /**
2153
2272
  * Add listener for all socket events
@@ -2163,6 +2282,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2163
2282
  * Close socket connection
2164
2283
  */
2165
2284
  close() {
2285
+ this._connected = false;
2166
2286
  this.reconnect = false;
2167
2287
  this.socket.close();
2168
2288
  }
@@ -2172,28 +2292,34 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2172
2292
  * @param {number} timeout Retry interval, defaults to 30s
2173
2293
  */
2174
2294
  connect(timeout = 3e4) {
2175
- if (this.options.url === false) throw new Error("Socket is disabled");
2176
- if (this.open) {
2295
+ if (!this.options.url) throw new Error("Socket is disabled");
2296
+ if (this.connected) {
2177
2297
  this.reconnect = false;
2178
2298
  this.socket.close();
2179
2299
  }
2180
2300
  this.socket = new WebSocket(this.options.url + (this.api.token ? `?token=${this.api.token}` : ""));
2181
- const t = setTimeout(() => {
2182
- if (this.open) return;
2183
- this.socket.close();
2184
- this.connect();
2301
+ let t = setTimeout(() => {
2302
+ if (this.reconnect && !this.connected) this.connect();
2185
2303
  }, timeout);
2186
- this.socket.onopen = () => {
2187
- clearTimeout(t);
2188
- this.reconnect = true;
2189
- console.debug("Datalynk Socket: Connected");
2190
- };
2191
2304
  this.socket.onmessage = (message) => {
2192
2305
  const payload = JSON.parse(message.data);
2193
- this.listeners.forEach((l) => l(payload));
2306
+ if (payload.connected != void 0) {
2307
+ if (payload.connected) {
2308
+ clearTimeout(t);
2309
+ t = null;
2310
+ this._connected = true;
2311
+ this.reconnect = true;
2312
+ console.debug("Datalynk Socket: Connected");
2313
+ } else {
2314
+ throw new Error(`Socket failed to connect: ${payload.error}`);
2315
+ }
2316
+ } else {
2317
+ this.listeners.forEach((l) => l(payload));
2318
+ }
2194
2319
  };
2195
2320
  this.socket.onclose = () => {
2196
- if (this.reconnect) this.connect();
2321
+ this._connected = false;
2322
+ if (this.reconnect && !t) this.connect();
2197
2323
  console.debug("Datalynk Socket: Disconnected");
2198
2324
  };
2199
2325
  }
@@ -2213,14 +2339,15 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2213
2339
  * @return {Unsubscribe} Run returned function to unsubscribe callback
2214
2340
  */
2215
2341
  sliceEvents(slice, callback) {
2216
- _t(() => {
2217
- var _a;
2218
- return ((_a = this.socket) == null ? void 0 : _a.readyState) != 1;
2219
- }).then(() => this.send({ onSliceEvents: slice }));
2342
+ let cancelled = false;
2343
+ ie(() => !this.connected).then(() => {
2344
+ if (!cancelled) this.send({ onSliceEvents: slice });
2345
+ });
2220
2346
  const unsubscribe = this.addListener((event) => {
2221
2347
  if (event.type == "sliceEvents" && event.data.slice == slice) callback(event);
2222
2348
  });
2223
2349
  return () => {
2350
+ cancelled = true;
2224
2351
  this.send({ offSliceEvents: slice });
2225
2352
  unsubscribe();
2226
2353
  };
@@ -2294,7 +2421,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2294
2421
  /** API Session token */
2295
2422
  __publicField(this, "token$", new BehaviorSubject(null));
2296
2423
  this.options = options;
2297
- this.url = `${url}${url.endsWith("/") ? "" : "/"}api/`;
2424
+ this.url = `${new URL(url).origin}/api/`;
2298
2425
  this.options = {
2299
2426
  bundleTime: 100,
2300
2427
  origin: typeof location !== "undefined" ? location.host : "Unknown",
@@ -2322,21 +2449,21 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2322
2449
  /** Get session info from JWT payload */
2323
2450
  get jwtPayload() {
2324
2451
  if (!this.token) return null;
2325
- return Ut(this.token);
2452
+ return Ht(this.token);
2326
2453
  }
2327
2454
  _request(req, options = {}) {
2328
2455
  return fetch(this.url, {
2329
2456
  method: "POST",
2330
- headers: ot({
2457
+ headers: ut({
2331
2458
  Authorization: this.token ? `Bearer ${this.token}` : void 0,
2332
2459
  "Content-Type": "application/json"
2333
2460
  }),
2334
2461
  body: JSON.stringify(Api.translateTokens(req))
2335
2462
  }).then(async (resp) => {
2336
- if (!resp.ok) throw kt(resp.status);
2463
+ if (!resp.ok) throw Yt(resp.status);
2337
2464
  let data = await resp.json();
2338
2465
  if (!options.raw) data = Api.translateTokens(data);
2339
- if (data["error"]) throw Object.assign(kt(resp.status, data["error"]) || {}, data);
2466
+ if (data["error"]) throw Object.assign(Yt(resp.status, data["error"]) || {}, data);
2340
2467
  return data;
2341
2468
  });
2342
2469
  }