@mitumba/sdk 5.24.0 → 5.26.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.d.mts +141 -5
- package/dist/index.d.ts +141 -5
- package/dist/index.js +365 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +365 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1882,13 +1882,344 @@ var MessagesModule = class {
|
|
|
1882
1882
|
}
|
|
1883
1883
|
};
|
|
1884
1884
|
|
|
1885
|
+
// src/realtime/notification-stream.ts
|
|
1886
|
+
var TICKET_PATH = "/notify/notifications/stream/tickets";
|
|
1887
|
+
var CLOSE_EXPIRED = 4001;
|
|
1888
|
+
var CLOSE_FINAL = [4003, 4004];
|
|
1889
|
+
function isRecord3(value) {
|
|
1890
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
1891
|
+
}
|
|
1892
|
+
var isCount = (value) => Number.isSafeInteger(value) && value >= 0;
|
|
1893
|
+
function isItem(value) {
|
|
1894
|
+
return isRecord3(value) && typeof value.id === "string" && typeof value.type === "string" && typeof value.category === "string" && typeof value.title === "string" && typeof value.body === "string" && Array.isArray(value.actions) && typeof value.createdAt === "string" && (value.readAt === null || typeof value.readAt === "string") && (value.flash === null || isRecord3(value.flash));
|
|
1895
|
+
}
|
|
1896
|
+
function parseNotificationFrame(value) {
|
|
1897
|
+
if (!isRecord3(value) || typeof value.type !== "string") throw new RealtimeProtocolError();
|
|
1898
|
+
switch (value.type) {
|
|
1899
|
+
case "hello":
|
|
1900
|
+
if (isCount(value.unread) && (value.flash === null || isItem(value.flash))) return value;
|
|
1901
|
+
break;
|
|
1902
|
+
case "item":
|
|
1903
|
+
if (isCount(value.unread) && isItem(value.item)) return value;
|
|
1904
|
+
break;
|
|
1905
|
+
case "read":
|
|
1906
|
+
if (isCount(value.unread) && (value.ids === "all" || Array.isArray(value.ids) && value.ids.every((id) => typeof id === "string"))) {
|
|
1907
|
+
return value;
|
|
1908
|
+
}
|
|
1909
|
+
break;
|
|
1910
|
+
case "flash":
|
|
1911
|
+
if (isItem(value.flash) && value.flash.flash !== null) return value;
|
|
1912
|
+
break;
|
|
1913
|
+
case "flash_resolved":
|
|
1914
|
+
if (typeof value.id === "string" && isCount(value.unread)) return value;
|
|
1915
|
+
break;
|
|
1916
|
+
case "pong":
|
|
1917
|
+
return { type: "pong" };
|
|
1918
|
+
}
|
|
1919
|
+
throw new RealtimeProtocolError();
|
|
1920
|
+
}
|
|
1921
|
+
function parseTicket2(value) {
|
|
1922
|
+
if (!isRecord3(value) || typeof value.ticket !== "string" || typeof value.expires_at !== "string" || typeof value.websocket_url !== "string" || typeof value.socket_closes_at !== "string") {
|
|
1923
|
+
throw new RealtimeProtocolError();
|
|
1924
|
+
}
|
|
1925
|
+
let url;
|
|
1926
|
+
try {
|
|
1927
|
+
url = new URL(value.websocket_url);
|
|
1928
|
+
} catch {
|
|
1929
|
+
throw new RealtimeProtocolError();
|
|
1930
|
+
}
|
|
1931
|
+
if (!["ws:", "wss:"].includes(url.protocol) || url.searchParams.has("token") || !url.searchParams.has("ticket")) throw new RealtimeProtocolError();
|
|
1932
|
+
return value;
|
|
1933
|
+
}
|
|
1934
|
+
var ManagedNotificationStream = class {
|
|
1935
|
+
constructor(client, options = {}) {
|
|
1936
|
+
this.client = client;
|
|
1937
|
+
this.options = options;
|
|
1938
|
+
}
|
|
1939
|
+
currentState = "closed";
|
|
1940
|
+
currentUnread = null;
|
|
1941
|
+
socket = null;
|
|
1942
|
+
reconnectTimer = null;
|
|
1943
|
+
frameListeners = /* @__PURE__ */ new Set();
|
|
1944
|
+
stateListeners = /* @__PURE__ */ new Set();
|
|
1945
|
+
explicitClose = false;
|
|
1946
|
+
reconnectAttempt = 0;
|
|
1947
|
+
startPromise = null;
|
|
1948
|
+
resolveStart = null;
|
|
1949
|
+
rejectStart = null;
|
|
1950
|
+
onMessage = (event) => {
|
|
1951
|
+
this.handleMessage(event);
|
|
1952
|
+
};
|
|
1953
|
+
onClose = (event) => {
|
|
1954
|
+
this.handleClose(event);
|
|
1955
|
+
};
|
|
1956
|
+
onIgnored = () => void 0;
|
|
1957
|
+
get state() {
|
|
1958
|
+
return this.currentState;
|
|
1959
|
+
}
|
|
1960
|
+
get unread() {
|
|
1961
|
+
return this.currentUnread;
|
|
1962
|
+
}
|
|
1963
|
+
onFrame(listener) {
|
|
1964
|
+
this.frameListeners.add(listener);
|
|
1965
|
+
return () => {
|
|
1966
|
+
this.frameListeners.delete(listener);
|
|
1967
|
+
};
|
|
1968
|
+
}
|
|
1969
|
+
onStateChange(listener) {
|
|
1970
|
+
this.stateListeners.add(listener);
|
|
1971
|
+
return () => {
|
|
1972
|
+
this.stateListeners.delete(listener);
|
|
1973
|
+
};
|
|
1974
|
+
}
|
|
1975
|
+
start() {
|
|
1976
|
+
if (this.explicitClose) return Promise.reject(new RealtimeUnavailableError());
|
|
1977
|
+
if (this.currentState === "open") return Promise.resolve();
|
|
1978
|
+
if (this.startPromise) return this.startPromise;
|
|
1979
|
+
this.startPromise = new Promise((resolve, reject) => {
|
|
1980
|
+
this.resolveStart = resolve;
|
|
1981
|
+
this.rejectStart = reject;
|
|
1982
|
+
});
|
|
1983
|
+
const pending = this.startPromise;
|
|
1984
|
+
void this.open(false).catch((error) => this.openFailed(error));
|
|
1985
|
+
return pending;
|
|
1986
|
+
}
|
|
1987
|
+
close() {
|
|
1988
|
+
this.explicitClose = true;
|
|
1989
|
+
this.clearReconnect();
|
|
1990
|
+
this.detachSocket(true);
|
|
1991
|
+
this.transition("closed");
|
|
1992
|
+
this.resolveStart?.();
|
|
1993
|
+
this.finishStart();
|
|
1994
|
+
this.frameListeners.clear();
|
|
1995
|
+
this.stateListeners.clear();
|
|
1996
|
+
}
|
|
1997
|
+
async open(reconnecting) {
|
|
1998
|
+
this.transition(reconnecting ? "reconnecting" : "connecting");
|
|
1999
|
+
const raw = await this.client.post(TICKET_PATH, {}, { signal: this.options.signal });
|
|
2000
|
+
if (this.explicitClose) return;
|
|
2001
|
+
const ticket = parseTicket2(raw);
|
|
2002
|
+
const url = new URL(ticket.websocket_url);
|
|
2003
|
+
url.searchParams.set("surface", this.options.surface ?? "web");
|
|
2004
|
+
const socket = this.client.createWebSocket(url.toString(), []);
|
|
2005
|
+
this.socket = socket;
|
|
2006
|
+
socket.addEventListener("open", this.onIgnored);
|
|
2007
|
+
socket.addEventListener("message", this.onMessage);
|
|
2008
|
+
socket.addEventListener("close", this.onClose);
|
|
2009
|
+
socket.addEventListener("error", this.onIgnored);
|
|
2010
|
+
}
|
|
2011
|
+
/** A ticket that cannot be had because of who is asking is final; anything else is retried. */
|
|
2012
|
+
openFailed(error) {
|
|
2013
|
+
if (error instanceof RealtimeProtocolError || error instanceof RealtimeUnavailableError || error instanceof APIError && [401, 403].includes(error.status)) {
|
|
2014
|
+
this.fail(error);
|
|
2015
|
+
} else {
|
|
2016
|
+
this.scheduleReconnect();
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
handleMessage(event) {
|
|
2020
|
+
let frame;
|
|
2021
|
+
try {
|
|
2022
|
+
if (typeof event.data !== "string") throw new RealtimeProtocolError();
|
|
2023
|
+
let decoded;
|
|
2024
|
+
try {
|
|
2025
|
+
decoded = JSON.parse(event.data);
|
|
2026
|
+
} catch {
|
|
2027
|
+
throw new RealtimeProtocolError();
|
|
2028
|
+
}
|
|
2029
|
+
frame = parseNotificationFrame(decoded);
|
|
2030
|
+
} catch (error) {
|
|
2031
|
+
this.fail(error);
|
|
2032
|
+
try {
|
|
2033
|
+
this.socket?.close(4004, "protocol_error");
|
|
2034
|
+
} catch {
|
|
2035
|
+
}
|
|
2036
|
+
return;
|
|
2037
|
+
}
|
|
2038
|
+
if (frame.type === "pong") return;
|
|
2039
|
+
if ("unread" in frame) this.currentUnread = frame.unread;
|
|
2040
|
+
if (frame.type === "hello") {
|
|
2041
|
+
this.reconnectAttempt = 0;
|
|
2042
|
+
this.transition("open");
|
|
2043
|
+
this.resolveStart?.();
|
|
2044
|
+
this.finishStart();
|
|
2045
|
+
}
|
|
2046
|
+
for (const listener of [...this.frameListeners]) {
|
|
2047
|
+
try {
|
|
2048
|
+
listener(frame);
|
|
2049
|
+
} catch {
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
handleClose(event) {
|
|
2054
|
+
this.detachSocket(false);
|
|
2055
|
+
if (this.explicitClose || this.currentState === "failed") return;
|
|
2056
|
+
if (CLOSE_FINAL.includes(event.code)) {
|
|
2057
|
+
this.fail(new RealtimeUnavailableError());
|
|
2058
|
+
return;
|
|
2059
|
+
}
|
|
2060
|
+
if (event.code === CLOSE_EXPIRED) this.reconnectAttempt = 0;
|
|
2061
|
+
this.scheduleReconnect();
|
|
2062
|
+
}
|
|
2063
|
+
scheduleReconnect() {
|
|
2064
|
+
if (this.reconnectTimer || this.explicitClose) return;
|
|
2065
|
+
this.transition("reconnecting");
|
|
2066
|
+
const base = Math.max(10, this.options.reconnectBaseDelayMs ?? 500);
|
|
2067
|
+
const maximum = Math.max(base, this.options.reconnectMaxDelayMs ?? 3e4);
|
|
2068
|
+
const cap = Math.min(maximum, base * 2 ** this.reconnectAttempt);
|
|
2069
|
+
const delay = Math.floor((this.options.random ?? Math.random)() * cap);
|
|
2070
|
+
this.reconnectAttempt += 1;
|
|
2071
|
+
this.reconnectTimer = setTimeout(() => {
|
|
2072
|
+
this.reconnectTimer = null;
|
|
2073
|
+
void this.open(true).catch((error) => this.openFailed(error));
|
|
2074
|
+
}, delay);
|
|
2075
|
+
}
|
|
2076
|
+
clearReconnect() {
|
|
2077
|
+
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
2078
|
+
this.reconnectTimer = null;
|
|
2079
|
+
}
|
|
2080
|
+
detachSocket(close) {
|
|
2081
|
+
const socket = this.socket;
|
|
2082
|
+
if (!socket) return;
|
|
2083
|
+
socket.removeEventListener("open", this.onIgnored);
|
|
2084
|
+
socket.removeEventListener("message", this.onMessage);
|
|
2085
|
+
socket.removeEventListener("close", this.onClose);
|
|
2086
|
+
socket.removeEventListener("error", this.onIgnored);
|
|
2087
|
+
if (close) {
|
|
2088
|
+
try {
|
|
2089
|
+
socket.close(1e3, "client_close");
|
|
2090
|
+
} catch {
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
this.socket = null;
|
|
2094
|
+
}
|
|
2095
|
+
transition(state) {
|
|
2096
|
+
if (this.currentState === state) return;
|
|
2097
|
+
this.currentState = state;
|
|
2098
|
+
for (const listener of [...this.stateListeners]) listener(state);
|
|
2099
|
+
}
|
|
2100
|
+
fail(error) {
|
|
2101
|
+
this.clearReconnect();
|
|
2102
|
+
this.transition("failed");
|
|
2103
|
+
this.rejectStart?.(error);
|
|
2104
|
+
this.finishStart();
|
|
2105
|
+
}
|
|
2106
|
+
finishStart() {
|
|
2107
|
+
this.resolveStart = null;
|
|
2108
|
+
this.rejectStart = null;
|
|
2109
|
+
this.startPromise = null;
|
|
2110
|
+
}
|
|
2111
|
+
};
|
|
2112
|
+
|
|
2113
|
+
// src/realtime/live-unread.ts
|
|
2114
|
+
var POLL_MS = 6e4;
|
|
2115
|
+
var STREAM_RETRY_MS = 10 * 6e4;
|
|
2116
|
+
function browserResume(listener) {
|
|
2117
|
+
const target = globalThis;
|
|
2118
|
+
if (!target.addEventListener || !target.document?.addEventListener) return () => void 0;
|
|
2119
|
+
const onVisible = () => {
|
|
2120
|
+
if (target.document?.visibilityState === "visible") listener();
|
|
2121
|
+
};
|
|
2122
|
+
target.addEventListener("focus", listener);
|
|
2123
|
+
target.document.addEventListener("visibilitychange", onVisible);
|
|
2124
|
+
return () => {
|
|
2125
|
+
target.removeEventListener?.("focus", listener);
|
|
2126
|
+
target.document?.removeEventListener?.("visibilitychange", onVisible);
|
|
2127
|
+
};
|
|
2128
|
+
}
|
|
2129
|
+
function createLiveUnread(deps) {
|
|
2130
|
+
let count = 0;
|
|
2131
|
+
let signedIn = false;
|
|
2132
|
+
let streamState = "closed";
|
|
2133
|
+
let stream = null;
|
|
2134
|
+
let timer = null;
|
|
2135
|
+
let offResume = null;
|
|
2136
|
+
let streamRetryAt = 0;
|
|
2137
|
+
const now = deps.now ?? Date.now;
|
|
2138
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
2139
|
+
const set = (next) => {
|
|
2140
|
+
if (next === count) return;
|
|
2141
|
+
count = next;
|
|
2142
|
+
for (const listener of [...listeners]) listener(count);
|
|
2143
|
+
};
|
|
2144
|
+
const read = async () => {
|
|
2145
|
+
try {
|
|
2146
|
+
set((await deps.unread()).unread_count);
|
|
2147
|
+
signedIn = true;
|
|
2148
|
+
} catch {
|
|
2149
|
+
signedIn = false;
|
|
2150
|
+
set(0);
|
|
2151
|
+
}
|
|
2152
|
+
return signedIn;
|
|
2153
|
+
};
|
|
2154
|
+
const startStream = () => {
|
|
2155
|
+
if (stream || !signedIn || now() < streamRetryAt) return;
|
|
2156
|
+
const current = deps.stream({ surface: deps.surface ?? "web" });
|
|
2157
|
+
stream = current;
|
|
2158
|
+
current.onFrame((frame) => {
|
|
2159
|
+
if ("unread" in frame) set(frame.unread);
|
|
2160
|
+
});
|
|
2161
|
+
current.onStateChange((state) => {
|
|
2162
|
+
streamState = state;
|
|
2163
|
+
if (state === "failed" && stream === current) {
|
|
2164
|
+
streamRetryAt = now() + STREAM_RETRY_MS;
|
|
2165
|
+
current.close();
|
|
2166
|
+
stream = null;
|
|
2167
|
+
streamState = "closed";
|
|
2168
|
+
}
|
|
2169
|
+
});
|
|
2170
|
+
current.start().catch(() => {
|
|
2171
|
+
});
|
|
2172
|
+
};
|
|
2173
|
+
const polling = () => streamState !== "open";
|
|
2174
|
+
const start = () => {
|
|
2175
|
+
offResume = deps.onResume(() => {
|
|
2176
|
+
if (!polling()) return;
|
|
2177
|
+
void read().then((ok) => {
|
|
2178
|
+
if (ok) startStream();
|
|
2179
|
+
});
|
|
2180
|
+
});
|
|
2181
|
+
timer = deps.setInterval(() => {
|
|
2182
|
+
if (polling() && signedIn) void read();
|
|
2183
|
+
}, POLL_MS);
|
|
2184
|
+
void read().then((ok) => {
|
|
2185
|
+
if (ok) startStream();
|
|
2186
|
+
});
|
|
2187
|
+
};
|
|
2188
|
+
const stop = () => {
|
|
2189
|
+
offResume?.();
|
|
2190
|
+
offResume = null;
|
|
2191
|
+
deps.clearInterval(timer);
|
|
2192
|
+
timer = null;
|
|
2193
|
+
stream?.close();
|
|
2194
|
+
stream = null;
|
|
2195
|
+
streamState = "closed";
|
|
2196
|
+
};
|
|
2197
|
+
return {
|
|
2198
|
+
get count() {
|
|
2199
|
+
return count;
|
|
2200
|
+
},
|
|
2201
|
+
subscribe(listener) {
|
|
2202
|
+
listeners.add(listener);
|
|
2203
|
+
listener(count);
|
|
2204
|
+
if (listeners.size === 1) start();
|
|
2205
|
+
return () => {
|
|
2206
|
+
if (!listeners.delete(listener)) return;
|
|
2207
|
+
if (listeners.size === 0) stop();
|
|
2208
|
+
};
|
|
2209
|
+
},
|
|
2210
|
+
async refresh() {
|
|
2211
|
+
await read();
|
|
2212
|
+
}
|
|
2213
|
+
};
|
|
2214
|
+
}
|
|
2215
|
+
|
|
1885
2216
|
// src/modules/notifications.ts
|
|
1886
2217
|
var NotificationsModule = class {
|
|
1887
2218
|
constructor(client) {
|
|
1888
2219
|
this.client = client;
|
|
1889
2220
|
}
|
|
1890
2221
|
/**
|
|
1891
|
-
* List paginated notifications with unread count.
|
|
2222
|
+
* List paginated notifications (20 per page, newest first) with the unread count.
|
|
1892
2223
|
*/
|
|
1893
2224
|
async list(page, options) {
|
|
1894
2225
|
return this.client.get(
|
|
@@ -1898,11 +2229,43 @@ var NotificationsModule = class {
|
|
|
1898
2229
|
);
|
|
1899
2230
|
}
|
|
1900
2231
|
/**
|
|
1901
|
-
*
|
|
2232
|
+
* The unread count alone: for a badge, on focus or on an interval when no stream is open.
|
|
2233
|
+
*/
|
|
2234
|
+
async unread(options) {
|
|
2235
|
+
return this.client.get("/notify/notifications/unread", void 0, options);
|
|
2236
|
+
}
|
|
2237
|
+
/**
|
|
2238
|
+
* Mark notifications as read (at most 90 ids per call). If ids are omitted, marks all as read.
|
|
1902
2239
|
*/
|
|
1903
2240
|
async markRead(ids, options) {
|
|
1904
2241
|
return this.client.post("/notify/notifications/read", ids ? { ids } : {}, options);
|
|
1905
2242
|
}
|
|
2243
|
+
/** Create an inert live notification stream. Register listeners before `start()`. */
|
|
2244
|
+
stream(options) {
|
|
2245
|
+
return new ManagedNotificationStream(this.client, options);
|
|
2246
|
+
}
|
|
2247
|
+
/** Create and start a live notification stream. Resolves once connected. */
|
|
2248
|
+
async connectStream(options) {
|
|
2249
|
+
const stream = this.stream(options);
|
|
2250
|
+
await stream.start();
|
|
2251
|
+
return stream;
|
|
2252
|
+
}
|
|
2253
|
+
/**
|
|
2254
|
+
* A bell's unread count, kept current without a reload. Reads the count once, then follows the
|
|
2255
|
+
* live stream; while the stream is not open (connecting, refused, failed) it polls on resume
|
|
2256
|
+
* and every minute, and asks for a new stream at most every ten minutes after a refusal.
|
|
2257
|
+
* Create one per page and share it between bells.
|
|
2258
|
+
*/
|
|
2259
|
+
liveUnread(options = {}) {
|
|
2260
|
+
return createLiveUnread({
|
|
2261
|
+
unread: () => this.unread(),
|
|
2262
|
+
stream: (streamOptions) => this.stream(streamOptions),
|
|
2263
|
+
onResume: options.onResume ?? browserResume,
|
|
2264
|
+
setInterval: (callback, ms) => setInterval(callback, ms),
|
|
2265
|
+
clearInterval: (handle) => clearInterval(handle),
|
|
2266
|
+
surface: options.surface
|
|
2267
|
+
});
|
|
2268
|
+
}
|
|
1906
2269
|
};
|
|
1907
2270
|
|
|
1908
2271
|
// src/modules/stores.ts
|