kosmio 0.1.2 → 0.1.3

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.
@@ -1,275 +0,0 @@
1
- let K = "kosmio:offline-write-queue:v1";
2
- const g = /* @__PURE__ */ new Map(), q = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Set(), R = /* @__PURE__ */ new Map(), W = /* @__PURE__ */ new Set([429, 500, 502, 503, 504]);
3
- let A = !1, d = !1, y = !1, c = null, S = 1500, T = async () => "anonymous:no-tenant", v = null, h = null;
4
- function L() {
5
- return (/* @__PURE__ */ new Date()).toISOString();
6
- }
7
- function N() {
8
- return typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : `${Date.now()}-${Math.random().toString(16).slice(2)}`;
9
- }
10
- function b(e) {
11
- return encodeURIComponent(String(e ?? "").trim() || "default");
12
- }
13
- function m(e) {
14
- return String(e ?? "").trim().toUpperCase();
15
- }
16
- function k(e) {
17
- if (!e) return;
18
- const n = typeof e.toJSON == "function" ? e.toJSON() : e;
19
- if (!n || typeof n != "object") return;
20
- const t = {};
21
- for (const [r, u] of Object.entries(n))
22
- (u == null || ["string", "number", "boolean"].includes(typeof u)) && (t[r] = u);
23
- return Object.keys(t).length ? t : void 0;
24
- }
25
- function B(e = {}) {
26
- const n = {}, t = k(e.headers);
27
- t && (n.headers = t);
28
- for (const r of ["timeout", "withCredentials", "responseType", "skipAuthRefresh"])
29
- e[r] !== void 0 && (n[r] = e[r]);
30
- return n;
31
- }
32
- function D(e) {
33
- return ["POST", "PUT", "PATCH"].includes(m(e));
34
- }
35
- function H(e) {
36
- return e?.offlineQueue === !1;
37
- }
38
- function E(e) {
39
- return e ? e.response ? !1 : typeof navigator < "u" && navigator.onLine === !1 ? !0 : ["ERR_NETWORK", "ECONNABORTED"].includes(String(e.code ?? "")) : !1;
40
- }
41
- function $(e) {
42
- return W.has(Number(e));
43
- }
44
- function _(e, n) {
45
- return `${K}:${b(e)}:${b(n)}`;
46
- }
47
- function C(e) {
48
- const n = q.get(e);
49
- return Array.isArray(n) ? n : [];
50
- }
51
- function x(e, n) {
52
- q.set(e, Array.isArray(n) ? n : []);
53
- }
54
- function Q(e) {
55
- if (typeof window > "u") return C(e);
56
- try {
57
- const n = window.localStorage.getItem(e);
58
- if (!n) return [];
59
- const t = JSON.parse(n);
60
- return Array.isArray(t) ? t : [];
61
- } catch {
62
- return C(e);
63
- }
64
- }
65
- function f(e, n) {
66
- const t = Array.isArray(n) ? n : [];
67
- if (x(e, t), a(), typeof window > "u") return !0;
68
- try {
69
- return window.localStorage.setItem(e, JSON.stringify(t)), !0;
70
- } catch {
71
- return !1;
72
- }
73
- }
74
- function j(e) {
75
- const n = String(e?.url ?? ""), t = m(e?.method);
76
- if (!["POST", "PUT", "PATCH"].includes(t)) return null;
77
- const r = n.match(/^(.*)\/(archive|unarchive)\/?$/i);
78
- return r ? { archiveResourceKey: r[1], archiveAction: r[2].toLowerCase() } : null;
79
- }
80
- function z({ clientKey: e, scopeKey: n, requestConfig: t, reason: r }) {
81
- const u = j(t);
82
- return {
83
- id: N(),
84
- clientKey: e,
85
- scopeKey: n,
86
- createdAt: L(),
87
- retryCount: 0,
88
- reason: r,
89
- request: {
90
- method: m(t.method),
91
- url: t.url,
92
- data: t.data,
93
- config: B(t)
94
- },
95
- archiveAction: u?.archiveAction ?? null,
96
- archiveResourceKey: u?.archiveResourceKey ?? null
97
- };
98
- }
99
- function J(e, n) {
100
- const t = Array.isArray(e) ? [...e] : [];
101
- if (!n?.archiveResourceKey || !n?.archiveAction)
102
- return t.push(n), { queue: t, queued: !0, canceled: !1 };
103
- const u = t.filter((i) => i?.archiveResourceKey === n.archiveResourceKey).some(
104
- (i) => i?.archiveAction && i.archiveAction !== n.archiveAction
105
- ), o = t.filter((i) => i?.archiveResourceKey !== n.archiveResourceKey);
106
- return u ? { queue: o, queued: !1, canceled: !0 } : (o.push(n), { queue: o, queued: !0, canceled: !1 });
107
- }
108
- async function I() {
109
- try {
110
- return String(await T() || "anonymous:no-tenant");
111
- } catch {
112
- return "anonymous:no-tenant";
113
- }
114
- }
115
- function w(e) {
116
- return e?.queueKey ?? e?.ax?.defaults?.baseURL ?? "default";
117
- }
118
- function O(e, n) {
119
- return _(w(e), n);
120
- }
121
- function F(e) {
122
- if (!e) return 0;
123
- let n = 0;
124
- for (const t of g.values())
125
- n += Q(O(t, e)).length;
126
- return n;
127
- }
128
- function Y() {
129
- return typeof navigator > "u" ? !0 : navigator.onLine !== !1;
130
- }
131
- function a() {
132
- for (const e of p)
133
- try {
134
- e();
135
- } catch {
136
- }
137
- }
138
- function V(e) {
139
- const n = String(e ?? "anonymous:no-tenant"), t = F(n), r = {
140
- isOnline: Y(),
141
- isSyncing: d,
142
- pendingSyncCount: t,
143
- hasPendingSync: t > 0
144
- }, u = R.get(n);
145
- return u && u.isOnline === r.isOnline && u.isSyncing === r.isSyncing && u.pendingSyncCount === r.pendingSyncCount && u.hasPendingSync === r.hasPendingSync ? u : (R.set(n, r), r);
146
- }
147
- function G() {
148
- if (c) return;
149
- c = setTimeout(() => {
150
- c = null, s();
151
- }, S), S = Math.min(S * 2, 6e4), a();
152
- }
153
- function X() {
154
- S = 1500, c && (clearTimeout(c), c = null);
155
- }
156
- async function Z(e, n) {
157
- const t = O(e, n), r = Q(t);
158
- if (!r.length) return { applied: 0, retryScheduled: !1 };
159
- let u = 0;
160
- const o = [...r];
161
- for (; o.length > 0; ) {
162
- const i = o[0];
163
- try {
164
- if (!P(i.request)) {
165
- o.shift(), f(t, o);
166
- continue;
167
- }
168
- await e.ax.request({
169
- ...i.request.config,
170
- method: i.request.method,
171
- url: i.request.url,
172
- data: i.request.data
173
- }), o.shift(), u += 1, f(t, o);
174
- } catch (l) {
175
- if (E(l) || $(l?.response?.status))
176
- return i.retryCount = (i.retryCount ?? 0) + 1, f(t, o), G(), { applied: u, retryScheduled: !0 };
177
- o.shift(), f(t, o);
178
- }
179
- }
180
- return f(t, o), { applied: u, retryScheduled: !1 };
181
- }
182
- async function ee() {
183
- const e = await I();
184
- let n = 0;
185
- for (const t of g.values()) {
186
- const r = await Z(t, e);
187
- if (n += r.applied, r.retryScheduled) break;
188
- }
189
- return n > 0 && typeof v == "function" && v(), a(), n;
190
- }
191
- async function ne() {
192
- if (d) {
193
- y = !0;
194
- return;
195
- }
196
- d = !0, a();
197
- try {
198
- do
199
- y = !1, await ee() > 0 && X();
200
- while (y);
201
- } finally {
202
- d = !1, a();
203
- }
204
- }
205
- function te() {
206
- a(), s();
207
- }
208
- function re() {
209
- a();
210
- }
211
- function ue() {
212
- typeof document < "u" && document.visibilityState === "visible" && s();
213
- }
214
- function ie(e) {
215
- e?.isActive && s();
216
- }
217
- function oe(e = {}) {
218
- typeof e.storagePrefix == "string" && e.storagePrefix.trim() && (K = e.storagePrefix.trim()), typeof e.resolveScopeKey == "function" && (T = e.resolveScopeKey), typeof e.invalidateQueries == "function" && (v = e.invalidateQueries), e.appStateAdapter !== void 0 && (h = e.appStateAdapter);
219
- }
220
- function ae(e) {
221
- if (!e) return e;
222
- const n = w(e);
223
- return e.queueKey = n, g.set(n, e), a(), e;
224
- }
225
- function fe(e) {
226
- return ["queued", "canceled"].includes(String(e?.offlineQueueStatus ?? ""));
227
- }
228
- function ce(e) {
229
- return String(e?.offlineQueueStatus ?? "") === "queued";
230
- }
231
- function se(e) {
232
- return String(e?.offlineQueueStatus ?? "") === "canceled";
233
- }
234
- function P(e) {
235
- return D(e?.method) && !H(e);
236
- }
237
- function le({ userId: e, tenantId: n } = {}) {
238
- const t = String(e ?? "").trim() || "anonymous", r = String(n ?? "").trim() || "no-tenant";
239
- return `${t}:${r}`;
240
- }
241
- function de(e) {
242
- return V(e);
243
- }
244
- function ye(e) {
245
- return typeof e != "function" ? () => {
246
- } : (p.add(e), () => p.delete(e));
247
- }
248
- async function Se(e, n, t = "offline") {
249
- const r = await I(), u = O(e, r), o = Q(u), i = z({ clientKey: w(e), scopeKey: r, requestConfig: n, reason: t }), { queue: l, queued: M, canceled: U } = J(o, i);
250
- return f(u, l), s(), U ? { offlineQueueStatus: "canceled", offlineQueueReason: "archive_canceled", offlineQueueScope: r } : M ? { offlineQueueStatus: "queued", offlineQueueId: i.id, offlineQueueScope: r } : { offlineQueueStatus: "canceled", offlineQueueReason: "deduped", offlineQueueScope: r };
251
- }
252
- function he(e, n) {
253
- return P(n) ? typeof navigator < "u" && navigator.onLine === !1 ? !0 : E(e) : !1;
254
- }
255
- function s() {
256
- y = !0, ne();
257
- }
258
- function pe() {
259
- A || typeof window > "u" || (A = !0, window.addEventListener("online", te), window.addEventListener("offline", re), typeof document < "u" && document.addEventListener("visibilitychange", ue), h && typeof h.addListener == "function" && h.addListener("appStateChange", ie), s());
260
- }
261
- export {
262
- de as a,
263
- fe as b,
264
- oe as c,
265
- ce as d,
266
- Se as e,
267
- P as f,
268
- le as g,
269
- s as h,
270
- se as i,
271
- pe as j,
272
- ye as k,
273
- ae as r,
274
- he as s
275
- };