@capacms/sdk 1.0.0-next.4 → 1.0.0-next.7

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 (69) hide show
  1. package/CHANGELOG.md +345 -0
  2. package/README.md +1069 -186
  3. package/bin/capa-codegen.js +192 -5
  4. package/bin/capa.js +208 -0
  5. package/bin/graphql-project.js +142 -0
  6. package/bin/project-env.js +58 -0
  7. package/dist/client.d.ts +5 -0
  8. package/dist/client.js +17 -0
  9. package/dist/codegen.d.ts +55 -0
  10. package/dist/codegen.js +320 -39
  11. package/dist/config.d.ts +5 -36
  12. package/dist/config.js +47 -1
  13. package/dist/graphql-codegen.d.ts +117 -0
  14. package/dist/graphql-codegen.js +705 -0
  15. package/dist/http.js +1 -1
  16. package/dist/index.d.ts +2 -2
  17. package/dist/index.js +2 -1
  18. package/dist/next/attrs.d.ts +51 -12
  19. package/dist/next/attrs.js +74 -20
  20. package/dist/next/client.d.ts +112 -38
  21. package/dist/next/client.js +131 -83
  22. package/dist/next/entry-fields.d.ts +162 -0
  23. package/dist/next/entry-fields.js +2 -0
  24. package/dist/next/errors.d.ts +136 -0
  25. package/dist/next/errors.js +214 -0
  26. package/dist/next/field-names.d.ts +37 -0
  27. package/dist/next/field-names.js +145 -0
  28. package/dist/next/graphql/build.d.ts +27 -0
  29. package/dist/next/graphql/build.js +98 -0
  30. package/dist/next/graphql/documents.d.ts +67 -0
  31. package/dist/next/graphql/documents.js +35 -0
  32. package/dist/next/graphql/edit-mode.d.ts +16 -0
  33. package/dist/next/graphql/edit-mode.js +93 -0
  34. package/dist/next/graphql/filter-values.d.ts +34 -0
  35. package/dist/next/graphql/filter-values.js +96 -0
  36. package/dist/next/graphql/introspection.d.ts +89 -0
  37. package/dist/next/graphql/introspection.js +102 -0
  38. package/dist/next/graphql/plan.d.ts +115 -0
  39. package/dist/next/graphql/plan.js +531 -0
  40. package/dist/next/graphql/request.d.ts +228 -0
  41. package/dist/next/graphql/request.js +283 -0
  42. package/dist/next/graphql/rest.d.ts +66 -0
  43. package/dist/next/graphql/rest.js +502 -0
  44. package/dist/next/graphql/selection.d.ts +55 -0
  45. package/dist/next/graphql/selection.js +212 -0
  46. package/dist/next/graphql/sha256.d.ts +13 -0
  47. package/dist/next/graphql/sha256.js +86 -0
  48. package/dist/next/graphql/summary.d.ts +83 -0
  49. package/dist/next/graphql/summary.js +151 -0
  50. package/dist/next/graphql/tree-layout.d.ts +36 -0
  51. package/dist/next/graphql/tree-layout.js +20 -0
  52. package/dist/next/graphql/tree.d.ts +171 -0
  53. package/dist/next/graphql/tree.js +249 -0
  54. package/dist/next/graphql/typed.d.ts +261 -0
  55. package/dist/next/graphql/typed.js +146 -0
  56. package/dist/next/index.d.ts +28 -5
  57. package/dist/next/index.js +25 -1
  58. package/dist/next/inflate.d.ts +25 -7
  59. package/dist/next/inflate.js +46 -32
  60. package/dist/next/key-family.d.ts +34 -0
  61. package/dist/next/key-family.js +74 -0
  62. package/dist/next/select-types.d.ts +44 -8
  63. package/dist/next/system-keys.d.ts +27 -0
  64. package/dist/next/system-keys.js +42 -0
  65. package/dist/nextjs/index.d.ts +174 -12
  66. package/dist/nextjs/index.js +270 -23
  67. package/dist/nextjs/overlay.d.ts +5 -0
  68. package/dist/nextjs/overlay.js +35 -0
  69. package/package.json +31 -13
@@ -1,11 +1,36 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LAYOUT_PAGE = exports.CapaError = void 0;
4
- exports.isCapaError = isCapaError;
3
+ exports.LAYOUT_PAGE = exports.isCapaError = exports.CapaError = void 0;
4
+ exports.graphqlClient = graphqlClient;
5
5
  exports.resolveNextConfig = resolveNextConfig;
6
6
  exports.serializeSelect = serializeSelect;
7
7
  exports.createClient = createClient;
8
8
  const attrs_1 = require("./attrs");
9
+ const errors_1 = require("./errors");
10
+ const edit_mode_1 = require("./graphql/edit-mode");
11
+ const key_family_1 = require("./key-family");
12
+ const system_keys_1 = require("./system-keys");
13
+ const field_names_1 = require("./field-names");
14
+ const introspection_1 = require("./graphql/introspection");
15
+ const request_1 = require("./graphql/request");
16
+ const summary_1 = require("./graphql/summary");
17
+ const typed_1 = require("./graphql/typed");
18
+ var errors_2 = require("./errors");
19
+ Object.defineProperty(exports, "CapaError", { enumerable: true, get: function () { return errors_2.CapaError; } });
20
+ Object.defineProperty(exports, "isCapaError", { enumerable: true, get: function () { return errors_2.isCapaError; } });
21
+ /**
22
+ * The platform fetch, called through `globalThis` on every request. Storing
23
+ * `globalThis.fetch` and calling it as a method of the config object gives it
24
+ * the wrong `this`, and a browser refuses that ("Failed to execute 'fetch' on
25
+ * 'Window': Illegal invocation"); Node does not care, which is why only
26
+ * browser reads broke. Looking it up per call also picks up a fetch a
27
+ * framework patches in after the client is built (Next does).
28
+ */
29
+ function defaultFetch() {
30
+ if (typeof globalThis.fetch !== "function")
31
+ return undefined;
32
+ return ((input, init) => globalThis.fetch(input, init));
33
+ }
9
34
  /**
10
35
  * What a `Capa-Schema` value may look like: hex, 8 to 64 characters.
11
36
  *
@@ -17,29 +42,24 @@ const attrs_1 = require("./attrs");
17
42
  * appears rather than as an error.
18
43
  */
19
44
  const SCHEMA_CHECKSUM = /^[a-f0-9]{8,64}$/;
20
- class CapaError extends Error {
21
- status;
22
- type;
23
- code;
24
- param;
25
- hint;
26
- requestId;
27
- docs;
28
- constructor(input) {
29
- super(input.message);
30
- this.name = "CapaError";
31
- this.status = input.status;
32
- this.type = input.type;
33
- this.code = input.code;
34
- this.param = input.param;
35
- this.hint = input.hint;
36
- this.requestId = input.requestId;
37
- this.docs = input.docs;
38
- }
39
- }
40
- exports.CapaError = CapaError;
41
- function isCapaError(error) {
42
- return error instanceof CapaError;
45
+ const BUILDER_NOT_PERSISTED = "@capacms/sdk/next: graphql.query() cannot send persisted: true. It builds its document when it runs, " +
46
+ "so capa persist never stored it and every read would fall back to an uncached POST. " +
47
+ "Leave persisted out (a builder read is already a cached GET), or write the read as a #graphql literal and run capa persist.";
48
+ /**
49
+ * `client.graphql` over one way of reading a document: called with a
50
+ * document, and `query()` printing a selection's document for it. `createClient`
51
+ * reads straight from the API, and `getCapaClient` from `/nextjs` the same way
52
+ * unless a call gives `tags` or `revalidate`, then through Next's data cache,
53
+ * with the same refusals.
54
+ */
55
+ function graphqlClient(read) {
56
+ const query = async (selection, options) => {
57
+ if (options?.persisted)
58
+ throw new TypeError(BUILDER_NOT_PERSISTED);
59
+ return read((0, typed_1.selectionToDocument)(selection, options?.operationName), undefined, options);
60
+ };
61
+ const call = (document, variables, options) => read(document, variables, options);
62
+ return Object.assign(call, { query });
43
63
  }
44
64
  function resolveNextConfig(config) {
45
65
  const value = (config ?? {});
@@ -48,13 +68,14 @@ function resolveNextConfig(config) {
48
68
  throw new Error(`@capacms/sdk/next: missing ${missing.join(", ")}. ` +
49
69
  "createClient needs baseUrl, apiKey and version.");
50
70
  }
51
- if (!value.apiKey.startsWith("cap_")) {
52
- throw new Error("@capacms/sdk/next: apiKey must start with cap_. Use the legacy @capacms/sdk client for pk_ and sk_ keys.");
71
+ if ((0, key_family_1.keyFamily)(value.apiKey) === null) {
72
+ throw new Error("@capacms/sdk/next: apiKey must be a string: a cap_ key, or the legacy key your site already holds. " +
73
+ "Mint a cap_ key in the Capa admin under Developers > Keys.");
53
74
  }
54
75
  if (value.contract !== undefined && value.contract !== 1) {
55
76
  throw new Error("@capacms/sdk/next: contract must be 1 when provided.");
56
77
  }
57
- const fetchImpl = value.fetch ?? globalThis.fetch;
78
+ const fetchImpl = value.fetch ?? defaultFetch();
58
79
  if (typeof fetchImpl !== "function") {
59
80
  throw new Error("@capacms/sdk/next: no fetch available. Pass one via config.fetch.");
60
81
  }
@@ -140,12 +161,41 @@ function resolvePath(configPath, callPath) {
140
161
  }
141
162
  return bare;
142
163
  }
143
- const NAME = /^[A-Za-z0-9_-]+$/;
164
+ /*
165
+ * The array form names each field by its namespace, as saved in the admin,
166
+ * and the select is written in REST's grammar: a namespace that holds a
167
+ * character the grammar uses goes quoted (`"price.usd"`, field-names.ts). A
168
+ * name starting with `$` is a system key, so no field can be named that way;
169
+ * select "*" returns such a field.
170
+ */
171
+ /** A relation's name: a field, since a system key expands nothing. */
144
172
  function selectName(value, context) {
145
- if (typeof value !== "string" || !NAME.test(value)) {
173
+ if (typeof value !== "string" || !(0, field_names_1.isNameable)(value)) {
146
174
  throw new TypeError(`@capacms/sdk/next: ${context} must be a field name.`);
147
175
  }
148
- return value;
176
+ return (0, field_names_1.writeName)(value);
177
+ }
178
+ /**
179
+ * A select item: a field, or a system key by its `$` name, which means the
180
+ * system key even beside a field of the same plain name (spec 17, amendment 29).
181
+ */
182
+ function selectItemName(value) {
183
+ if ((0, system_keys_1.sigilSystemKey)(value) !== null)
184
+ return value;
185
+ if ((0, field_names_1.isNameable)(value))
186
+ return (0, field_names_1.writeName)(value);
187
+ throw new TypeError(`@capacms/sdk/next: select item must be a field name, or a system key: ${system_keys_1.SYSTEM_KEY_LIST}.`);
188
+ }
189
+ /** One nested sort key: a field, or a system key the API sorts by, either with a leading `-`. */
190
+ function nestedSort(value, relation) {
191
+ const descending = typeof value === "string" && value.startsWith("-");
192
+ const key = descending ? value.slice(1) : value;
193
+ const system = typeof key === "string" ? (0, system_keys_1.sigilSystemKey)(key) : null;
194
+ if (system !== null && (0, system_keys_1.isSortableSystemKey)(system))
195
+ return value;
196
+ if (typeof key === "string" && system === null && (0, field_names_1.isNameable)(key))
197
+ return `${descending ? "-" : ""}${(0, field_names_1.writeName)(key)}`;
198
+ throw new TypeError(`@capacms/sdk/next: ${relation}.sort must be one field name, or one of ${system_keys_1.SORTABLE_SYSTEM_KEYS.map((k) => `$${k}`).join(", ")}, with - to sort descending.`);
149
199
  }
150
200
  function serializeSelectItems(items) {
151
201
  if (items.length === 0)
@@ -153,7 +203,7 @@ function serializeSelectItems(items) {
153
203
  return items
154
204
  .map((item) => {
155
205
  if (typeof item === "string") {
156
- return item === "*" ? item : selectName(item, "select item");
206
+ return item === "*" ? item : selectItemName(item);
157
207
  }
158
208
  if (!item || typeof item !== "object" || Array.isArray(item)) {
159
209
  throw new TypeError("@capacms/sdk/next: each select item must be a field name or relation object.");
@@ -162,12 +212,12 @@ function serializeSelectItems(items) {
162
212
  if (entries.length !== 1) {
163
213
  throw new TypeError("@capacms/sdk/next: a relation select object must name exactly one field.");
164
214
  }
165
- const [rawName, value] = entries[0];
166
- const name = selectName(rawName, "relation select key");
215
+ const [name, value] = entries[0];
216
+ const written = selectName(name, "relation select key");
167
217
  if (value === "*")
168
- return `${name}(*)`;
218
+ return `${written}(*)`;
169
219
  if (Array.isArray(value))
170
- return `${name}(${serializeSelectItems(value)})`;
220
+ return `${written}(${serializeSelectItems(value)})`;
171
221
  if (!value || typeof value !== "object") {
172
222
  throw new TypeError(`@capacms/sdk/next: ${name} must select fields, "*", or select options.`);
173
223
  }
@@ -192,19 +242,15 @@ function serializeSelectItems(items) {
192
242
  }
193
243
  args.push(`limit:${options.limit}`);
194
244
  }
195
- if (options.sort !== undefined) {
196
- if (typeof options.sort !== "string" || !/^-?[A-Za-z0-9_-]+$/.test(options.sort)) {
197
- throw new TypeError(`@capacms/sdk/next: ${name}.sort must be one field name.`);
198
- }
199
- args.push(`sort:${options.sort}`);
200
- }
245
+ if (options.sort !== undefined)
246
+ args.push(`sort:${nestedSort(options.sort, name)}`);
201
247
  if (options.after !== undefined) {
202
248
  if (typeof options.after !== "string" || options.after === "") {
203
249
  throw new TypeError(`@capacms/sdk/next: ${name}.after must be a non-empty cursor.`);
204
250
  }
205
251
  args.push(`after:${options.after}`);
206
252
  }
207
- return `${name}(${args.join(",")})`;
253
+ return `${written}(${args.join(",")})`;
208
254
  })
209
255
  .join(",");
210
256
  }
@@ -287,39 +333,6 @@ function listQuery(options) {
287
333
  query.set("before", options.before);
288
334
  return query;
289
335
  }
290
- function optionalString(value) {
291
- return typeof value === "string" ? value : undefined;
292
- }
293
- function unparseable(status, requestId) {
294
- return new CapaError({
295
- status,
296
- type: "api_error",
297
- code: "unparseable_response",
298
- message: "Capa returned a response that was not a valid /api/ JSON envelope.",
299
- requestId,
300
- docs: "",
301
- });
302
- }
303
- function errorFromEnvelope(status, body, fallbackRequestId) {
304
- const detail = body.error;
305
- if (!detail ||
306
- typeof detail.type !== "string" ||
307
- typeof detail.code !== "string" ||
308
- typeof detail.message !== "string" ||
309
- typeof detail.docs !== "string") {
310
- return unparseable(status, fallbackRequestId);
311
- }
312
- return new CapaError({
313
- status,
314
- type: detail.type,
315
- code: detail.code,
316
- message: detail.message,
317
- param: optionalString(detail.param),
318
- hint: optionalString(detail.hint),
319
- requestId: optionalString(body.meta?.requestId) ?? fallbackRequestId,
320
- docs: detail.docs,
321
- });
322
- }
323
336
  function cacheTags(response) {
324
337
  const value = response.headers?.get?.("Surrogate-Key") ?? "";
325
338
  return value.split(/\s+/).filter(Boolean);
@@ -354,12 +367,13 @@ function createRequester(config) {
354
367
  body = JSON.parse(text);
355
368
  }
356
369
  catch {
357
- throw unparseable(response.status, requestId);
370
+ throw (0, errors_1.unparseable)(response.status, requestId);
371
+ }
372
+ if (!response.ok) {
373
+ throw (0, errors_1.errorFromEnvelope)(response.status, body, requestId, (0, errors_1.retryAfterOf)(response.headers?.get?.("Retry-After")));
358
374
  }
359
- if (!response.ok)
360
- throw errorFromEnvelope(response.status, body, requestId);
361
375
  if (!body || typeof body !== "object")
362
- throw unparseable(response.status, requestId);
376
+ throw (0, errors_1.unparseable)(response.status, requestId);
363
377
  if (config.editMode === true) {
364
378
  // `included` holds a flat read's related entries, which are marked
365
379
  // exactly as they are when the tree nests them inside `data`.
@@ -373,7 +387,36 @@ function createRequester(config) {
373
387
  }
374
388
  function createClient(config) {
375
389
  const resolved = resolveNextConfig(config);
390
+ if ((0, key_family_1.keyFamily)(resolved.apiKey) === "legacy")
391
+ (0, key_family_1.warnLegacyKeyOnce)(resolved.apiKey);
376
392
  const request = createRequester(resolved);
393
+ // By POST: introspection is never cached (G plan D16), and the admin host
394
+ // serves GraphQL by POST only.
395
+ const readSchema = async (signal) => {
396
+ const result = await (0, request_1.runGraphQL)(resolved, introspection_1.INTROSPECTION_QUERY, undefined, { signal, method: "POST" });
397
+ const requestId = result.extensions.capa?.requestId ?? "";
398
+ // The API answered 200, so the status says so; the errors say what failed.
399
+ if (result.errors.length > 0)
400
+ throw (0, errors_1.errorFromGraphQLErrors)(200, result.errors, requestId);
401
+ if (!result.data)
402
+ throw (0, errors_1.unparseable)(200, requestId);
403
+ return (0, summary_1.summarizeIntrospection)(result.data);
404
+ };
405
+ // By POST, as the schema above: it selects only `__schema`.
406
+ const readFieldNames = async () => {
407
+ const result = await (0, request_1.runGraphQL)(resolved, introspection_1.FIELD_NAMES_QUERY, undefined, { method: "POST" });
408
+ if (!result.data || result.errors.length > 0)
409
+ throw new Error("the key's field names could not be read");
410
+ return result.data;
411
+ };
412
+ /** One GraphQL read; in edit mode its entries are marked for `capaAttrs`, as REST's are. */
413
+ const readGraphQL = (document, variables, options) => {
414
+ const read = () => (0, request_1.runGraphQL)(resolved, document, variables, options);
415
+ if (resolved.editMode !== true)
416
+ return read();
417
+ return (0, edit_mode_1.readMarked)(document, `${resolved.baseUrl}\n${resolved.apiKey}\n${resolved.version}`, readFieldNames, read);
418
+ };
419
+ const graphql = graphqlClient(readGraphQL);
377
420
  /**
378
421
  * A flat read's result carries the select it sent, so `inflate(result)`
379
422
  * needs nothing else. A tree read's result is exactly what it always was.
@@ -399,7 +442,7 @@ function createClient(config) {
399
442
  return withSelect({ ...result.body, cacheTags: result.cacheTags }, options);
400
443
  }
401
444
  catch (error) {
402
- if (error instanceof CapaError &&
445
+ if (error instanceof errors_1.CapaError &&
403
446
  error.status === 404 &&
404
447
  error.type === "not_found" &&
405
448
  error.code === "entry_not_found") {
@@ -441,7 +484,7 @@ function createClient(config) {
441
484
  // Same shape as `entries.get`: a page nobody has declared or read is a
442
485
  // null, not a throw, because "is this page known to Capa" is a question
443
486
  // a caller asks on purpose.
444
- if (error instanceof CapaError &&
487
+ if (error instanceof errors_1.CapaError &&
445
488
  error.status === 404 &&
446
489
  error.code === "page_not_found") {
447
490
  return null;
@@ -453,7 +496,12 @@ function createClient(config) {
453
496
  return {
454
497
  entries,
455
498
  pages,
499
+ graphql,
500
+ graphqlSchema(options = {}) {
501
+ return readSchema(options.signal);
502
+ },
456
503
  async preview(token, options = {}) {
504
+ (0, key_family_1.requirePreviewKey)(resolved.apiKey);
457
505
  const query = new URLSearchParams();
458
506
  query.set("token", token);
459
507
  try {
@@ -464,7 +512,7 @@ function createClient(config) {
464
512
  // preview route has the same fallback for either: render the published
465
513
  // page. Anything else (a 500, a network failure, a missing scope) is a
466
514
  // real problem and must not be mistaken for a stale link.
467
- if (error instanceof CapaError &&
515
+ if (error instanceof errors_1.CapaError &&
468
516
  (error.code === "preview_token_invalid" || error.code === "preview_token_expired")) {
469
517
  return null;
470
518
  }
@@ -0,0 +1,162 @@
1
+ /**
2
+ * entry-fields.ts — an entry's `fields` as `/api/entries` returns them, typed
3
+ * from the model and the select.
4
+ *
5
+ * A model type says what an editor stores: `capa-codegen` writes a relation as
6
+ * the related model (`CapaRelation<Authors>`), a relation list as a list of
7
+ * them, and media as the upload (`CapaImage`). A read answers in its own shape
8
+ * (docs/api/entries.md), and these types say which:
9
+ *
10
+ * - a field the select names is always there, and `null` when it was never
11
+ * filled, as the API writes it: optional in the model, never absent here;
12
+ * - a field the select does not name is not there;
13
+ * - a relation the select does not expand is a reference, `{ id, model }`,
14
+ * or null when it is empty;
15
+ * - an expanded relation is the related entry, with `fields` of its own, or
16
+ * `{ id, model, missing: true }` when that entry was deleted, is
17
+ * unpublished for a production key, or is in a model the key cannot read;
18
+ * - a relation list is `{ items, pageInfo }`, expanded or not;
19
+ * - media is `{ id, url, alt, type, width, height }`, never the upload's
20
+ * own keys.
21
+ *
22
+ * A select the compiler can read exactly, a literal passed as its type
23
+ * (`list<Articles, typeof select>`), types what it names. One it cannot, a
24
+ * string or a value typed as `Select<T>` itself, types every field, and each
25
+ * relation as any of the three it may be, which the caller narrows
26
+ * (`"fields" in author`). An untyped model (`Record<string, unknown>`) stays
27
+ * untyped.
28
+ */
29
+ import type { Entry } from "./client";
30
+ import type { Select } from "./select-types";
31
+ /** A relation a read did not expand: the entry's id, and its model's namespace, null when the key cannot read that model. */
32
+ export interface EntryReference {
33
+ id: string;
34
+ model: string | null;
35
+ }
36
+ /**
37
+ * An expanded relation the API looked for and did not find: the entry was
38
+ * deleted, is unpublished for a production key, or is in a model the key
39
+ * cannot read.
40
+ */
41
+ export interface MissingEntry extends EntryReference {
42
+ missing: true;
43
+ }
44
+ /** A relation list, expanded or not. `limit` is there when the read expanded it. */
45
+ export interface RelationItems<E> {
46
+ items: E[];
47
+ pageInfo: {
48
+ limit?: number;
49
+ hasNext: boolean;
50
+ next: string | null;
51
+ };
52
+ }
53
+ /** An image, video or file as `/api/` returns it. */
54
+ export interface EntryMedia {
55
+ id: string;
56
+ url: string | null;
57
+ alt: string | null;
58
+ type: string | null;
59
+ width: number | null;
60
+ height: number | null;
61
+ }
62
+ /**
63
+ * Levels of entries left to type: the root entry and 4 below it, the API's 5
64
+ * levels. A relation past the last level is typed as a reference, which is all
65
+ * the API returns there.
66
+ */
67
+ type Depth = [never, 0, 1, 2, 3, 4];
68
+ type Flatten<T> = {
69
+ [K in keyof T]: T[K];
70
+ };
71
+ /** A field type that says nothing: `unknown`, or `any`. */
72
+ type Untyped<V> = 0 extends 1 & V ? true : unknown extends V ? true : false;
73
+ type RelationBrand = {
74
+ readonly __capaRelation: "one" | "many";
75
+ readonly __capaRelationTarget: unknown;
76
+ };
77
+ type IsRelation<V> = Untyped<V> extends true ? false : NonNullable<V> extends RelationBrand ? true : false;
78
+ type IsList<V> = NonNullable<V> extends {
79
+ readonly __capaRelation: "many";
80
+ } ? true : false;
81
+ type TargetOf<V> = NonNullable<V> extends {
82
+ readonly __capaRelationTarget: infer R;
83
+ } ? R : never;
84
+ /** Media as codegen types it: `CapaImage`, `CapaVideo` and `CapaFile` each have a `url`. */
85
+ type StoredMedia = {
86
+ url: string;
87
+ };
88
+ /**
89
+ * A field that is no relation: as stored, or `null` when it was never filled,
90
+ * but media, which the read returns in its public shape.
91
+ */
92
+ type ValueTree<V> = Untyped<V> extends true ? V : NonNullable<V> extends ReadonlyArray<infer M> ? [M] extends [StoredMedia] ? EntryMedia[] | null : NonNullable<V> | null : NonNullable<V> extends StoredMedia ? EntryMedia | null : NonNullable<V> | null;
93
+ /** A relation the read did not expand. */
94
+ type ReferenceTree<V> = IsList<V> extends true ? RelationItems<EntryReference> : EntryReference | null;
95
+ /** A field the read names without expanding it. */
96
+ type FieldTree<V> = IsRelation<V> extends true ? ReferenceTree<V> : ValueTree<V>;
97
+ /** An expanded relation, its entries read with `S`, at level `D`. */
98
+ type ExpandedTree<V, S, D extends number> = IsList<V> extends true ? RelationItems<Entry<FieldsAt<TargetOf<V>, S, D>> | MissingEntry> : Entry<FieldsAt<TargetOf<V>, S, D>> | MissingEntry | null;
99
+ /** A relation the read may or may not have expanded, with any select, at level `D`. */
100
+ type EitherTree<V, D extends number> = IsList<V> extends true ? RelationItems<EntryReference | MissingEntry | Entry<FieldsAt<TargetOf<V>, string, D>>> : EntryReference | MissingEntry | Entry<FieldsAt<TargetOf<V>, string, D>> | null;
101
+ /** Every field, relations as references: a read with no select, or `*`. */
102
+ type ReferenceFields<T> = {
103
+ [K in keyof T]-?: FieldTree<T[K]>;
104
+ };
105
+ /** Every field, each relation as whatever it may be: a select the compiler cannot read. */
106
+ type LooseFields<T, D extends number> = {
107
+ [K in keyof T]-?: IsRelation<T[K]> extends true ? EitherTree<T[K], Depth[D]> : ValueTree<T[K]>;
108
+ };
109
+ /** The relations a select item expands, by name. */
110
+ type ExpandedIn<I> = I extends string ? never : Extract<keyof I, string>;
111
+ /** What the select writes for relation `K`: its list, `*`, or its options' `select`. */
112
+ type SubSelect<I, K extends string> = I extends {
113
+ readonly [P in K]: infer V;
114
+ } ? V extends {
115
+ readonly select: infer S;
116
+ } ? S : V : never;
117
+ /**
118
+ * The fields a literal select names, `I` being the union of its items: each
119
+ * named field, every field for `*`, and each expanded relation as the entries
120
+ * its own select reads. A system key (`$tags`) sits beside `fields`, on the entry.
121
+ */
122
+ type SelectedFields<T, I, D extends number> = Flatten<{
123
+ [K in keyof T as K extends ExpandedIn<I> ? IsRelation<T[K]> extends true ? never : K : "*" extends I ? K : K extends I ? K : never]-?: FieldTree<T[K]>;
124
+ } & {
125
+ [K in keyof T as K extends ExpandedIn<I> ? (IsRelation<T[K]> extends true ? K : never) : never]-?: ExpandedTree<T[K], SubSelect<I, K & string>, Depth[D]>;
126
+ }>;
127
+ type FieldsAt<T, S, D extends number> = string extends keyof T ? T : [D] extends [never] ? ReferenceFields<T> : [S] extends [undefined] ? ReferenceFields<T> : [S] extends ["*"] ? ReferenceFields<T> : S extends ReadonlyArray<infer I> ? Select<T> extends S ? LooseFields<T, D> : SelectedFields<T, I, D> : LooseFields<T, D>;
128
+ /**
129
+ * `fields` of an entry of model `T` read with select `S`, as `/api/entries`
130
+ * returns them. Pass the select's own type for exact fields
131
+ * (`list<Articles, typeof select>`); without it every field is typed, and
132
+ * each relation as whatever it may be.
133
+ */
134
+ export type EntryFields<T, S = Select<T> | string> = FieldsAt<T, S, 4>;
135
+ /** A field of a `shape=flat` read: each relation a reference, since the entry it names is in `included`. */
136
+ type FlatTree<V> = IsRelation<V> extends true ? IsList<V> extends true ? RelationItems<EntryReference | MissingEntry> : EntryReference | MissingEntry | null : ValueTree<V>;
137
+ /** The fields a select names at its root: every field for none, `*`, a string or `Select<T>`. */
138
+ type NamedKeys<T, S> = [S] extends [undefined] ? keyof T : S extends ReadonlyArray<infer I> ? Select<T> extends S ? keyof T : "*" extends I ? keyof T : Extract<keyof T, I | ExpandedIn<I>> : keyof T;
139
+ /** `fields` of an entry in a `shape=flat` read's `data`. */
140
+ export type FlatFields<T, S = Select<T>> = string extends keyof T ? T : {
141
+ [K in keyof T as K extends NamedKeys<T, S> ? K : never]-?: FlatTree<T[K]>;
142
+ };
143
+ /**
144
+ * `fields` of an entry in `included`: the union of what every path that
145
+ * reached it selected, so any field may be absent.
146
+ */
147
+ export type IncludedFields<I> = I extends unknown ? string extends keyof I ? I : {
148
+ [K in keyof I]?: FlatTree<I[K]>;
149
+ } : never;
150
+ /**
151
+ * Where a flat result keeps the model and select it was read with, for
152
+ * `inflate` to type the tree it returns. A symbol, never a field, and never a
153
+ * runtime value.
154
+ */
155
+ declare const flatRead: unique symbol;
156
+ export type { flatRead };
157
+ export interface FlatRead<T, S> {
158
+ readonly [flatRead]?: {
159
+ model: T;
160
+ select: S;
161
+ };
162
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,136 @@
1
+ /**
2
+ * errors.ts — what a failed `/api/` call throws.
3
+ *
4
+ * `CapaError` is one refusal of the whole request: the REST envelope's
5
+ * `{ error }`, or a GraphQL response with `errors` and no `data`, whatever
6
+ * its status. `CapaGraphQLError` is one entry of a GraphQL `errors` array. A
7
+ * GraphQL request the API ran resolves with those in `errors` rather than
8
+ * throwing, because its `data` carries the root fields that did succeed. They
9
+ * are returned, not thrown, so they are plain objects: a result goes through
10
+ * `Response.json` or into a client component's props with every field kept.
11
+ */
12
+ export interface ApiErrorEnvelope {
13
+ error?: {
14
+ type?: unknown;
15
+ code?: unknown;
16
+ message?: unknown;
17
+ param?: unknown;
18
+ hint?: unknown;
19
+ docs?: unknown;
20
+ };
21
+ meta?: {
22
+ requestId?: unknown;
23
+ };
24
+ }
25
+ export declare class CapaError extends Error {
26
+ readonly status: number;
27
+ readonly type: string;
28
+ readonly code: string;
29
+ readonly param?: string;
30
+ readonly hint?: string;
31
+ readonly requestId: string;
32
+ readonly docs: string;
33
+ /**
34
+ * Every error of a refused GraphQL request, in order. The fields above come
35
+ * from the first one. Empty for a REST call.
36
+ */
37
+ readonly graphqlErrors: readonly CapaGraphQLError[];
38
+ /**
39
+ * Seconds the API asked the caller to wait before trying again, from a 429's
40
+ * `Retry-After` (seconds or an HTTP date). Undefined when it sent none.
41
+ */
42
+ readonly retryAfter?: number;
43
+ constructor(input: {
44
+ status: number;
45
+ type: string;
46
+ code: string;
47
+ message: string;
48
+ param?: string;
49
+ hint?: string;
50
+ requestId: string;
51
+ docs: string;
52
+ graphqlErrors?: readonly CapaGraphQLError[];
53
+ retryAfter?: number;
54
+ });
55
+ }
56
+ /**
57
+ * A `Retry-After` header in seconds: delta-seconds as sent, an HTTP date as
58
+ * the whole seconds from `now` until it (0 once it has passed). Undefined when
59
+ * the header is absent or neither.
60
+ */
61
+ export declare function retryAfterOf(value: string | null | undefined, now?: number): number | undefined;
62
+ export declare function isCapaError(error: unknown): error is CapaError;
63
+ export declare function optionalString(value: unknown): string | undefined;
64
+ export declare function unparseable(status: number, requestId: string): CapaError;
65
+ export declare function errorFromEnvelope(status: number, body: ApiErrorEnvelope, fallbackRequestId: string, retryAfter?: number): CapaError;
66
+ /** One `errors[]` item as the API sends it. */
67
+ export interface GraphQLErrorItem {
68
+ message: string;
69
+ locations?: Array<{
70
+ line: number;
71
+ column: number;
72
+ }>;
73
+ path?: Array<string | number>;
74
+ extensions?: {
75
+ code?: string;
76
+ type?: string;
77
+ param?: string;
78
+ hint?: string;
79
+ docs?: string;
80
+ requestId?: string;
81
+ [key: string]: unknown;
82
+ };
83
+ }
84
+ /**
85
+ * One GraphQL error: the spec's `{ message, locations, path, extensions }`,
86
+ * with the fields a caller acts on lifted out of `extensions`: `code` to
87
+ * branch on, `hint` for the fix, `docs` for the page that explains it,
88
+ * `param` for the argument at fault. `path` names the root field it belongs
89
+ * to.
90
+ *
91
+ * A plain object, not an `Error`: an `Error`'s `message` is not enumerable,
92
+ * so `JSON.stringify` drops it, and React sends an `Error` to a client
93
+ * component as a generic message in production. Only what the API sent is
94
+ * set, so a JSON round trip gives back an equal object. Check one with
95
+ * `isCapaGraphQLError`.
96
+ */
97
+ export interface CapaGraphQLError {
98
+ readonly message: string;
99
+ readonly locations?: ReadonlyArray<{
100
+ line: number;
101
+ column: number;
102
+ }>;
103
+ readonly path?: ReadonlyArray<string | number>;
104
+ readonly extensions: Readonly<Record<string, unknown>>;
105
+ /** `extensions.code`, or `unknown` when the API sent none. */
106
+ readonly code: string;
107
+ readonly type?: string;
108
+ readonly param?: string;
109
+ readonly hint?: string;
110
+ readonly docs?: string;
111
+ }
112
+ /** Whether `value` is a `CapaGraphQLError`: by its shape, so one that went through JSON is one too. */
113
+ export declare function isCapaGraphQLError(value: unknown): value is CapaGraphQLError;
114
+ /** Turn a parsed `errors` value into typed errors, skipping anything that is not one. */
115
+ export declare function graphqlErrorsOf(value: unknown): CapaGraphQLError[];
116
+ /**
117
+ * A GraphQL request the API refused as a whole: a status other than 200, or
118
+ * a 200 with `errors` and no `data`, passed in as the refusal's `status`. The
119
+ * `CapaError` fields come from the first error, and `graphqlErrors` holds all
120
+ * of them. A body in the REST envelope shape, which a proxy or an older API
121
+ * can send, is read as one.
122
+ */
123
+ export declare function errorFromGraphQLBody(status: number, body: unknown, fallbackRequestId: string, retryAfter?: number): CapaError;
124
+ /** A `CapaError` for errors already read: the fields of the first, and all of them in `graphqlErrors`. */
125
+ export declare function errorFromGraphQLErrors(status: number, errors: readonly CapaGraphQLError[], fallbackRequestId: string, retryAfter?: number): CapaError;
126
+ /**
127
+ * `/api/graphql` answered as a path the API does not serve: a POST
128
+ * `405 mutations_not_enabled`, a GET `404 route_not_found`, in the REST
129
+ * envelope. `CAPA_API_GRAPHQL=off` does that, and so does the admin host for a
130
+ * GET, since it serves GraphQL by POST only. The GraphQL handler never answers
131
+ * in that envelope (a real mutation's refusal is `{ errors }`), so the
132
+ * envelope is what tells "GraphQL is not here" from "this document was
133
+ * refused", and a query is never told it tried to write. Null for any other
134
+ * answer.
135
+ */
136
+ export declare function graphqlNotServed(status: number, body: unknown, method: "GET" | "POST", fallbackRequestId: string): CapaError | null;