totalum-sdk 1.11.0 → 1.12.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 +20 -0
- package/dist/_types/{coerce.d-BKhuaDya.d.ts → coerce.d-Cp5NZQN-.d.ts} +1 -1
- package/dist/_types/{errors.d-RTRkidv5.d.ts → errors.d-ve4rY-Hx.d.ts} +37 -5
- package/dist/_types/{integrations.d-CzTa9T-L.d.ts → integrations.d-BRhNKT7K.d.ts} +2 -2
- package/dist/_types/{ops.d-CNsrT4Ma.d.ts → ops.d-DIC4u0go.d.ts} +2 -2
- package/dist/ai/index.d.ts +275 -23
- package/dist/ai/index.js +305 -24
- package/dist/analytics/index.d.ts +58 -12
- package/dist/analytics/index.js +46 -3
- package/dist/browser/index.d.ts +228 -20
- package/dist/browser/index.js +122 -13
- package/dist/connections/index.d.ts +305 -23
- package/dist/connections/index.js +302 -21
- package/dist/cron/index.d.ts +147 -20
- package/dist/cron/index.js +157 -15
- package/dist/d1/errors.js +15 -0
- package/dist/d1/index.d.ts +254 -10
- package/dist/d1/index.js +24 -4
- package/dist/d1/session.js +26 -2
- package/dist/email/index.d.ts +65 -10
- package/dist/email/index.js +60 -5
- package/dist/errors.js +17 -0
- package/dist/files/index.d.ts +243 -20
- package/dist/files/index.js +247 -14
- package/dist/index.d.ts +25 -10
- package/dist/index.js +19 -5
- package/dist/logs/index.d.ts +48 -10
- package/dist/logs/index.js +39 -4
- package/dist/payments/index.d.ts +221 -23
- package/dist/payments/index.js +216 -18
- package/dist/pdf/index.d.ts +59 -9
- package/dist/pdf/index.js +54 -4
- package/dist/react/index.d.ts +78 -6
- package/dist/react/index.js +51 -5
- package/dist/realtime/index.d.ts +55 -8
- package/dist/realtime/index.js +53 -6
- package/dist/scan/index.d.ts +78 -10
- package/dist/scan/index.js +63 -3
- package/dist/seo/index.d.ts +1118 -8
- package/dist/seo/index.js +325 -4
- package/dist/speech/index.d.ts +34 -10
- package/dist/speech/index.js +25 -5
- package/dist/vectors/index.d.ts +96 -9
- package/dist/vectors/index.js +93 -7
- package/dist/web/index.d.ts +168 -15
- package/dist/web/index.js +163 -10
- package/dist/webhooks/index.d.ts +122 -15
- package/dist/webhooks/index.js +119 -13
- package/package.json +2 -1
- package/totalum-sdk.md +10336 -754
- package/totalum-sdk.signatures.json +69 -1
package/dist/pdf/index.d.ts
CHANGED
|
@@ -1,16 +1,45 @@
|
|
|
1
|
-
import { T as TotalumClientOptions, U as Usage } from '../_types/errors.d-
|
|
2
|
-
export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-
|
|
3
|
-
import { q as PdfFromHtmlInput, r as PdfFromUrlInput } from '../_types/integrations.d-
|
|
4
|
-
export { s as PdfOutput } from '../_types/integrations.d-
|
|
5
|
-
import '../_types/coerce.d-
|
|
1
|
+
import { T as TotalumClientOptions, U as Usage } from '../_types/errors.d-ve4rY-Hx.js';
|
|
2
|
+
export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-ve4rY-Hx.js';
|
|
3
|
+
import { q as PdfFromHtmlInput, r as PdfFromUrlInput } from '../_types/integrations.d-BRhNKT7K.js';
|
|
4
|
+
export { s as PdfOutput } from '../_types/integrations.d-BRhNKT7K.js';
|
|
5
|
+
import '../_types/coerce.d-Cp5NZQN-.js';
|
|
6
6
|
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Creates the `pdf` namespace (the same object as `totalum.pdf`, scope `files:write`): each call renders a PDF and
|
|
9
|
+
* stores it as a new public file of the project.
|
|
10
|
+
*
|
|
11
|
+
* @param options - The project key, API URL and environment; each falls back to its environment variable.
|
|
12
|
+
* @returns The `pdf` namespace: the methods below.
|
|
13
|
+
* @example
|
|
14
|
+
* import { totalumPdf } from 'totalum-sdk/pdf';
|
|
15
|
+
*
|
|
16
|
+
* const pdf = totalumPdf();
|
|
17
|
+
* const invoice = await pdf.fromHtml({ html: '<h1>Invoice 12</h1>', name: 'invoice-12' });
|
|
18
|
+
*/
|
|
8
19
|
declare function totalumPdf(options?: TotalumClientOptions): {
|
|
9
20
|
/**
|
|
10
|
-
*
|
|
21
|
+
* Renders HTML (with its CSS) to a PDF in a real browser and stores it as a project file: invoices, tickets,
|
|
22
|
+
* certificates, reports. Use inline `<style>` and absolute image URLs. Every call stores a new file, even with the
|
|
23
|
+
* same `name`.
|
|
11
24
|
*
|
|
25
|
+
* @param input - The HTML, the file name and the page options.
|
|
26
|
+
* @returns The stored PDF's `key`, `url` and `fileName`, and the credits it cost.
|
|
27
|
+
* @throws PDF_TOO_LARGE (413) The HTML is over 10 MB.
|
|
28
|
+
* @throws CREATE_PDF_FROM_HTML_ERROR (502) The HTML could not be rendered.
|
|
29
|
+
* @throws VALIDATION_ERROR (400) Neither `html` nor `base64HtmlString` was given, or `name` is empty.
|
|
12
30
|
* @example
|
|
13
|
-
* const
|
|
31
|
+
* const pdf = await totalum.pdf.fromHtml({
|
|
32
|
+
* html: '<style>body{font-family:sans-serif}</style><h1>Invoice 12</h1><p>Total: 49.00 €</p>',
|
|
33
|
+
* name: 'invoice-12',
|
|
34
|
+
* options: { format: 'A4', printBackground: true, margin: { top: '20mm', bottom: '20mm' } },
|
|
35
|
+
* });
|
|
36
|
+
* //=>
|
|
37
|
+
* {
|
|
38
|
+
* "url": "https://files.totalum-project.com/p/acme-crm/01M4P0S5Q26K57B47C6E9A65Y5.pdf",
|
|
39
|
+
* "fileName": "invoice-12.pdf",
|
|
40
|
+
* "key": "p/acme-crm/01M4P0S5Q26K57B47C6E9A65Y5.pdf",
|
|
41
|
+
* "usage": { "credits": 0.0004 }
|
|
42
|
+
* }
|
|
14
43
|
*/
|
|
15
44
|
fromHtml: (input: PdfFromHtmlInput) => Promise<Omit<{
|
|
16
45
|
url: string;
|
|
@@ -19,7 +48,28 @@ declare function totalumPdf(options?: TotalumClientOptions): {
|
|
|
19
48
|
}, "usage"> & {
|
|
20
49
|
usage: Usage;
|
|
21
50
|
}>;
|
|
22
|
-
/**
|
|
51
|
+
/**
|
|
52
|
+
* Prints a web page to a PDF and stores it as a project file — archive a page, or render one of your own pages
|
|
53
|
+
* (a printable report route) as a PDF.
|
|
54
|
+
*
|
|
55
|
+
* @param input - The page URL, the file name, page options, and cookies or headers to send.
|
|
56
|
+
* @returns The stored PDF's `key`, `url` and `fileName`, and the credits it cost.
|
|
57
|
+
* @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL.
|
|
58
|
+
* @throws GENERATE_PDF_BY_URL_ERROR (502) The page could not be rendered.
|
|
59
|
+
* @example
|
|
60
|
+
* const pdf = await totalum.pdf.fromUrl({
|
|
61
|
+
* url: 'https://acme.totalum.app/reports/2026-09/print',
|
|
62
|
+
* name: 'report-2026-09',
|
|
63
|
+
* options: { format: 'A4', landscape: true },
|
|
64
|
+
* });
|
|
65
|
+
* //=>
|
|
66
|
+
* {
|
|
67
|
+
* "url": "https://files.totalum-project.com/p/acme-crm/01M4P1D2E3F4G5H6J7K8M9N0PQ.pdf",
|
|
68
|
+
* "fileName": "report-2026-09.pdf",
|
|
69
|
+
* "key": "p/acme-crm/01M4P1D2E3F4G5H6J7K8M9N0PQ.pdf",
|
|
70
|
+
* "usage": { "credits": 0.0006 }
|
|
71
|
+
* }
|
|
72
|
+
*/
|
|
23
73
|
fromUrl: (input: PdfFromUrlInput) => Promise<Omit<{
|
|
24
74
|
url: string;
|
|
25
75
|
fileName: string;
|
package/dist/pdf/index.js
CHANGED
|
@@ -1,17 +1,67 @@
|
|
|
1
1
|
import { envelopeClient, withUsage } from '../http.js';
|
|
2
2
|
export { TotalumError, isTotalumError } from '../errors.js';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Creates the `pdf` namespace (the same object as `totalum.pdf`, scope `files:write`): each call renders a PDF and
|
|
5
|
+
* stores it as a new public file of the project.
|
|
6
|
+
*
|
|
7
|
+
* @param options - The project key, API URL and environment; each falls back to its environment variable.
|
|
8
|
+
* @returns The `pdf` namespace: the methods below.
|
|
9
|
+
* @example
|
|
10
|
+
* import { totalumPdf } from 'totalum-sdk/pdf';
|
|
11
|
+
*
|
|
12
|
+
* const pdf = totalumPdf();
|
|
13
|
+
* const invoice = await pdf.fromHtml({ html: '<h1>Invoice 12</h1>', name: 'invoice-12' });
|
|
14
|
+
*/
|
|
4
15
|
export function totalumPdf(options = {}) {
|
|
5
16
|
const call = envelopeClient(options);
|
|
6
17
|
return {
|
|
7
18
|
/**
|
|
8
|
-
*
|
|
19
|
+
* Renders HTML (with its CSS) to a PDF in a real browser and stores it as a project file: invoices, tickets,
|
|
20
|
+
* certificates, reports. Use inline `<style>` and absolute image URLs. Every call stores a new file, even with the
|
|
21
|
+
* same `name`.
|
|
9
22
|
*
|
|
23
|
+
* @param input - The HTML, the file name and the page options.
|
|
24
|
+
* @returns The stored PDF's `key`, `url` and `fileName`, and the credits it cost.
|
|
25
|
+
* @throws PDF_TOO_LARGE (413) The HTML is over 10 MB.
|
|
26
|
+
* @throws CREATE_PDF_FROM_HTML_ERROR (502) The HTML could not be rendered.
|
|
27
|
+
* @throws VALIDATION_ERROR (400) Neither `html` nor `base64HtmlString` was given, or `name` is empty.
|
|
10
28
|
* @example
|
|
11
|
-
* const
|
|
29
|
+
* const pdf = await totalum.pdf.fromHtml({
|
|
30
|
+
* html: '<style>body{font-family:sans-serif}</style><h1>Invoice 12</h1><p>Total: 49.00 €</p>',
|
|
31
|
+
* name: 'invoice-12',
|
|
32
|
+
* options: { format: 'A4', printBackground: true, margin: { top: '20mm', bottom: '20mm' } },
|
|
33
|
+
* });
|
|
34
|
+
* //=>
|
|
35
|
+
* {
|
|
36
|
+
* "url": "https://files.totalum-project.com/p/acme-crm/01M4P0S5Q26K57B47C6E9A65Y5.pdf",
|
|
37
|
+
* "fileName": "invoice-12.pdf",
|
|
38
|
+
* "key": "p/acme-crm/01M4P0S5Q26K57B47C6E9A65Y5.pdf",
|
|
39
|
+
* "usage": { "credits": 0.0004 }
|
|
40
|
+
* }
|
|
12
41
|
*/
|
|
13
42
|
fromHtml: async (input) => withUsage(await call('POST', '/v1/pdf/from-html', input)),
|
|
14
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Prints a web page to a PDF and stores it as a project file — archive a page, or render one of your own pages
|
|
45
|
+
* (a printable report route) as a PDF.
|
|
46
|
+
*
|
|
47
|
+
* @param input - The page URL, the file name, page options, and cookies or headers to send.
|
|
48
|
+
* @returns The stored PDF's `key`, `url` and `fileName`, and the credits it cost.
|
|
49
|
+
* @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL.
|
|
50
|
+
* @throws GENERATE_PDF_BY_URL_ERROR (502) The page could not be rendered.
|
|
51
|
+
* @example
|
|
52
|
+
* const pdf = await totalum.pdf.fromUrl({
|
|
53
|
+
* url: 'https://acme.totalum.app/reports/2026-09/print',
|
|
54
|
+
* name: 'report-2026-09',
|
|
55
|
+
* options: { format: 'A4', landscape: true },
|
|
56
|
+
* });
|
|
57
|
+
* //=>
|
|
58
|
+
* {
|
|
59
|
+
* "url": "https://files.totalum-project.com/p/acme-crm/01M4P1D2E3F4G5H6J7K8M9N0PQ.pdf",
|
|
60
|
+
* "fileName": "report-2026-09.pdf",
|
|
61
|
+
* "key": "p/acme-crm/01M4P1D2E3F4G5H6J7K8M9N0PQ.pdf",
|
|
62
|
+
* "usage": { "credits": 0.0006 }
|
|
63
|
+
* }
|
|
64
|
+
*/
|
|
15
65
|
fromUrl: async (input) => withUsage(await call('POST', '/v1/pdf/from-url', input)),
|
|
16
66
|
};
|
|
17
67
|
}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -4,26 +4,44 @@
|
|
|
4
4
|
* socket, and on every drop — a Totalum deploy drops them all — re-tickets and reconnects with backoff. Presence is
|
|
5
5
|
* built on the same socket: `presence:*` messages plus a 30 s heartbeat (a room with present users stays awake).
|
|
6
6
|
*/
|
|
7
|
+
/**
|
|
8
|
+
* The state of a room's connection: `connecting` (first ticket and socket), `open`, `reconnecting` (after a drop —
|
|
9
|
+
* every deploy drops sockets — show it as an ordinary state, not an error) or `closed` (after `close()` or unmount).
|
|
10
|
+
*/
|
|
7
11
|
type RealtimeStatus = 'connecting' | 'open' | 'reconnecting' | 'closed';
|
|
12
|
+
/** One message received in a room. */
|
|
8
13
|
interface RealtimeMessage<T = unknown> {
|
|
14
|
+
/** The message id. */
|
|
9
15
|
id: string;
|
|
16
|
+
/** The event name the sender gave (`message` by default). */
|
|
10
17
|
event: string;
|
|
18
|
+
/** Who sent it: the identity of their ticket, or `{ id: 'server' }` for a server-side publish. */
|
|
11
19
|
identity: {
|
|
20
|
+
/** The sender's user id. */
|
|
12
21
|
id: string;
|
|
22
|
+
/** The sender's display name, when their ticket had one. */
|
|
13
23
|
name?: string;
|
|
14
24
|
};
|
|
25
|
+
/** What was sent. */
|
|
15
26
|
data: T;
|
|
27
|
+
/** When the room received it (ISO date-time). */
|
|
16
28
|
ts: string;
|
|
17
29
|
}
|
|
30
|
+
/** Another connection present in the room (with the `presence` option). */
|
|
18
31
|
interface RealtimePeer<M = Record<string, unknown>> {
|
|
19
32
|
/** One per open socket: the same user in two tabs is two peers with the same `identity`. */
|
|
20
33
|
connectionId: string;
|
|
34
|
+
/** The peer's user, from their ticket. */
|
|
21
35
|
identity: {
|
|
36
|
+
/** Their user id. */
|
|
22
37
|
id: string;
|
|
38
|
+
/** Their display name, when their ticket had one. */
|
|
23
39
|
name?: string;
|
|
24
40
|
};
|
|
41
|
+
/** The `presence` data they announced (name, colour, cursor…). */
|
|
25
42
|
meta: M;
|
|
26
43
|
}
|
|
44
|
+
/** How `useRealtimeChannel` / `RealtimeChannel` get their ticket, announce presence and keep messages. */
|
|
27
45
|
interface RealtimeChannelOptions<M = Record<string, unknown>> {
|
|
28
46
|
/** The app's route that mints the ticket; it receives `{ room }` and answers `{ url }` or `{ data: { url } }`. */
|
|
29
47
|
ticketUrl?: string;
|
|
@@ -35,16 +53,35 @@ interface RealtimeChannelOptions<M = Record<string, unknown>> {
|
|
|
35
53
|
presence?: M;
|
|
36
54
|
/** Messages kept in memory, newest last (default 100). */
|
|
37
55
|
keep?: number;
|
|
38
|
-
/**
|
|
56
|
+
/** A `WebSocket` implementation, for tests only. Default: the browser's. */
|
|
39
57
|
WebSocket?: typeof WebSocket;
|
|
40
58
|
}
|
|
59
|
+
/** The current state of a room: what `useRealtimeChannel` returns (plus `publish`), and `RealtimeChannel.getSnapshot()`. */
|
|
41
60
|
interface RealtimeSnapshot<M = Record<string, unknown>> {
|
|
61
|
+
/** The connection's state. */
|
|
42
62
|
status: RealtimeStatus;
|
|
63
|
+
/** The messages received since the room opened, oldest first (at most `keep`). */
|
|
43
64
|
messages: RealtimeMessage[];
|
|
65
|
+
/** Everyone else present (with `presence`); empty otherwise. */
|
|
44
66
|
others: RealtimePeer<M>[];
|
|
45
67
|
}
|
|
46
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* One room over one socket, without React (Vue, Svelte, plain scripts): `connect()`, read `getSnapshot()`, listen with
|
|
70
|
+
* `subscribe()`, send with `publish()`, `close()` when done. It re-tickets and reconnects by itself after a drop.
|
|
71
|
+
* `useRealtimeChannel` is a thin React wrapper over it.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* import { RealtimeChannel } from 'totalum-sdk/react';
|
|
75
|
+
*
|
|
76
|
+
* const channel = new RealtimeChannel('chat:team-1', { presence: { name: 'Ana' } });
|
|
77
|
+
* channel.subscribe(() => render(channel.getSnapshot()));
|
|
78
|
+
* channel.connect();
|
|
79
|
+
* channel.publish({ text: 'Hello' });
|
|
80
|
+
* // later
|
|
81
|
+
* channel.close();
|
|
82
|
+
*/
|
|
47
83
|
declare class RealtimeChannel<M = Record<string, unknown>> {
|
|
84
|
+
/** The room's name. */
|
|
48
85
|
readonly room: string;
|
|
49
86
|
private readonly options;
|
|
50
87
|
private ws;
|
|
@@ -56,12 +93,18 @@ declare class RealtimeChannel<M = Record<string, unknown>> {
|
|
|
56
93
|
private readonly listeners;
|
|
57
94
|
private readonly connectionId;
|
|
58
95
|
private snapshot;
|
|
59
|
-
constructor(
|
|
96
|
+
constructor(
|
|
97
|
+
/** The room's name. */
|
|
98
|
+
room: string, options?: RealtimeChannelOptions<M>);
|
|
99
|
+
/** Calls `listener` on every change of the snapshot; returns the function that stops it. */
|
|
60
100
|
subscribe: (listener: () => void) => (() => void);
|
|
101
|
+
/** The current state: `status`, `messages` and `others` (a new object after each change). */
|
|
61
102
|
getSnapshot: () => RealtimeSnapshot<M>;
|
|
103
|
+
/** Fetches a ticket and opens the socket (and starts the presence heartbeat); does nothing if already connected. */
|
|
62
104
|
connect(): void;
|
|
63
105
|
/** Sends `data` to everyone in the room (you included); `false` when the socket is not open. */
|
|
64
106
|
publish: (data: unknown, event?: string) => boolean;
|
|
107
|
+
/** Leaves the room (announcing it to the others), closes the socket and stops reconnecting. */
|
|
65
108
|
close(): void;
|
|
66
109
|
private open;
|
|
67
110
|
private ticket;
|
|
@@ -74,16 +117,45 @@ declare class RealtimeChannel<M = Record<string, unknown>> {
|
|
|
74
117
|
private set;
|
|
75
118
|
}
|
|
76
119
|
/**
|
|
77
|
-
*
|
|
78
|
-
* `publish(data, event?)`.
|
|
120
|
+
* Joins a realtime room from a React client component and re-renders on every message: `status`, the `messages`
|
|
121
|
+
* received since it opened, `others` (with `presence`), and `publish(data, event?)`. It asks your route for a ticket
|
|
122
|
+
* (`POST /api/realtime/ticket` with `{ room }`, which calls `totalum.realtime.ticket` after your auth check), opens the
|
|
123
|
+
* socket, and re-tickets and reconnects with backoff after every drop. Render `status === 'reconnecting'` as an
|
|
124
|
+
* ordinary state, not an error. The options of the first render are kept; a new `room` opens a new connection.
|
|
79
125
|
*
|
|
126
|
+
* @param room - The room's name (the same your ticket route allows).
|
|
127
|
+
* @param options - Where to get the ticket, presence data, and how many messages to keep.
|
|
128
|
+
* @returns The room's snapshot (`status`, `messages`, `others`) and `publish(data, event?)`, which sends to everyone in
|
|
129
|
+
* the room (you included) and returns `false` when the socket is not open.
|
|
80
130
|
* @example
|
|
81
|
-
*
|
|
131
|
+
* 'use client';
|
|
132
|
+
* import { useRealtimeChannel } from 'totalum-sdk/react';
|
|
133
|
+
*
|
|
134
|
+
* export function Chat({ teamId, name }: { teamId: string; name: string }) {
|
|
135
|
+
* const { messages, others, publish, status } = useRealtimeChannel(`chat:${teamId}`, { presence: { name } });
|
|
136
|
+
* return (
|
|
137
|
+
* <div>
|
|
138
|
+
* <p>{status === 'open' ? `${others.length} others online` : 'Connecting…'}</p>
|
|
139
|
+
* {messages.map((m) => <p key={m.id}><b>{m.identity.name}</b>: {String(m.data)}</p>)}
|
|
140
|
+
* <button onClick={() => publish('Hello!')}>Say hello</button>
|
|
141
|
+
* </div>
|
|
142
|
+
* );
|
|
143
|
+
* }
|
|
144
|
+
* //=>
|
|
145
|
+
* {
|
|
146
|
+
* status: 'open',
|
|
147
|
+
* messages: [{ id: '5f0c…', event: 'message', identity: { id: 'user_7', name: 'Bob' }, data: 'Hello!', ts: '2026-10-11T18:02:11.204Z' }],
|
|
148
|
+
* others: [{ connectionId: '1b9e…', identity: { id: 'user_7', name: 'Bob' }, meta: { name: 'Bob' } }],
|
|
149
|
+
* publish: [Function]
|
|
150
|
+
* }
|
|
82
151
|
*/
|
|
83
152
|
declare function useRealtimeChannel<M = Record<string, unknown>>(room: string, options?: RealtimeChannelOptions<M>): {
|
|
84
153
|
publish: (data: unknown, event?: string) => boolean;
|
|
154
|
+
/** The connection's state. */
|
|
85
155
|
status: RealtimeStatus;
|
|
156
|
+
/** The messages received since the room opened, oldest first (at most `keep`). */
|
|
86
157
|
messages: RealtimeMessage[];
|
|
158
|
+
/** Everyone else present (with `presence`); empty otherwise. */
|
|
87
159
|
others: RealtimePeer<M>[];
|
|
88
160
|
};
|
|
89
161
|
|
package/dist/react/index.js
CHANGED
|
@@ -2,7 +2,21 @@ import { useEffect, useMemo, useSyncExternalStore } from 'react';
|
|
|
2
2
|
const HEARTBEAT_MS = 30_000;
|
|
3
3
|
const STALE_MS = 75_000;
|
|
4
4
|
const PRESENCE = 'presence:';
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* One room over one socket, without React (Vue, Svelte, plain scripts): `connect()`, read `getSnapshot()`, listen with
|
|
7
|
+
* `subscribe()`, send with `publish()`, `close()` when done. It re-tickets and reconnects by itself after a drop.
|
|
8
|
+
* `useRealtimeChannel` is a thin React wrapper over it.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* import { RealtimeChannel } from 'totalum-sdk/react';
|
|
12
|
+
*
|
|
13
|
+
* const channel = new RealtimeChannel('chat:team-1', { presence: { name: 'Ana' } });
|
|
14
|
+
* channel.subscribe(() => render(channel.getSnapshot()));
|
|
15
|
+
* channel.connect();
|
|
16
|
+
* channel.publish({ text: 'Hello' });
|
|
17
|
+
* // later
|
|
18
|
+
* channel.close();
|
|
19
|
+
*/
|
|
6
20
|
export class RealtimeChannel {
|
|
7
21
|
room;
|
|
8
22
|
options;
|
|
@@ -15,15 +29,20 @@ export class RealtimeChannel {
|
|
|
15
29
|
listeners = new Set();
|
|
16
30
|
connectionId = crypto.randomUUID();
|
|
17
31
|
snapshot = { status: 'closed', messages: [], others: [] };
|
|
18
|
-
constructor(
|
|
32
|
+
constructor(
|
|
33
|
+
/** The room's name. */
|
|
34
|
+
room, options = {}) {
|
|
19
35
|
this.room = room;
|
|
20
36
|
this.options = options;
|
|
21
37
|
}
|
|
38
|
+
/** Calls `listener` on every change of the snapshot; returns the function that stops it. */
|
|
22
39
|
subscribe = (listener) => {
|
|
23
40
|
this.listeners.add(listener);
|
|
24
41
|
return () => this.listeners.delete(listener);
|
|
25
42
|
};
|
|
43
|
+
/** The current state: `status`, `messages` and `others` (a new object after each change). */
|
|
26
44
|
getSnapshot = () => this.snapshot;
|
|
45
|
+
/** Fetches a ticket and opens the socket (and starts the presence heartbeat); does nothing if already connected. */
|
|
27
46
|
connect() {
|
|
28
47
|
if (!this.stopped)
|
|
29
48
|
return;
|
|
@@ -38,6 +57,7 @@ export class RealtimeChannel {
|
|
|
38
57
|
}
|
|
39
58
|
/** Sends `data` to everyone in the room (you included); `false` when the socket is not open. */
|
|
40
59
|
publish = (data, event = 'message') => this.send(event, data);
|
|
60
|
+
/** Leaves the room (announcing it to the others), closes the socket and stops reconnecting. */
|
|
41
61
|
close() {
|
|
42
62
|
if (this.options.presence)
|
|
43
63
|
this.send(`${PRESENCE}leave`, null);
|
|
@@ -158,11 +178,37 @@ export class RealtimeChannel {
|
|
|
158
178
|
}
|
|
159
179
|
const SERVER = { status: 'connecting', messages: [], others: [] };
|
|
160
180
|
/**
|
|
161
|
-
*
|
|
162
|
-
* `publish(data, event?)`.
|
|
181
|
+
* Joins a realtime room from a React client component and re-renders on every message: `status`, the `messages`
|
|
182
|
+
* received since it opened, `others` (with `presence`), and `publish(data, event?)`. It asks your route for a ticket
|
|
183
|
+
* (`POST /api/realtime/ticket` with `{ room }`, which calls `totalum.realtime.ticket` after your auth check), opens the
|
|
184
|
+
* socket, and re-tickets and reconnects with backoff after every drop. Render `status === 'reconnecting'` as an
|
|
185
|
+
* ordinary state, not an error. The options of the first render are kept; a new `room` opens a new connection.
|
|
163
186
|
*
|
|
187
|
+
* @param room - The room's name (the same your ticket route allows).
|
|
188
|
+
* @param options - Where to get the ticket, presence data, and how many messages to keep.
|
|
189
|
+
* @returns The room's snapshot (`status`, `messages`, `others`) and `publish(data, event?)`, which sends to everyone in
|
|
190
|
+
* the room (you included) and returns `false` when the socket is not open.
|
|
164
191
|
* @example
|
|
165
|
-
*
|
|
192
|
+
* 'use client';
|
|
193
|
+
* import { useRealtimeChannel } from 'totalum-sdk/react';
|
|
194
|
+
*
|
|
195
|
+
* export function Chat({ teamId, name }: { teamId: string; name: string }) {
|
|
196
|
+
* const { messages, others, publish, status } = useRealtimeChannel(`chat:${teamId}`, { presence: { name } });
|
|
197
|
+
* return (
|
|
198
|
+
* <div>
|
|
199
|
+
* <p>{status === 'open' ? `${others.length} others online` : 'Connecting…'}</p>
|
|
200
|
+
* {messages.map((m) => <p key={m.id}><b>{m.identity.name}</b>: {String(m.data)}</p>)}
|
|
201
|
+
* <button onClick={() => publish('Hello!')}>Say hello</button>
|
|
202
|
+
* </div>
|
|
203
|
+
* );
|
|
204
|
+
* }
|
|
205
|
+
* //=>
|
|
206
|
+
* {
|
|
207
|
+
* status: 'open',
|
|
208
|
+
* messages: [{ id: '5f0c…', event: 'message', identity: { id: 'user_7', name: 'Bob' }, data: 'Hello!', ts: '2026-10-11T18:02:11.204Z' }],
|
|
209
|
+
* others: [{ connectionId: '1b9e…', identity: { id: 'user_7', name: 'Bob' }, meta: { name: 'Bob' } }],
|
|
210
|
+
* publish: [Function]
|
|
211
|
+
* }
|
|
166
212
|
*/
|
|
167
213
|
export function useRealtimeChannel(room, options = {}) {
|
|
168
214
|
// one channel per room; the options of its first render are the ones it keeps
|
package/dist/realtime/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Z as ZodObject, c as ZodOptional, d as ZodString, g as ZodUnknown, m as $strip, o as output, u as ZodISODateTime, l as ZodInt, h as ZodRecord, j as ZodBoolean, T as TotalumClientOptions } from '../_types/errors.d-
|
|
2
|
-
export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-
|
|
1
|
+
import { Z as ZodObject, c as ZodOptional, d as ZodString, g as ZodUnknown, m as $strip, o as output, u as ZodISODateTime, l as ZodInt, h as ZodRecord, j as ZodBoolean, T as TotalumClientOptions } from '../_types/errors.d-ve4rY-Hx.js';
|
|
2
|
+
export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-ve4rY-Hx.js';
|
|
3
3
|
|
|
4
4
|
/** `POST /v1/realtime/tickets` (scope `realtime`). `ttl` is the join window in seconds, not the session length. */
|
|
5
5
|
declare const RealtimeTicketInput: ZodObject<{
|
|
@@ -55,7 +55,7 @@ declare const RealtimePublishOutput: ZodObject<{
|
|
|
55
55
|
type RealtimePublishOutput = output<typeof RealtimePublishOutput>;
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
* `
|
|
58
|
+
* Creates the `realtime` namespace (the same object as `totalum.realtime`, scope `realtime`), server side. The app's own
|
|
59
59
|
* server mints a short-lived ticket after its own auth check and hands `{ticket, url}` to the page; the page opens the
|
|
60
60
|
* `wss://` `url` and never holds a key.
|
|
61
61
|
*
|
|
@@ -64,14 +64,51 @@ type RealtimePublishOutput = output<typeof RealtimePublishOutput>;
|
|
|
64
64
|
* Free plan, 600 on Starter, 1,200 on Builder, 1,800 on Professional and 3,600 on Enterprise; the next one closes its
|
|
65
65
|
* socket with code 4429 (`REALTIME_MESSAGE_RATE_EXCEEDED`). Keep one room under about 1,000 messages a second in
|
|
66
66
|
* total: give each match or table its own room.
|
|
67
|
+
*
|
|
68
|
+
* @param options - The project key, API URL and environment; each falls back to its environment variable.
|
|
69
|
+
* @returns The `realtime` namespace: the methods below.
|
|
70
|
+
* @example
|
|
71
|
+
* import { totalumRealtime } from 'totalum-sdk/realtime';
|
|
72
|
+
*
|
|
73
|
+
* const realtime = totalumRealtime();
|
|
74
|
+
* await realtime.publish('orders:shop-1', { event: 'order.created', data: { orderId: 'ord_42' } });
|
|
67
75
|
*/
|
|
68
76
|
declare function totalumRealtime(options?: TotalumClientOptions): {
|
|
69
77
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
78
|
+
* Mints a short-lived ticket that lets one browser join one room as one identity. Call it from your own route
|
|
79
|
+
* after your auth check (is this user allowed in this room?) and hand `{ url }` to the page, which opens the
|
|
80
|
+
* WebSocket with it (`useRealtimeChannel` does this for you, from `POST /api/realtime/ticket`). By default the
|
|
81
|
+
* ticket only receives; pass `permissions: { publish: true }` to let the page send. The ticket must be used within
|
|
82
|
+
* `ttl` seconds (5–300, default 60); the socket then stays open. Minting is free. When the page opens the socket,
|
|
83
|
+
* the plan's live rooms and simultaneous connections are checked (the socket is refused with
|
|
84
|
+
* `REALTIME_ROOM_LIMIT_REACHED` or `REALTIME_CONNECTION_LIMIT_REACHED`).
|
|
72
85
|
*
|
|
86
|
+
* @param input - The room, who joins, what they may do and how long the ticket is valid.
|
|
87
|
+
* @returns The ticket, the ready-to-open `wss://` URL and what it grants.
|
|
88
|
+
* @throws VALIDATION_ERROR (400) A room name with other characters than letters, digits and `._:-`, or over 128.
|
|
89
|
+
* @throws RATE_LIMIT_EXCEEDED (429) The plan's tickets per minute are used up; `details.retryAfterSeconds`.
|
|
73
90
|
* @example
|
|
74
|
-
*
|
|
91
|
+
* // src/app/api/realtime/ticket/route.ts
|
|
92
|
+
* export async function POST(request: Request) {
|
|
93
|
+
* const user = await requireUser(); // your own session
|
|
94
|
+
* const { room } = (await request.json()) as { room: string };
|
|
95
|
+
* if (room !== `chat:${user.teamId}`) return new Response('Forbidden', { status: 403 });
|
|
96
|
+
* const ticket = await totalum.realtime.ticket({
|
|
97
|
+
* room,
|
|
98
|
+
* identity: { id: user.id, name: user.name },
|
|
99
|
+
* permissions: { publish: true },
|
|
100
|
+
* });
|
|
101
|
+
* return Response.json({ url: ticket.url });
|
|
102
|
+
* }
|
|
103
|
+
* //=>
|
|
104
|
+
* {
|
|
105
|
+
* "ticket": "eyJ2IjoxLCJraWQiOiJrMjAyNnEzIiwicHJvamVjdElkIjoiYWNtZS1jcm0iLCJyb29tIjoiY2hhdDp0ZWFtLTEi…",
|
|
106
|
+
* "url": "wss://realtime.totalum.app/v1/rooms/chat:team-1?ticket=eyJ2IjoxLCJraWQiOiJrMjAyNnEz…",
|
|
107
|
+
* "room": "chat:team-1",
|
|
108
|
+
* "identity": { "id": "user_42", "name": "Ana" },
|
|
109
|
+
* "permissions": { "subscribe": true, "publish": true },
|
|
110
|
+
* "expiresAt": "2026-10-11T17:39:44.317Z"
|
|
111
|
+
* }
|
|
75
112
|
*/
|
|
76
113
|
ticket: (input: RealtimeTicketInput) => Promise<{
|
|
77
114
|
ticket: string;
|
|
@@ -92,10 +129,20 @@ declare function totalumRealtime(options?: TotalumClientOptions): {
|
|
|
92
129
|
* Publishes one message to every socket subscribed to `room`, from the app's own server (a route handler, a cron
|
|
93
130
|
* job): no socket needed. Subscribers receive the same `message` frame a socket publish sends, with `event`
|
|
94
131
|
* (default `message`) and `identity` (default `{ id: 'server' }`). Resolves with the message id, its timestamp
|
|
95
|
-
* and the number of sockets it was sent to (`0` when nobody is connected — it is not queued).
|
|
132
|
+
* and the number of sockets it was sent to (`0` when nobody is connected — it is not queued). Billed per message.
|
|
96
133
|
*
|
|
134
|
+
* @param room - The room's name (letters, digits and `._:-`, at most 128 characters).
|
|
135
|
+
* @param input - The message: its `data`, an `event` name and the sender's identity.
|
|
136
|
+
* @returns The message id, when it was sent, and how many sockets received it.
|
|
137
|
+
* @throws VALIDATION_ERROR (400) A bad room name, or an `event` over 64 characters.
|
|
138
|
+
* @throws INSUFFICIENT_CREDITS (402) No credits left.
|
|
97
139
|
* @example
|
|
98
|
-
* const
|
|
140
|
+
* const out = await totalum.realtime.publish('orders:shop-1', {
|
|
141
|
+
* event: 'order.created',
|
|
142
|
+
* data: { orderId: 'ord_42', total: 4900 },
|
|
143
|
+
* });
|
|
144
|
+
* //=>
|
|
145
|
+
* { "messageId": "d4d3ef78-9c21-4d15-a385-dc55af625cd3", "at": "2026-10-11T17:38:45.234Z", "connections": 3 }
|
|
99
146
|
*/
|
|
100
147
|
publish: (room: string, input: RealtimePublishInput) => Promise<{
|
|
101
148
|
messageId: string;
|
package/dist/realtime/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsonClient } from '../http.js';
|
|
2
2
|
export { TotalumError, isTotalumError } from '../errors.js';
|
|
3
3
|
/**
|
|
4
|
-
* `
|
|
4
|
+
* Creates the `realtime` namespace (the same object as `totalum.realtime`, scope `realtime`), server side. The app's own
|
|
5
5
|
* server mints a short-lived ticket after its own auth check and hands `{ticket, url}` to the page; the page opens the
|
|
6
6
|
* `wss://` `url` and never holds a key.
|
|
7
7
|
*
|
|
@@ -10,26 +10,73 @@ export { TotalumError, isTotalumError } from '../errors.js';
|
|
|
10
10
|
* Free plan, 600 on Starter, 1,200 on Builder, 1,800 on Professional and 3,600 on Enterprise; the next one closes its
|
|
11
11
|
* socket with code 4429 (`REALTIME_MESSAGE_RATE_EXCEEDED`). Keep one room under about 1,000 messages a second in
|
|
12
12
|
* total: give each match or table its own room.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The project key, API URL and environment; each falls back to its environment variable.
|
|
15
|
+
* @returns The `realtime` namespace: the methods below.
|
|
16
|
+
* @example
|
|
17
|
+
* import { totalumRealtime } from 'totalum-sdk/realtime';
|
|
18
|
+
*
|
|
19
|
+
* const realtime = totalumRealtime();
|
|
20
|
+
* await realtime.publish('orders:shop-1', { event: 'order.created', data: { orderId: 'ord_42' } });
|
|
13
21
|
*/
|
|
14
22
|
export function totalumRealtime(options = {}) {
|
|
15
23
|
const call = jsonClient(options);
|
|
16
24
|
return {
|
|
17
25
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
26
|
+
* Mints a short-lived ticket that lets one browser join one room as one identity. Call it from your own route
|
|
27
|
+
* after your auth check (is this user allowed in this room?) and hand `{ url }` to the page, which opens the
|
|
28
|
+
* WebSocket with it (`useRealtimeChannel` does this for you, from `POST /api/realtime/ticket`). By default the
|
|
29
|
+
* ticket only receives; pass `permissions: { publish: true }` to let the page send. The ticket must be used within
|
|
30
|
+
* `ttl` seconds (5–300, default 60); the socket then stays open. Minting is free. When the page opens the socket,
|
|
31
|
+
* the plan's live rooms and simultaneous connections are checked (the socket is refused with
|
|
32
|
+
* `REALTIME_ROOM_LIMIT_REACHED` or `REALTIME_CONNECTION_LIMIT_REACHED`).
|
|
20
33
|
*
|
|
34
|
+
* @param input - The room, who joins, what they may do and how long the ticket is valid.
|
|
35
|
+
* @returns The ticket, the ready-to-open `wss://` URL and what it grants.
|
|
36
|
+
* @throws VALIDATION_ERROR (400) A room name with other characters than letters, digits and `._:-`, or over 128.
|
|
37
|
+
* @throws RATE_LIMIT_EXCEEDED (429) The plan's tickets per minute are used up; `details.retryAfterSeconds`.
|
|
21
38
|
* @example
|
|
22
|
-
*
|
|
39
|
+
* // src/app/api/realtime/ticket/route.ts
|
|
40
|
+
* export async function POST(request: Request) {
|
|
41
|
+
* const user = await requireUser(); // your own session
|
|
42
|
+
* const { room } = (await request.json()) as { room: string };
|
|
43
|
+
* if (room !== `chat:${user.teamId}`) return new Response('Forbidden', { status: 403 });
|
|
44
|
+
* const ticket = await totalum.realtime.ticket({
|
|
45
|
+
* room,
|
|
46
|
+
* identity: { id: user.id, name: user.name },
|
|
47
|
+
* permissions: { publish: true },
|
|
48
|
+
* });
|
|
49
|
+
* return Response.json({ url: ticket.url });
|
|
50
|
+
* }
|
|
51
|
+
* //=>
|
|
52
|
+
* {
|
|
53
|
+
* "ticket": "eyJ2IjoxLCJraWQiOiJrMjAyNnEzIiwicHJvamVjdElkIjoiYWNtZS1jcm0iLCJyb29tIjoiY2hhdDp0ZWFtLTEi…",
|
|
54
|
+
* "url": "wss://realtime.totalum.app/v1/rooms/chat:team-1?ticket=eyJ2IjoxLCJraWQiOiJrMjAyNnEz…",
|
|
55
|
+
* "room": "chat:team-1",
|
|
56
|
+
* "identity": { "id": "user_42", "name": "Ana" },
|
|
57
|
+
* "permissions": { "subscribe": true, "publish": true },
|
|
58
|
+
* "expiresAt": "2026-10-11T17:39:44.317Z"
|
|
59
|
+
* }
|
|
23
60
|
*/
|
|
24
61
|
ticket: (input) => call('POST', '/v1/realtime/tickets', input),
|
|
25
62
|
/**
|
|
26
63
|
* Publishes one message to every socket subscribed to `room`, from the app's own server (a route handler, a cron
|
|
27
64
|
* job): no socket needed. Subscribers receive the same `message` frame a socket publish sends, with `event`
|
|
28
65
|
* (default `message`) and `identity` (default `{ id: 'server' }`). Resolves with the message id, its timestamp
|
|
29
|
-
* and the number of sockets it was sent to (`0` when nobody is connected — it is not queued).
|
|
66
|
+
* and the number of sockets it was sent to (`0` when nobody is connected — it is not queued). Billed per message.
|
|
30
67
|
*
|
|
68
|
+
* @param room - The room's name (letters, digits and `._:-`, at most 128 characters).
|
|
69
|
+
* @param input - The message: its `data`, an `event` name and the sender's identity.
|
|
70
|
+
* @returns The message id, when it was sent, and how many sockets received it.
|
|
71
|
+
* @throws VALIDATION_ERROR (400) A bad room name, or an `event` over 64 characters.
|
|
72
|
+
* @throws INSUFFICIENT_CREDITS (402) No credits left.
|
|
31
73
|
* @example
|
|
32
|
-
* const
|
|
74
|
+
* const out = await totalum.realtime.publish('orders:shop-1', {
|
|
75
|
+
* event: 'order.created',
|
|
76
|
+
* data: { orderId: 'ord_42', total: 4900 },
|
|
77
|
+
* });
|
|
78
|
+
* //=>
|
|
79
|
+
* { "messageId": "d4d3ef78-9c21-4d15-a385-dc55af625cd3", "at": "2026-10-11T17:38:45.234Z", "connections": 3 }
|
|
33
80
|
*/
|
|
34
81
|
publish: (room, input) => call('POST', `/v1/realtime/rooms/${encodeURIComponent(room)}/publish`, input),
|
|
35
82
|
};
|