@auxilium/datalynk-client 0.8.0 → 0.8.1

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.mjs CHANGED
@@ -1,344 +1,400 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- var it = Object.defineProperty;
5
- var ct = (r, t, e) => t in r ? it(r, t, { enumerable: true, configurable: true, writable: true, value: e }) : r[t] = e;
6
- var c = (r, t, e) => ct(r, typeof t != "symbol" ? t + "" : t, e);
7
- function ut(r, t = false) {
8
- if (r == null) throw new Error("Cannot clean a NULL value");
9
- return Array.isArray(r) ? r = r.filter((e) => e != null) : Object.entries(r).forEach(([e, n]) => {
10
- (t && n === void 0 || !t && n == null) && delete r[e];
11
- }), r;
4
+ var __defProp2 = Object.defineProperty;
5
+ var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField2 = (obj, key, value) => __defNormalProp2(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ function clean(obj, undefinedOnly = false) {
8
+ if (obj == null) throw new Error("Cannot clean a NULL value");
9
+ if (Array.isArray(obj)) {
10
+ obj = obj.filter((o) => o != null);
11
+ } else {
12
+ Object.entries(obj).forEach(([key, value]) => {
13
+ if (undefinedOnly && value === void 0 || !undefinedOnly && value == null) delete obj[key];
14
+ });
15
+ }
16
+ return obj;
12
17
  }
13
- function $t(r) {
14
- const t = new FormData();
15
- return Object.entries(r).forEach(([e, n]) => t.append(e, n)), t;
18
+ function formData(target) {
19
+ const data = new FormData();
20
+ Object.entries(target).forEach(([key, value]) => data.append(key, value));
21
+ return data;
16
22
  }
17
- function M(r) {
23
+ function JSONAttemptParse(json) {
18
24
  try {
19
- return JSON.parse(r);
25
+ return JSON.parse(json);
20
26
  } catch {
21
- return r;
27
+ return json;
22
28
  }
23
29
  }
24
- class E extends Promise {
25
- constructor(e) {
26
- super((n, s) => e(
27
- (o) => n(o),
28
- (o) => s(o),
29
- (o) => this.progress = o
30
+ function JSONSanitize(obj, space) {
31
+ let cache = [];
32
+ return JSON.stringify(obj, (key, value) => {
33
+ if (typeof value === "object" && value !== null) {
34
+ if (cache.includes(value)) return;
35
+ cache.push(value);
36
+ }
37
+ return value;
38
+ }, space);
39
+ }
40
+ class PromiseProgress extends Promise {
41
+ constructor(executor) {
42
+ super((resolve, reject) => executor(
43
+ (value) => resolve(value),
44
+ (reason) => reject(reason),
45
+ (progress) => this.progress = progress
30
46
  ));
31
- c(this, "listeners", []);
32
- c(this, "_progress", 0);
47
+ __publicField2(this, "listeners", []);
48
+ __publicField2(this, "_progress", 0);
33
49
  }
34
50
  get progress() {
35
51
  return this._progress;
36
52
  }
37
- set progress(e) {
38
- e != this._progress && (this._progress = e, this.listeners.forEach((n) => n(e)));
53
+ set progress(p) {
54
+ if (p == this._progress) return;
55
+ this._progress = p;
56
+ this.listeners.forEach((l) => l(p));
39
57
  }
40
- static from(e) {
41
- return e instanceof E ? e : new E((n, s) => e.then((...o) => n(...o)).catch((...o) => s(...o)));
58
+ static from(promise) {
59
+ if (promise instanceof PromiseProgress) return promise;
60
+ return new PromiseProgress((res, rej) => promise.then((...args) => res(...args)).catch((...args) => rej(...args)));
42
61
  }
43
- from(e) {
44
- const n = E.from(e);
45
- return this.onProgress((s) => n.progress = s), n;
62
+ from(promise) {
63
+ const newPromise = PromiseProgress.from(promise);
64
+ this.onProgress((p) => newPromise.progress = p);
65
+ return newPromise;
46
66
  }
47
- onProgress(e) {
48
- return this.listeners.push(e), this;
67
+ onProgress(callback) {
68
+ this.listeners.push(callback);
69
+ return this;
49
70
  }
50
- then(e, n) {
51
- const s = super.then(e, n);
52
- return this.from(s);
71
+ then(res, rej) {
72
+ const resp = super.then(res, rej);
73
+ return this.from(resp);
53
74
  }
54
- catch(e) {
55
- return this.from(super.catch(e));
75
+ catch(rej) {
76
+ return this.from(super.catch(rej));
56
77
  }
57
- finally(e) {
58
- return this.from(super.finally(e));
78
+ finally(res) {
79
+ return this.from(super.finally(res));
59
80
  }
60
81
  }
61
- class _ {
82
+ class TypedEmitter {
62
83
  constructor() {
63
- c(this, "listeners", {});
64
- }
65
- static emit(t, ...e) {
66
- (this.listeners["*"] || []).forEach((n) => n(t, ...e)), (this.listeners[t.toString()] || []).forEach((n) => n(...e));
84
+ __publicField2(this, "listeners", {});
67
85
  }
68
- static off(t, e) {
69
- const n = t.toString();
70
- this.listeners[n] = (this.listeners[n] || []).filter((s) => s === e);
86
+ static emit(event, ...args) {
87
+ (this.listeners["*"] || []).forEach((l) => l(event, ...args));
88
+ (this.listeners[event.toString()] || []).forEach((l) => l(...args));
71
89
  }
72
- static on(t, e) {
73
- var s;
74
- const n = t.toString();
75
- return this.listeners[n] || (this.listeners[n] = []), (s = this.listeners[n]) == null || s.push(e), () => this.off(t, e);
90
+ static off(event, listener) {
91
+ const e = event.toString();
92
+ this.listeners[e] = (this.listeners[e] || []).filter((l) => l === listener);
76
93
  }
77
- static once(t, e) {
78
- return new Promise((n) => {
79
- const s = this.on(t, (...o) => {
80
- n(o.length == 1 ? o[0] : o), e && e(...o), s();
94
+ static on(event, listener) {
95
+ var _a;
96
+ const e = event.toString();
97
+ if (!this.listeners[e]) this.listeners[e] = [];
98
+ (_a = this.listeners[e]) == null ? void 0 : _a.push(listener);
99
+ return () => this.off(event, listener);
100
+ }
101
+ static once(event, listener) {
102
+ return new Promise((res) => {
103
+ const unsubscribe = this.on(event, (...args) => {
104
+ res(args.length == 1 ? args[0] : args);
105
+ if (listener) listener(...args);
106
+ unsubscribe();
81
107
  });
82
108
  });
83
109
  }
84
- emit(t, ...e) {
85
- (this.listeners["*"] || []).forEach((n) => n(t, ...e)), (this.listeners[t] || []).forEach((n) => n(...e));
86
- }
87
- off(t, e) {
88
- this.listeners[t] = (this.listeners[t] || []).filter((n) => n === e);
110
+ emit(event, ...args) {
111
+ (this.listeners["*"] || []).forEach((l) => l(event, ...args));
112
+ (this.listeners[event] || []).forEach((l) => l(...args));
89
113
  }
90
- on(t, e) {
91
- var n;
92
- return this.listeners[t] || (this.listeners[t] = []), (n = this.listeners[t]) == null || n.push(e), () => this.off(t, e);
114
+ off(event, listener) {
115
+ this.listeners[event] = (this.listeners[event] || []).filter((l) => l === listener);
93
116
  }
94
- once(t, e) {
95
- return new Promise((n) => {
96
- const s = this.on(t, (...o) => {
97
- n(o.length == 1 ? o[0] : o), e && e(...o), s();
117
+ on(event, listener) {
118
+ var _a;
119
+ if (!this.listeners[event]) this.listeners[event] = [];
120
+ (_a = this.listeners[event]) == null ? void 0 : _a.push(listener);
121
+ return () => this.off(event, listener);
122
+ }
123
+ once(event, listener) {
124
+ return new Promise((res) => {
125
+ const unsubscribe = this.on(event, (...args) => {
126
+ res(args.length == 1 ? args[0] : args);
127
+ if (listener) listener(...args);
128
+ unsubscribe();
98
129
  });
99
130
  });
100
131
  }
101
132
  }
102
- c(_, "listeners", {});
103
- class p extends Error {
104
- constructor(e, n) {
105
- super(e);
106
- c(this, "_code");
107
- n != null && (this._code = n);
133
+ __publicField2(TypedEmitter, "listeners", {});
134
+ class CustomError extends Error {
135
+ constructor(message, code) {
136
+ super(message);
137
+ __publicField2(this, "_code");
138
+ if (code != null) this._code = code;
108
139
  }
109
140
  get code() {
110
141
  return this._code || this.constructor.code;
111
142
  }
112
- set code(e) {
113
- this._code = e;
143
+ set code(c) {
144
+ this._code = c;
114
145
  }
115
- static from(e) {
116
- const n = Number(e.statusCode) ?? Number(e.code), s = new this(e.message || e.toString());
117
- return Object.assign(s, {
118
- stack: e.stack,
119
- ...e,
120
- code: n ?? void 0
146
+ static from(err) {
147
+ const code = Number(err.statusCode) ?? Number(err.code);
148
+ const newErr = new this(err.message || err.toString());
149
+ return Object.assign(newErr, {
150
+ stack: err.stack,
151
+ ...err,
152
+ code: code ?? void 0
121
153
  });
122
154
  }
123
- static instanceof(e) {
124
- return e.constructor.code != null;
155
+ static instanceof(err) {
156
+ return err.constructor.code != void 0;
125
157
  }
126
158
  toString() {
127
159
  return this.message || super.toString();
128
160
  }
129
161
  }
130
- c(p, "code", 500);
131
- class W extends p {
132
- constructor(t = "Bad Request") {
133
- super(t);
162
+ __publicField2(CustomError, "code", 500);
163
+ class BadRequestError extends CustomError {
164
+ constructor(message = "Bad Request") {
165
+ super(message);
134
166
  }
135
- static instanceof(t) {
136
- return t.constructor.code == this.code;
167
+ static instanceof(err) {
168
+ return err.constructor.code == this.code;
137
169
  }
138
170
  }
139
- c(W, "code", 400);
140
- class J extends p {
141
- constructor(t = "Unauthorized") {
142
- super(t);
171
+ __publicField2(BadRequestError, "code", 400);
172
+ class UnauthorizedError extends CustomError {
173
+ constructor(message = "Unauthorized") {
174
+ super(message);
143
175
  }
144
- static instanceof(t) {
145
- return t.constructor.code == this.code;
176
+ static instanceof(err) {
177
+ return err.constructor.code == this.code;
146
178
  }
147
179
  }
148
- c(J, "code", 401);
149
- class z extends p {
150
- constructor(t = "Payment Required") {
151
- super(t);
180
+ __publicField2(UnauthorizedError, "code", 401);
181
+ class PaymentRequiredError extends CustomError {
182
+ constructor(message = "Payment Required") {
183
+ super(message);
152
184
  }
153
- static instanceof(t) {
154
- return t.constructor.code == this.code;
185
+ static instanceof(err) {
186
+ return err.constructor.code == this.code;
155
187
  }
156
188
  }
157
- c(z, "code", 402);
158
- class K extends p {
159
- constructor(t = "Forbidden") {
160
- super(t);
189
+ __publicField2(PaymentRequiredError, "code", 402);
190
+ class ForbiddenError extends CustomError {
191
+ constructor(message = "Forbidden") {
192
+ super(message);
161
193
  }
162
- static instanceof(t) {
163
- return t.constructor.code == this.code;
194
+ static instanceof(err) {
195
+ return err.constructor.code == this.code;
164
196
  }
165
197
  }
166
- c(K, "code", 403);
167
- class Z extends p {
168
- constructor(t = "Not Found") {
169
- super(t);
198
+ __publicField2(ForbiddenError, "code", 403);
199
+ class NotFoundError extends CustomError {
200
+ constructor(message = "Not Found") {
201
+ super(message);
170
202
  }
171
- static instanceof(t) {
172
- return t.constructor.code == this.code;
203
+ static instanceof(err) {
204
+ return err.constructor.code == this.code;
173
205
  }
174
206
  }
175
- c(Z, "code", 404);
176
- class V extends p {
177
- constructor(t = "Method Not Allowed") {
178
- super(t);
207
+ __publicField2(NotFoundError, "code", 404);
208
+ class MethodNotAllowedError extends CustomError {
209
+ constructor(message = "Method Not Allowed") {
210
+ super(message);
179
211
  }
180
- static instanceof(t) {
181
- return t.constructor.code == this.code;
212
+ static instanceof(err) {
213
+ return err.constructor.code == this.code;
182
214
  }
183
215
  }
184
- c(V, "code", 405);
185
- class X extends p {
186
- constructor(t = "Not Acceptable") {
187
- super(t);
216
+ __publicField2(MethodNotAllowedError, "code", 405);
217
+ class NotAcceptableError extends CustomError {
218
+ constructor(message = "Not Acceptable") {
219
+ super(message);
188
220
  }
189
- static instanceof(t) {
190
- return t.constructor.code == this.code;
221
+ static instanceof(err) {
222
+ return err.constructor.code == this.code;
191
223
  }
192
224
  }
193
- c(X, "code", 406);
194
- class Q extends p {
195
- constructor(t = "Internal Server Error") {
196
- super(t);
225
+ __publicField2(NotAcceptableError, "code", 406);
226
+ class InternalServerError extends CustomError {
227
+ constructor(message = "Internal Server Error") {
228
+ super(message);
197
229
  }
198
- static instanceof(t) {
199
- return t.constructor.code == this.code;
230
+ static instanceof(err) {
231
+ return err.constructor.code == this.code;
200
232
  }
201
233
  }
202
- c(Q, "code", 500);
203
- class tt extends p {
204
- constructor(t = "Not Implemented") {
205
- super(t);
234
+ __publicField2(InternalServerError, "code", 500);
235
+ class NotImplementedError extends CustomError {
236
+ constructor(message = "Not Implemented") {
237
+ super(message);
206
238
  }
207
- static instanceof(t) {
208
- return t.constructor.code == this.code;
239
+ static instanceof(err) {
240
+ return err.constructor.code == this.code;
209
241
  }
210
242
  }
211
- c(tt, "code", 501);
212
- class et extends p {
213
- constructor(t = "Bad Gateway") {
214
- super(t);
243
+ __publicField2(NotImplementedError, "code", 501);
244
+ class BadGatewayError extends CustomError {
245
+ constructor(message = "Bad Gateway") {
246
+ super(message);
215
247
  }
216
- static instanceof(t) {
217
- return t.constructor.code == this.code;
248
+ static instanceof(err) {
249
+ return err.constructor.code == this.code;
218
250
  }
219
251
  }
220
- c(et, "code", 502);
221
- class rt extends p {
222
- constructor(t = "Service Unavailable") {
223
- super(t);
252
+ __publicField2(BadGatewayError, "code", 502);
253
+ class ServiceUnavailableError extends CustomError {
254
+ constructor(message = "Service Unavailable") {
255
+ super(message);
224
256
  }
225
- static instanceof(t) {
226
- return t.constructor.code == this.code;
257
+ static instanceof(err) {
258
+ return err.constructor.code == this.code;
227
259
  }
228
260
  }
229
- c(rt, "code", 503);
230
- class nt extends p {
231
- constructor(t = "Gateway Timeout") {
232
- super(t);
261
+ __publicField2(ServiceUnavailableError, "code", 503);
262
+ class GatewayTimeoutError extends CustomError {
263
+ constructor(message = "Gateway Timeout") {
264
+ super(message);
233
265
  }
234
- static instanceof(t) {
235
- return t.constructor.code == this.code;
266
+ static instanceof(err) {
267
+ return err.constructor.code == this.code;
236
268
  }
237
269
  }
238
- c(nt, "code", 504);
239
- function Yt(r, t) {
240
- if (r >= 200 && r < 300) return null;
241
- switch (r) {
270
+ __publicField2(GatewayTimeoutError, "code", 504);
271
+ function errorFromCode(code, message) {
272
+ switch (code) {
242
273
  case 400:
243
- return new W(t);
274
+ return new BadRequestError(message);
244
275
  case 401:
245
- return new J(t);
276
+ return new UnauthorizedError(message);
246
277
  case 402:
247
- return new z(t);
278
+ return new PaymentRequiredError(message);
248
279
  case 403:
249
- return new K(t);
280
+ return new ForbiddenError(message);
250
281
  case 404:
251
- return new Z(t);
282
+ return new NotFoundError(message);
252
283
  case 405:
253
- return new V(t);
284
+ return new MethodNotAllowedError(message);
254
285
  case 406:
255
- return new X(t);
286
+ return new NotAcceptableError(message);
256
287
  case 500:
257
- return new Q(t);
288
+ return new InternalServerError(message);
258
289
  case 501:
259
- return new tt(t);
290
+ return new NotImplementedError(message);
260
291
  case 502:
261
- return new et(t);
292
+ return new BadGatewayError(message);
262
293
  case 503:
263
- return new rt(t);
294
+ return new ServiceUnavailableError(message);
264
295
  case 504:
265
- return new nt(t);
296
+ return new GatewayTimeoutError(message);
266
297
  default:
267
- return new p(t, r);
298
+ return new CustomError(message, code);
268
299
  }
269
300
  }
270
- const w = class w2 {
271
- constructor(t = {}) {
272
- c(this, "interceptors", {});
273
- c(this, "headers", {});
274
- c(this, "url");
275
- this.url = t.url ?? null, this.headers = t.headers || {}, t.interceptors && t.interceptors.forEach((e) => w2.addInterceptor(e));
276
- }
277
- static addInterceptor(t) {
278
- const e = Object.keys(w2.interceptors).length.toString();
279
- return w2.interceptors[e] = t, () => {
280
- w2.interceptors[e] = null;
301
+ const _Http = class _Http2 {
302
+ constructor(defaults = {}) {
303
+ __publicField2(this, "interceptors", {});
304
+ __publicField2(this, "headers", {});
305
+ __publicField2(this, "url");
306
+ this.url = defaults.url ?? null;
307
+ this.headers = defaults.headers || {};
308
+ if (defaults.interceptors) {
309
+ defaults.interceptors.forEach((i) => _Http2.addInterceptor(i));
310
+ }
311
+ }
312
+ static addInterceptor(fn) {
313
+ const key = Object.keys(_Http2.interceptors).length.toString();
314
+ _Http2.interceptors[key] = fn;
315
+ return () => {
316
+ _Http2.interceptors[key] = null;
281
317
  };
282
318
  }
283
- addInterceptor(t) {
284
- const e = Object.keys(this.interceptors).length.toString();
285
- return this.interceptors[e] = t, () => {
286
- this.interceptors[e] = null;
319
+ addInterceptor(fn) {
320
+ const key = Object.keys(this.interceptors).length.toString();
321
+ this.interceptors[key] = fn;
322
+ return () => {
323
+ this.interceptors[key] = null;
287
324
  };
288
325
  }
289
- request(t = {}) {
290
- var s;
291
- if (!this.url && !t.url) throw new Error("URL needs to be set");
292
- let e = ((s = t.url) != null && s.startsWith("http") ? t.url : (this.url || "") + (t.url || "")).replace(/([^:]\/)\/+/g, "$1");
293
- if (t.fragment && (e.includes("#") ? e.replace(/#.*(\?|\n)/g, (o, i) => `#${t.fragment}${i}`) : e += "#" + t.fragment), t.query) {
294
- const o = Array.isArray(t.query) ? t.query : Object.keys(t.query).map((i) => ({ key: i, value: t.query[i] }));
295
- e += (e.includes("?") ? "&" : "?") + o.map((i) => `${i.key}=${i.value}`).join("&");
326
+ request(opts = {}) {
327
+ var _a;
328
+ if (!this.url && !opts.url) throw new Error("URL needs to be set");
329
+ let url = (((_a = opts.url) == null ? void 0 : _a.startsWith("http")) ? opts.url : (this.url || "") + (opts.url || "")).replace(/([^:]\/)\/+/g, "$1");
330
+ if (opts.fragment) url.includes("#") ? url.replace(/#.*(\?|\n)/g, (match, arg1) => `#${opts.fragment}${arg1}`) : url += "#" + opts.fragment;
331
+ if (opts.query) {
332
+ const q = Array.isArray(opts.query) ? opts.query : Object.keys(opts.query).map((k) => ({ key: k, value: opts.query[k] }));
333
+ url += (url.includes("?") ? "&" : "?") + q.map((q2) => `${q2.key}=${q2.value}`).join("&");
296
334
  }
297
- const n = ut({
298
- "Content-Type": t.body ? t.body instanceof FormData ? "multipart/form-data" : "application/json" : void 0,
299
- ...w2.headers,
335
+ const headers = clean({
336
+ "Content-Type": !opts.body ? void 0 : opts.body instanceof FormData ? "multipart/form-data" : "application/json",
337
+ ..._Http2.headers,
300
338
  ...this.headers,
301
- ...t.headers
339
+ ...opts.headers
302
340
  });
303
- return typeof t.body == "object" && t.body != null && n["Content-Type"] == "application/json" && (t.body = JSON.stringify(t.body)), new E((o, i, a) => {
304
- fetch(e, {
305
- headers: n,
306
- method: t.method || (t.body ? "POST" : "GET"),
307
- body: t.body
308
- }).then(async (u) => {
309
- var G, U;
310
- for (let l of [...Object.values(w2.interceptors), ...Object.values(this.interceptors)])
311
- await new Promise((R) => l(u, () => R()));
312
- const v = u.headers.get("Content-Length"), N = v ? parseInt(v, 10) : 0;
313
- let P = 0;
314
- const I = (G = u.body) == null ? void 0 : G.getReader(), ot = new ReadableStream({
315
- start(l) {
316
- function R() {
317
- I == null || I.read().then((O) => {
318
- if (O.done) return l.close();
319
- P += O.value.byteLength, a(P / N), l.enqueue(O.value), R();
320
- }).catch((O) => l.error(O));
341
+ if (typeof opts.body == "object" && opts.body != null && headers["Content-Type"] == "application/json")
342
+ opts.body = JSON.stringify(opts.body);
343
+ return new PromiseProgress((res, rej, prog) => {
344
+ try {
345
+ fetch(url, {
346
+ headers,
347
+ method: opts.method || (opts.body ? "POST" : "GET"),
348
+ body: opts.body
349
+ }).then(async (resp) => {
350
+ var _a2, _b;
351
+ for (let fn of [...Object.values(_Http2.interceptors), ...Object.values(this.interceptors)]) {
352
+ await new Promise((res2) => fn(resp, () => res2()));
353
+ }
354
+ const contentLength = resp.headers.get("Content-Length");
355
+ const total = contentLength ? parseInt(contentLength, 10) : 0;
356
+ let loaded = 0;
357
+ const reader = (_a2 = resp.body) == null ? void 0 : _a2.getReader();
358
+ const stream = new ReadableStream({
359
+ start(controller) {
360
+ function push() {
361
+ reader == null ? void 0 : reader.read().then((event) => {
362
+ if (event.done) return controller.close();
363
+ loaded += event.value.byteLength;
364
+ prog(loaded / total);
365
+ controller.enqueue(event.value);
366
+ push();
367
+ }).catch((error) => controller.error(error));
368
+ }
369
+ push();
321
370
  }
322
- R();
371
+ });
372
+ resp.data = new Response(stream);
373
+ if (opts.decode == null || opts.decode) {
374
+ const content = (_b = resp.headers.get("Content-Type")) == null ? void 0 : _b.toLowerCase();
375
+ if (content == null ? void 0 : content.includes("form")) resp.data = await resp.data.formData();
376
+ else if (content == null ? void 0 : content.includes("json")) resp.data = await resp.data.json();
377
+ else if (content == null ? void 0 : content.includes("text")) resp.data = await resp.data.text();
378
+ else if (content == null ? void 0 : content.includes("application")) resp.data = await resp.data.blob();
323
379
  }
324
- });
325
- if (u.data = new Response(ot), t.decode == null || t.decode) {
326
- const l = (U = u.headers.get("Content-Type")) == null ? void 0 : U.toLowerCase();
327
- 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());
328
- }
329
- u.ok ? o(u) : i(u);
330
- });
380
+ if (resp.ok) res(resp);
381
+ else rej(resp);
382
+ }).catch((err) => rej(err));
383
+ } catch (err) {
384
+ rej(err);
385
+ }
331
386
  });
332
387
  }
333
388
  };
334
- c(w, "interceptors", {}), c(w, "headers", {});
335
- function Ht(r) {
336
- const t = r.split(".")[1].replace(/-/g, "+").replace(/_/g, "/");
337
- return M(decodeURIComponent(atob(t).split("").map(function(e) {
338
- return "%" + ("00" + e.charCodeAt(0).toString(16)).slice(-2);
389
+ __publicField2(_Http, "interceptors", {});
390
+ __publicField2(_Http, "headers", {});
391
+ function jwtDecode(token) {
392
+ const base64 = token.split(".")[1].replace(/-/g, "+").replace(/_/g, "/");
393
+ return JSONAttemptParse(decodeURIComponent(atob(base64).split("").map(function(c) {
394
+ return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
339
395
  }).join("")));
340
396
  }
341
- const x = {
397
+ const CliEffects = {
342
398
  CLEAR: "\x1B[0m",
343
399
  BRIGHT: "\x1B[1m",
344
400
  DIM: "\x1B[2m",
@@ -346,7 +402,8 @@ const x = {
346
402
  BLINK: "\x1B[5m",
347
403
  REVERSE: "\x1B[7m",
348
404
  HIDDEN: "\x1B[8m"
349
- }, j = {
405
+ };
406
+ const CliForeground = {
350
407
  BLACK: "\x1B[30m",
351
408
  RED: "\x1B[31m",
352
409
  GREEN: "\x1B[32m",
@@ -364,191 +421,59 @@ const x = {
364
421
  LIGHT_CYAN: "\x1B[96m",
365
422
  WHITE: "\x1B[97m"
366
423
  };
367
- const g = class g2 extends _ {
368
- constructor(t) {
369
- super(), this.namespace = t;
370
- }
371
- pad(t, e, n, s = false) {
372
- const o = t.toString(), i = e - o.length;
373
- if (i <= 0) return o;
374
- const a = Array(~~(i / n.length)).fill(n).join("");
375
- return s ? o + a : a + o;
376
- }
377
- format(...t) {
378
- const e = /* @__PURE__ */ new Date();
379
- 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(" ")}`;
380
- }
381
- debug(...t) {
382
- if (g2.LOG_LEVEL < 4) return;
383
- const e = this.format(...t);
384
- g2.emit(4, e), console.debug(j.LIGHT_GREY + e + x.CLEAR);
385
- }
386
- log(...t) {
387
- if (g2.LOG_LEVEL < 3) return;
388
- const e = this.format(...t);
389
- g2.emit(3, e), console.log(x.CLEAR + e);
390
- }
391
- info(...t) {
392
- if (g2.LOG_LEVEL < 2) return;
393
- const e = this.format(...t);
394
- g2.emit(2, e), console.info(j.BLUE + e + x.CLEAR);
395
- }
396
- warn(...t) {
397
- if (g2.LOG_LEVEL < 1) return;
398
- const e = this.format(...t);
399
- g2.emit(1, e), console.warn(j.YELLOW + e + x.CLEAR);
400
- }
401
- error(...t) {
402
- if (g2.LOG_LEVEL < 0) return;
403
- const e = this.format(...t);
404
- g2.emit(0, e), console.error(j.RED + e + x.CLEAR);
424
+ const _Logger = class _Logger2 extends TypedEmitter {
425
+ constructor(namespace) {
426
+ super();
427
+ this.namespace = namespace;
428
+ }
429
+ format(...text) {
430
+ const now = /* @__PURE__ */ new Date();
431
+ const timestamp = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}:${now.getSeconds().toString().padStart(2, "0")}.${now.getMilliseconds().toString().padEnd(3, "0")}`;
432
+ return `${timestamp}${this.namespace ? ` [${this.namespace}]` : ""} ${text.map((t) => typeof t == "string" ? t : JSONSanitize(t, 2)).join(" ")}`;
433
+ }
434
+ debug(...args) {
435
+ if (_Logger2.LOG_LEVEL < 4) return;
436
+ const str = this.format(...args);
437
+ _Logger2.emit(4, str);
438
+ console.debug(CliForeground.LIGHT_GREY + str + CliEffects.CLEAR);
439
+ }
440
+ log(...args) {
441
+ if (_Logger2.LOG_LEVEL < 3) return;
442
+ const str = this.format(...args);
443
+ _Logger2.emit(3, str);
444
+ console.log(CliEffects.CLEAR + str);
445
+ }
446
+ info(...args) {
447
+ if (_Logger2.LOG_LEVEL < 2) return;
448
+ const str = this.format(...args);
449
+ _Logger2.emit(2, str);
450
+ console.info(CliForeground.BLUE + str + CliEffects.CLEAR);
451
+ }
452
+ warn(...args) {
453
+ if (_Logger2.LOG_LEVEL < 1) return;
454
+ const str = this.format(...args);
455
+ _Logger2.emit(1, str);
456
+ console.warn(CliForeground.YELLOW + str + CliEffects.CLEAR);
457
+ }
458
+ error(...args) {
459
+ if (_Logger2.LOG_LEVEL < 0) return;
460
+ const str = this.format(...args);
461
+ _Logger2.emit(0, str);
462
+ console.error(CliForeground.RED + str + CliEffects.CLEAR);
405
463
  }
406
464
  };
407
- c(g, "LOG_LEVEL", 4);
408
- function Ot(r) {
409
- return new Promise((t) => setTimeout(t, r));
465
+ __publicField2(_Logger, "LOG_LEVEL", 4);
466
+ function sleep(ms) {
467
+ return new Promise((res) => setTimeout(res, ms));
410
468
  }
411
- async function ie(r, t = 100) {
412
- for (; await r(); ) await Ot(t);
469
+ async function sleepWhile(fn, checkInterval = 100) {
470
+ while (await fn()) await sleep(checkInterval);
413
471
  }
414
- var $ = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}, xt = {}, S = {};
415
- Object.defineProperty(S, "__esModule", { value: true });
416
- S.persist = S.Persist = void 0;
417
- class st {
418
- /**
419
- * @param {string} key Primary key value will be stored under
420
- * @param {PersistOptions<T>} options Configure using {@link PersistOptions}
421
- */
422
- constructor(t, e = {}) {
423
- c(this, "key");
424
- c(this, "options");
425
- c(this, "storage");
426
- c(this, "watches", {});
427
- c(this, "_value");
428
- this.key = t, this.options = e, this.storage = e.storage || localStorage, this.load();
429
- }
430
- /** Current value or default if undefined */
431
- get value() {
432
- var t;
433
- return this._value !== void 0 ? this._value : (t = this.options) == null ? void 0 : t.default;
434
- }
435
- /** Set value with proxy object wrapper to sync future changes */
436
- set value(t) {
437
- t == null || typeof t != "object" ? this._value = t : this._value = new Proxy(t, {
438
- get: (e, n) => typeof e[n] == "function" ? (...o) => {
439
- const i = e[n](...o);
440
- return this.save(), i;
441
- } : e[n],
442
- set: (e, n, s) => (e[n] = s, this.save(), true)
443
- }), this.save();
444
- }
445
- /** Notify listeners of change */
446
- notify(t) {
447
- Object.values(this.watches).forEach((e) => e(t));
448
- }
449
- /** Delete value from storage */
450
- clear() {
451
- this.storage.removeItem(this.key);
452
- }
453
- /** Save current value to storage */
454
- save() {
455
- this._value === void 0 ? this.clear() : this.storage.setItem(this.key, JSON.stringify(this._value)), this.notify(this.value);
456
- }
457
- /** Load value from storage */
458
- load() {
459
- if (this.storage[this.key] != null) {
460
- let t = JSON.parse(this.storage.getItem(this.key));
461
- t != null && typeof t == "object" && this.options.type && (t.__proto__ = this.options.type.prototype), this.value = t;
462
- } else
463
- this.value = this.options.default || void 0;
464
- }
465
- /**
466
- * Callback function which is run when there are changes
467
- *
468
- * @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
469
- * @returns {() => void} Function which will unsubscribe the watch/callback when called
470
- */
471
- watch(t) {
472
- const e = Object.keys(this.watches).length;
473
- return this.watches[e] = t, () => {
474
- delete this.watches[e];
475
- };
476
- }
477
- /**
478
- * Return value as JSON string
479
- *
480
- * @returns {string} Stringified object as JSON
481
- */
482
- toString() {
483
- return JSON.stringify(this.value);
484
- }
485
- /**
486
- * Return current value
487
- *
488
- * @returns {T} Current value
489
- */
490
- valueOf() {
491
- return this.value;
492
- }
493
- }
494
- S.Persist = st;
495
- function Bt(r) {
496
- return (t, e) => {
497
- const n = (r == null ? void 0 : r.key) || `${t.constructor.name}.${e.toString()}`, s = new st(n, r);
498
- Object.defineProperty(t, e, {
499
- get: function() {
500
- return s.value;
501
- },
502
- set: function(o) {
503
- s.value = o;
504
- }
505
- });
506
- };
507
- }
508
- S.persist = Bt;
509
- var L = {};
510
- Object.defineProperty(L, "__esModule", { value: true });
511
- L.MemoryStorage = void 0;
512
- class At {
513
- get length() {
514
- return Object.keys(this).length;
515
- }
516
- clear() {
517
- Object.keys(this).forEach((t) => this.removeItem(t));
518
- }
519
- getItem(t) {
520
- return this[t];
521
- }
522
- key(t) {
523
- return Object.keys(this)[t];
524
- }
525
- removeItem(t) {
526
- delete this[t];
527
- }
528
- setItem(t, e) {
529
- this[t] = e;
530
- }
531
- }
532
- L.MemoryStorage = At;
533
- (function(r) {
534
- var t = $ && $.__createBinding || (Object.create ? function(n, s, o, i) {
535
- i === void 0 && (i = o);
536
- var a = Object.getOwnPropertyDescriptor(s, o);
537
- (!a || ("get" in a ? !s.__esModule : a.writable || a.configurable)) && (a = { enumerable: true, get: function() {
538
- return s[o];
539
- } }), Object.defineProperty(n, i, a);
540
- } : function(n, s, o, i) {
541
- i === void 0 && (i = o), n[i] = s[o];
542
- }), e = $ && $.__exportStar || function(n, s) {
543
- for (var o in n) o !== "default" && !Object.prototype.hasOwnProperty.call(s, o) && t(s, n, o);
544
- };
545
- Object.defineProperty(r, "__esModule", { value: true }), e(S, r), e(L, r);
546
- })(xt);
547
472
  var extendStatics = function(d, b) {
548
473
  extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
549
474
  d2.__proto__ = b2;
550
475
  } || function(d2, b2) {
551
- for (var p2 in b2) if (Object.prototype.hasOwnProperty.call(b2, p2)) d2[p2] = b2[p2];
476
+ for (var p in b2) if (Object.prototype.hasOwnProperty.call(b2, p)) d2[p] = b2[p];
552
477
  };
553
478
  return extendStatics(d, b);
554
479
  };
@@ -951,8 +876,8 @@ var EMPTY_OBSERVER = {
951
876
  var observable = function() {
952
877
  return typeof Symbol === "function" && Symbol.observable || "@@observable";
953
878
  }();
954
- function identity(x2) {
955
- return x2;
879
+ function identity(x) {
880
+ return x;
956
881
  }
957
882
  function pipeFromArray(fns) {
958
883
  if (fns.length === 0) {
@@ -1033,8 +958,8 @@ var Observable = function() {
1033
958
  promiseCtor = getPromiseCtor(promiseCtor);
1034
959
  return new promiseCtor(function(resolve, reject) {
1035
960
  var value;
1036
- _this.subscribe(function(x2) {
1037
- return value = x2;
961
+ _this.subscribe(function(x) {
962
+ return value = x;
1038
963
  }, function(err) {
1039
964
  return reject(err);
1040
965
  }, function() {
@@ -1670,7 +1595,7 @@ class Auth {
1670
1595
  login(spoke, login, password, twoFactor) {
1671
1596
  return fetch(`${this.api.url}login`, {
1672
1597
  method: "POST",
1673
- body: $t(ut({
1598
+ body: formData(clean({
1674
1599
  realm: spoke.trim(),
1675
1600
  login: login.trim(),
1676
1601
  password: password.trim(),
@@ -1679,7 +1604,7 @@ class Auth {
1679
1604
  }))
1680
1605
  }).then(async (resp) => {
1681
1606
  const data = await resp.json().catch(() => ({}));
1682
- if (!resp.ok || data["error"]) throw Object.assign(Yt(resp.status, data.error) || {}, data);
1607
+ if (!resp.ok || data["error"]) throw Object.assign(errorFromCode(resp.status, data.error) || {}, data);
1683
1608
  this.api.token = data["token"];
1684
1609
  return data;
1685
1610
  });
@@ -1692,7 +1617,7 @@ class Auth {
1692
1617
  loginGuest() {
1693
1618
  return fetch(`${this.api.url}guest`).then(async (resp) => {
1694
1619
  const data = await resp.json().catch(() => ({}));
1695
- if (!resp.ok || data["error"]) throw Object.assign(Yt(resp.status, data.error) || {}, data);
1620
+ if (!resp.ok || data["error"]) throw Object.assign(errorFromCode(resp.status, data.error) || {}, data);
1696
1621
  this.api.token = data["token"];
1697
1622
  return data;
1698
1623
  });
@@ -1784,11 +1709,11 @@ class Files {
1784
1709
  data.append("", file, file.name);
1785
1710
  return fetch(this.url, {
1786
1711
  method: "POST",
1787
- headers: ut({ "Authorization": this.api.token ? `Bearer ${this.api.token}` : "" }),
1712
+ headers: clean({ "Authorization": this.api.token ? `Bearer ${this.api.token}` : "" }),
1788
1713
  body: data
1789
1714
  }).then(async (resp) => {
1790
1715
  const data2 = await resp.json().catch(() => ({}));
1791
- if (!resp.ok || data2["error"]) throw Object.assign(Yt(resp.status, data2.error) || {}, data2);
1716
+ if (!resp.ok || data2["error"]) throw Object.assign(errorFromCode(resp.status, data2.error) || {}, data2);
1792
1717
  return resp;
1793
1718
  });
1794
1719
  })).then(async (files2) => {
@@ -2183,7 +2108,7 @@ class Slice {
2183
2108
  new Slice(this.slice, this.api).select().exec().rows().then((rows) => this.cache = rows);
2184
2109
  if (!this.unsubscribe) this.unsubscribe = this.api.socket.sliceEvents(this.slice, (event) => {
2185
2110
  const ids = [...event.data.new, ...event.data.changed];
2186
- 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]);
2111
+ new Slice(this.slice, this.api).select(ids).exec().rows().then((rows) => this.cache = [...this.cache.filter((c) => c.id != null && !ids.includes(c.id)), ...rows]);
2187
2112
  this.cache = this.cache.filter((v) => v.id && !event.data.lost.includes(v.id));
2188
2113
  });
2189
2114
  return this.cache$;
@@ -2336,7 +2261,7 @@ class Socket {
2336
2261
  */
2337
2262
  sliceEvents(slice, callback) {
2338
2263
  let cancelled = false;
2339
- ie(() => !this.connected).then(() => {
2264
+ sleepWhile(() => !this.connected).then(() => {
2340
2265
  if (!cancelled) this.send({ onSliceEvents: slice });
2341
2266
  });
2342
2267
  const unsubscribe = this.addListener((event) => {
@@ -2445,21 +2370,20 @@ class Api {
2445
2370
  /** Get session info from JWT payload */
2446
2371
  get jwtPayload() {
2447
2372
  if (!this.token) return null;
2448
- return Ht(this.token);
2373
+ return jwtDecode(this.token);
2449
2374
  }
2450
2375
  _request(req, options = {}) {
2451
2376
  return fetch(this.url, {
2452
2377
  method: "POST",
2453
- headers: ut({
2378
+ headers: clean({
2454
2379
  Authorization: this.token ? `Bearer ${this.token}` : void 0,
2455
2380
  "Content-Type": "application/json"
2456
2381
  }),
2457
2382
  body: JSON.stringify(Api.translateTokens(req))
2458
2383
  }).then(async (resp) => {
2459
- if (!resp.ok) throw Yt(resp.status);
2460
- let data = await resp.json();
2384
+ let data = JSONAttemptParse(await resp.text());
2385
+ if (!resp.ok || (data == null ? void 0 : data.error)) throw Object.assign(errorFromCode(resp.status, data.error), data);
2461
2386
  if (!options.raw) data = Api.translateTokens(data);
2462
- if (data["error"]) throw Object.assign(Yt(resp.status, data["error"]) || {}, data);
2463
2387
  return data;
2464
2388
  });
2465
2389
  }