@thecms/sdk 0.0.0-stage → 0.20.1-dev.122

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.
@@ -0,0 +1,1075 @@
1
+ type RecordStatus = "draft" | "published" | "archived";
2
+ interface RecordMeta {
3
+ _id: string;
4
+ _entity: string;
5
+ _version: number;
6
+ _schemaVersion: number;
7
+ _status: RecordStatus;
8
+ _createdAt: string;
9
+ _updatedAt: string;
10
+ _publishedAt?: string | null;
11
+ /** NEXT-META (D326): the first publication, set once and kept by every later version */
12
+ _firstPublishedAt?: string | null;
13
+ _deletedAt?: string | null;
14
+ /** F1.13: actors (user:/apikey:/system:) — management API only, never in public delivery */
15
+ _createdBy?: string;
16
+ _updatedBy?: string;
17
+ _publishedBy?: string;
18
+ _deletedBy?: string;
19
+ }
20
+ type EntityRecord = RecordMeta & Record<string, unknown>;
21
+ interface QueryResult<T = EntityRecord> {
22
+ items: T[];
23
+ total?: number;
24
+ hasMore: boolean;
25
+ nextCursor?: string;
26
+ }
27
+ type GeoRestrictionMode = "none" | "whitelist";
28
+ interface ResolvedLocale {
29
+ requested: string;
30
+ resolved: string;
31
+ isDefault: boolean;
32
+ defaultLocale: string;
33
+ }
34
+ /** F3.3 — the point of interest of an image (0..1 from the left / top): `fit=cover` crops around it. */
35
+ interface FocalPoint {
36
+ x: number;
37
+ y: number;
38
+ }
39
+ /**
40
+ * F3.0 (D38) — an `asset` field in public delivery. `url` is the API path of
41
+ * the original file (variants: `?w=&fmt=`, closed presets).
42
+ */
43
+ interface ResolvedAsset {
44
+ id: string;
45
+ /**
46
+ * with the asset's focal point in it (`?fp=`) when it has one; a photo
47
+ * (JPEG, PNG, WebP, AVIF) asks for `fmt=auto` (F3b.6: the best format the
48
+ * browser takes). VIDEO VI3 (D549): absent for a paid video — its file is
49
+ * not served publicly.
50
+ */
51
+ url?: string;
52
+ /** F3b.6: a photo's `srcset` — width steps up to its own width (`w=…`, `fmt=auto`) */
53
+ srcset?: string;
54
+ mimeType: string;
55
+ width?: number;
56
+ height?: number;
57
+ /** F3.3: alternative text in the locale of the answer (else the default one) */
58
+ alt?: string;
59
+ focal?: FocalPoint;
60
+ /**
61
+ * VIDEO (D539): a video with a stream ready at the workspace's video
62
+ * provider — adaptive playback (HLS), its poster image and its length in
63
+ * seconds; built at answer time, never stored. Absent while the stream is
64
+ * being made, when it failed, or without the `video` feature: the file only.
65
+ */
66
+ stream?: {
67
+ hls: string;
68
+ poster: string;
69
+ duration: number;
70
+ };
71
+ /**
72
+ * VIDEO VI3 (D540, D550, D551): a paid video (protected by a product in some
73
+ * field) — wherever it shows, never its file's nor its stream's address: its
74
+ * poster (served by the API, no token) and its length; the playback comes
75
+ * from `unlock` with the key of the field that sells it.
76
+ */
77
+ paid?: {
78
+ poster: string | null;
79
+ duration: number | null;
80
+ };
81
+ }
82
+ /**
83
+ * 22/10/2026 (D150) — a `link` field in public delivery: a page as its path
84
+ * in the locale of the answer (it follows slug changes) with the slug of its
85
+ * application, or a free address as written. A page no longer online → null.
86
+ */
87
+ type ResolvedLink = {
88
+ kind: "page";
89
+ app: string;
90
+ path: string;
91
+ } | {
92
+ kind: "url";
93
+ href: string;
94
+ };
95
+ type WidgetType = "entity" | "container";
96
+ interface ResolvedViewSeo {
97
+ title: string | null;
98
+ description: string | null;
99
+ ogImage: string | null;
100
+ canonicalUrl: string | null;
101
+ }
102
+ interface ResolvedBodyItem {
103
+ id: string;
104
+ /** SITE-EDITOR (D279): the placement — unique in the page, stable across edits (a site's React `key`) */
105
+ uid: string;
106
+ /** SITE-EDITOR (D279): where the placement lives — the page itself or its layout */
107
+ in: "view" | "layout";
108
+ set: string;
109
+ type: WidgetType;
110
+ shape: string | null;
111
+ /** the heading, in the answer's language (DESIGN R10) */
112
+ title?: string;
113
+ /** DESIGN R9: the values of the shape's own fields, in the answer's language (files and links resolved) */
114
+ values?: Record<string, unknown>;
115
+ items?: EntityRecord[];
116
+ /** F2.2a: only when the instance paginates; links built by the CMS */
117
+ pagination?: ResolvedPagination;
118
+ /** F2.2a: the filters the visitor may narrow, with their links */
119
+ filters?: ResolvedExposedFilter[];
120
+ slots?: Record<string, ResolvedBodyItem[]>;
121
+ /**
122
+ * F3b.7 (D59): the products on sale that unlock fields of these records,
123
+ * priced for the visitor's country (items carry `_protected: { field:
124
+ * { key, products } }`)
125
+ */
126
+ products?: Record<string, PublicProduct>;
127
+ _error?: string;
128
+ _geoBlocked?: boolean;
129
+ }
130
+ /**
131
+ * SITE-EDITOR E6 (D304) — a link of a public list (a page, a filter): `token`
132
+ * is the CMS's signed state of the list — a site puts it in its own address
133
+ * (`?f=<token>`, read by `@thecms/next`) and the page's resolve applies it to
134
+ * the block it comes from (`route?list=`, D305); `url` is the same list on the
135
+ * API (`GET /public/records/:token`), for whoever calls it alone. A preview has
136
+ * only the token (that route reads the published site).
137
+ */
138
+ interface ListLink {
139
+ token: string;
140
+ url?: string;
141
+ }
142
+ /**
143
+ * F2.2a — pagination of a resolved list. The public never builds a query:
144
+ * the next / previous / numbered pages are links (D304: `{ token, url }`).
145
+ */
146
+ interface ResolvedPagination {
147
+ page: number;
148
+ pageSize: number;
149
+ total: number;
150
+ totalPages: number;
151
+ prev?: ListLink;
152
+ next?: ListLink;
153
+ /** a window: first, last and the pages around the current one */
154
+ pages: ({
155
+ page: number;
156
+ } & ListLink)[];
157
+ }
158
+ /** F2.2a — a filter the visitor may narrow (multiple choice among the editor's values). */
159
+ interface ResolvedExposedFilter {
160
+ field: string;
161
+ label: string;
162
+ multiple: true;
163
+ /** back to every value the editor allowed */
164
+ clear: ListLink;
165
+ options: ({
166
+ value: string;
167
+ label: string;
168
+ selected: boolean;
169
+ } & ListLink)[];
170
+ }
171
+ /**
172
+ * F3.1 — the public answer of `GET /:tenant/views/:appSlug/route?path=`
173
+ * (F1.2 / F2.2e / F2.2i): the resolved page with its route, or a redirect
174
+ * returned as data, or the application's "not found" page (status 404).
175
+ */
176
+ interface PageRoute {
177
+ locale: string;
178
+ path: string;
179
+ canonical: string;
180
+ /** F2.2e: the page in every locale where it is translated + `x-default` */
181
+ alternates: {
182
+ locale: string;
183
+ path: string;
184
+ }[];
185
+ }
186
+ /**
187
+ * `_visit` (VISITS, D159): the signed key of this page view, the same for every
188
+ * visitor — the SDK sends it back (`POST /public/visits`). Absent in previews.
189
+ */
190
+ type ResolvedPage = ResolvedView & {
191
+ _route: PageRoute | null;
192
+ _visit?: string;
193
+ _live?: LiveSource;
194
+ /** PREVIEW (D222): drafts shown through a preview session — never a public page */
195
+ _preview?: true;
196
+ };
197
+ /** VISITS (D159) — one page seen, as the histories answer it. */
198
+ interface VisitItem {
199
+ /** ISO time */
200
+ at: string;
201
+ /** the application's id */
202
+ app: string;
203
+ /** the page's path now, in the locale it was seen in; null when no longer online */
204
+ path: string | null;
205
+ locale: string;
206
+ /** the page's record (a template page), else null */
207
+ entity: string | null;
208
+ record: string | null;
209
+ /** a reader's history: the anonymous id the visit came from (before signing in), else null */
210
+ visitor: string | null;
211
+ }
212
+ /** VISITS — a history, newest first; `nextCursor` asks for the next page. */
213
+ interface VisitHistory {
214
+ items: VisitItem[];
215
+ nextCursor: string | null;
216
+ }
217
+ /** VISITS — a page among the most visited of the last days. */
218
+ interface PopularPage {
219
+ app: string;
220
+ /** the view (with `record`, it names the page: LIVE L4 matches what is read right now) */
221
+ view: string;
222
+ /** in the default locale; null when no longer online */
223
+ path: string | null;
224
+ entity: string | null;
225
+ record: string | null;
226
+ visits: number;
227
+ /** distinct visitors and readers (visits without an id not counted) */
228
+ visitors: number;
229
+ }
230
+ /**
231
+ * LIVE L3 — what lets a public answer be followed (`cms.live.watch` of the
232
+ * SDK): the cache tags it is made of (without the workspace) and its own
233
+ * address on the API, to ask it again (with `?v=`) when one of them changes.
234
+ */
235
+ interface LiveSource {
236
+ tags: string[];
237
+ /** `/v1/{workspace}/…` with its query, without `v` */
238
+ url: string;
239
+ /** PREVIEW P4 (D225): a preview follows the drafts on the preview stream */
240
+ channel?: "preview";
241
+ /**
242
+ * SITE-CONNECTION (D271): a preview's listening token — it opens the preview's
243
+ * stream of changes, never a page. The session's token is never in the answer.
244
+ */
245
+ listen?: string;
246
+ }
247
+ interface RedirectAnswer {
248
+ redirect: {
249
+ status: 301 | 302;
250
+ location: string;
251
+ };
252
+ }
253
+ /**
254
+ * F3b.7 (D55) — the answer of `POST /monetization/unlock`: one result per key,
255
+ * in the order sent. `not_owned`: no product the end-user bought unlocks it;
256
+ * `unavailable`: the record is no longer readable for them (withdrawn,
257
+ * deleted or outside its area — indistinguishable); `invalid`: an altered
258
+ * key or one of another workspace.
259
+ */
260
+ interface UnlockAnswer {
261
+ results: ({
262
+ key: string;
263
+ value: unknown;
264
+ } | {
265
+ key: string;
266
+ error: "not_owned" | "unavailable" | "invalid";
267
+ })[];
268
+ }
269
+ /** F3b.7 — a protected field of a public record: its value is `null`; the key unlocks it, any of the products sells it. */
270
+ interface ProtectedField {
271
+ key: string;
272
+ products: string[];
273
+ }
274
+ /** F3b.7 (D59) — a product on sale in a widget block, priced for the visitor's country. */
275
+ interface PublicProduct {
276
+ id: string;
277
+ title: string;
278
+ /** PAYMENTS (D353): `provider` = who collects this price (the price's own, else the workspace's default; null = nobody yet) */
279
+ price: {
280
+ amount: number;
281
+ currency: string;
282
+ provider: string | null;
283
+ };
284
+ }
285
+ /** PAYMENTS (D354) — what `POST /checkout` answers: the order and the provider's payment page. */
286
+ interface CheckoutStarted {
287
+ order: string;
288
+ redirectUrl: string;
289
+ }
290
+ /** PAYMENTS (D355) — an order as its reader sees it (`GET /me/orders/:id`). */
291
+ interface ReaderOrder {
292
+ id: string;
293
+ productId: string;
294
+ status: "pending" | "paid" | "failed" | "partially_refunded" | "refunded" | "disputed";
295
+ /** minor units */
296
+ amount: number;
297
+ currency: string;
298
+ refundedAmount: number;
299
+ createdAt: string;
300
+ updatedAt: string;
301
+ }
302
+ /** PAYMENTS (D360) — one purchase of a reader (`GET /me/purchases`, `GET /readers/:id/purchases`), newest first. */
303
+ interface ReaderPurchase {
304
+ id: string;
305
+ product: {
306
+ id: string;
307
+ title: string | null;
308
+ };
309
+ /** the order it came from; null = given by hand from the dashboard */
310
+ order: string | null;
311
+ /** when it was bought or given */
312
+ at: string;
313
+ /** what was paid (minor units) and in which currency; null when given by hand */
314
+ amount: number | null;
315
+ currency: string | null;
316
+ /** when the access ends; null = for ever */
317
+ endsAt: string | null;
318
+ /** the access is in force now */
319
+ active: boolean;
320
+ /** the product can still be bought (to buy it again or renew) */
321
+ onSale: boolean;
322
+ }
323
+ interface ResolvedView {
324
+ id: string;
325
+ application: string;
326
+ entity: string | null;
327
+ /** SITE-EDITOR (D280): the page's record (a template's, or a static page's own), else null */
328
+ record: string | null;
329
+ /** NEXT-META (D326): the page's record first published and last published (Open Graph article dates); only with `record` */
330
+ recordDates?: {
331
+ published: string | null;
332
+ modified: string | null;
333
+ };
334
+ seo: ResolvedViewSeo;
335
+ geo: {
336
+ mode: GeoRestrictionMode;
337
+ countries: string[] | null;
338
+ continents: string[] | null;
339
+ };
340
+ /**
341
+ * Fully composed render tree. If the view has a layout, this IS the layout
342
+ * tree with the view's own body spliced in at the content sentinel position
343
+ * (root level or inside a container slot). No layout awareness needed client-side.
344
+ */
345
+ body: ResolvedBodyItem[];
346
+ _version: number;
347
+ _locale: ResolvedLocale;
348
+ _cacheTtl: number;
349
+ _resolvedAt: string;
350
+ }
351
+ /**
352
+ * 21/10/2026 — the workspace's languages and, per language, its frontends'
353
+ * texts nested by dotted key (part of `PublicSite` since SITE-KEY).
354
+ */
355
+ interface PublicMessages {
356
+ defaultLocale: string;
357
+ locales: string[];
358
+ messages: Record<string, Record<string, unknown>>;
359
+ }
360
+ /**
361
+ * SITE-KEY (D253) — what `GET /:tenant/views/:app/site` answers: what holds for
362
+ * the whole site of an application. `globals` are the workspace's variables
363
+ * (`{ key: value }`, every variant), `messages` its texts per language.
364
+ */
365
+ interface PublicSite {
366
+ /** the published site (the application's Site URL), null when not set */
367
+ url: string | null;
368
+ /** NEXT-META (D325): the site's name (the application's Site name), null when not set */
369
+ name: string | null;
370
+ /** the application publishes a sitemap */
371
+ sitemap: boolean;
372
+ /** SEO-AI (D490): the site offers itself to AI readers (llms.txt, the pages' clean text) — with the sitemap, unless switched off */
373
+ aiReaders: boolean;
374
+ /** SEO-AI (D490): the AI crawlers robots.txt shuts out while `aiReaders` is off (the platform's list, updated with its releases) */
375
+ aiCrawlers: string[];
376
+ defaultLocale: string;
377
+ locales: string[];
378
+ globals: Record<string, unknown>;
379
+ messages: Record<string, Record<string, unknown>>;
380
+ /** SIGN-IN (D345): the workspace's sign-in methods that are on — `icon` is the template's name, null for a method of its own */
381
+ signIn: {
382
+ key: string;
383
+ label: string;
384
+ icon: "google" | "github" | "microsoft" | "apple" | null;
385
+ }[];
386
+ /** KIT (D384): the shapes of every set of the workspace, names only (`block.set` → its `block.shape`s; a container has none) */
387
+ shapes: Record<string, string[]>;
388
+ _live?: LiveSource;
389
+ }
390
+
391
+ /** A filter of a management list: `where[field][op]=value` (lists for in / nin). */
392
+ interface ListFilter {
393
+ field: string;
394
+ op: "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "contains" | "starts_with" | "in" | "nin" | "within" | "descendantsOf";
395
+ value: string | number | boolean | (string | number)[];
396
+ }
397
+ /** F3b.13 — a management list of records: the API key's query parameters (D39 reversed). */
398
+ interface RecordsQuery {
399
+ where?: ListFilter[];
400
+ sort?: {
401
+ field: string;
402
+ direction?: "asc" | "desc";
403
+ };
404
+ /** 1..200 (S68) */
405
+ limit?: number;
406
+ cursor?: string;
407
+ offset?: number;
408
+ /** full-text (F3.3) */
409
+ search?: string;
410
+ status?: "draft" | "published" | "all";
411
+ /** FAST-PATHS D508: only the records with no value in this language (not the default one) in any translatable field */
412
+ untranslated?: string;
413
+ /** relations resolved (0..5) */
414
+ depth?: number;
415
+ lang?: string;
416
+ /**
417
+ * F4.8: the whole documents (read from storage, slower). Without it each
418
+ * item is the list's summary: the filterable and table fields, relations
419
+ * and system fields — never markdown or json content.
420
+ */
421
+ full?: boolean;
422
+ }
423
+ /** A media file as the management API returns it. */
424
+ interface MediaFile {
425
+ id: string;
426
+ filename: string;
427
+ mimeType: string;
428
+ sizeBytes: number;
429
+ storagePath: string;
430
+ links?: Record<string, string[]>;
431
+ [key: string]: unknown;
432
+ }
433
+ /** A product as the management API returns it. */
434
+ interface ManagedProduct {
435
+ id: string;
436
+ title: string;
437
+ price: {
438
+ default: {
439
+ amount: number;
440
+ currency: string;
441
+ };
442
+ regional?: unknown[];
443
+ };
444
+ rules: unknown[];
445
+ links?: Record<string, string[]>;
446
+ archivedAt?: string;
447
+ [key: string]: unknown;
448
+ }
449
+ /** What an upload takes beside the file. */
450
+ interface UploadOptions {
451
+ /** the records it belongs to (entity → ids) */
452
+ links?: Record<string, string[]>;
453
+ /** the file field whose rule it must respect — READER-WRITES (D566): required for an app's user */
454
+ for?: {
455
+ schema: string;
456
+ field: string;
457
+ };
458
+ }
459
+ /** A manual redirect of an application (F2.2i). */
460
+ interface ManagedRedirect {
461
+ fromPath: string;
462
+ toLocation: string;
463
+ status: number;
464
+ }
465
+
466
+ interface LiveData {
467
+ _live?: LiveSource;
468
+ }
469
+
470
+ interface SdkOptions {
471
+ /**
472
+ * SITE-KEY (D252): the public key of the application this site shows
473
+ * (dashboard → the application's settings). It names the workspace and the
474
+ * application; not a secret — it may sit in the browser's code.
475
+ */
476
+ publicKey: string;
477
+ /**
478
+ * SITE-KEY (D258): the workspace's API key — server-side only (BFF, build,
479
+ * app backend), never in a browser. With it the API trusts `forwardedFor`
480
+ * for the geo, and the management part (`records`, `media`, `products`) works.
481
+ */
482
+ secretKey?: string;
483
+ /**
484
+ * AGENT D461: the personal token of `thecms login`, for the project's own
485
+ * scripts (an import, a migration of contents): the management part acts as
486
+ * the person, with their role, and ends with them. Read from the CLI
487
+ * (`personalToken()` of the CLI's package) at each run, never written in a
488
+ * file, never in a site. The API key (`secretKey`) stays for what runs
489
+ * without a person: the CI, a site's server that writes. Not both.
490
+ */
491
+ personalToken?: string;
492
+ /**
493
+ * READER-WRITES (D559–D566): an app without a server — the signed-in
494
+ * user's access token, asked at each call (the app keeps it and renews it
495
+ * with `refreshSession`). The management part (`records`, `media`) then
496
+ * acts as that user, with the role the owner gives to the users of this
497
+ * application: their own records and files, others' only as that role reads
498
+ * them. Never with `secretKey` or `personalToken`.
499
+ */
500
+ reader?: () => string | null | undefined;
501
+ /** a custom fetch (tests, edge runtimes, caching) */
502
+ fetch?: (input: string, init?: RequestInit) => Promise<Response>;
503
+ /**
504
+ * VISITS (D159): `page()` records each page seen (default true); `false`
505
+ * turns it off for the whole site, `track: false` on a call for one page.
506
+ */
507
+ track?: boolean;
508
+ /**
509
+ * VISITS (D160): the site's id of the current anonymous visitor, ONLY once
510
+ * they consented (e.g. read from the site's own cookie); without it a page
511
+ * view counts in the totals, not in a history. See `newVisitorId()`.
512
+ */
513
+ visitor?: () => string | null | undefined;
514
+ /** LIVE (`live.watch`): the longest random wait before asking changed data again (ms, default 3000), the polling interval without a stream (default 10000) */
515
+ live?: {
516
+ jitterMs?: number;
517
+ pollMs?: number;
518
+ };
519
+ }
520
+ /** SITE-KEY (D253): what `site()` answers — the site's settings, languages, globals, messages, and whose site it is. */
521
+ type SiteAnswer = PublicSite & {
522
+ workspace: string;
523
+ app: string;
524
+ };
525
+ /** What `page()` takes beside the path. */
526
+ interface PageOptions extends CallOptions {
527
+ /** a preview token (`?preview=` of the dashboard's preview URL) */
528
+ preview?: string;
529
+ /** preview only: render as seen from this country */
530
+ country?: string;
531
+ /** VISITS: record this page view (default: the SDK's `track`) */
532
+ track?: boolean;
533
+ /** VISITS: the anonymous visitor of this view (default: the SDK's `visitor()`) */
534
+ visitor?: string;
535
+ /** VISITS: the signed-in reader's token — the view joins their history */
536
+ readerToken?: string;
537
+ /**
538
+ * SITE-EDITOR E6 (D305): list states to apply (a list link's `token`, or the
539
+ * link / its `url`): each block of the page they come from resolves at that
540
+ * state, in the same answer. One of another page is ignored.
541
+ */
542
+ lists?: (string | ListLink)[];
543
+ }
544
+ /** VISITS (D160): a new random id for an anonymous visitor — the site keeps it (its cookie) once they consented. */
545
+ declare function newVisitorId(): string;
546
+ interface CallOptions {
547
+ /** the visitor's IP, forwarded by a server caller (needs `secretKey`) */
548
+ forwardedFor?: string;
549
+ /** SEO-AI SA7 (D506): read from nowhere known (`XX`) — an AI reader's text; it only narrows what the answer shows */
550
+ unknownVisitor?: boolean;
551
+ signal?: AbortSignal;
552
+ }
553
+ /** The answer of `page()`: what a catch-all route of the site renders. */
554
+ type PageResult = {
555
+ kind: "page";
556
+ page: ResolvedPage;
557
+ } | {
558
+ kind: "redirect";
559
+ status: 301 | 302;
560
+ location: string;
561
+ }
562
+ /** no page at this address — the site answers with its own 404 page */
563
+ | {
564
+ kind: "notFound";
565
+ }
566
+ /** the page's record is not available in the visitor's country — answer with 403 */
567
+ | {
568
+ kind: "geoBlocked";
569
+ country?: string;
570
+ continent?: string;
571
+ };
572
+ interface EndUserSession {
573
+ accessToken: string;
574
+ /** seconds */
575
+ expiresIn: number;
576
+ /** READER-WRITES (D571): only for a sign-in with PKCE (an app) — `refreshSession` renews the session with it, 60 days from its last use */
577
+ refreshToken?: string;
578
+ user: {
579
+ id: string;
580
+ email: string;
581
+ };
582
+ }
583
+ /** READER-WRITES (D571): a renewed app session — a new 1-hour token, the same refresh token. */
584
+ interface RenewedSession {
585
+ accessToken: string;
586
+ refreshToken: string;
587
+ /** seconds */
588
+ expiresIn: number;
589
+ }
590
+ /**
591
+ * theCMS. SDK (D54) for sites and apps, server or browser. One instance = one
592
+ * site (SITE-KEY D255): the application its public key names.
593
+ *
594
+ * - **Presentation** (anyone, browser included): pages, the site's settings,
595
+ * globals and messages, their lists' links, sitemap, end-user login,
596
+ * purchases, orders, checkout, unlock. The public answers are never personalised
597
+ * (D9): protected fields are `null` with a key in `_protected`, and
598
+ * `unlock(keys)` gives the values the visitor bought.
599
+ * - **Management** (`records`, `media`, `products`, `languages`,
600
+ * `applications`): server-side, with the workspace's `secretKey` or the
601
+ * person's `personalToken` (D461) — without either every call fails at once
602
+ * (D39 reversed, F3b.13), before any request.
603
+ */
604
+ declare function createSdk(options: SdkOptions): {
605
+ live: {
606
+ /**
607
+ * Follow data the site already has — a page (`r.page`), a list (`follow`),
608
+ * the globals, the messages: `callback(fresh)` with the new data
609
+ * whenever it changes (`null` when it no longer exists). The site applies
610
+ * it as it wants. Returns `stop()`.
611
+ */
612
+ watch: <T extends LiveData>(data: T, callback: (fresh: T | null) => void) => () => void;
613
+ };
614
+ visits: {
615
+ of: (who: {
616
+ visitor: string;
617
+ } | {
618
+ reader: string;
619
+ }, page?: {
620
+ limit?: number;
621
+ cursor?: string;
622
+ }) => Promise<VisitHistory & {
623
+ reader: string | null;
624
+ }>;
625
+ popular: (q?: {
626
+ entity?: string;
627
+ days?: number;
628
+ limit?: number;
629
+ }) => Promise<{
630
+ items: PopularPage[];
631
+ }>;
632
+ forget: (visitor: string) => Promise<{
633
+ removed: number;
634
+ }>;
635
+ /** The signed-in reader's pages, newest first (also the ones their browser read before signing in). */
636
+ me(readerToken: string, page?: {
637
+ limit?: number;
638
+ cursor?: string;
639
+ }, opts?: CallOptions): Promise<VisitHistory>;
640
+ /**
641
+ * Record a view of a page you already have (NEXT, D187: resolved on a server, drawn in the browser):
642
+ * its `_visit` key goes in the queue like `page()` would send it. Nothing without a key (a preview).
643
+ */
644
+ track(data: {
645
+ _visit?: string;
646
+ }, opts?: {
647
+ visitor?: string;
648
+ readerToken?: string;
649
+ }): void;
650
+ /** Send the queued page views now (a browser sends them by itself when the page hides, or every 30 s). */
651
+ flush(): void;
652
+ };
653
+ records: {
654
+ list: (entity: string, query?: RecordsQuery) => Promise<QueryResult<EntityRecord>>;
655
+ get: (entity: string, id: string, opts?: {
656
+ depth?: number;
657
+ lang?: string;
658
+ }) => Promise<EntityRecord>;
659
+ create: (entity: string, data: Record<string, unknown>, opts?: {
660
+ forwardedFor?: string;
661
+ }) => Promise<EntityRecord>;
662
+ update: (entity: string, id: string, data: Record<string, unknown>) => Promise<EntityRecord>;
663
+ publish: (entity: string, id: string, opts?: {
664
+ publishAt?: string;
665
+ unpublishAt?: string;
666
+ }) => Promise<EntityRecord>;
667
+ unpublish: (entity: string, id: string) => Promise<EntityRecord>;
668
+ delete: (entity: string, id: string) => Promise<unknown>;
669
+ };
670
+ media: {
671
+ list: (query?: {
672
+ limit?: number;
673
+ offset?: number;
674
+ search?: string;
675
+ mime?: string;
676
+ }) => Promise<{
677
+ items: MediaFile[];
678
+ total: number;
679
+ }>;
680
+ get: (id: string) => Promise<MediaFile>;
681
+ upload: (file: Blob, filename: string, opts?: Record<string, string[]> | UploadOptions) => Promise<MediaFile>;
682
+ setLinks: (id: string, links: Record<string, string[]>) => Promise<MediaFile>;
683
+ update: (id: string, details: {
684
+ alt?: Record<string, string> | null;
685
+ focal?: {
686
+ x: number;
687
+ y: number;
688
+ } | null;
689
+ }) => Promise<MediaFile>;
690
+ delete: (id: string) => Promise<unknown>;
691
+ };
692
+ products: {
693
+ list: () => Promise<ManagedProduct[]>;
694
+ get: (id: string) => Promise<ManagedProduct>;
695
+ create: (input: Record<string, unknown>) => Promise<ManagedProduct>;
696
+ update: (id: string, input: Record<string, unknown>) => Promise<ManagedProduct>;
697
+ archive: (id: string) => Promise<ManagedProduct>;
698
+ };
699
+ languages: {
700
+ get: () => Promise<{
701
+ defaultLocale: string;
702
+ locales: string[];
703
+ }>;
704
+ add: (locale: string) => Promise<{
705
+ defaultLocale: string;
706
+ locales: string[];
707
+ }>;
708
+ };
709
+ applications: {
710
+ list: () => Promise<{
711
+ id: string;
712
+ slug: string;
713
+ name: string;
714
+ siteUrl?: string;
715
+ publicKey: string | null;
716
+ }[]>;
717
+ redirects: {
718
+ list: (appId: string) => Promise<ManagedRedirect[]>;
719
+ add: (appId: string, redirect: {
720
+ fromPath: string;
721
+ toLocation: string;
722
+ status?: 301 | 302;
723
+ }) => Promise<ManagedRedirect>;
724
+ remove: (appId: string, fromPath: string) => Promise<{
725
+ deleted: true;
726
+ }>;
727
+ };
728
+ };
729
+ /**
730
+ * The page of the site at a full public path (locale prefix included, the
731
+ * default locale has none). Redirects and "not found" come back as data:
732
+ * the site decides how to answer.
733
+ */
734
+ page(path: string, opts?: PageOptions): Promise<PageResult>;
735
+ /**
736
+ * PREVIEW P5 (D227): is this preview token's session still in force, and
737
+ * until when? `{ active: false }` once it expired or was ended.
738
+ */
739
+ previewStatus(token: string, opts?: CallOptions): Promise<{
740
+ active: true;
741
+ expiresAt: string;
742
+ } | {
743
+ active: false;
744
+ }>;
745
+ /**
746
+ * SITE-KEY (D253): what holds for the whole site — its address, the prefix
747
+ * of its preview route, whether it has a sitemap, the languages, the
748
+ * workspace's globals (`{ key: value }`, every variant: a translated value
749
+ * as `{ language: text }`, a file as its public object) and messages (per
750
+ * language, nested by key: ready for i18next or next-intl). Kept a minute;
751
+ * `live.watch(site, …)` follows it.
752
+ */
753
+ site(opts?: CallOptions): Promise<SiteAnswer>;
754
+ /**
755
+ * The site's sitemap (`file`: one part of an index, `"1.xml"`), to serve it
756
+ * from the site's own address; null when the application publishes none.
757
+ */
758
+ sitemap(file?: string, opts?: CallOptions): Promise<string | null>;
759
+ /**
760
+ * SEO-AI (D491): the site's llms.txt (`part`: one part of an index), to
761
+ * serve from the site's own address (`/llms.txt`); null while the
762
+ * application has AI readers off.
763
+ */
764
+ llms(part?: number, opts?: CallOptions): Promise<string | null>;
765
+ /**
766
+ * SEO-AI (D492): the clean text (markdown) of the published page at `path`,
767
+ * as a reader from nowhere known reads it — to serve from the site's
768
+ * address of the text (`/llms` + the page's path); null when there is no
769
+ * page to read or AI readers are off.
770
+ */
771
+ pageText(path: string, opts?: CallOptions): Promise<string | null>;
772
+ /**
773
+ * F3b.8: a list is a block of a page (`page()`); its next pages and
774
+ * narrowed filters come back as links built by the CMS (D304: `{ token, url }`
775
+ * — `pagination.next`, `pages[]`, `filters[].options[]`, `filters[].clear`).
776
+ * The whole page at a list's state: `page(path, { lists: [link.token] })`.
777
+ * The list alone (e.g. "load more" without moving): `follow(link)`.
778
+ */
779
+ follow(link: string | ListLink, opts?: CallOptions): Promise<ResolvedBodyItem>;
780
+ /**
781
+ * F3b.7 (D55): the values of protected fields by their keys (`_protected`
782
+ * of a public answer), for the end-user's purchases. One result per key,
783
+ * in order; more than 100 keys go in several calls.
784
+ */
785
+ unlock(keys: string[], endUserToken: string, opts?: CallOptions): Promise<UnlockAnswer>;
786
+ /**
787
+ * SIGN-IN (D346): where to send the browser to sign a reader in with one
788
+ * of the site's methods (`site().signIn[].key`). The platform does the
789
+ * whole sign-in and comes back to the site's `/api/cms/signed-in?code=…&to=…`
790
+ * (or to `returnTo` with `?signin=<reason>` when it refuses). `returnTo` is
791
+ * a path of the site (`/…`).
792
+ */
793
+ signInUrl(key: string, returnTo?: string, opts?: {
794
+ challenge?: string;
795
+ }): Promise<string>;
796
+ /**
797
+ * D346 — server side: the code of `/api/cms/signed-in` for the reader's
798
+ * session (60 s, once). The token stays on the server (a cookie of the site).
799
+ */
800
+ exchangeSignIn(code: string, opts?: CallOptions & {
801
+ verifier?: string;
802
+ }): Promise<EndUserSession>;
803
+ /**
804
+ * READER-WRITES (D567, D571): an app user's session renewed with its
805
+ * refresh token — a new 1-hour token; the refresh token stays the same.
806
+ * Refused (401) once the reader is revoked or forgotten or the app's users
807
+ * lost their role: sign in again.
808
+ */
809
+ refreshSession(refreshToken: string, opts?: CallOptions): Promise<RenewedSession>;
810
+ auth: {
811
+ me(endUserToken: string, opts?: CallOptions): Promise<{
812
+ user: {
813
+ id: string;
814
+ email: string;
815
+ purchases: string[];
816
+ };
817
+ }>;
818
+ };
819
+ me: {
820
+ /** What the end-user owns and which fields of which entity it unlocks. */
821
+ entitlements(endUserToken: string, opts?: CallOptions): Promise<{
822
+ products: {
823
+ id: string;
824
+ title: string;
825
+ }[];
826
+ unlocks: Record<string, string[]>;
827
+ }>;
828
+ };
829
+ /**
830
+ * D361 — buy a product: theCMS. prices it for the reader's country, picks
831
+ * its payment provider and creates the order; send the reader to
832
+ * `redirectUrl`. They come back to `returnTo` (a path of this site) with
833
+ * `?order=<id>`. Refusals are a `CmsError` with `details.reason`
834
+ * (`not_on_sale`, `not_in_your_country`, `no_methods_for_you`,
835
+ * `provider_disabled`, `provider_refused`, `provider_unreachable`,
836
+ * `already_owned_forever`). From a server, pass `forwardedFor` (and the
837
+ * secret key) so the reader's country is theirs, not the server's.
838
+ */
839
+ checkout(productId: string, returnTo: string, endUserToken: string, opts?: CallOptions): Promise<CheckoutStarted>;
840
+ /** D360 — the reader's purchases, newest first: product, when, what was paid, until when, in force, still on sale. */
841
+ purchases(endUserToken: string, opts?: CallOptions): Promise<ReaderPurchase[]>;
842
+ /** D355 — the state of one of the reader's orders (the return page's `?order=<id>`): `pending` until the provider's notice. */
843
+ order(id: string, endUserToken: string, opts?: CallOptions): Promise<ReaderOrder>;
844
+ };
845
+ type Sdk = ReturnType<typeof createSdk>;
846
+
847
+ declare const PLATFORM_API: string;
848
+
849
+ /**
850
+ * SITE-KEY (D251): where the platform answers. Not an option: theCMS. is a
851
+ * service, its address is ours. D629: one address per release channel, fixed
852
+ * at build time (`./channel.ts`): `latest` = production, `dev` = the dev.
853
+ */
854
+
855
+ /** An address the CMS gave (an image, a file, a link of a list), absolute on the platform. */
856
+ declare function platformUrl(path: string): string;
857
+ /** SITE-CONNECTION (D266): this SDK's version, as its server announces it (`X-CMS-Site: sdk/<version>`; kept equal to package.json by a test). */
858
+ declare const SDK_VERSION = "0.20.1-dev.122";
859
+ /**
860
+ * SITE-CONNECTION (D269): the protocol this SDK keeps and the header its
861
+ * server announces it with — the same as core's `SITE_PROTOCOL` / `SITE_HEADER`
862
+ * (a test keeps them equal; the SDK has no runtime dependency).
863
+ */
864
+ declare const SITE_PROTOCOL = 2;
865
+ declare const SITE_HEADER = "X-CMS-Site";
866
+ declare const siteHeader: (library: string, version: string) => string;
867
+ /**
868
+ * SEO-AI SA7 (D506): a public read from nowhere known (`XX`) — the site's
869
+ * own request for its page's text, made for an AI reader; the same as core's
870
+ * `UNKNOWN_VISITOR_HEADER` / `UNKNOWN_VISITOR` (a test keeps them equal).
871
+ */
872
+ declare const UNKNOWN_VISITOR_HEADER = "X-CMS-Visitor";
873
+ declare const UNKNOWN_VISITOR = "unknown";
874
+
875
+ /**
876
+ * READER-WRITES (D569) — PKCE for an app without a server: a random
877
+ * verifier the app keeps, and its S256 challenge it sends at the start of a
878
+ * sign-in (`signInUrl(…, { challenge })`); the exchange then needs the
879
+ * verifier (`exchangeSignIn(code, { verifier })`). Web Crypto only — browsers,
880
+ * React Native with its crypto, Node ≥ 20, Bun; zero dependencies.
881
+ */
882
+ interface Pkce {
883
+ /** keep it on the device until the exchange; never in an address */
884
+ verifier: string;
885
+ /** S256 of the verifier, base64url (43 characters): goes in the sign-in address */
886
+ challenge: string;
887
+ }
888
+ /** The S256 challenge of a verifier. */
889
+ declare function pkceChallenge(verifier: string): Promise<string>;
890
+ /** A new PKCE pair: 32 random bytes as the verifier (43 characters), its S256 challenge. */
891
+ declare function createPkce(): Promise<Pkce>;
892
+
893
+ /**
894
+ * F3b.13 — browser only: the URL of an image sized for the element that
895
+ * shows it — its width × the screen's density, up to the next fixed step
896
+ * (never beyond the original), cropped `fit=cover` around the focal point
897
+ * (already in the asset's URL), in the best format the browser takes
898
+ * (`fmt=auto`). Server-side, use `asset.url` + `asset.srcset` instead.
899
+ */
900
+ /** What `imageFor` reads of the page (kept structural: the SDK compiles without the DOM lib). */
901
+ interface Sized {
902
+ getBoundingClientRect(): {
903
+ width: number;
904
+ height: number;
905
+ };
906
+ }
907
+ declare function imageFor(asset: ResolvedAsset, element: Sized, opts?: {
908
+ height?: boolean;
909
+ }): string;
910
+
911
+ /** SITE-KEY (D254): the query parameter that brings a preview session's token into the site. */
912
+ declare const PREVIEW_PARAM = "preview";
913
+ /**
914
+ * SITE-KEY (D254) / SITE-EDITOR (D287): the address the editor's frame opens —
915
+ * the page itself on the site (its Site URL), with the session's token
916
+ * (`https://site.it` + `/attivita` → `https://site.it/attivita?preview=…`).
917
+ */
918
+ declare function previewEntryUrl(siteUrl: string, path: string, token: string): string;
919
+ /**
920
+ * SITE-KEY (D254): a request that enters the preview (`?preview=` in its
921
+ * address): the token to keep in the cookie and the clean address to go to
922
+ * (the same, without the token). Null for any other request.
923
+ */
924
+ declare function previewEntry(url: string | URL): {
925
+ token: string;
926
+ clean: string;
927
+ } | null;
928
+ /**
929
+ * SITE-EDITOR E6 (D306): the query parameter of a site's own address that
930
+ * brings a list's state (a list link's `token`): `page(path, { lists })`.
931
+ * `@thecms/next` reads it by itself; repeatable.
932
+ */
933
+ declare const LIST_PARAM = "f";
934
+ /** The cookie of the preview session's token (the same for the SDK and `@thecms/next`). */
935
+ declare const PREVIEW_COOKIE = "cms_preview";
936
+ /**
937
+ * SITE-EDITOR E6 (D308): the header the editor's bridge puts on the site's
938
+ * own requests inside the editor's frame (`markFrameRequests`, editor.ts) —
939
+ * a router's data (`Sec-Fetch-Dest: empty`) looks the same in a frame and in a
940
+ * tab: only this says "inside the editor".
941
+ */
942
+ declare const FRAME_HEADER = "X-CMS-Frame";
943
+ /**
944
+ * SITE-EDITOR (D289, D290, D308): may this request use the preview's token?
945
+ * Only inside the editor's frame — `secFetchDest` is the request's
946
+ * `Sec-Fetch-Dest`, `frame` its `X-CMS-Frame`. The entry (`?preview=`) only
947
+ * as the frame's own document (`iframe`); every other request as the frame's
948
+ * document or with the bridge's header (the site's own requests inside the
949
+ * frame). A tab never — a reload (`document`) nor a click (`empty`), with or
950
+ * without a partitioned cookie: never the drafts outside the editor.
951
+ */
952
+ declare function previewRequestAllowed(secFetchDest: string | null | undefined, opts?: {
953
+ entry?: boolean;
954
+ frame?: string | null;
955
+ }): boolean;
956
+ /**
957
+ * SITE-EDITOR (D289): the `Set-Cookie` value that keeps the token for the
958
+ * whole site until the session ends (`expiresAt`, from `cms.previewStatus`).
959
+ */
960
+ declare function previewCookie(token: string, expiresAt: string, opts?: {
961
+ secure?: boolean;
962
+ }): string;
963
+ /** The preview token of a request's `Cookie` header, if any. */
964
+ declare function readPreviewCookie(cookieHeader: string | null | undefined): string | undefined;
965
+
966
+ /** The page shown, as the editor opens it. */
967
+ interface EditorPage {
968
+ /** the view (`ResolvedView.id`) */
969
+ view: string;
970
+ entity: string | null;
971
+ /** the page's record (a template's, a bound static page's), else null */
972
+ record: string | null;
973
+ /** the answer's locale */
974
+ locale: string;
975
+ }
976
+ /** Where a block is drawn, in the frame's viewport. */
977
+ interface EditorBlock {
978
+ uid: string;
979
+ in: "view" | "layout";
980
+ set: string;
981
+ shape: string | null;
982
+ rect: {
983
+ x: number;
984
+ y: number;
985
+ width: number;
986
+ height: number;
987
+ };
988
+ /** the site has no component for it */
989
+ missing?: true;
990
+ /** it could not be drawn (`geoBlocked`, or the resolver's error) */
991
+ error?: string;
992
+ }
993
+ /** What the page tells the editor. */
994
+ type EditorMessage = ({
995
+ type: "thecms:preview";
996
+ protocol: number;
997
+ path: string;
998
+ } & EditorPage) | {
999
+ type: "thecms:blocks";
1000
+ blocks: EditorBlock[];
1001
+ } | {
1002
+ type: "thecms:pick";
1003
+ uid: string;
1004
+ } | {
1005
+ type: "thecms:navigate";
1006
+ path: string;
1007
+ external?: true;
1008
+ };
1009
+ /** What the editor asks the page. */
1010
+ interface EditorCommand {
1011
+ type: "thecms:focus";
1012
+ uid: string;
1013
+ }
1014
+ /**
1015
+ * SITE-EDITOR E6 (D308) — browser only, inside the editor's frame: from now on
1016
+ * every request of the page to its own site carries `X-CMS-Frame: 1`, so the
1017
+ * site's server gives the drafts to the frame's own requests (a router's data,
1018
+ * a refresh) and never to a tab's. Called by the preview page before anything
1019
+ * else asks (`CmsEditorBridge` renders it first); outside a frame, nothing.
1020
+ * Once per page; other origins untouched.
1021
+ */
1022
+ declare function markFrameRequests(): void;
1023
+ /**
1024
+ * Browser only: inside the editor's frame, on a preview page, keeps the
1025
+ * conversation with the editor (D276–D278) — greets it with the page, reports
1026
+ * the blocks (at once, on scroll, resize and every change of the page, at most
1027
+ * once a frame), turns a click on a link into a request to the editor (the
1028
+ * page never moves by itself) and a click in a block into `thecms:pick`,
1029
+ * brings a block into view on `thecms:focus`. Nothing outside a frame or on
1030
+ * the server. Returns `stop()`.
1031
+ */
1032
+ declare function editorBridge(opts: {
1033
+ page: EditorPage;
1034
+ }): () => void;
1035
+
1036
+ /** An error answer of the API: `{ error: { code, message, details? } }`. */
1037
+ declare class CmsError extends Error {
1038
+ readonly status: number;
1039
+ readonly code: string;
1040
+ readonly details?: Record<string, unknown> | undefined;
1041
+ constructor(status: number, code: string, message: string, details?: Record<string, unknown> | undefined);
1042
+ }
1043
+
1044
+ interface RssItem {
1045
+ title: string;
1046
+ link: string;
1047
+ description?: string;
1048
+ /** a date (ISO string or Date) */
1049
+ pubDate?: string | Date;
1050
+ guid?: string;
1051
+ }
1052
+ interface RssChannel<T> {
1053
+ title: string;
1054
+ link: string;
1055
+ description?: string;
1056
+ /** turns one item of a list into an RSS entry */
1057
+ item: (record: T) => RssItem;
1058
+ }
1059
+ /**
1060
+ * RSS 2.0 of a list (e.g. the items of `list()`): the CMS serves the
1061
+ * sitemap, the feed is built by the site from the content it shows.
1062
+ */
1063
+ declare function rss<T>(records: T[], channel: RssChannel<T>): string;
1064
+
1065
+ /** A JSON-LD the site builds: an object, or a list of objects. */
1066
+ type JsonLd = Record<string, unknown> | Record<string, unknown>[];
1067
+ /**
1068
+ * Any JSON-LD the site builds (lib/jsonld.ts) as the text of a `<script
1069
+ * type="application/ld+json">`, safe inside the page's HTML (`<`, `>`, `&`
1070
+ * and the line separators written as escapes, so no value can close the
1071
+ * script). Null for nothing.
1072
+ */
1073
+ declare function jsonLdText(value: JsonLd | Record<string, unknown> | null | undefined): string | null;
1074
+
1075
+ export { type CallOptions, type CheckoutStarted, CmsError, type EditorBlock, type EditorCommand, type EditorMessage, type EditorPage, type EndUserSession, type EntityRecord, FRAME_HEADER, type JsonLd, LIST_PARAM, type ListFilter, type ListLink, type LiveData, type LiveSource, type ManagedProduct, type ManagedRedirect, type MediaFile, PLATFORM_API, PREVIEW_COOKIE, PREVIEW_PARAM, type PageOptions, type PageResult, type PageRoute, type Pkce, type PopularPage, type ProtectedField, type PublicMessages, type PublicProduct, type PublicSite, type QueryResult, type ReaderOrder, type ReaderPurchase, type RecordsQuery, type RedirectAnswer, type RenewedSession, type ResolvedAsset, type ResolvedBodyItem, type ResolvedExposedFilter, type ResolvedLink, type ResolvedPage, type ResolvedPagination, type ResolvedView, type RssChannel, type RssItem, SDK_VERSION, SITE_HEADER, SITE_PROTOCOL, type Sdk, type SdkOptions, type SiteAnswer, UNKNOWN_VISITOR, UNKNOWN_VISITOR_HEADER, type UnlockAnswer, type UploadOptions, type VisitHistory, type VisitItem, createPkce, createSdk, editorBridge, imageFor, jsonLdText, markFrameRequests, newVisitorId, pkceChallenge, platformUrl, previewCookie, previewEntry, previewEntryUrl, previewRequestAllowed, readPreviewCookie, rss, siteHeader };