@capacms/sdk 1.0.0-next.16 → 1.0.0-next.17

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 CHANGED
@@ -2,6 +2,37 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 1.0.0-next.17
6
+
7
+ Published 2026-11-25. New since 1.0.0-next.16: `search()` reads Site search through the new `siteSearchKey` option, since `/v2/api/search` answers 410 on every key. Each refusal throws with the steps to fix it.
8
+
9
+ - `search()` reads Site search. It called `/v2/api/search`, which answers
10
+ 410 on every key since 2026-10-30. It now calls `GET /v2/site-search`
11
+ with the project's site search key, passed to `createClient` as the new
12
+ `siteSearchKey` option (a `capss_` key from Settings > Search > Site
13
+ keys). That key is the only credential sent there: no `apiKey` and no
14
+ `X-Tenant-Key`.
15
+ - Without `siteSearchKey`, `search()` throws before sending anything, with
16
+ the steps to get one. A project without Site search (404), a refused key
17
+ (401) and a project whose first index is not built yet (503
18
+ `search_not_ready`) each throw a `CapaError` whose message says what to
19
+ do. Any other refusal keeps the API's own words.
20
+ - `SearchHit` keeps its four fields. `model` is now always a `string`
21
+ (it was optional) and `title` a `string` (it was `unknown`). `id` and
22
+ `namespace` stay `string | null` so code that checks for `null` still
23
+ compiles, but Site search sends both on every hit, and `id` is always
24
+ the entry's instance id: a model with its own `id` field no longer
25
+ hides it.
26
+ - What `search()` finds changed. Site search holds the published version
27
+ of the models and fields the project picked in Settings > Search. It used
28
+ to be every model marked searchable.
29
+ - `limit` is 1 to 50 (default 20), where it was up to 500, and `page`
30
+ starts at 1. A limit outside that range, a page below 1, or a page that
31
+ starts past 1,000 hits throws a `RangeError` before any request, since
32
+ Site search would cut the offset to 1,000 and answer the wrong page.
33
+ - `SearchOptions` is exported, the type of `search()`'s options.
34
+ - `CapaError` takes an optional fourth argument, its message.
35
+
5
36
  ## 1.0.0-next.16
6
37
 
7
38
  Published 2026-11-18. New since 1.0.0-next.15: a shared draft link that does not open lands on `?preview=link-ended`, so a site can word it for someone with no Capa account; `draftLinkVisit({ headers })` tells a page whether a draft-link visit's link is still live, for a "Draft preview" bar; and `preview()` takes `checkOnly`.
package/README.md CHANGED
@@ -2565,9 +2565,9 @@ page.data[0].id // the model's OWN id field whenever one exists
2565
2565
  the model shadows `id` and the row carries nothing else to read the entry's
2566
2566
  id from. `instanceIdOf(row)` reads one row the same way, and prefers an
2567
2567
  `instanceId` when a row carries one. `getContentById` throws for an id that is
2568
- not a UUID rather than spending a request on a certain 400. `search()` reads
2569
- its hits the same way, so `SearchHit.id` is `string | null` too. `/api/` has
2570
- no such collision: an entry's system keys sit beside its `fields`.
2568
+ not a UUID rather than spending a request on a certain 400. `search()` has
2569
+ no such problem: Site search sends the instance id on every hit. `/api/` has
2570
+ no such collision either: an entry's system keys sit beside its `fields`.
2571
2571
 
2572
2572
  ### Related entries: `Page.relations`
2573
2573
 
@@ -2628,6 +2628,39 @@ read it here instead.
2628
2628
  related entry, nested where it was selected, and with `shape: "flat"` once in
2629
2629
  `included`. See [Flat responses](#flat-responses-each-related-entry-once).
2630
2630
 
2631
+ ### Search
2632
+
2633
+ `search()` reads Site search, `GET /v2/site-search`. `/v2/api/search`
2634
+ answers 410 on every key since 2026-10-30. Site search takes a key of its
2635
+ own, so pass the project's site search key as `siteSearchKey`:
2636
+
2637
+ ```ts
2638
+ const capa = createClient({
2639
+ baseUrl: process.env.CAPA_API_URL!,
2640
+ apiKey: process.env.CAPA_KEY!,
2641
+ tenantId: process.env.CAPA_TENANT_ID!,
2642
+ siteSearchKey: process.env.CAPA_SITE_SEARCH_KEY, // capss_...
2643
+ });
2644
+
2645
+ const hits = await capa.search("kitchen", { namespace: "articles", limit: 10, page: 1 });
2646
+ // [{ id, namespace, model, title }]
2647
+ ```
2648
+
2649
+ To get the key, turn on Site search for the project in the Capa admin,
2650
+ Settings > Search. It is in pilot, so ask Capa to turn it on if that page is
2651
+ missing. Pick the models and fields to search, then make a key there under
2652
+ Site keys. It starts `capss_`, reads nothing but Site search, and goes only
2653
+ to that route: the legacy key in `apiKey` is not sent there.
2654
+
2655
+ Site search holds the published version of the models and fields the project
2656
+ picked. `limit` is 1 to 50 (default 20) and `page` starts at 1. A page that
2657
+ starts past 1,000 hits throws, since Site search reads no further.
2658
+
2659
+ `search()` throws with the fix when it cannot search. With no
2660
+ `siteSearchKey` it throws before sending anything. A project without Site
2661
+ search (404), a refused key (401) and a project whose first index is not
2662
+ built yet (503) each throw a `CapaError` whose message says what to do.
2663
+
2631
2664
  ### Preview is a key, not a flag
2632
2665
 
2633
2666
  Capa shows unpublished content to a key whose environment is not
package/dist/client.d.ts CHANGED
@@ -131,11 +131,12 @@ export interface CapaClient {
131
131
  }): Promise<T | null>;
132
132
  /** A `ReadOneOptions` value, or a `withRelations` known only at run time: either shape. */
133
133
  findOne<T = Record<string, unknown>, R = RelatedEntry>(namespace: string, where: Record<string, string | number | boolean>, options?: ReadOneOptions): Promise<T | OneWithRelations<T, R> | null>;
134
- search(q: string, options?: {
135
- namespace?: string;
136
- limit?: number;
137
- page?: number;
138
- }): Promise<SearchHit[]>;
134
+ /**
135
+ * Full-text search over the project's published content, through Site
136
+ * search (`GET /v2/site-search`). Needs `siteSearchKey` in the config, and
137
+ * Site search turned on for the project; it throws with the fix otherwise.
138
+ */
139
+ search(q: string, options?: SearchOptions): Promise<SearchHit[]>;
139
140
  /** Saved arrangements of the Capa admin's left rail. ADMIN_UI_OVERHAUL 0h. */
140
141
  workspaces: WorkspacesResource;
141
142
  /** Model schema reads, and the entry-editor layout. ADMIN_UI_OVERHAUL 0m. */
@@ -456,17 +457,30 @@ export interface ScheduledActionsResource {
456
457
  /** Creates a NEW pending row; the failed one keeps its history. */
457
458
  retry(id: string): Promise<ScheduledAction>;
458
459
  }
460
+ export interface SearchOptions {
461
+ /** One model's namespace. Omit to search every model in the project's Site search setup. */
462
+ namespace?: string;
463
+ /** Hits per page, 1 to 50. Default 20. */
464
+ limit?: number;
465
+ /** 1-based page number. Site search reads at most 1,000 hits in, so `(page - 1) * limit` is at most 1000. */
466
+ page?: number;
467
+ }
468
+ /**
469
+ * One Site search hit, as `search()` returns it. Since 1.0.0-next.17 it comes
470
+ * from `/v2/site-search`.
471
+ */
459
472
  export interface SearchHit {
460
473
  /**
461
- * The instance UUID, via `instanceIdOf`. `null` means the server sent no
462
- * usable id: an extended search row hoists the model's own fields the same
463
- * way a content row does, so a model with a field named `id` shadows it, and
464
- * a server older than #4628 sends no `instanceId` beside it.
474
+ * The entry's instance id, a UUID. Site search sends it on every hit. The
475
+ * type keeps `null`, as before, so code that checks for it still compiles.
465
476
  */
466
477
  id: string | null;
478
+ /** The model's namespace. Always sent; `null` stays in the type for the same reason as `id`. */
467
479
  namespace: string | null;
468
- model?: string;
469
- title: unknown;
480
+ /** The model's name, as the admin shows it. */
481
+ model: string;
482
+ /** The entry's title as plain text, `""` when it has none. */
483
+ title: string;
470
484
  }
471
485
  /**
472
486
  * The Capa instance id for a row.
package/dist/client.js CHANGED
@@ -21,6 +21,35 @@ const config_1 = require("./config");
21
21
  const http_1 = require("./http");
22
22
  const key_family_1 = require("./next/key-family");
23
23
  const webhooks_1 = require("./webhooks");
24
+ /** Site search's own limits (packages/search/src/tenant/query.ts). */
25
+ const SITE_SEARCH_MAX_LIMIT = 50;
26
+ const SITE_SEARCH_MAX_OFFSET = 1000;
27
+ const SITE_SEARCH_PATH = "/v2/site-search";
28
+ const SITE_SEARCH_SETUP = "Turn on Site search for the project in the Capa admin (Settings > Search; while Site search is in pilot, " +
29
+ "ask Capa to turn it on if that page is missing), make a site search key there under Site keys " +
30
+ "(it starts capss_), and pass it to createClient as siteSearchKey.";
31
+ /** What a Site search refusal means for a `search()` caller, or null to keep the API's own words. */
32
+ function siteSearchRefusal(status, text) {
33
+ let code;
34
+ try {
35
+ code = JSON.parse(text)?.code;
36
+ }
37
+ catch {
38
+ code = undefined;
39
+ }
40
+ if (status === 401) {
41
+ return ("@capacms/sdk: Site search refused siteSearchKey: it was revoked, or it is not this project's capss_ key. " +
42
+ "Make a new one in the Capa admin under Settings > Search > Site keys and pass it as siteSearchKey.");
43
+ }
44
+ // The route answers an unknown path's 404 to a project Site search is not on for.
45
+ if (status === 404)
46
+ return `@capacms/sdk: Site search is not turned on for this project, so search() has nothing to read. ${SITE_SEARCH_SETUP}`;
47
+ if (status === 503 && code === "search_not_ready") {
48
+ return ("@capacms/sdk: Site search is on for this project but has no index yet. In the Capa admin, pick the models " +
49
+ "to search in Settings > Search, save, and wait for the first build to finish.");
50
+ }
51
+ return null;
52
+ }
24
53
  const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
25
54
  /**
26
55
  * The Capa instance id for a row.
@@ -50,10 +79,6 @@ function instanceIdOf(row) {
50
79
  const id = r?.id;
51
80
  return typeof id === "string" && UUID_RE.test(id) ? id : null;
52
81
  }
53
- /** Capa wraps most values as `{type, value, sortOrder}` — including `title`. */
54
- const unwrap = (v) => v && typeof v === "object" && !Array.isArray(v) && "value" in v
55
- ? v.value
56
- : v ?? null;
57
82
  function contentQuery(options = {}) {
58
83
  const q = {
59
84
  limit: options.limit,
@@ -137,35 +162,59 @@ function createClient(config) {
137
162
  },
138
163
  async search(q, options = {}) {
139
164
  const text = String(q ?? "").trim();
140
- // The server answers an absent `q` with 500 carrying `Cannot read
141
- // properties of undefined (reading 'trim')` — the parameter is trimmed
142
- // before it is checked. #4627 made it a 400 and the public-surface freeze
143
- // put the 500 back on 2026-09-20, so this guard is what a caller actually
144
- // gets: a named error, at no request cost, on every server.
165
+ // Site search answers an empty q with every entry it holds, which is not
166
+ // a search. Refused here, as it always was, at no request cost.
145
167
  if (!text)
146
168
  throw new Error("@capacms/sdk: search(q) requires a non-empty query.");
147
- const { body } = await (0, http_1.getJson)(resolved, "/v2/api/search", {
148
- q: text,
149
- modelNamespace: options.namespace,
150
- size: options.limit ?? 20,
151
- page: options.page,
152
- // The default (non-extended) branch answers {id, title} and says
153
- // nothing about which model a hit belongs to, so the namespace has to
154
- // be bought with a hydration round trip. #4627 briefly added it to the
155
- // default row; the public-surface freeze reverted that on 2026-09-20.
156
- extended: true,
157
- depth: 0,
169
+ // `/v2/api/search` answers 410 on every key since 2026-10-30. Site search
170
+ // is the search Capa serves now, and it takes its own key: the legacy
171
+ // key in `apiKey` is not sent there.
172
+ const key = String(resolved.siteSearchKey ?? "").trim();
173
+ if (!key)
174
+ throw new Error(`@capacms/sdk: search() reads Site search, which needs its own key. ${SITE_SEARCH_SETUP}`);
175
+ const limit = options.limit ?? 20;
176
+ if (!Number.isInteger(limit) || limit < 1 || limit > SITE_SEARCH_MAX_LIMIT) {
177
+ throw new RangeError(`@capacms/sdk: search() takes a limit from 1 to ${SITE_SEARCH_MAX_LIMIT}; got ${options.limit}.`);
178
+ }
179
+ const page = options.page ?? 1;
180
+ if (!Number.isInteger(page) || page < 1)
181
+ throw new RangeError(`@capacms/sdk: search() takes a page from 1; got ${options.page}.`);
182
+ // The server cuts a larger offset to 1000 without saying so, which
183
+ // would answer the wrong page.
184
+ const offset = (page - 1) * limit;
185
+ if (offset > SITE_SEARCH_MAX_OFFSET) {
186
+ throw new RangeError(`@capacms/sdk: Site search reads at most ${SITE_SEARCH_MAX_OFFSET} hits in; page ${page} of ${limit} starts at ${offset}.`);
187
+ }
188
+ const url = new URL(resolved.baseUrl + SITE_SEARCH_PATH);
189
+ url.searchParams.set("q", text);
190
+ url.searchParams.set("limit", String(limit));
191
+ if (offset)
192
+ url.searchParams.set("offset", String(offset));
193
+ if (options.namespace)
194
+ url.searchParams.set("filter[model]", options.namespace);
195
+ // Only the site search key, as the admin's copyable widget sends it: no
196
+ // X-Tenant-Key, since the key names the project.
197
+ const res = await resolved.fetchImpl(url.toString(), {
198
+ method: "GET",
199
+ headers: { "x-api-key": key, Accept: "application/json" },
158
200
  });
159
- return (body.data ?? []).map((row) => ({
160
- // instanceIdOf, not row.id. `/v2/api/search?extended=true` hoists the
161
- // model's own fields exactly as `/v2/api/:ns` does, so a model with a
162
- // field named `id` shadows the id here too; #4628 now sends
163
- // `instanceId` on these rows as well. `null` means an old server AND a
164
- // shadowing model, the same as `Page.ids`.
165
- id: instanceIdOf(row),
166
- namespace: row.dataModel?.namespace ?? row.modelNamespace ?? null,
167
- model: row.dataModel?.modelName ?? undefined,
168
- title: unwrap(row.title),
201
+ const body = await res.text();
202
+ if (!res.ok) {
203
+ const message = siteSearchRefusal(res.status, body);
204
+ throw new http_1.CapaError(res.status, SITE_SEARCH_PATH, body, message ?? undefined);
205
+ }
206
+ let answer;
207
+ try {
208
+ answer = JSON.parse(body);
209
+ }
210
+ catch {
211
+ throw new http_1.CapaError(res.status, SITE_SEARCH_PATH, `response was not JSON: ${body.slice(0, 200)}`);
212
+ }
213
+ return (answer.hits ?? []).map((hit) => ({
214
+ id: typeof hit.id === "string" ? hit.id : null,
215
+ namespace: typeof hit.model === "string" ? hit.model : null,
216
+ model: String(hit.modelName ?? ""),
217
+ title: typeof hit.title === "string" ? hit.title : "",
169
218
  }));
170
219
  },
171
220
  /**
package/dist/config.d.ts CHANGED
@@ -19,6 +19,16 @@ export interface CapaConfig {
19
19
  * this field and keeps using `apiKey`.
20
20
  */
21
21
  accessToken?: string;
22
+ /**
23
+ * The project's site search key (`capss_`), for `search()` only.
24
+ *
25
+ * `search()` reads Site search, `GET /v2/site-search`, because
26
+ * `/v2/api/search` answers 410 on every key since 2026-10-30. Site search
27
+ * takes a key of its own: make one in the Capa admin under Settings >
28
+ * Search > Site keys, once Site search is on for the project. Without it,
29
+ * `search()` throws before it sends anything. No other method reads it.
30
+ */
31
+ siteSearchKey?: string;
22
32
  /** Injected in tests; defaults to the global fetch. */
23
33
  fetch?: typeof fetch;
24
34
  }
package/dist/http.d.ts CHANGED
@@ -2,7 +2,8 @@ import type { ResolvedConfig } from "./config";
2
2
  export declare class CapaError extends Error {
3
3
  readonly status: number;
4
4
  readonly path: string;
5
- constructor(status: number, path: string, body: string);
5
+ /** `message` replaces the default one, for an answer the SDK can explain better than its body does. */
6
+ constructor(status: number, path: string, body: string, message?: string);
6
7
  }
7
8
  export type Query = Record<string, string | number | boolean | undefined | null>;
8
9
  /**
package/dist/http.js CHANGED
@@ -9,8 +9,9 @@ exports.writeJson = writeJson;
9
9
  class CapaError extends Error {
10
10
  status;
11
11
  path;
12
- constructor(status, path, body) {
13
- super(`Capa API ${status} on ${path}${body ? `: ${body.slice(0, 200)}` : ""}`);
12
+ /** `message` replaces the default one, for an answer the SDK can explain better than its body does. */
13
+ constructor(status, path, body, message) {
14
+ super(message ?? `Capa API ${status} on ${path}${body ? `: ${body.slice(0, 200)}` : ""}`);
14
15
  this.name = "CapaError";
15
16
  this.status = status;
16
17
  this.path = path;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { createClient, instanceIdOf } from "./client";
2
- export type { CapaClient, CreateScheduledActionsInput, CreateScheduledActionsResult, FieldValue, LayoutCard, LayoutDisplay, LayoutField, LayoutInline, LayoutWidth, ListOptions, ListScheduledActionsOptions, ModelLayout, ModelLayoutInfo, ModelsResource, OneWithRelations, Page, ReadOneOptions, RelatedEntry, Relations, ResolvedScheduleTime, ScheduledAction, ScheduledActionsPage, ScheduledActionsResource, ScheduledActionStatus, ScheduledActionTarget, SearchHit, WorkspaceApplyResult, WorkspaceChanges, WorkspaceDocNode, WorkspaceDocument, WorkspaceSummary, WorkspaceTemplate, WorkspaceTreeDocument, WorkspacesResource, } from "./client";
2
+ export type { CapaClient, CreateScheduledActionsInput, CreateScheduledActionsResult, FieldValue, LayoutCard, LayoutDisplay, LayoutField, LayoutInline, LayoutWidth, ListOptions, ListScheduledActionsOptions, ModelLayout, ModelLayoutInfo, ModelsResource, OneWithRelations, Page, ReadOneOptions, RelatedEntry, Relations, ResolvedScheduleTime, ScheduledAction, ScheduledActionsPage, ScheduledActionsResource, ScheduledActionStatus, ScheduledActionTarget, SearchHit, SearchOptions, WorkspaceApplyResult, WorkspaceChanges, WorkspaceDocNode, WorkspaceDocument, WorkspaceSummary, WorkspaceTemplate, WorkspaceTreeDocument, WorkspacesResource, } from "./client";
3
3
  export type { CapaConfig } from "./config";
4
4
  export { CapaError } from "./http";
5
5
  export { WEBHOOKS_NEED_ACCESS_TOKEN } from "./webhooks";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacms/sdk",
3
- "version": "1.0.0-next.16",
3
+ "version": "1.0.0-next.17",
4
4
  "description": "The TypeScript SDK for Capa's content API: REST and GraphQL reads typed from your models, Next.js caching and live preview, and codegen.",
5
5
  "license": "UNLICENSED",
6
6
  "homepage": "https://docs.capacms.com/api",