totalum-sdk 1.10.5 → 1.11.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/_types/{coerce.d-Dgx5wOUv.d.ts → coerce.d-D_rylvPs.d.ts} +1 -1
  3. package/dist/_types/{errors.d-CIkIZmY-.d.ts → errors.d-BPpY5QKu.d.ts} +32 -2
  4. package/dist/_types/{integrations.d-B3Ea8Iuc.d.ts → integrations.d-BTZHy48D.d.ts} +2 -2
  5. package/dist/_types/{ops.d-G13QAhKA.d.ts → ops.d-Ce5zJyX0.d.ts} +2 -2
  6. package/dist/ai/index.d.ts +275 -23
  7. package/dist/ai/index.js +305 -24
  8. package/dist/analytics/index.d.ts +58 -12
  9. package/dist/analytics/index.js +46 -3
  10. package/dist/browser/index.d.ts +228 -20
  11. package/dist/browser/index.js +122 -13
  12. package/dist/connections/index.d.ts +601 -0
  13. package/dist/connections/index.js +353 -0
  14. package/dist/cron/index.d.ts +147 -20
  15. package/dist/cron/index.js +157 -15
  16. package/dist/d1/errors.js +15 -0
  17. package/dist/d1/index.d.ts +254 -10
  18. package/dist/d1/index.js +24 -4
  19. package/dist/d1/session.js +26 -2
  20. package/dist/email/index.d.ts +65 -10
  21. package/dist/email/index.js +60 -5
  22. package/dist/errors.js +17 -0
  23. package/dist/files/index.d.ts +243 -20
  24. package/dist/files/index.js +247 -14
  25. package/dist/index.d.ts +28 -10
  26. package/dist/index.js +22 -5
  27. package/dist/logs/index.d.ts +48 -10
  28. package/dist/logs/index.js +39 -4
  29. package/dist/payments/index.d.ts +221 -23
  30. package/dist/payments/index.js +216 -18
  31. package/dist/pdf/index.d.ts +59 -9
  32. package/dist/pdf/index.js +54 -4
  33. package/dist/react/index.d.ts +78 -6
  34. package/dist/react/index.js +51 -5
  35. package/dist/realtime/index.d.ts +55 -8
  36. package/dist/realtime/index.js +53 -6
  37. package/dist/scan/index.d.ts +78 -10
  38. package/dist/scan/index.js +63 -3
  39. package/dist/seo/index.d.ts +43 -7
  40. package/dist/seo/index.js +39 -3
  41. package/dist/speech/index.d.ts +34 -10
  42. package/dist/speech/index.js +25 -5
  43. package/dist/vectors/index.d.ts +96 -9
  44. package/dist/vectors/index.js +93 -7
  45. package/dist/web/index.d.ts +168 -15
  46. package/dist/web/index.js +163 -10
  47. package/dist/webhooks/index.d.ts +122 -15
  48. package/dist/webhooks/index.js +119 -13
  49. package/package.json +6 -1
  50. package/totalum-sdk.md +8900 -664
  51. package/totalum-sdk.signatures.json +61 -1
@@ -1,4 +1,12 @@
1
- /** Set on every mutating response; while the browser holds it, its reads go to the primary (read-your-writes). */
1
+ /**
2
+ * The name of the cookie `withTotalumSession` sets for 10 s after a write (`__tlm_d1_w`): while the browser holds it,
3
+ * its reads go to the primary database, so a user always sees what they just saved. Exported for apps that clear or
4
+ * inspect it.
5
+ *
6
+ * @example
7
+ * import { READ_YOUR_WRITES_COOKIE } from 'totalum-sdk/d1';
8
+ * const fresh = request.headers.get('cookie')?.includes(`${READ_YOUR_WRITES_COOKIE}=`);
9
+ */
2
10
  export const READ_YOUR_WRITES_COOKIE = '__tlm_d1_w';
3
11
  /** Replicas lag the primary by 30–75 ms (Cloudflare); 10 s leaves a wide margin. */
4
12
  const WINDOW_SECONDS = 10;
@@ -13,7 +21,8 @@ export const REPLICA_READ_TIMEOUT_MS = 2000;
13
21
  const PURE_READ = /^\s*select\b/i;
14
22
  const LATE = Symbol('late');
15
23
  /**
16
- * Runs one request of a Worker with a replicated D1: a `GET`/`HEAD` reads through a
24
+ * Wraps one request of the app's Worker entry (the template does it for you) so reads use the nearest read replica
25
+ * and writes stay consistent for the user who made them. In detail: a `GET`/`HEAD` reads through a
17
26
  * `first-unconstrained` session — the nearest replica — unless the browser wrote in the last 10 s; any other method
18
27
  * uses the plain binding (writes go to the primary; measured faster than any session) and its response sets the short
19
28
  * cookie that sends that browser's next reads to the primary. A replica read that takes over `REPLICA_READ_TIMEOUT_MS`
@@ -22,6 +31,21 @@ const LATE = Symbol('late');
22
31
  * others; a sign-up waited on its first SELECT until the Worker was cancelled). `run` receives the env to hand to the
23
32
  * app, whose `totalumD1()` reads `TOTALUM_DB` from it on every statement. No D1 binding (Turso, a laptop): `run(env)`
24
33
  * unchanged.
34
+ *
35
+ * @param request - The incoming request.
36
+ * @param env - The Worker's env (with the `TOTALUM_DB` binding).
37
+ * @param run - Handles the request with the env to use (its `TOTALUM_DB` may be a session).
38
+ * @returns The response, with the short read-your-writes cookie after a write.
39
+ * @throws SQL_ERROR Whatever `run` throws is passed on unchanged.
40
+ * @example
41
+ * // worker.ts (the template's entry)
42
+ * import { withTotalumSession } from 'totalum-sdk/d1';
43
+ * import handler from './.open-next/worker.js';
44
+ *
45
+ * export default {
46
+ * fetch: (request: Request, env: Env, ctx: ExecutionContext) =>
47
+ * withTotalumSession(request, env, (env) => handler.fetch(request, env, ctx)),
48
+ * };
25
49
  */
26
50
  export async function withTotalumSession(request, env, run) {
27
51
  const db = env.TOTALUM_DB;
@@ -1,19 +1,52 @@
1
- import { T as TotalumClientOptions, U as Usage } from '../_types/errors.d-CIkIZmY-.js';
2
- export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-CIkIZmY-.js';
3
- import { E as EmailSendInput, k as EmailView } from '../_types/integrations.d-B3Ea8Iuc.js';
4
- export { j as EmailSendOutput } from '../_types/integrations.d-B3Ea8Iuc.js';
5
- import '../_types/coerce.d-Dgx5wOUv.js';
1
+ import { T as TotalumClientOptions, U as Usage } from '../_types/errors.d-BPpY5QKu.js';
2
+ export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-BPpY5QKu.js';
3
+ import { E as EmailSendInput, k as EmailView } from '../_types/integrations.d-BTZHy48D.js';
4
+ export { j as EmailSendOutput } from '../_types/integrations.d-BTZHy48D.js';
5
+ import '../_types/coerce.d-D_rylvPs.js';
6
6
 
7
7
  /**
8
- * `totalum.email` over the Totalum API `/v1/email/*` (scope `email`): HTML sent byte for byte, charged
9
- * per recipient. On dev only allow-listed recipients are accepted (`DEV_RECIPIENT_NOT_ALLOWED`).
8
+ * Creates the `email` namespace (the same object as `totalum.email`, scope `email`): HTML sent byte for byte, charged
9
+ * per recipient, sent from the project's own address `p-<project>@mail.totalum-project.com`.
10
+ *
11
+ * @param options - The project key, API URL and environment; each falls back to its environment variable.
12
+ * @returns The `email` namespace: the methods below.
13
+ * @example
14
+ * import { totalumEmail } from 'totalum-sdk/email';
15
+ *
16
+ * const email = totalumEmail();
17
+ * await email.send({ to: 'ana@example.com', subject: 'Welcome', html: '<p>Hi Ana</p>' });
10
18
  */
11
19
  declare function totalumEmail(options?: TotalumClientOptions): {
12
20
  /**
13
- * Sends one e-mail (HTML byte for byte); charged per recipient (`usage.credits`).
21
+ * Sends one e-mail from the project's sender address: sign-up confirmations, receipts, notifications, password
22
+ * resets. The HTML is sent byte for byte (inline your CSS); add `text` for clients that show plain text. Charged
23
+ * per recipient (`usage.credits`). Keep the returned `id` to check delivery with `get(id)`.
14
24
  *
25
+ * @param msg - The recipients, subject, content and options.
26
+ * @returns The e-mail's `id`, its status and what it cost.
27
+ * @throws DEV_RECIPIENT_NOT_ALLOWED (403) Only on Totalum's internal test environment (never for a customer
28
+ * project): a recipient that is not allow-listed.
29
+ * @throws VALIDATION_ERROR (400) A bad address, no `html`/`text`, more than 10 attachments, or a custom sender
30
+ * domain (not available yet).
31
+ * @throws EMAIL_SEND_FAILED (502) The e-mail could not be sent; the message says why.
15
32
  * @example
16
- * await email.send({ to: 'ana@example.com', subject: 'Welcome', html: '<p>Hi Ana</p>' });
33
+ * const sent = await totalum.email.send({
34
+ * to: 'ana@example.com',
35
+ * subject: 'Your invoice F-2026-0012',
36
+ * html: '<p>Hi Ana,</p><p>Your invoice is attached.</p>',
37
+ * text: 'Hi Ana, your invoice is attached.',
38
+ * attachments: [{ filename: 'F-2026-0012.pdf', url: invoice.url }],
39
+ * replyTo: 'billing@acme.com',
40
+ * });
41
+ * //=>
42
+ * {
43
+ * "success": true,
44
+ * "message": "Email sent successfully",
45
+ * "messageId": "01a12c0c-9e5f-74ea-a128-a72d3fdd47ae",
46
+ * "id": "em_01M4P0S77M6XCM69SYZWP1XME9",
47
+ * "status": "queued",
48
+ * "usage": { "credits": 0.0515 }
49
+ * }
17
50
  */
18
51
  send: (msg: EmailSendInput) => Promise<Omit<{
19
52
  success: true;
@@ -24,7 +57,29 @@ declare function totalumEmail(options?: TotalumClientOptions): {
24
57
  }, "usage"> & {
25
58
  usage: Usage;
26
59
  }>;
27
- /** `null` when the e-mail is not this project's. */
60
+ /**
61
+ * An e-mail's delivery status and history (queued, sent, delivered, bounced, …): show it on an admin page or
62
+ * retry a bounced address. Free.
63
+ *
64
+ * @param id - The `id` `send()` returned (`em_…`).
65
+ * @returns The e-mail and its events, or `null` when it is not this project's.
66
+ * @throws ENGINE_UNAVAILABLE (500) An unexpected error; retry.
67
+ * @example
68
+ * const view = await totalum.email.get('em_01M4P0S77M6XCM69SYZWP1XME9');
69
+ * //=>
70
+ * {
71
+ * "id": "em_01M4P0S77M6XCM69SYZWP1XME9",
72
+ * "from": "\"Acme\" <p-acme-crm@mail.totalum-project.com>",
73
+ * "to": ["ana@example.com"],
74
+ * "subject": "Your invoice F-2026-0012",
75
+ * "status": "delivered",
76
+ * "lastEventAt": "2026-10-11T17:39:02.000Z",
77
+ * "events": [
78
+ * { "type": "email.sent", "at": "2026-10-11T17:38:58.000Z" },
79
+ * { "type": "email.delivered", "at": "2026-10-11T17:39:02.000Z" }
80
+ * ]
81
+ * }
82
+ */
28
83
  get: (id: string) => Promise<EmailView | null>;
29
84
  };
30
85
  type TotalumEmail = ReturnType<typeof totalumEmail>;
@@ -2,21 +2,76 @@ import { isTotalumError } from '../errors.js';
2
2
  import { envelopeClient, jsonClient, withUsage } from '../http.js';
3
3
  export { TotalumError, isTotalumError } from '../errors.js';
4
4
  /**
5
- * `totalum.email` over the Totalum API `/v1/email/*` (scope `email`): HTML sent byte for byte, charged
6
- * per recipient. On dev only allow-listed recipients are accepted (`DEV_RECIPIENT_NOT_ALLOWED`).
5
+ * Creates the `email` namespace (the same object as `totalum.email`, scope `email`): HTML sent byte for byte, charged
6
+ * per recipient, sent from the project's own address `p-<project>@mail.totalum-project.com`.
7
+ *
8
+ * @param options - The project key, API URL and environment; each falls back to its environment variable.
9
+ * @returns The `email` namespace: the methods below.
10
+ * @example
11
+ * import { totalumEmail } from 'totalum-sdk/email';
12
+ *
13
+ * const email = totalumEmail();
14
+ * await email.send({ to: 'ana@example.com', subject: 'Welcome', html: '<p>Hi Ana</p>' });
7
15
  */
8
16
  export function totalumEmail(options = {}) {
9
17
  const call = jsonClient(options);
10
18
  const envelope = envelopeClient(options);
11
19
  return {
12
20
  /**
13
- * Sends one e-mail (HTML byte for byte); charged per recipient (`usage.credits`).
21
+ * Sends one e-mail from the project's sender address: sign-up confirmations, receipts, notifications, password
22
+ * resets. The HTML is sent byte for byte (inline your CSS); add `text` for clients that show plain text. Charged
23
+ * per recipient (`usage.credits`). Keep the returned `id` to check delivery with `get(id)`.
14
24
  *
25
+ * @param msg - The recipients, subject, content and options.
26
+ * @returns The e-mail's `id`, its status and what it cost.
27
+ * @throws DEV_RECIPIENT_NOT_ALLOWED (403) Only on Totalum's internal test environment (never for a customer
28
+ * project): a recipient that is not allow-listed.
29
+ * @throws VALIDATION_ERROR (400) A bad address, no `html`/`text`, more than 10 attachments, or a custom sender
30
+ * domain (not available yet).
31
+ * @throws EMAIL_SEND_FAILED (502) The e-mail could not be sent; the message says why.
15
32
  * @example
16
- * await email.send({ to: 'ana@example.com', subject: 'Welcome', html: '<p>Hi Ana</p>' });
33
+ * const sent = await totalum.email.send({
34
+ * to: 'ana@example.com',
35
+ * subject: 'Your invoice F-2026-0012',
36
+ * html: '<p>Hi Ana,</p><p>Your invoice is attached.</p>',
37
+ * text: 'Hi Ana, your invoice is attached.',
38
+ * attachments: [{ filename: 'F-2026-0012.pdf', url: invoice.url }],
39
+ * replyTo: 'billing@acme.com',
40
+ * });
41
+ * //=>
42
+ * {
43
+ * "success": true,
44
+ * "message": "Email sent successfully",
45
+ * "messageId": "01a12c0c-9e5f-74ea-a128-a72d3fdd47ae",
46
+ * "id": "em_01M4P0S77M6XCM69SYZWP1XME9",
47
+ * "status": "queued",
48
+ * "usage": { "credits": 0.0515 }
49
+ * }
17
50
  */
18
51
  send: async (msg) => withUsage(await envelope('POST', '/v1/email/send', msg)),
19
- /** `null` when the e-mail is not this project's. */
52
+ /**
53
+ * An e-mail's delivery status and history (queued, sent, delivered, bounced, …): show it on an admin page or
54
+ * retry a bounced address. Free.
55
+ *
56
+ * @param id - The `id` `send()` returned (`em_…`).
57
+ * @returns The e-mail and its events, or `null` when it is not this project's.
58
+ * @throws ENGINE_UNAVAILABLE (500) An unexpected error; retry.
59
+ * @example
60
+ * const view = await totalum.email.get('em_01M4P0S77M6XCM69SYZWP1XME9');
61
+ * //=>
62
+ * {
63
+ * "id": "em_01M4P0S77M6XCM69SYZWP1XME9",
64
+ * "from": "\"Acme\" <p-acme-crm@mail.totalum-project.com>",
65
+ * "to": ["ana@example.com"],
66
+ * "subject": "Your invoice F-2026-0012",
67
+ * "status": "delivered",
68
+ * "lastEventAt": "2026-10-11T17:39:02.000Z",
69
+ * "events": [
70
+ * { "type": "email.sent", "at": "2026-10-11T17:38:58.000Z" },
71
+ * { "type": "email.delivered", "at": "2026-10-11T17:39:02.000Z" }
72
+ * ]
73
+ * }
74
+ */
20
75
  get: async (id) => {
21
76
  try {
22
77
  return await call('GET', `/v1/email/${encodeURIComponent(id)}`);
package/dist/errors.js CHANGED
@@ -4,10 +4,13 @@
4
4
  * extend it, so one `isTotalumError(e)` check covers the whole package.
5
5
  */
6
6
  export class TotalumError extends Error {
7
+ /** `'TotalumError'` (or the subclass's name). */
7
8
  name = 'TotalumError';
9
+ /** The frozen error code (`FILE_NOT_FOUND`, `INSUFFICIENT_CREDITS`, …): branch on it, never on `message`. */
8
10
  errorCode;
9
11
  /** HTTP status; 0 for network errors, 500 for client-side refusals. */
10
12
  status;
13
+ /** Extra data about the failure (`maxBytes`, `retryAfterMs`, the field that failed validation), when any. */
11
14
  details;
12
15
  /** the Totalum API's request id, for support and the logs explorer. */
13
16
  requestId;
@@ -36,6 +39,20 @@ export class TotalumError extends Error {
36
39
  };
37
40
  }
38
41
  }
42
+ /**
43
+ * Whether `e` is an error thrown by `totalum-sdk` (`TotalumError` or a subclass): then `e.errorCode`, `e.status`,
44
+ * `e.details` and `e.requestId` are typed.
45
+ *
46
+ * @param e - Anything caught.
47
+ * @returns `true` for a `TotalumError`.
48
+ * @example
49
+ * try {
50
+ * await totalum.email.send(msg);
51
+ * } catch (e) {
52
+ * if (isTotalumError(e) && e.errorCode === 'INSUFFICIENT_CREDITS') return Response.json({ error: 'Out of credits' }, { status: 402 });
53
+ * throw e;
54
+ * }
55
+ */
39
56
  export function isTotalumError(e) {
40
57
  return e instanceof TotalumError;
41
58
  }
@@ -1,32 +1,120 @@
1
- import { F as FileDescriptor, l as FileUploadUrlInput, n as FilesListQuery } from '../_types/integrations.d-B3Ea8Iuc.js';
2
- export { m as FileUploadUrlOutput, o as FilesPage } from '../_types/integrations.d-B3Ea8Iuc.js';
3
- import { T as TotalumClientOptions } from '../_types/errors.d-CIkIZmY-.js';
4
- export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-CIkIZmY-.js';
5
- import '../_types/coerce.d-Dgx5wOUv.js';
1
+ import { F as FileDescriptor, l as FileUploadUrlInput, n as FilesListQuery } from '../_types/integrations.d-BTZHy48D.js';
2
+ export { m as FileUploadUrlOutput, o as FilesPage } from '../_types/integrations.d-BTZHy48D.js';
3
+ import { T as TotalumClientOptions } from '../_types/errors.d-BPpY5QKu.js';
4
+ export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-BPpY5QKu.js';
5
+ import '../_types/coerce.d-D_rylvPs.js';
6
6
 
7
+ /** The options of `totalumFiles()`: the client options plus the public files host `files.url()` builds URLs on. */
7
8
  interface TotalumFilesOptions extends TotalumClientOptions {
8
- /** The public files host; default `TOTALUM_FILES_URL`, else `https://files.totalum-project.com`. */
9
+ /** The public files host `files.url()` uses; default `TOTALUM_FILES_URL`, else `https://files.totalum-project.com`. */
9
10
  filesUrl?: string;
10
11
  }
12
+ /** How `files.upload()` stores a file: its name, type, visibility and folder. */
11
13
  interface UploadOptions {
14
+ /** The file name users see and downloads get (`invoice-12.pdf`). Default: the `File`'s own name, else `file`. */
12
15
  name?: string;
16
+ /**
17
+ * The MIME type (`image/png`, `application/pdf`). Default: the `Blob`'s type, else `application/octet-stream` — pass
18
+ * it for a string or bytes so browsers display the file. HTML and executables are refused.
19
+ */
13
20
  contentType?: string;
14
21
  /** `true` stores the file under the private prefix: no public URL, read it through `getSignedUrl`. */
15
22
  private?: boolean;
23
+ /** A folder name to group files by (`avatars`, `invoices/2026`); `files.list({ folder })` filters on it. */
16
24
  folder?: string;
17
25
  }
18
26
  /**
19
- * `totalum.files` (the Totalum API ) over HTTPS with the project key. Every call returns the file
27
+ * Creates the `files` namespace (the same object as `totalum.files`) with its own options. Every call returns the file
20
28
  * descriptor `{key, fileName, fileSize, contentType, fileUrl, visibility, createdAt}` meant to be stored as-is in the
21
29
  * row's file column; failures throw `TotalumError` with the Totalum API's frozen code.
30
+ *
31
+ * @param options - The project key, API URL, environment and public files host; each falls back to its environment
32
+ * variable.
33
+ * @returns The `files` namespace: the methods below.
34
+ * @example
35
+ * import { totalumFiles } from 'totalum-sdk/files';
36
+ *
37
+ * const files = totalumFiles({ env: 'dev' });
38
+ * const page = await files.list({ limit: 10 });
22
39
  */
23
40
  declare function totalumFiles(options?: TotalumFilesOptions): {
24
- /** Up to 25 MB through the Totalum API; larger files go through `getUploadUrl` + `complete`. */
41
+ /**
42
+ * Stores a file of up to 25 MB and returns its descriptor. Call it from a route handler or server action with the
43
+ * `File` from a form, a `Blob`, bytes or a string; larger files go through `getUploadUrl` + `complete`. Public files
44
+ * (the default) get a permanent, unguessable `fileUrl`; private ones only signed URLs. Each upload gets a new key:
45
+ * uploading the same name again never overwrites a file.
46
+ *
47
+ * @param input - The content: a `File`/`Blob` (its name and type are used), an `ArrayBuffer`/`Uint8Array`, or a
48
+ * string (stored as UTF-8 text).
49
+ * @param opts - The name, type, visibility and folder.
50
+ * @returns The stored file's descriptor: save it as-is in the row's file column.
51
+ * @throws FILE_TOO_LARGE (413) Over 25 MB (use `getUploadUrl`), or over the plan's largest file; `details.maxBytes`.
52
+ * @throws UNSUPPORTED_FILE_TYPE (415) HTML and executable files are never stored.
53
+ * @throws STORAGE_LIMIT_EXCEEDED (429) The plan's storage is full; `details.capBytes`.
54
+ * @throws RATE_LIMIT_EXCEEDED (429) The plan's uploads per minute or per month are used up; `details.period`.
55
+ * @throws FILE_UPLOAD_ERROR (502) The file could not be stored; retry.
56
+ * @example
57
+ * // src/app/api/avatar/route.ts
58
+ * import { totalum } from 'totalum-sdk';
59
+ *
60
+ * export async function POST(request: Request) {
61
+ * const form = await request.formData();
62
+ * const file = form.get('file') as File;
63
+ * const stored = await totalum.files.upload(file, { folder: 'avatars' });
64
+ * // save `stored` in the user's avatar column, then show stored.fileUrl
65
+ * return Response.json(stored);
66
+ * }
67
+ * //=>
68
+ * {
69
+ * "key": "p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.png",
70
+ * "fileName": "ana.png",
71
+ * "fileSize": 48213,
72
+ * "contentType": "image/png",
73
+ * "fileUrl": "https://files.totalum-project.com/p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.png",
74
+ * "visibility": "public-by-link",
75
+ * "createdAt": "2026-10-11T17:38:33.872Z",
76
+ * "etag": "\"8e72fb50886ffd6a1cdef31c54e3596b\"",
77
+ * "name": "ana.png",
78
+ * "url": "https://files.totalum-project.com/p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.png",
79
+ * "size": 48213,
80
+ * "id": "p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.png",
81
+ * "fileNameId": "p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.png"
82
+ * }
83
+ * @example
84
+ * // a private file: no fileUrl, read it with getSignedUrl
85
+ * const contract = await totalum.files.upload(pdfBytes, {
86
+ * name: 'contract-ana.pdf',
87
+ * contentType: 'application/pdf',
88
+ * private: true,
89
+ * });
90
+ * //=>
91
+ * { "key": "pv/acme-crm/01M4P0Q0X79QHFPBKBK2XHFTDG.pdf", "fileName": "contract-ana.pdf", "fileUrl": null, "visibility": "private", … }
92
+ */
25
93
  upload(input: Blob | ArrayBuffer | Uint8Array | string, opts?: UploadOptions): Promise<FileDescriptor>;
26
94
  /**
27
- * A presigned PUT (15 min, bound to this type and size) for a direct browser/Node upload; then `complete(key)`.
28
- * The largest file is 50 MB on the Free plan, 500 MB on Starter, Builder and Professional, and 1 GB on Enterprise;
29
- * above it `FILE_TOO_LARGE` (413, `details.maxBytes`).
95
+ * Starts a direct upload for a large file (over 25 MB, or any file you want the browser to send straight to
96
+ * storage): your server asks for an upload URL, the browser `PUT`s the bytes to it with the returned `headers`,
97
+ * then your server calls `complete(key)`. The URL lasts 15 minutes and only accepts exactly this type and size.
98
+ * The largest file is 50 MB on the Free plan, 500 MB on Starter, Builder and Professional, and 1 GB on Enterprise.
99
+ *
100
+ * @param input - The file's name, type and exact size in bytes, plus its folder and visibility.
101
+ * @returns Where and how to `PUT` the bytes, and the key to pass to `complete()`.
102
+ * @throws FILE_TOO_LARGE (413) Over the plan's largest file; `details.maxBytes`.
103
+ * @throws UNSUPPORTED_FILE_TYPE (415) HTML and executable files are never stored.
104
+ * @throws STORAGE_LIMIT_EXCEEDED (429) The plan's storage is full.
105
+ * @example
106
+ * // server: hand the browser an upload URL
107
+ * const upload = await totalum.files.getUploadUrl({ name: 'demo.mp4', contentType: 'video/mp4', size: 73400320 });
108
+ * // browser: await fetch(upload.uploadUrl, { method: 'PUT', headers: upload.headers, body: file });
109
+ * // server, afterwards:
110
+ * const stored = await totalum.files.complete(upload.key);
111
+ * //=>
112
+ * {
113
+ * "uploadUrl": "https://2e99a1160c18f1f4e0d589ff6105397e.r2.cloudflarestorage.com/project-files/p/acme-crm/01M4P0Q21EK7JVGY3MXK224DAH.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=900&…",
114
+ * "key": "p/acme-crm/01M4P0Q21EK7JVGY3MXK224DAH.mp4",
115
+ * "headers": { "content-type": "video/mp4", "content-length": "73400320" },
116
+ * "expiresAt": "2026-10-11T17:53:35.830Z"
117
+ * }
30
118
  */
31
119
  getUploadUrl: (input: FileUploadUrlInput) => Promise<{
32
120
  uploadUrl: string;
@@ -34,7 +122,30 @@ declare function totalumFiles(options?: TotalumFilesOptions): {
34
122
  headers: Record<string, string>;
35
123
  expiresAt: string;
36
124
  }>;
37
- /** Registers a file uploaded with `getUploadUrl` and returns its descriptor. */
125
+ /**
126
+ * Finishes a direct upload started with `getUploadUrl`: checks the bytes arrived with the declared size and type and
127
+ * returns the file's descriptor. Calling it again for a finished file returns the same descriptor.
128
+ *
129
+ * @param key - The `key` `getUploadUrl` returned.
130
+ * @returns The stored file's descriptor, the same shape `upload()` returns.
131
+ * @throws UPLOAD_NOT_COMPLETED (409) Nothing was uploaded to the URL yet, or the bytes did not match the declared size
132
+ * or type (the object is then deleted: start again).
133
+ * @throws FILE_NOT_FOUND (404) No upload was started for this key.
134
+ * @throws FILE_KEY_FORBIDDEN (403) The key belongs to another project.
135
+ * @example
136
+ * const stored = await totalum.files.complete('p/acme-crm/01M4P0Q21EK7JVGY3MXK224DAH.mp4');
137
+ * //=>
138
+ * {
139
+ * "key": "p/acme-crm/01M4P0Q21EK7JVGY3MXK224DAH.mp4",
140
+ * "fileName": "demo.mp4",
141
+ * "fileSize": 73400320,
142
+ * "contentType": "video/mp4",
143
+ * "fileUrl": "https://files.totalum-project.com/p/acme-crm/01M4P0Q21EK7JVGY3MXK224DAH.mp4",
144
+ * "visibility": "public-by-link",
145
+ * "createdAt": "2026-10-11T17:38:35.830Z",
146
+ * …
147
+ * }
148
+ */
38
149
  complete: (key: string) => Promise<{
39
150
  key: string;
40
151
  fileName: string;
@@ -52,8 +163,41 @@ declare function totalumFiles(options?: TotalumFilesOptions): {
52
163
  fileNameId?: string | undefined;
53
164
  }>;
54
165
  /**
55
- * One page of the project's files, newest first: filter by `folder`, `name`, `type`, created and size ranges; `cursor`
56
- * from the previous page's `nextCursor`.
166
+ * One page of the project's files, newest first, with the project's total storage. Filter by folder, name, type,
167
+ * creation date and size; fetch the next page with `cursor` set to the previous page's `nextCursor`.
168
+ *
169
+ * @param q - The filters and paging; every field is optional.
170
+ * @param q.folder - Only files in this folder (as given to `upload`).
171
+ * @param q.name - Only files whose name contains this text.
172
+ * @param q.type - Only files whose content type starts with this (`image/`, `application/pdf`).
173
+ * @param q.createdFrom - Only files created at or after this ISO date-time.
174
+ * @param q.createdTo - Only files created at or before this ISO date-time.
175
+ * @param q.sizeFrom - Only files of at least this many bytes.
176
+ * @param q.sizeTo - Only files of at most this many bytes.
177
+ * @param q.cursor - The previous page's `nextCursor`, for the next page.
178
+ * @param q.limit - Files per page, 1–100. Default 100.
179
+ * @returns The page of files and the project's totals.
180
+ * @throws VALIDATION_ERROR (400) A filter has the wrong format (a date that is not ISO, a limit over 100).
181
+ * @example
182
+ * const page = await totalum.files.list({ folder: 'invoices', limit: 2 });
183
+ * //=>
184
+ * {
185
+ * "items": [
186
+ * {
187
+ * "key": "p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.pdf",
188
+ * "fileName": "invoice-12.pdf",
189
+ * "fileSize": 18233,
190
+ * "contentType": "application/pdf",
191
+ * "fileUrl": "https://files.totalum-project.com/p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.pdf",
192
+ * "visibility": "public-by-link",
193
+ * "createdAt": "2026-10-11T17:38:33.872Z",
194
+ * …
195
+ * }
196
+ * ],
197
+ * "nextCursor": null,
198
+ * "totalBytes": 2445409,
199
+ * "fileCount": 11
200
+ * }
57
201
  */
58
202
  list: (q?: Partial<Record<keyof FilesListQuery, string | number>>) => Promise<{
59
203
  items: {
@@ -76,7 +220,27 @@ declare function totalumFiles(options?: TotalumFilesOptions): {
76
220
  totalBytes: number;
77
221
  fileCount: number;
78
222
  }>;
79
- /** The descriptor of one file; `FILE_NOT_FOUND` (404) when it does not exist. */
223
+ /**
224
+ * The descriptor of one file: check that a stored key still exists, or read its name, size and type.
225
+ *
226
+ * @param key - The file's `key`.
227
+ * @returns The file's descriptor.
228
+ * @throws FILE_NOT_FOUND (404) No such file (or it was deleted).
229
+ * @throws FILE_KEY_FORBIDDEN (403) The key belongs to another project.
230
+ * @example
231
+ * const file = await totalum.files.get('p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.txt');
232
+ * //=>
233
+ * {
234
+ * "key": "p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.txt",
235
+ * "fileName": "hello.txt",
236
+ * "fileSize": 19,
237
+ * "contentType": "text/plain",
238
+ * "fileUrl": "https://files.totalum-project.com/p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.txt",
239
+ * "visibility": "public-by-link",
240
+ * "createdAt": "2026-10-11T17:38:33.872Z",
241
+ * …
242
+ * }
243
+ */
80
244
  get: (key: string) => Promise<{
81
245
  key: string;
82
246
  fileName: string;
@@ -93,7 +257,6 @@ declare function totalumFiles(options?: TotalumFilesOptions): {
93
257
  id?: string | undefined;
94
258
  fileNameId?: string | undefined;
95
259
  }>;
96
- /** A presigned GET that R2 verifies: 15 min by default, at most 7 days — the way to hand a private file to a browser. */
97
260
  getSignedUrl: (key: string, opts?: {
98
261
  expiresInSeconds?: number;
99
262
  download?: boolean;
@@ -103,20 +266,80 @@ declare function totalumFiles(options?: TotalumFilesOptions): {
103
266
  expiresInSeconds?: number;
104
267
  download?: boolean;
105
268
  }) => Promise<string>;
106
- /** Signed GET URLs for many keys at once (`expiresInSeconds` ≤ 7 days, default 15 min), keyed by file key. */
269
+ /**
270
+ * Signed links for many files in one call — for a gallery or a table of private files.
271
+ *
272
+ * @param keys - The files' keys.
273
+ * @param opts - How long the links work and whether browsers download the files.
274
+ * @param opts.expiresInSeconds - Seconds the links work, at most 604800 (7 days). Default 900 (15 minutes).
275
+ * @param opts.download - `true` makes browsers download the files instead of showing them.
276
+ * @returns An object from each key to its signed URL.
277
+ * @throws FILE_NOT_FOUND (404) One of the keys does not exist; `details.key` names it.
278
+ * @throws FILE_KEY_FORBIDDEN (403) A key belongs to another project.
279
+ * @example
280
+ * const urls = await totalum.files.presign(photos.map((p) => p.key), { expiresInSeconds: 600 });
281
+ * //=>
282
+ * {
283
+ * "pv/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.jpg": "https://2e99a1160c18f1f4e0d589ff6105397e.r2.cloudflarestorage.com/project-files/pv/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=600&…",
284
+ * "pv/acme-crm/01M4P0Q0X79QHFPBKBK2XHFTDG.jpg": "https://2e99a1160c18f1f4e0d589ff6105397e.r2.cloudflarestorage.com/project-files/pv/acme-crm/01M4P0Q0X79QHFPBKBK2XHFTDG.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=600&…"
285
+ * }
286
+ */
107
287
  presign(keys: string[], opts?: {
108
288
  expiresInSeconds?: number;
109
289
  download?: boolean;
110
290
  /** @deprecated Since 1.0.0: use `expiresInSeconds`. */
111
291
  ttl?: number;
112
292
  }): Promise<Record<string, string>>;
113
- /** Deletes one key, or up to 1000 keys at once. */
293
+ /**
294
+ * Deletes one file, or up to 1,000 at once. The public URL stops working within the hour (edge caches expire).
295
+ * Deleting a list skips keys that are already gone; deleting a single key that does not exist throws.
296
+ *
297
+ * @param keys - One key, or an array of up to 1,000 keys.
298
+ * @returns Nothing once the files are deleted.
299
+ * @throws FILE_NOT_FOUND (404) The single key does not exist.
300
+ * @throws FILE_KEY_FORBIDDEN (403) A key belongs to another project.
301
+ * @throws VALIDATION_ERROR (400) More than 1,000 keys, or an empty list.
302
+ * @example
303
+ * await totalum.files.delete(user.avatar.key);
304
+ * await totalum.files.delete(oldInvoices.map((f) => f.key));
305
+ */
114
306
  delete(keys: string | string[]): Promise<void>;
115
- /** The object itself (private files included), with `Range` support; errors throw like every other call. */
307
+ /**
308
+ * The file's bytes as a `Response` (private files included), to read on the server or to return from a route that
309
+ * serves the file after your own permission check. Supports HTTP ranges for partial reads (video seeking).
310
+ *
311
+ * @param key - The file's `key`.
312
+ * @param opts - A byte range to read.
313
+ * @param opts.range - An HTTP `Range` header value, such as `bytes=0-1023`; the answer is then `206` with those bytes.
314
+ * @returns The fetch `Response` with the file's bytes, `content-type` and length.
315
+ * @throws FILE_NOT_FOUND (404) No such file.
316
+ * @throws FILE_KEY_FORBIDDEN (403) The key belongs to another project.
317
+ * @example
318
+ * // src/app/api/contracts/[id]/route.ts — serve a private file to its owner only
319
+ * const contract = await getContractForCurrentUser(params.id); // your own permission check
320
+ * const res = await totalum.files.stream(contract.fileKey);
321
+ * return new Response(res.body, { headers: { 'content-type': res.headers.get('content-type') ?? 'application/pdf' } });
322
+ * @example
323
+ * const res = await totalum.files.stream('p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.txt', { range: 'bytes=0-4' });
324
+ * console.log(res.status, await res.text());
325
+ * //=>
326
+ * 206 Hello
327
+ */
116
328
  stream: (key: string, opts?: {
117
329
  range?: string;
118
330
  }) => Promise<Response>;
119
- /** Sync, no I/O: the public URL of a `p/` key, or the authenticated the Totalum API URL of a private `pv/` key. */
331
+ /**
332
+ * The URL of a file, computed locally (no request): the public URL of a public `p/…` key, or for a private `pv/…`
333
+ * key the Totalum API URL that serves it only with the project key (a server-side fetch). To show a private file in
334
+ * a browser use `getSignedUrl` instead.
335
+ *
336
+ * @param key - The file's `key`.
337
+ * @returns The file's URL.
338
+ * @example
339
+ * totalum.files.url('p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.png');
340
+ * //=>
341
+ * "https://files.totalum-project.com/p/acme-crm/01M4P0PZXJ0DPQM7R20QVZ0EKC.png"
342
+ */
120
343
  url(key: string): string;
121
344
  };
122
345
  type TotalumFiles = ReturnType<typeof totalumFiles>;