@voltro/api-client 0.85.0 → 0.86.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/CHANGELOG.md +666 -0
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/dist/client.js +54 -0
- package/dist/errors.js +77 -0
- package/dist/index.js +9 -859
- package/dist/live.js +230 -0
- package/dist/manifest.js +17 -0
- package/dist/mock.js +91 -0
- package/dist/rest.js +240 -0
- package/dist/rowPatch.js +76 -0
- package/dist/wire.js +101 -0
- package/package.json +1 -1
package/dist/live.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { projectionOf as e } from "./manifest.js";
|
|
2
|
+
import { decodeWire as t, encodeWire as n, rpcCodec as r } from "./wire.js";
|
|
3
|
+
import { TransportError as i } from "./errors.js";
|
|
4
|
+
import { declaredErrorOf as a } from "./rest.js";
|
|
5
|
+
import { createRowPatchApplier as o } from "./rowPatch.js";
|
|
6
|
+
//#region src/live.ts
|
|
7
|
+
var s = (e) => {
|
|
8
|
+
let t = new URL(e);
|
|
9
|
+
return t.protocol = t.protocol === "https:" ? "wss:" : "ws:", t.pathname = "/ws", t.search = "", t.toString();
|
|
10
|
+
}, c = (e) => {
|
|
11
|
+
for (let t of e) if (t._tag === "Fail") return {
|
|
12
|
+
kind: "fail",
|
|
13
|
+
error: t.error
|
|
14
|
+
};
|
|
15
|
+
for (let t of e) if (t._tag === "Die") return {
|
|
16
|
+
kind: "die",
|
|
17
|
+
defect: t.defect
|
|
18
|
+
};
|
|
19
|
+
}, l = (l, u, d, f = {}) => {
|
|
20
|
+
let p = f.wsUrl ?? s(d), m = f.webSocketConstructor ?? ((e) => new globalThis.WebSocket(e)), h = f.pingIntervalMs ?? 1e4, g = f.reconnect?.initialMs ?? 500, _ = f.reconnect?.maxMs ?? 5e3, v = r(l.wire.models), y = /* @__PURE__ */ new Set(), b = /* @__PURE__ */ new Map(), x, S = !1, C = !1, w = !1, T = 0, E = 1, D, O, k = !0, A = (e) => {
|
|
21
|
+
x !== void 0 && C && x.send(JSON.stringify(e));
|
|
22
|
+
}, j = async () => {
|
|
23
|
+
let e = typeof f.token == "function" ? await f.token() : f.token, t = Object.entries(f.headers ?? {});
|
|
24
|
+
return e !== void 0 && t.push(["authorization", `Bearer ${e}`]), f.tenant !== void 0 && t.push(["x-tenant", f.tenant]), t;
|
|
25
|
+
}, M = (e, t) => {
|
|
26
|
+
if (typeof t == "object" && t && "_tag" in t && typeof t._tag == "string") {
|
|
27
|
+
let { _tag: n, ...r } = t;
|
|
28
|
+
if (u[n] !== void 0) return a(l, u, e.tag, n, r, {
|
|
29
|
+
status: 0,
|
|
30
|
+
requestId: void 0,
|
|
31
|
+
detail: typeof r.message == "string" ? r.message : void 0,
|
|
32
|
+
body: t
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
let n = typeof t == "object" && t && typeof t.message == "string" ? t.message : String(t);
|
|
36
|
+
return new i({
|
|
37
|
+
tag: e.tag,
|
|
38
|
+
reason: "network",
|
|
39
|
+
message: n,
|
|
40
|
+
cause: t
|
|
41
|
+
});
|
|
42
|
+
}, N = (e, t) => {
|
|
43
|
+
if (e.waiting !== void 0) {
|
|
44
|
+
let n = e.waiting;
|
|
45
|
+
e.waiting = void 0, n(t);
|
|
46
|
+
} else t.done || e.queue.push(t.value);
|
|
47
|
+
}, P = (e) => {
|
|
48
|
+
let n = l.wire.procedures[e.tag]?.output;
|
|
49
|
+
N(e, {
|
|
50
|
+
value: t(n, e.rawBase, v),
|
|
51
|
+
done: !1
|
|
52
|
+
});
|
|
53
|
+
}, F = (e, t) => {
|
|
54
|
+
e.failure = t, e.ended = !0, e.patchApplier?.reset(), y.delete(e), N(e, {
|
|
55
|
+
value: void 0,
|
|
56
|
+
done: !0
|
|
57
|
+
}), W();
|
|
58
|
+
}, I = (e) => {
|
|
59
|
+
e.ended = !0, e.patchApplier?.reset(), y.delete(e), N(e, {
|
|
60
|
+
value: void 0,
|
|
61
|
+
done: !0
|
|
62
|
+
}), W();
|
|
63
|
+
}, L = (e, t) => {
|
|
64
|
+
if (typeof t != "object" || !t) return;
|
|
65
|
+
let n = t;
|
|
66
|
+
n._tag === "snapshot" ? (e.patchApplier?.reset(), e.rawBase = n.data, typeof n.revision == "number" && (e.revision = n.revision), P(e)) : n._tag === "delta" ? (Array.isArray(e.rawBase) && (e.patchApplier ??= o(), e.rawBase = e.patchApplier.apply(e.rawBase, n.patch)), typeof n.revision == "number" && (e.revision = n.revision), e.rawBase !== void 0 && P(e)) : n._tag === "error" && F(e, M(e, n.error));
|
|
67
|
+
}, R = (e, t) => {
|
|
68
|
+
if (t._tag === "Success") {
|
|
69
|
+
I(e);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
let n = c(t.cause);
|
|
73
|
+
n === void 0 ? I(e) : F(e, n.kind === "fail" ? M(e, n.error) : new i({
|
|
74
|
+
tag: e.tag,
|
|
75
|
+
reason: "network",
|
|
76
|
+
message: `the subscription died on the server: ${String(n.defect?.message ?? n.defect)}`,
|
|
77
|
+
cause: n.defect
|
|
78
|
+
}));
|
|
79
|
+
}, z = async (e) => {
|
|
80
|
+
let t = await j();
|
|
81
|
+
e.revision >= 0 && t.push(["voltro-resume-from", String(e.revision)]);
|
|
82
|
+
let r = String(E++);
|
|
83
|
+
e.requestId = r, b.set(r, e);
|
|
84
|
+
let i = l.wire.procedures[e.tag]?.input;
|
|
85
|
+
A({
|
|
86
|
+
_tag: "Request",
|
|
87
|
+
id: r,
|
|
88
|
+
tag: e.tag,
|
|
89
|
+
payload: n(i, e.input ?? {}, v) ?? {},
|
|
90
|
+
headers: t
|
|
91
|
+
});
|
|
92
|
+
}, B = (e, t) => {
|
|
93
|
+
for (let n of [...y]) F(n, new i({
|
|
94
|
+
tag: n.tag,
|
|
95
|
+
reason: "network",
|
|
96
|
+
message: e,
|
|
97
|
+
cause: t
|
|
98
|
+
}));
|
|
99
|
+
}, V = (e) => {
|
|
100
|
+
let t;
|
|
101
|
+
try {
|
|
102
|
+
t = JSON.parse(typeof e == "string" ? e : String(e));
|
|
103
|
+
} catch {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
switch (t._tag) {
|
|
107
|
+
case "Chunk": {
|
|
108
|
+
let e = b.get(String(t.requestId));
|
|
109
|
+
if (e !== void 0 && Array.isArray(t.values)) for (let n of t.values) L(e, n);
|
|
110
|
+
A({
|
|
111
|
+
_tag: "Ack",
|
|
112
|
+
requestId: String(t.requestId)
|
|
113
|
+
});
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
case "Exit": {
|
|
117
|
+
let e = b.get(String(t.requestId));
|
|
118
|
+
b.delete(String(t.requestId)), e !== void 0 && R(e, t.exit);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
case "Pong":
|
|
122
|
+
k = !0;
|
|
123
|
+
return;
|
|
124
|
+
case "Defect":
|
|
125
|
+
B("the socket died on a defect", t.defect);
|
|
126
|
+
return;
|
|
127
|
+
case "ClientProtocolError":
|
|
128
|
+
B("the socket answered with a protocol error", t.error);
|
|
129
|
+
return;
|
|
130
|
+
default: return;
|
|
131
|
+
}
|
|
132
|
+
}, H = () => {
|
|
133
|
+
D !== void 0 && (clearInterval(D), D = void 0);
|
|
134
|
+
}, U = () => {
|
|
135
|
+
if (x !== void 0 || S || w) return;
|
|
136
|
+
S = !0, f.onStatus?.({
|
|
137
|
+
kind: "connecting",
|
|
138
|
+
attempt: T
|
|
139
|
+
});
|
|
140
|
+
let e = m(p);
|
|
141
|
+
x = e, e.onopen = () => {
|
|
142
|
+
S = !1, C = !0, T = 0, k = !0, f.onStatus?.({ kind: "open" }), H(), D = setInterval(() => {
|
|
143
|
+
k ? (k = !1, A({ _tag: "Ping" })) : e.close(4e3, "ping timeout");
|
|
144
|
+
}, h);
|
|
145
|
+
for (let e of y) z(e);
|
|
146
|
+
}, e.onmessage = (e) => {
|
|
147
|
+
V(e.data);
|
|
148
|
+
}, e.onerror = () => {}, e.onclose = () => {
|
|
149
|
+
if (x !== e) return;
|
|
150
|
+
x = void 0, S = !1, C = !1, H(), b.clear();
|
|
151
|
+
for (let e of y) e.requestId = void 0;
|
|
152
|
+
let t = !w && y.size > 0 ? Math.min(_, g * 1.5 ** T) : void 0;
|
|
153
|
+
f.onStatus?.({
|
|
154
|
+
kind: "closed",
|
|
155
|
+
reconnectInMs: t
|
|
156
|
+
}), t !== void 0 && (T += 1, O = setTimeout(() => {
|
|
157
|
+
O = void 0, U();
|
|
158
|
+
}, t));
|
|
159
|
+
};
|
|
160
|
+
}, W = () => {
|
|
161
|
+
if (y.size > 0 || x === void 0) return;
|
|
162
|
+
let e = x;
|
|
163
|
+
x = void 0, C = !1, H(), e.close(1e3, "no subscriptions"), f.onStatus?.({
|
|
164
|
+
kind: "closed",
|
|
165
|
+
reconnectInMs: void 0
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
subscribe: (t, n = {}) => ({ [Symbol.asyncIterator]: () => {
|
|
170
|
+
e(l, t);
|
|
171
|
+
let r = {
|
|
172
|
+
tag: t,
|
|
173
|
+
input: n,
|
|
174
|
+
requestId: void 0,
|
|
175
|
+
rawBase: void 0,
|
|
176
|
+
revision: -1,
|
|
177
|
+
ended: !1,
|
|
178
|
+
failure: void 0,
|
|
179
|
+
queue: [],
|
|
180
|
+
waiting: void 0
|
|
181
|
+
};
|
|
182
|
+
return y.add(r), C ? z(r) : U(), {
|
|
183
|
+
next: async () => {
|
|
184
|
+
if (r.queue.length > 0) return {
|
|
185
|
+
value: r.queue.shift(),
|
|
186
|
+
done: !1
|
|
187
|
+
};
|
|
188
|
+
if (r.failure !== void 0) {
|
|
189
|
+
let e = r.failure;
|
|
190
|
+
throw r.failure = void 0, e;
|
|
191
|
+
}
|
|
192
|
+
if (r.ended) return {
|
|
193
|
+
value: void 0,
|
|
194
|
+
done: !0
|
|
195
|
+
};
|
|
196
|
+
let e = await new Promise((e) => {
|
|
197
|
+
r.waiting = e;
|
|
198
|
+
});
|
|
199
|
+
if (e.done && r.failure !== void 0) {
|
|
200
|
+
let e = r.failure;
|
|
201
|
+
throw r.failure = void 0, e;
|
|
202
|
+
}
|
|
203
|
+
return e;
|
|
204
|
+
},
|
|
205
|
+
return: async () => (r.ended || (r.ended = !0, y.delete(r), r.requestId !== void 0 && (A({
|
|
206
|
+
_tag: "Interrupt",
|
|
207
|
+
requestId: r.requestId
|
|
208
|
+
}), b.delete(r.requestId)), N(r, {
|
|
209
|
+
value: void 0,
|
|
210
|
+
done: !0
|
|
211
|
+
}), W()), {
|
|
212
|
+
value: void 0,
|
|
213
|
+
done: !0
|
|
214
|
+
})
|
|
215
|
+
};
|
|
216
|
+
} }),
|
|
217
|
+
close: async () => {
|
|
218
|
+
w = !0, O !== void 0 && (clearTimeout(O), O = void 0);
|
|
219
|
+
for (let e of [...y]) I(e);
|
|
220
|
+
H();
|
|
221
|
+
let e = x;
|
|
222
|
+
x = void 0, C = !1, e !== void 0 && (e.close(1e3, "closed"), f.onStatus?.({
|
|
223
|
+
kind: "closed",
|
|
224
|
+
reconnectInMs: void 0
|
|
225
|
+
}));
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
//#endregion
|
|
230
|
+
export { l as openLiveQueries, s as wsUrlFromBase };
|
package/dist/manifest.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/manifest.ts
|
|
2
|
+
var e = (e) => {
|
|
3
|
+
let t = e.indexOf(".");
|
|
4
|
+
return t < 0 ? {
|
|
5
|
+
namespace: void 0,
|
|
6
|
+
method: e
|
|
7
|
+
} : {
|
|
8
|
+
namespace: e.slice(0, t),
|
|
9
|
+
method: e.slice(t + 1).split(".").map((e, t) => t === 0 ? e : e.charAt(0).toUpperCase() + e.slice(1)).join("")
|
|
10
|
+
};
|
|
11
|
+
}, t = (e, t) => {
|
|
12
|
+
let n = e.projections.find((e) => e.tag === t);
|
|
13
|
+
if (n === void 0) throw Error(`@voltro/api-client: no projection for tag '${t}' in the manifest (${e.projections.length} projection(s))`);
|
|
14
|
+
return n;
|
|
15
|
+
};
|
|
16
|
+
//#endregion
|
|
17
|
+
export { t as projectionOf, e as splitTag };
|
package/dist/mock.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { buildNamespaces as e } from "./client.js";
|
|
2
|
+
//#region src/mock.ts
|
|
3
|
+
var t = (e) => e instanceof Error || typeof e == "object" && !!e && "_tag" in e && typeof e._tag == "string" && e._tag.endsWith("Error"), n = (n, r = {}) => {
|
|
4
|
+
let i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = [], l = async (e, t) => {
|
|
5
|
+
c.push({
|
|
6
|
+
tag: e,
|
|
7
|
+
input: t
|
|
8
|
+
});
|
|
9
|
+
let n = a.get(e);
|
|
10
|
+
if (n !== void 0) throw n.times !== Infinity && (--n.times, n.times <= 0 && a.delete(e)), n.error;
|
|
11
|
+
return {
|
|
12
|
+
value: i.get(e),
|
|
13
|
+
nextCursor: void 0,
|
|
14
|
+
status: 200,
|
|
15
|
+
requestId: void 0,
|
|
16
|
+
etag: void 0,
|
|
17
|
+
fromCache: !1
|
|
18
|
+
};
|
|
19
|
+
}, u = {
|
|
20
|
+
manifest: n,
|
|
21
|
+
errors: r,
|
|
22
|
+
call: async (e, t = {}) => (await l(e, t)).value,
|
|
23
|
+
callRaw: (e, t = {}) => l(e, t),
|
|
24
|
+
pages: (e, n = {}) => ({ async *[Symbol.asyncIterator]() {
|
|
25
|
+
let r = o.get(e);
|
|
26
|
+
if (r === void 0) yield (await l(e, n)).value;
|
|
27
|
+
else {
|
|
28
|
+
c.push({
|
|
29
|
+
tag: e,
|
|
30
|
+
input: n
|
|
31
|
+
});
|
|
32
|
+
for (let e of r) {
|
|
33
|
+
if (t(e)) throw e;
|
|
34
|
+
yield e;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
} }),
|
|
38
|
+
subscribe: (e) => ({ [Symbol.asyncIterator]: () => {
|
|
39
|
+
let t = [], n, r = !1, a = (e) => {
|
|
40
|
+
if (n !== void 0) {
|
|
41
|
+
let t = n;
|
|
42
|
+
n = void 0, t({
|
|
43
|
+
value: e,
|
|
44
|
+
done: !1
|
|
45
|
+
});
|
|
46
|
+
} else t.push(e);
|
|
47
|
+
}, o = s.get(e) ?? /* @__PURE__ */ new Set();
|
|
48
|
+
return o.add(a), s.set(e, o), i.has(e) && a(i.get(e)), {
|
|
49
|
+
next: async () => t.length > 0 ? {
|
|
50
|
+
value: t.shift(),
|
|
51
|
+
done: !1
|
|
52
|
+
} : r ? {
|
|
53
|
+
value: void 0,
|
|
54
|
+
done: !0
|
|
55
|
+
} : new Promise((e) => {
|
|
56
|
+
n = e;
|
|
57
|
+
}),
|
|
58
|
+
return: async () => (r = !0, o.delete(a), n?.({
|
|
59
|
+
value: void 0,
|
|
60
|
+
done: !0
|
|
61
|
+
}), n = void 0, {
|
|
62
|
+
value: void 0,
|
|
63
|
+
done: !0
|
|
64
|
+
})
|
|
65
|
+
};
|
|
66
|
+
} }),
|
|
67
|
+
close: async () => {},
|
|
68
|
+
setData: (e, t) => {
|
|
69
|
+
i.set(e, t);
|
|
70
|
+
for (let n of s.get(e) ?? []) n(t);
|
|
71
|
+
},
|
|
72
|
+
setError: (e, t, n = Infinity) => {
|
|
73
|
+
a.set(e, {
|
|
74
|
+
error: t,
|
|
75
|
+
times: n
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
setPages: (e, t) => {
|
|
79
|
+
o.set(e, t);
|
|
80
|
+
},
|
|
81
|
+
push: (e, t) => {
|
|
82
|
+
for (let n of s.get(e) ?? []) n(t);
|
|
83
|
+
},
|
|
84
|
+
get calls() {
|
|
85
|
+
return c;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
return Object.assign(e(u), u);
|
|
89
|
+
};
|
|
90
|
+
//#endregion
|
|
91
|
+
export { n as createMockClient };
|
package/dist/rest.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { projectionOf as e } from "./manifest.js";
|
|
2
|
+
import { decodeWire as t, encodeWire as n } from "./wire.js";
|
|
3
|
+
import { ApiHttpError as r, InputEncodeError as i, ProfileMismatchError as a, RateLimitedError as o, TransportError as s } from "./errors.js";
|
|
4
|
+
//#region src/rest.ts
|
|
5
|
+
var c = (e = Date.now()) => {
|
|
6
|
+
let t = /* @__PURE__ */ new Uint8Array(16);
|
|
7
|
+
crypto.getRandomValues(t);
|
|
8
|
+
let n = BigInt(e);
|
|
9
|
+
t[0] = Number(n >> 40n & 255n), t[1] = Number(n >> 32n & 255n), t[2] = Number(n >> 24n & 255n), t[3] = Number(n >> 16n & 255n), t[4] = Number(n >> 8n & 255n), t[5] = Number(n & 255n), t[6] = t[6] & 15 | 112, t[8] = t[8] & 63 | 128;
|
|
10
|
+
let r = [...t].map((e) => e.toString(16).padStart(2, "0")).join("");
|
|
11
|
+
return `${r.slice(0, 8)}-${r.slice(8, 12)}-${r.slice(12, 16)}-${r.slice(16, 20)}-${r.slice(20)}`;
|
|
12
|
+
}, l = (e) => typeof e == "string" && e.startsWith("urn:voltro:error:") ? e.slice(17) : void 0, u = (e, t) => {
|
|
13
|
+
let n = e.get("retry-after");
|
|
14
|
+
if (n !== null) {
|
|
15
|
+
let e = Number(n);
|
|
16
|
+
if (Number.isFinite(e)) return Math.max(0, e * 1e3);
|
|
17
|
+
let r = Date.parse(n);
|
|
18
|
+
if (Number.isFinite(r)) return Math.max(0, r - t);
|
|
19
|
+
}
|
|
20
|
+
let r = e.get("ratelimit-reset"), i = r === null ? NaN : Number(r);
|
|
21
|
+
return Number.isFinite(i) ? Math.max(0, i * 1e3) : 1e3;
|
|
22
|
+
}, d = (e, t) => {
|
|
23
|
+
if (e !== null) for (let n of e.split(",")) {
|
|
24
|
+
let e = /<([^>]+)>\s*;\s*rel="?next"?/.exec(n.trim());
|
|
25
|
+
if (e === null) continue;
|
|
26
|
+
let r = e[1].split("?")[1] ?? "", i = new URLSearchParams(r).get(t);
|
|
27
|
+
if (i !== null) return i;
|
|
28
|
+
}
|
|
29
|
+
}, f = (e) => {
|
|
30
|
+
let t = new URLSearchParams();
|
|
31
|
+
for (let [n, r] of Object.entries(e)) if (r !== void 0) {
|
|
32
|
+
if (Array.isArray(r)) for (let e of r) t.append(n, typeof e == "object" && e ? JSON.stringify(e) : String(e));
|
|
33
|
+
else t.set(n, typeof r == "object" && r ? JSON.stringify(r) : String(r));
|
|
34
|
+
}
|
|
35
|
+
return t.toString();
|
|
36
|
+
}, p = (e, t, n) => {
|
|
37
|
+
if (typeof e != "object" || !e) return e;
|
|
38
|
+
let r = e, i = (e) => {
|
|
39
|
+
if (typeof e != "object" || !e) return e;
|
|
40
|
+
let { id: t, attributes: n } = e;
|
|
41
|
+
return {
|
|
42
|
+
...t === void 0 ? {} : { id: t },
|
|
43
|
+
...n ?? {}
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
return Array.isArray(r.data) ? {
|
|
47
|
+
items: r.data.map(i),
|
|
48
|
+
[t]: n ?? null
|
|
49
|
+
} : i(r.data);
|
|
50
|
+
}, m = (e) => {
|
|
51
|
+
let t = e.sunset === void 0 ? "" : ` — sunset ${e.sunset}`;
|
|
52
|
+
console.warn(`[@voltro/api-client] ${e.tag} (${e.path}) is deprecated${t}${e.deprecated === void 0 ? "" : `: ${e.deprecated}`}`);
|
|
53
|
+
}, h = (e) => new Promise((t) => {
|
|
54
|
+
setTimeout(t, e);
|
|
55
|
+
}), g = (e, n, i, a, o, s) => {
|
|
56
|
+
if (a !== void 0) {
|
|
57
|
+
let r = n[a], c = e.wire.procedures[i]?.errors?.[a];
|
|
58
|
+
if (r !== void 0) {
|
|
59
|
+
let n = t(c, o, {
|
|
60
|
+
dates: e.profile.dates,
|
|
61
|
+
naming: e.profile.naming,
|
|
62
|
+
models: e.wire.models
|
|
63
|
+
});
|
|
64
|
+
return new r(typeof n == "object" && n ? n : {}, {
|
|
65
|
+
status: s.status,
|
|
66
|
+
requestId: s.requestId,
|
|
67
|
+
detail: s.detail
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return new r({
|
|
72
|
+
tag: i,
|
|
73
|
+
status: s.status,
|
|
74
|
+
errorTag: a,
|
|
75
|
+
detail: s.detail,
|
|
76
|
+
body: s.body,
|
|
77
|
+
requestId: s.requestId
|
|
78
|
+
});
|
|
79
|
+
}, _ = (r, _, v = {}) => {
|
|
80
|
+
let y = r.profile, b = (v.baseUrl ?? r.servers[0]?.url ?? "").replace(/\/+$/, "");
|
|
81
|
+
if (b === "") throw Error("@voltro/api-client: no baseUrl — pass one, or generate the client with `--base-url` or `servers`");
|
|
82
|
+
let x = v.fetch ?? globalThis.fetch, S = v.clock ?? (() => Date.now()), C = v.sleep ?? h, w = v.etagCache ?? y.etag, T = /* @__PURE__ */ new Map(), E = /* @__PURE__ */ new Set(), D = v.onSunset ?? m, O = !1, k = v.userAgent ?? (r.name === void 0 ? void 0 : `${r.name}/${r.version ?? "0.0.0"} (${r.generatedBy})`), A = {
|
|
83
|
+
dates: y.dates,
|
|
84
|
+
naming: y.naming,
|
|
85
|
+
models: r.wire.models
|
|
86
|
+
}, j = async () => typeof v.token == "function" ? await v.token() : v.token, M = (e, t) => {
|
|
87
|
+
let a = r.wire.procedures[e.tag]?.input;
|
|
88
|
+
try {
|
|
89
|
+
let e = n(a, t ?? {}, A);
|
|
90
|
+
return typeof e == "object" && e ? e : {};
|
|
91
|
+
} catch (t) {
|
|
92
|
+
throw new i({
|
|
93
|
+
tag: e.tag,
|
|
94
|
+
message: String(t instanceof Error ? t.message.split("\n")[0] : t)
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}, N = (e, t, n, i) => {
|
|
98
|
+
let a = typeof n == "object" && n ? n : {}, o, s, c;
|
|
99
|
+
if (y.errors === "problem-details" && y.envelope !== "jsonapi") {
|
|
100
|
+
o = l(a.type);
|
|
101
|
+
let { type: e, title: t, status: n, detail: r, ...i } = a;
|
|
102
|
+
c = typeof r == "string" ? r : void 0, s = i;
|
|
103
|
+
} else if (y.envelope === "jsonapi") {
|
|
104
|
+
let e = Array.isArray(a.errors) ? a.errors[0] : void 0;
|
|
105
|
+
o = typeof e?.code == "string" ? e.code : void 0, c = typeof e?.detail == "string" ? e.detail : void 0, s = typeof e?.meta == "object" && e.meta !== null ? e.meta : {};
|
|
106
|
+
} else {
|
|
107
|
+
let { error: e, ...t } = a;
|
|
108
|
+
o = typeof e == "string" ? e : void 0, s = t, c = typeof e == "string" ? e : void 0;
|
|
109
|
+
}
|
|
110
|
+
return g(r, _, e.tag, o, s, {
|
|
111
|
+
status: t,
|
|
112
|
+
requestId: i,
|
|
113
|
+
detail: c,
|
|
114
|
+
body: n
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
manifest: r,
|
|
119
|
+
call: async (n, i, l = {}) => {
|
|
120
|
+
let m = e(r, n), h = M(m, i), g = m.path, _ = { ...h };
|
|
121
|
+
for (let e of m.pathParams) {
|
|
122
|
+
let t = _[e];
|
|
123
|
+
delete _[e], g = g.replace(`:${e}`, encodeURIComponent(t === void 0 ? "" : String(t)));
|
|
124
|
+
}
|
|
125
|
+
let P = m.method === "GET", F = P ? f(_) : "", I = `${b}${g}${F.length > 0 ? `?${F}` : ""}`, L = await j(), R = {
|
|
126
|
+
accept: y.envelope === "jsonapi" ? "application/vnd.api+json, application/json" : "application/json, application/problem+json",
|
|
127
|
+
...v.headers ?? {},
|
|
128
|
+
...l.headers ?? {}
|
|
129
|
+
};
|
|
130
|
+
L !== void 0 && (R.authorization = `Bearer ${L}`), v.tenant !== void 0 && (R["x-tenant"] = v.tenant), k !== void 0 && (R["user-agent"] = k);
|
|
131
|
+
let z = v.traceparent?.();
|
|
132
|
+
z !== void 0 && (R.traceparent = z), P || (R["content-type"] = "application/json"), !P && (m.idempotent === !0 || l.idempotencyKey !== void 0) && (R["idempotency-key"] = l.idempotencyKey ?? c(S()));
|
|
133
|
+
let B = P && w ? T.get(I) : void 0;
|
|
134
|
+
B !== void 0 && (R["if-none-match"] = B.etag);
|
|
135
|
+
let V = async (e) => {
|
|
136
|
+
let i;
|
|
137
|
+
try {
|
|
138
|
+
i = await x(I, {
|
|
139
|
+
method: m.method,
|
|
140
|
+
headers: R,
|
|
141
|
+
...P ? {} : { body: JSON.stringify(_) },
|
|
142
|
+
...l.signal === void 0 ? {} : { signal: l.signal }
|
|
143
|
+
});
|
|
144
|
+
} catch (e) {
|
|
145
|
+
throw new s({
|
|
146
|
+
tag: n,
|
|
147
|
+
reason: l.signal?.aborted ? "aborted" : "network",
|
|
148
|
+
message: String(e instanceof Error ? e.message : e),
|
|
149
|
+
cause: e
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
let c = i.headers.get("x-request-id") ?? void 0, f = i.headers.get("x-voltro-rest-profile");
|
|
153
|
+
if (!O && f !== null && (O = !0, f !== y.name)) throw new a({
|
|
154
|
+
expected: y.name,
|
|
155
|
+
actual: f
|
|
156
|
+
});
|
|
157
|
+
let h = i.headers.get("sunset"), g = i.headers.get("deprecation");
|
|
158
|
+
if ((h !== null || g !== null) && !E.has(n) && (E.add(n), D({
|
|
159
|
+
tag: n,
|
|
160
|
+
path: m.path,
|
|
161
|
+
sunset: h ?? m.sunset,
|
|
162
|
+
deprecated: m.deprecated
|
|
163
|
+
})), i.status === 304 && B !== void 0) return {
|
|
164
|
+
...B.result,
|
|
165
|
+
fromCache: !0,
|
|
166
|
+
requestId: c
|
|
167
|
+
};
|
|
168
|
+
let k;
|
|
169
|
+
try {
|
|
170
|
+
k = await i.text();
|
|
171
|
+
} catch (e) {
|
|
172
|
+
throw new s({
|
|
173
|
+
tag: n,
|
|
174
|
+
reason: "network",
|
|
175
|
+
message: "the body could not be read",
|
|
176
|
+
cause: e,
|
|
177
|
+
requestId: c
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
let j;
|
|
181
|
+
if (k.length > 0) try {
|
|
182
|
+
j = JSON.parse(k);
|
|
183
|
+
} catch {
|
|
184
|
+
throw new s({
|
|
185
|
+
tag: n,
|
|
186
|
+
reason: "decode",
|
|
187
|
+
message: `the answer was not JSON (${i.status})`,
|
|
188
|
+
requestId: c
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
if (i.status === 429) {
|
|
192
|
+
let t = u(i.headers, S()), r = Number(i.headers.get("ratelimit-limit") ?? NaN), a = Number(i.headers.get("ratelimit-remaining") ?? NaN), s = v.retry?.on429?.maxWaitMs;
|
|
193
|
+
if (s !== void 0 && e + t <= s) return await C(t), V(e + t);
|
|
194
|
+
throw new o({
|
|
195
|
+
tag: n,
|
|
196
|
+
retryAfterMs: t,
|
|
197
|
+
requestId: c,
|
|
198
|
+
...Number.isFinite(r) ? { limit: r } : {},
|
|
199
|
+
...Number.isFinite(a) ? { remaining: a } : {},
|
|
200
|
+
resetAtMs: S() + t
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
if (i.status >= 400) throw N(m, i.status, j, c);
|
|
204
|
+
let M, F = j;
|
|
205
|
+
if (y.pagination === "link-header" && (M = d(i.headers.get("link"), y.cursorParam)), y.envelope === "data" && typeof j == "object" && j && "data" in j) {
|
|
206
|
+
let e = j;
|
|
207
|
+
F = e.data, M === void 0 && typeof e.next == "string" && (M = e.next);
|
|
208
|
+
} else if (y.envelope === "jsonapi") {
|
|
209
|
+
let e = j?.links;
|
|
210
|
+
M === void 0 && typeof e?.next == "string" && (M = new URL(e.next, b).searchParams.get(y.cursorParam) ?? void 0), F = p(j, y.cursorField, M);
|
|
211
|
+
}
|
|
212
|
+
if (y.pagination === "body-cursor" && typeof F == "object" && F) {
|
|
213
|
+
let e = F[y.cursorField];
|
|
214
|
+
typeof e == "string" && (M = e);
|
|
215
|
+
}
|
|
216
|
+
let L = i.status === 204 || F === void 0 ? F : t(r.wire.procedures[n]?.output, F, A);
|
|
217
|
+
if (M === void 0 && typeof L == "object" && L) {
|
|
218
|
+
let e = L[y.cursorField];
|
|
219
|
+
typeof e == "string" && (M = e);
|
|
220
|
+
}
|
|
221
|
+
let z = i.headers.get("etag") ?? void 0, H = {
|
|
222
|
+
value: L,
|
|
223
|
+
nextCursor: M,
|
|
224
|
+
status: i.status,
|
|
225
|
+
requestId: c,
|
|
226
|
+
etag: z,
|
|
227
|
+
fromCache: !1
|
|
228
|
+
};
|
|
229
|
+
return P && w && z !== void 0 && T.set(I, {
|
|
230
|
+
etag: z,
|
|
231
|
+
result: H
|
|
232
|
+
}), H;
|
|
233
|
+
};
|
|
234
|
+
return V(0);
|
|
235
|
+
},
|
|
236
|
+
baseUrl: b
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
//#endregion
|
|
240
|
+
export { d as cursorFromLink, g as declaredErrorOf, _ as makeRestCore, c as uuidV7 };
|
package/dist/rowPatch.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//#region src/rowPatch.ts
|
|
2
|
+
var e = (e) => (e.startsWith("/") ? e.slice(1) : e).replace(/~1/g, "/").replace(/~0/g, "~"), t = (e, t, r = {}) => {
|
|
3
|
+
let i = /* @__PURE__ */ new Map();
|
|
4
|
+
for (let t of e) i.set(t.id, t);
|
|
5
|
+
return n(e, t, r, i);
|
|
6
|
+
}, n = (t, n, r, i, a) => {
|
|
7
|
+
let o = /* @__PURE__ */ new Map();
|
|
8
|
+
for (let t of n.ops) t.op === "mergeCells" && o.set(e(t.path), Object.keys(t.value));
|
|
9
|
+
for (let t of n.ops) if (t.op === "mergeCells") {
|
|
10
|
+
let n = e(t.path), a = i.get(n) ?? i.get(Number(n)), o = i.has(n) ? n : Number(n);
|
|
11
|
+
if (a === void 0) continue;
|
|
12
|
+
let s = { ...a };
|
|
13
|
+
for (let [e, n] of Object.entries(t.value)) s[e] = r.mergeCell === void 0 ? n : r.mergeCell(e, a[e], n);
|
|
14
|
+
i.set(o, s);
|
|
15
|
+
} else if (t.op === "add" && (n.order !== null || i.has(t.value.id)) && i.set(t.value.id, t.value), t.op === "replace") {
|
|
16
|
+
let e = i.get(t.value.id), r = (e === void 0 ? [] : o.get(String(t.value.id)) ?? []).filter((n) => !(n in t.value) && n in e);
|
|
17
|
+
(n.order !== null || e !== void 0) && i.set(t.value.id, r.length === 0 ? t.value : {
|
|
18
|
+
...t.value,
|
|
19
|
+
...Object.fromEntries(r.map((t) => [t, e[t]]))
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
if (n.order === null && a !== void 0) {
|
|
23
|
+
let r = t.slice();
|
|
24
|
+
for (let t of n.ops) {
|
|
25
|
+
if (t.op === "remove") continue;
|
|
26
|
+
let n = t.op === "mergeCells" ? e(t.path) : void 0, o = t.op === "mergeCells" ? i.has(n) ? n : Number(n) : t.value.id, s = a.get(o), c = i.get(o);
|
|
27
|
+
s !== void 0 && c !== void 0 && (r[s] = c);
|
|
28
|
+
}
|
|
29
|
+
return r;
|
|
30
|
+
}
|
|
31
|
+
let s = [];
|
|
32
|
+
if (n.order === null) for (let e of t) {
|
|
33
|
+
let t = i.get(e.id);
|
|
34
|
+
t !== void 0 && s.push(t);
|
|
35
|
+
}
|
|
36
|
+
else for (let e of n.order) {
|
|
37
|
+
let t = i.get(e);
|
|
38
|
+
t !== void 0 && s.push(t);
|
|
39
|
+
}
|
|
40
|
+
return s;
|
|
41
|
+
}, r = () => {
|
|
42
|
+
let e, t, r, i = () => {
|
|
43
|
+
e = void 0, t = void 0, r = void 0;
|
|
44
|
+
}, a = (e) => {
|
|
45
|
+
let n = /* @__PURE__ */ new Map(), i = Object.getPrototypeOf(e) === Array.prototype;
|
|
46
|
+
for (let t = 0; t < e.length; t++) {
|
|
47
|
+
let r = e[t];
|
|
48
|
+
n.has(r.id) && (i = !1), n.set(r.id, t);
|
|
49
|
+
}
|
|
50
|
+
r = i ? n : void 0, t = i ? void 0 : new Map(e.map((e) => [e.id, e]));
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
reset: i,
|
|
54
|
+
apply: (o, s, c = {}) => {
|
|
55
|
+
(o !== e || r === void 0 && t === void 0) && a(o);
|
|
56
|
+
try {
|
|
57
|
+
let i = r, l = /* @__PURE__ */ new Map(), u = n(o, s, c, i === void 0 ? t : {
|
|
58
|
+
get: (e) => {
|
|
59
|
+
if (l.has(e)) return l.get(e);
|
|
60
|
+
let t = i.get(e);
|
|
61
|
+
return t === void 0 ? void 0 : o[t];
|
|
62
|
+
},
|
|
63
|
+
has: (e) => l.has(e) || i.has(e),
|
|
64
|
+
set: (e, t) => {
|
|
65
|
+
l.set(e, t);
|
|
66
|
+
}
|
|
67
|
+
}, i);
|
|
68
|
+
return (s.order !== null || s.ops.some((e) => e.op === "mergeCells" && "id" in e.value)) && a(u), e = u, u;
|
|
69
|
+
} catch (e) {
|
|
70
|
+
throw i(), e;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
//#endregion
|
|
76
|
+
export { t as applyRowPatch, r as createRowPatchApplier, e as pathToId };
|