@capacms/sdk 1.0.0-next.13 → 1.0.0-next.15
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 +114 -0
- package/README.md +220 -1
- package/bin/capa-codegen.js +90 -8
- package/bin/capa.js +1 -1
- package/dist/codegen.d.ts +62 -4
- package/dist/codegen.js +216 -7
- package/dist/esm/overlay/index.js +13 -2
- package/dist/esm/overlay/protocol.d.ts +3 -0
- package/dist/esm/overlay/protocol.js +4 -1
- package/dist/index.d.ts +1 -1
- package/dist/next/client.d.ts +44 -1
- package/dist/next/client.js +23 -0
- package/dist/next/entry-fields.d.ts +6 -7
- package/dist/next/index.d.ts +3 -3
- package/dist/next/index.js +2 -1
- package/dist/next/select-types.d.ts +43 -4
- package/dist/nextjs/index.d.ts +5 -0
- package/dist/nextjs/index.js +28 -9
- package/dist/overlay/index.js +13 -2
- package/dist/overlay/protocol.d.ts +3 -0
- package/dist/overlay/protocol.js +4 -1
- package/dist/sanitize/index.d.ts +70 -0
- package/dist/sanitize/index.js +296 -0
- package/package.json +17 -3
package/dist/codegen.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LEGACY_PREAMBLE = exports.LEGACY_TYPE_MAP = void 0;
|
|
3
|
+
exports.API_PREAMBLE = exports.LEGACY_PREAMBLE = exports.LEGACY_TYPE_MAP = void 0;
|
|
4
|
+
exports.codegenShape = codegenShape;
|
|
5
|
+
exports.readStampedShape = readStampedShape;
|
|
4
6
|
exports.readStampedChecksum = readStampedChecksum;
|
|
5
7
|
exports.interfaceName = interfaceName;
|
|
6
8
|
exports.modelInterfaceNames = modelInterfaceNames;
|
|
@@ -8,6 +10,7 @@ exports.normalizeTypes = normalizeTypes;
|
|
|
8
10
|
exports.typeNames = typeNames;
|
|
9
11
|
exports.generate = generate;
|
|
10
12
|
exports.restTypesFromIntrospection = restTypesFromIntrospection;
|
|
13
|
+
exports.apiTypes = apiTypes;
|
|
11
14
|
/**
|
|
12
15
|
* codegen.ts — pull a tenant's generated types and write them to disk.
|
|
13
16
|
*
|
|
@@ -64,6 +67,24 @@ const GENERATED_HEADER = new Set([
|
|
|
64
67
|
"// Generated by @capacms/sdk. Do not edit by hand.",
|
|
65
68
|
"// Re-run `capa-codegen` after changing a model in Capa.",
|
|
66
69
|
]);
|
|
70
|
+
/** The line an `api` module carries, so a run asked for the other shape does not take the file as current. */
|
|
71
|
+
const SHAPE_MARKER = "// @capa-codegen-shape ";
|
|
72
|
+
/** The shape `options` asks for, `"legacy"` when it names none. */
|
|
73
|
+
function codegenShape(shape) {
|
|
74
|
+
if (shape === undefined || shape === null || shape === "legacy")
|
|
75
|
+
return "legacy";
|
|
76
|
+
if (shape === "api")
|
|
77
|
+
return "api";
|
|
78
|
+
throw new Error(`shape must be "legacy" or "api", not ${JSON.stringify(shape)}.`);
|
|
79
|
+
}
|
|
80
|
+
/** The shape a generated file was written in: `"api"` when it says so, `"legacy"` otherwise. */
|
|
81
|
+
function readStampedShape(existing) {
|
|
82
|
+
for (const line of (existing ?? "").split("\n", 20)) {
|
|
83
|
+
if (line.startsWith(SHAPE_MARKER))
|
|
84
|
+
return line.slice(SHAPE_MARKER.length).trim() === "api" ? "api" : "legacy";
|
|
85
|
+
}
|
|
86
|
+
return "legacy";
|
|
87
|
+
}
|
|
67
88
|
/** Read the checksum a previous run stamped into the generated file. */
|
|
68
89
|
function readStampedChecksum(existing) {
|
|
69
90
|
if (!existing)
|
|
@@ -122,8 +143,8 @@ function namespaceInterface(namespace) {
|
|
|
122
143
|
* cannot declare an alias twice. Every model caught in such a collision is
|
|
123
144
|
* named by `namespaceInterface` instead, whatever order the models come in.
|
|
124
145
|
*/
|
|
125
|
-
function modelInterfaceNames(models) {
|
|
126
|
-
const claims = new Map(
|
|
146
|
+
function modelInterfaceNames(models, shared = SHARED_NAMES) {
|
|
147
|
+
const claims = new Map(shared.map((name) => [name, new Set([""])]));
|
|
127
148
|
for (const { namespace } of models) {
|
|
128
149
|
const base = modelInterface(namespace);
|
|
129
150
|
for (const name of [base, `${base}Select`, `${base}Attrs`]) {
|
|
@@ -341,18 +362,26 @@ function typeNames(source) {
|
|
|
341
362
|
}
|
|
342
363
|
/**
|
|
343
364
|
* Fetch types if — and only if — the tenant's schema has changed since the
|
|
344
|
-
* checksum stamped in `existing
|
|
365
|
+
* checksum stamped in `existing`, or `existing` is in the other shape.
|
|
345
366
|
*/
|
|
346
|
-
async function generate(config, existing) {
|
|
367
|
+
async function generate(config, existing, options = {}) {
|
|
368
|
+
const shape = codegenShape(options.shape);
|
|
347
369
|
const resolved = (0, config_1.resolveConfig)(config);
|
|
348
370
|
const stamped = readStampedChecksum(existing);
|
|
349
|
-
|
|
371
|
+
// The checksum says the models did not move, not that the file is in the
|
|
372
|
+
// shape asked for: a file in the other shape is read again.
|
|
373
|
+
const etag = stamped && readStampedShape(existing) === shape ? `"${stamped}"` : null;
|
|
350
374
|
const schema = await (0, http_1.getJsonConditional)(resolved, "/v2/schema", etag);
|
|
351
375
|
if (schema === null) {
|
|
352
376
|
// 304 — the model set is byte-identical to what produced the file on disk.
|
|
353
377
|
return { changed: false, checksum: stamped, source: null, types: [] };
|
|
354
378
|
}
|
|
355
379
|
const checksum = schema.body?.checksum ?? (schema.etag ? schema.etag.replace(/"/g, "") : null);
|
|
380
|
+
if (shape === "api") {
|
|
381
|
+
// `/v2/schema` holds everything the shape needs, so `/v2/schema/types` is not read.
|
|
382
|
+
const source = apiTypes(schema.body?.models ?? [], checksum);
|
|
383
|
+
return { changed: source !== (existing ?? null), checksum, source, types: typeNames(source) };
|
|
384
|
+
}
|
|
356
385
|
const raw = await (0, http_1.getText)(resolved, "/v2/schema/types");
|
|
357
386
|
const source = normalizeTypes(raw, checksum, schema.body);
|
|
358
387
|
return { changed: source !== (existing ?? null), checksum, source, types: typeNames(source) };
|
|
@@ -442,9 +471,16 @@ function legacyFieldType(field) {
|
|
|
442
471
|
* no values; and a field GraphQL leaves out (its description's `Not exposed:`
|
|
443
472
|
* line) is `unknown`. No checksum is stamped: `CAPA_SCHEMA_CHECKSUM` is the
|
|
444
473
|
* `/v2/schema` checksum, which a `cap_` key cannot read.
|
|
474
|
+
*
|
|
475
|
+
* `shape: "api"` writes `apiTypes` from the same models instead, each enum
|
|
476
|
+
* a `string` (`apiModelsFromSummary`).
|
|
445
477
|
*/
|
|
446
|
-
function restTypesFromIntrospection(introspection) {
|
|
478
|
+
function restTypesFromIntrospection(introspection, options = {}) {
|
|
447
479
|
const summary = (0, summary_1.summarizeIntrospection)(introspection);
|
|
480
|
+
if (codegenShape(options.shape) === "api") {
|
|
481
|
+
const source = apiTypes(apiModelsFromSummary(summary.models), null);
|
|
482
|
+
return { source, types: typeNames(source), restOnly: summary.restOnly };
|
|
483
|
+
}
|
|
448
484
|
let text = `// Auto-generated Capa CMS types\n\n${exports.LEGACY_PREAMBLE}`;
|
|
449
485
|
const models = [];
|
|
450
486
|
for (const model of summary.models) {
|
|
@@ -467,3 +503,176 @@ function restTypesFromIntrospection(introspection) {
|
|
|
467
503
|
const source = normalizeTypes(text, null, { models });
|
|
468
504
|
return { source, types: typeNames(source), restOnly: summary.restOnly };
|
|
469
505
|
}
|
|
506
|
+
// -------------------------------------------------- shape: "api" (ENG-5667) ---
|
|
507
|
+
/**
|
|
508
|
+
* The declarations every `api` module holds beside its models, written as
|
|
509
|
+
* `/api/entries` returns each value (apps/api/src/api-next/entries/shape.ts):
|
|
510
|
+
*
|
|
511
|
+
* media `renderMedia`: the six keys every image, video and file has,
|
|
512
|
+
* then the placeholders and the focal point and crop, which are
|
|
513
|
+
* on every media value of a project that has them and absent from
|
|
514
|
+
* one that does not. `EntryMedia` in `@capacms/sdk/next`.
|
|
515
|
+
* relations a read with no select, or `*`, does not expand them: a
|
|
516
|
+
* relation is `{ id, model }` (`renderSingleRelation`), a relation
|
|
517
|
+
* list `{ items, pageInfo }`, never null (`renderArrayRelation`).
|
|
518
|
+
* The brand is optional, so the reference the API sends is the
|
|
519
|
+
* type, and it still names the related model for `Select<T>` and
|
|
520
|
+
* `EntryFields<T, S>`, which type an expanded read. A list into a
|
|
521
|
+
* model the module lacks carries `CapaReferenceListBrand`
|
|
522
|
+
* instead, which is how `EntryFields` knows it is never null.
|
|
523
|
+
* Both brands are keyed by symbols `@capacms/sdk/next` declares
|
|
524
|
+
* (next/select-types.ts), imported type-only (`API_IMPORT`).
|
|
525
|
+
*/
|
|
526
|
+
exports.API_PREAMBLE = `/**
|
|
527
|
+
* An image, video or file, as /api/ returns it. The keys after \`height\` are
|
|
528
|
+
* there when the project has image placeholders or focal points and crops,
|
|
529
|
+
* and null for a file without one.
|
|
530
|
+
*/
|
|
531
|
+
export interface CapaMedia {
|
|
532
|
+
id: string;
|
|
533
|
+
url: string | null;
|
|
534
|
+
alt: string | null;
|
|
535
|
+
type: string | null;
|
|
536
|
+
width: number | null;
|
|
537
|
+
height: number | null;
|
|
538
|
+
blurDataURL?: string | null;
|
|
539
|
+
averageColor?: string | null;
|
|
540
|
+
dominantColor?: string | null;
|
|
541
|
+
palette?: string[] | null;
|
|
542
|
+
blurhash?: string | null;
|
|
543
|
+
focalPoint?: { x: number; y: number } | null;
|
|
544
|
+
crop?: { x: number; y: number; width: number; height: number } | null;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
export type CapaImage = CapaMedia;
|
|
548
|
+
export type CapaVideo = CapaMedia;
|
|
549
|
+
export type CapaFile = CapaMedia;
|
|
550
|
+
|
|
551
|
+
/** A relation a read did not expand: the entry's id, and its model's namespace, null when the key cannot read that model. */
|
|
552
|
+
export interface CapaReference {
|
|
553
|
+
id: string;
|
|
554
|
+
model: string | null;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* A relation list a read did not expand. \`limit\` is there when a read
|
|
559
|
+
* expands it. Never null, which its type-only brand tells the SDK's typed reads.
|
|
560
|
+
*/
|
|
561
|
+
export interface CapaReferenceList extends CapaReferenceListBrand {
|
|
562
|
+
items: CapaReference[];
|
|
563
|
+
pageInfo: { limit?: number; hasNext: boolean; next: string | null };
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/** A relation to a model of this file: a reference, branded with the model a select can expand it into. */
|
|
567
|
+
export type CapaRelation<T> = CapaReference & CapaRelationBrand<"one", T>;
|
|
568
|
+
export type CapaRelationList<T> = CapaReferenceList & CapaRelationBrand<"many", T>;
|
|
569
|
+
`;
|
|
570
|
+
/**
|
|
571
|
+
* The api module's one import: the brands, type-only, from the SDK that
|
|
572
|
+
* declares their symbols, so no type of the customer's can carry them. The
|
|
573
|
+
* same specifier the \`Select\` aliases name.
|
|
574
|
+
*/
|
|
575
|
+
const API_IMPORT = 'import type { CapaReferenceListBrand, CapaRelationBrand } from "@capacms/sdk/next";';
|
|
576
|
+
/**
|
|
577
|
+
* The names an api module declares or imports, which a model's interface
|
|
578
|
+
* must not take. Both shapes' names, so a model keeps its name across shapes
|
|
579
|
+
* where it can.
|
|
580
|
+
*/
|
|
581
|
+
const API_SHARED_NAMES = [...new Set([...SHARED_NAMES, ...typeNames(exports.API_PREAMBLE), "CapaReferenceListBrand", "CapaRelationBrand"])];
|
|
582
|
+
/** A field's type in an `api` module: what `renderField` returns for it. */
|
|
583
|
+
function apiFieldType(field, models, names) {
|
|
584
|
+
const target = field.relationRef ? models.find((model) => model.id === field.relationRef || model.namespace === field.relationRef) : undefined;
|
|
585
|
+
const targetName = target ? names.get(target.namespace) : null;
|
|
586
|
+
if (field.type === "relation")
|
|
587
|
+
return targetName ? `CapaRelation<${targetName}> | null` : "CapaReference | null";
|
|
588
|
+
if (field.type === "array" && field.arrayType === "relation")
|
|
589
|
+
return targetName ? `CapaRelationList<${targetName}>` : "CapaReferenceList";
|
|
590
|
+
// An enum is checked against its options on every write (validateFieldType
|
|
591
|
+
// in apps/api/src/helpers/utils.ts), and narrowing them is a field
|
|
592
|
+
// conversion that moves the stored values (sameSig in
|
|
593
|
+
// packages/shared/src/field-conversion/types.ts). A single enum also takes
|
|
594
|
+
// "", which a cleared select saves; an item of a list does not. One with no
|
|
595
|
+
// options, or whose options are not known (a cap_ key), takes any text.
|
|
596
|
+
// Each option is written by JSON.stringify, which escapes every character a
|
|
597
|
+
// string literal cannot hold; the legacy shape's `enumLiteral` keeps its bytes.
|
|
598
|
+
const scalar = (type, single) => {
|
|
599
|
+
if (type !== "enum")
|
|
600
|
+
return exports.LEGACY_TYPE_MAP[type ?? ""] ?? "unknown";
|
|
601
|
+
const options = field.enumValues ?? [];
|
|
602
|
+
if (options.length === 0)
|
|
603
|
+
return "string";
|
|
604
|
+
return (single && !options.includes("") ? [...options, ""] : options).map((option) => JSON.stringify(option)).join(" | ");
|
|
605
|
+
};
|
|
606
|
+
if (field.type === "array") {
|
|
607
|
+
const item = scalar(field.arrayType && field.arrayType !== "array" ? field.arrayType : null, false);
|
|
608
|
+
return `${item.includes(" | ") ? `(${item})` : item}[] | null`;
|
|
609
|
+
}
|
|
610
|
+
const type = scalar(field.type, true);
|
|
611
|
+
return type === "unknown" ? type : `${type} | null`;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* An `api` module: each model's interface is its entries' `fields` as
|
|
615
|
+
* `/api/entries` returns them to a read with no select, or `select=*`.
|
|
616
|
+
*
|
|
617
|
+
* Every field is there, since that read prints every field of the model, and
|
|
618
|
+
* every field but a relation list may be `null`: a field never saved, or
|
|
619
|
+
* emptied, reads null (`storedValue`), and so does a required one, since
|
|
620
|
+
* `required` is checked when an entry is saved, not when it is read, and an
|
|
621
|
+
* entry saved before the field was made required keeps no value for it. A
|
|
622
|
+
* list may read `[]` or `null`, as stored.
|
|
623
|
+
*
|
|
624
|
+
* Models and their `Select` and `Attrs` aliases are named and ordered as the
|
|
625
|
+
* legacy shape names and orders them (`modelInterfaceNames`). No `CapaInstance`:
|
|
626
|
+
* `Entry<T>` from `@capacms/sdk/next` is an `/api/` entry.
|
|
627
|
+
*/
|
|
628
|
+
function apiTypes(models, checksum) {
|
|
629
|
+
const names = modelInterfaceNames(models, API_SHARED_NAMES);
|
|
630
|
+
const blocks = models
|
|
631
|
+
.map((model) => {
|
|
632
|
+
const lines = [`export interface ${names.get(model.namespace)} {`];
|
|
633
|
+
for (const field of model.fields ?? [])
|
|
634
|
+
lines.push(` ${propertyKey(field.namespace)}: ${apiFieldType(field, models, names)};`);
|
|
635
|
+
lines.push("}");
|
|
636
|
+
return lines.join("\n");
|
|
637
|
+
})
|
|
638
|
+
.sort((a, b) => typeNames(a)[0].localeCompare(typeNames(b)[0]));
|
|
639
|
+
const aliases = [...names.values()].sort().flatMap((name) => [
|
|
640
|
+
`export type ${name}Select = import("@capacms/sdk/next").Select<${name}>;`,
|
|
641
|
+
`export type ${name}Attrs = import("@capacms/sdk/next").FieldAttrs<${name}>;`,
|
|
642
|
+
]);
|
|
643
|
+
const head = [
|
|
644
|
+
"// Generated by @capacms/sdk. Do not edit by hand.",
|
|
645
|
+
"// Re-run `capa-codegen --shape api` after changing a model in Capa.",
|
|
646
|
+
`${SHAPE_MARKER}api`,
|
|
647
|
+
checksum ? MARKER + checksum : null,
|
|
648
|
+
]
|
|
649
|
+
.filter(Boolean)
|
|
650
|
+
.join("\n");
|
|
651
|
+
const constant = checksum ? `${CHECKSUM_CONST}${JSON.stringify(checksum)};\n\n` : "";
|
|
652
|
+
const intro = "// Each model's fields as /api/entries returns them with no select: every field there, null when empty.";
|
|
653
|
+
return `${head}\n\n${API_IMPORT}\n\n${constant}${intro}\n\n${[exports.API_PREAMBLE.trimEnd(), ...blocks, ...aliases].join("\n\n")}\n`;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* A cap_ key's models for `apiTypes`, from what its GraphQL schema says of
|
|
657
|
+
* them. No enum's options: GraphQL types an enum field as `String`, and the
|
|
658
|
+
* options its description lists for people are cut at 25, joined with `, `
|
|
659
|
+
* and with their whitespace folded (fieldDescription in the API's
|
|
660
|
+
* api-next/graphql/schema.ts), so `red, blue` cannot be told from two
|
|
661
|
+
* options. A wrong union refuses a value the API sends, so the enum is a
|
|
662
|
+
* `string`. A `pk_` key reads the options as saved.
|
|
663
|
+
*/
|
|
664
|
+
function apiModelsFromSummary(summary) {
|
|
665
|
+
return summary.map((model) => ({
|
|
666
|
+
namespace: model.namespace,
|
|
667
|
+
fields: [
|
|
668
|
+
...model.fields.map((field) => ({
|
|
669
|
+
namespace: field.namespace,
|
|
670
|
+
type: field.capaType,
|
|
671
|
+
arrayType: field.arrayType,
|
|
672
|
+
relationRef: field.target,
|
|
673
|
+
})),
|
|
674
|
+
// A field GraphQL leaves out (N5) is still on the entry, of a type it does not say.
|
|
675
|
+
...model.notExposed.map((namespace) => ({ namespace, type: "" })),
|
|
676
|
+
],
|
|
677
|
+
}));
|
|
678
|
+
}
|
|
@@ -54,6 +54,16 @@ function timeoutOf(ms) {
|
|
|
54
54
|
const noop = () => { };
|
|
55
55
|
/** A scroller's overflow that lets a reader scroll it (not `hidden` or `clip`). */
|
|
56
56
|
const SCROLLABLE = /^(auto|scroll|overlay)$/;
|
|
57
|
+
/**
|
|
58
|
+
* Smooth, or "instant" for a viewer who asked for less motion. A site's
|
|
59
|
+
* `prefers-reduced-motion` CSS does not reach a script that asks for
|
|
60
|
+
* "smooth" by name, so the overlay asks the browser itself (`motion` in
|
|
61
|
+
* `SITE_FEATURES` tells the admin it does).
|
|
62
|
+
*/
|
|
63
|
+
function scrollBehavior() {
|
|
64
|
+
const reduce = typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
65
|
+
return reduce ? "instant" : "smooth";
|
|
66
|
+
}
|
|
57
67
|
/**
|
|
58
68
|
* Brings the highlighted element to the middle of the page, scrolling only
|
|
59
69
|
* this page: its own scrollers (a drawer, a horizontal rail) and its window.
|
|
@@ -72,6 +82,7 @@ const SCROLLABLE = /^(auto|scroll|overlay)$/;
|
|
|
72
82
|
function revealOnPage(el) {
|
|
73
83
|
if (typeof el.getClientRects === "function" && el.getClientRects().length === 0)
|
|
74
84
|
return;
|
|
85
|
+
const behavior = scrollBehavior();
|
|
75
86
|
const start = el.getBoundingClientRect();
|
|
76
87
|
let top = start.top;
|
|
77
88
|
let left = start.left;
|
|
@@ -112,13 +123,13 @@ function revealOnPage(el) {
|
|
|
112
123
|
// Where the element will be once this scroller has moved, for the next one out.
|
|
113
124
|
top -= toTop - box.scrollTop;
|
|
114
125
|
left -= toLeft - box.scrollLeft;
|
|
115
|
-
box.scrollTo({ top: toTop, left: toLeft, behavior
|
|
126
|
+
box.scrollTo({ top: toTop, left: toLeft, behavior });
|
|
116
127
|
}
|
|
117
128
|
const dy = move(top, height, 0, window.innerHeight, true);
|
|
118
129
|
const dx = move(left, width, 0, window.innerWidth, false);
|
|
119
130
|
if (Math.abs(dy) < 1 && Math.abs(dx) < 1)
|
|
120
131
|
return;
|
|
121
|
-
window.scrollTo({ top: window.scrollY + dy, left: (window.scrollX ?? 0) + dx, behavior
|
|
132
|
+
window.scrollTo({ top: window.scrollY + dy, left: (window.scrollX ?? 0) + dx, behavior });
|
|
122
133
|
}
|
|
123
134
|
let running = null;
|
|
124
135
|
/**
|
|
@@ -65,6 +65,9 @@ export declare const SITE_SOURCE = "capa";
|
|
|
65
65
|
* What this overlay can do beyond v1, listed in its `ready`. `release`: this
|
|
66
66
|
* SDK sends a release preview's token on its reads (`Capa-Release`), so the
|
|
67
67
|
* admin can tell a site that shows a release from one that shows every draft.
|
|
68
|
+
* `motion`: a highlight scrolls the page at once, not smoothly, for a viewer
|
|
69
|
+
* who asked for less motion (`prefers-reduced-motion: reduce`), so the admin
|
|
70
|
+
* can take the page to an entry without animating it for them.
|
|
68
71
|
*/
|
|
69
72
|
export declare const SITE_FEATURES: readonly string[];
|
|
70
73
|
/** One field of a `patch`: the text being typed, and the saved value the page was rendered from. */
|
|
@@ -65,8 +65,11 @@ export const SITE_SOURCE = "capa";
|
|
|
65
65
|
* What this overlay can do beyond v1, listed in its `ready`. `release`: this
|
|
66
66
|
* SDK sends a release preview's token on its reads (`Capa-Release`), so the
|
|
67
67
|
* admin can tell a site that shows a release from one that shows every draft.
|
|
68
|
+
* `motion`: a highlight scrolls the page at once, not smoothly, for a viewer
|
|
69
|
+
* who asked for less motion (`prefers-reduced-motion: reduce`), so the admin
|
|
70
|
+
* can take the page to an entry without animating it for them.
|
|
68
71
|
*/
|
|
69
|
-
export const SITE_FEATURES = ["patch", "fields", "release"];
|
|
72
|
+
export const SITE_FEATURES = ["patch", "fields", "release", "motion"];
|
|
70
73
|
/** A `patch` past these is refused whole: no form has that many text fields, or that much text in one. */
|
|
71
74
|
export const PATCH_MAX_FIELDS = 500;
|
|
72
75
|
export const PATCH_MAX_LENGTH = 100_000;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,4 +7,4 @@ export type { CreateWebhookEndpointInput, ListWebhookDeliveriesOptions, ResumeWe
|
|
|
7
7
|
export { DEFAULT_WEBHOOK_TOLERANCE_SECONDS, parseWebhookSignatureHeader, signWebhookPayload, verifyWebhookSignature, WEBHOOK_SIGNATURE_HEADER, } from "./webhook-signature";
|
|
8
8
|
export type { ParsedWebhookSignature, VerifyWebhookSignatureInput, } from "./webhook-signature";
|
|
9
9
|
export { generate, normalizeTypes, readStampedChecksum, restTypesFromIntrospection, typeNames } from "./codegen";
|
|
10
|
-
export type { CodegenResult, RestTypesResult } from "./codegen";
|
|
10
|
+
export type { CodegenOptions, CodegenResult, CodegenShape, RestTypesResult } from "./codegen";
|
package/dist/next/client.d.ts
CHANGED
|
@@ -384,12 +384,55 @@ export interface PreviewClaim {
|
|
|
384
384
|
* draft mode on for it.
|
|
385
385
|
*/
|
|
386
386
|
draftLink?: true;
|
|
387
|
-
/**
|
|
387
|
+
/**
|
|
388
|
+
* A release preview's releases, lowest first: the later one wins where two
|
|
389
|
+
* name an entry. One that is done since the link was made carries `state`
|
|
390
|
+
* and `at`: it went live, was rolled back, ended at its scheduled end, or
|
|
391
|
+
* was archived without going live, and the reads no longer apply it.
|
|
392
|
+
*/
|
|
388
393
|
releases?: Array<{
|
|
389
394
|
id: string;
|
|
390
395
|
name: string;
|
|
396
|
+
state?: ReleasePreviewState;
|
|
397
|
+
at?: string | null;
|
|
391
398
|
}>;
|
|
399
|
+
/**
|
|
400
|
+
* A release preview naming a release that is done: the sentence to show
|
|
401
|
+
* over the page, for the topmost one ("This release went live on 6 Nov
|
|
402
|
+
* 2026. You're seeing the live site."), or null. Absent while every
|
|
403
|
+
* release named is open or scheduled.
|
|
404
|
+
*/
|
|
405
|
+
banner?: {
|
|
406
|
+
releaseId: string;
|
|
407
|
+
state: ReleasePreviewState;
|
|
408
|
+
at: string | null;
|
|
409
|
+
message: string;
|
|
410
|
+
} | null;
|
|
411
|
+
/** True when no release named still previews: the reads are the live site, published versions only. */
|
|
412
|
+
servesLive?: boolean;
|
|
413
|
+
}
|
|
414
|
+
/** What a site's bar shows for a release preview: `releasePreviewBar(claim)`. */
|
|
415
|
+
export interface ReleasePreviewBar {
|
|
416
|
+
/** The names of the releases the page shows, lowest first: the later one wins where two name an entry. */
|
|
417
|
+
previewing: string[];
|
|
418
|
+
/** The sentence for a release that is done since the link was made ("This release ended on ..."), or null. */
|
|
419
|
+
message: string | null;
|
|
392
420
|
}
|
|
421
|
+
/**
|
|
422
|
+
* The release preview's bar, from what `preview()` answered, or null for
|
|
423
|
+
* any other preview. A release preview opens the site in its own tab, so a
|
|
424
|
+
* site shows there which releases it is previewing, and over the live site
|
|
425
|
+
* why a release done since is not among them:
|
|
426
|
+
*
|
|
427
|
+
* const bar = releasePreviewBar(await client.preview(token));
|
|
428
|
+
* // { previewing: ["Spring menu", "Summer menu"], message: null }
|
|
429
|
+
*/
|
|
430
|
+
export declare function releasePreviewBar(claim: PreviewClaim | null | undefined): ReleasePreviewBar | null;
|
|
431
|
+
/**
|
|
432
|
+
* What became of a release a preview link names: live, rolled back by a
|
|
433
|
+
* person, ended at its scheduled end, or archived without going live.
|
|
434
|
+
*/
|
|
435
|
+
export type ReleasePreviewState = "went_live" | "rolled_back" | "ended" | "archived";
|
|
393
436
|
/**
|
|
394
437
|
* `client.graphql`: run a document, or build one from a typed selection with
|
|
395
438
|
* `client.graphql.query()`. `Q` is the `CapaQuery` type `capa-codegen
|
package/dist/next/client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LAYOUT_PAGE = exports.isCapaError = exports.CapaError = void 0;
|
|
4
|
+
exports.releasePreviewBar = releasePreviewBar;
|
|
4
5
|
exports.graphqlClient = graphqlClient;
|
|
5
6
|
exports.resolveNextConfig = resolveNextConfig;
|
|
6
7
|
exports.serializeSelect = serializeSelect;
|
|
@@ -42,6 +43,24 @@ function defaultFetch() {
|
|
|
42
43
|
* appears rather than as an error.
|
|
43
44
|
*/
|
|
44
45
|
const SCHEMA_CHECKSUM = /^[a-f0-9]{8,64}$/;
|
|
46
|
+
/**
|
|
47
|
+
* The release preview's bar, from what `preview()` answered, or null for
|
|
48
|
+
* any other preview. A release preview opens the site in its own tab, so a
|
|
49
|
+
* site shows there which releases it is previewing, and over the live site
|
|
50
|
+
* why a release done since is not among them:
|
|
51
|
+
*
|
|
52
|
+
* const bar = releasePreviewBar(await client.preview(token));
|
|
53
|
+
* // { previewing: ["Spring menu", "Summer menu"], message: null }
|
|
54
|
+
*/
|
|
55
|
+
function releasePreviewBar(claim) {
|
|
56
|
+
if (!claim || claim.kind !== "release")
|
|
57
|
+
return null;
|
|
58
|
+
const releases = Array.isArray(claim.releases) ? claim.releases : [];
|
|
59
|
+
return {
|
|
60
|
+
previewing: releases.filter((r) => !r.state).map((r) => r.name),
|
|
61
|
+
message: claim.banner?.message ?? null,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
45
64
|
const BUILDER_NOT_PERSISTED = "@capacms/sdk/next: graphql.query() cannot send persisted: true. It builds its document when it runs, " +
|
|
46
65
|
"so capa persist never stored it and every read would fall back to an uncached POST. " +
|
|
47
66
|
"Leave persisted out (a builder read is already a cached GET), or write the read as a #graphql literal and run capa persist.";
|
|
@@ -526,6 +545,8 @@ function createClient(config) {
|
|
|
526
545
|
options.draftLinks === true ? { "Capa-Draft-Links": "narrow" } : undefined)).body.data;
|
|
527
546
|
// A release preview names releases, not an entry: the entry keys are
|
|
528
547
|
// filled so a site that reads them reads a string, never undefined.
|
|
548
|
+
// A link to a release that is done since carries its banner, for the
|
|
549
|
+
// site to show over the live site its reads now serve.
|
|
529
550
|
if (claim?.kind === "release") {
|
|
530
551
|
return {
|
|
531
552
|
kind: "release",
|
|
@@ -535,6 +556,8 @@ function createClient(config) {
|
|
|
535
556
|
path: claim.path ?? null,
|
|
536
557
|
expiresAt: claim.expiresAt,
|
|
537
558
|
releases: Array.isArray(claim.releases) ? claim.releases : [],
|
|
559
|
+
...(claim.banner !== undefined ? { banner: claim.banner } : {}),
|
|
560
|
+
...(claim.servesLive !== undefined ? { servesLive: claim.servesLive } : {}),
|
|
538
561
|
};
|
|
539
562
|
}
|
|
540
563
|
return claim;
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* untyped.
|
|
29
29
|
*/
|
|
30
30
|
import type { Entry } from "./client";
|
|
31
|
-
import type { Select } from "./select-types";
|
|
31
|
+
import type { BrandTarget, IsMarkedReferenceList, Select, SymbolBrandKind } from "./select-types";
|
|
32
32
|
/** A relation a read did not expand: the entry's id, and its model's namespace, null when the key cannot read that model. */
|
|
33
33
|
export interface EntryReference {
|
|
34
34
|
id: string;
|
|
@@ -115,13 +115,12 @@ type RelationBrand = {
|
|
|
115
115
|
readonly __capaRelation: "one" | "many";
|
|
116
116
|
readonly __capaRelationTarget: unknown;
|
|
117
117
|
};
|
|
118
|
-
|
|
118
|
+
/** A relation codegen wrote: the legacy shape's required brand, as before, or `--shape api`'s `CapaRelationBrand`. */
|
|
119
|
+
type IsRelation<V> = Untyped<V> extends true ? false : NonNullable<V> extends RelationBrand ? true : [SymbolBrandKind<V>] extends [never] ? false : true;
|
|
119
120
|
type IsList<V> = NonNullable<V> extends {
|
|
120
121
|
readonly __capaRelation: "many";
|
|
121
|
-
} ? true : false;
|
|
122
|
-
type TargetOf<V> =
|
|
123
|
-
readonly __capaRelationTarget: infer R;
|
|
124
|
-
} ? R : never;
|
|
122
|
+
} ? true : "many" extends SymbolBrandKind<V> ? true : false;
|
|
123
|
+
type TargetOf<V> = BrandTarget<V>;
|
|
125
124
|
/** Media as codegen types it: `CapaImage`, `CapaVideo` and `CapaFile` each have a `url`. */
|
|
126
125
|
type StoredMedia = {
|
|
127
126
|
url: string;
|
|
@@ -130,7 +129,7 @@ type StoredMedia = {
|
|
|
130
129
|
* A field that is no relation: as stored, or `null` when it was never filled,
|
|
131
130
|
* but media, which the read returns in its public shape.
|
|
132
131
|
*/
|
|
133
|
-
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;
|
|
132
|
+
type ValueTree<V> = Untyped<V> extends true ? V : IsMarkedReferenceList<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;
|
|
134
133
|
/** A relation the read did not expand. */
|
|
135
134
|
type ReferenceTree<V> = IsList<V> extends true ? RelationItems<EntryReference> : EntryReference | null;
|
|
136
135
|
/** A field the read names without expanding it. */
|
package/dist/next/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { CapaError, createClient, isCapaError, LAYOUT_PAGE, resolveNextConfig, serializeSelect, } from "./client";
|
|
1
|
+
export { CapaError, createClient, isCapaError, LAYOUT_PAGE, releasePreviewBar, resolveNextConfig, serializeSelect, } from "./client";
|
|
2
2
|
export { CAPA_EDIT, capaAttrs, fieldAttrs, isEditEntry, itemPath, ITEM_MAX_DEPTH, markEditEntries, markGraphQLEntries } from "./attrs";
|
|
3
3
|
export { inflate } from "./inflate";
|
|
4
4
|
export { isCapaGraphQLError } from "./errors";
|
|
@@ -26,5 +26,5 @@ export type { ArgsInput, CapaField, Exact, ExactSelection, NodeOf, QueryResult,
|
|
|
26
26
|
export type { FlatResponse, Inflated } from "./inflate";
|
|
27
27
|
export type { EntryFields, EntryMedia, EntryReference, FlatFields, IncludedFields, MediaCrop, MediaFocalPoint, MissingEntry, RelationItems, } from "./entry-fields";
|
|
28
28
|
export type { CapaAttrs, CapaAttrsOptions, CapaItem, FieldAttrs, TaggableField } from "./attrs";
|
|
29
|
-
export type { BuilderCallOptions, CallOptions, CapaNextClient, CapaNextConfig, EntriesResource, Entry, GraphQLClient, Filter, FilterOperator, FlatGetOptions, FlatListOptions, FlatPage, FlatSingle, Included, FilterScalar, FilterValue, GetOptions, ListOptions, Page, PageDetail, PageInfo, PageSummary, PagesListOptions, PagesResource, PreviewClaim, PreviewOptions, ResponseMeta, ResponseShape, Single, } from "./client";
|
|
30
|
-
export type { ExpandedTargets, CapaRelation, CapaRelationList, RelationSelectOptions, Select, SelectItem, SelectSort, SortableSystemKey, SystemKey, } from "./select-types";
|
|
29
|
+
export type { BuilderCallOptions, CallOptions, CapaNextClient, CapaNextConfig, EntriesResource, Entry, GraphQLClient, Filter, FilterOperator, FlatGetOptions, FlatListOptions, FlatPage, FlatSingle, Included, FilterScalar, FilterValue, GetOptions, ListOptions, Page, PageDetail, PageInfo, PageSummary, PagesListOptions, PagesResource, PreviewClaim, PreviewOptions, ReleasePreviewBar, ReleasePreviewState, ResponseMeta, ResponseShape, Single, } from "./client";
|
|
30
|
+
export type { ExpandedTargets, CapaReferenceListBrand, CapaRelation, CapaRelationBrand, CapaRelationList, RelationSelectOptions, Select, SelectItem, SelectSort, SortableSystemKey, SystemKey, } from "./select-types";
|
package/dist/next/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.selectionToDocument = exports.toTree = exports.selectToSelection = exports.graphqlToSelect = exports.selectToGraphQL = exports.buildGraphQLQuery = exports.CapaBuildError = exports.summarizeIntrospection = exports.gql = exports.PERSISTED_QUERY_NOT_FOUND = exports.INTROSPECTION_QUERY = exports.isCapaGraphQLError = exports.inflate = exports.markGraphQLEntries = exports.markEditEntries = exports.ITEM_MAX_DEPTH = exports.itemPath = exports.isEditEntry = exports.fieldAttrs = exports.capaAttrs = exports.CAPA_EDIT = exports.serializeSelect = exports.resolveNextConfig = exports.LAYOUT_PAGE = exports.isCapaError = exports.createClient = exports.CapaError = void 0;
|
|
3
|
+
exports.selectionToDocument = exports.toTree = exports.selectToSelection = exports.graphqlToSelect = exports.selectToGraphQL = exports.buildGraphQLQuery = exports.CapaBuildError = exports.summarizeIntrospection = exports.gql = exports.PERSISTED_QUERY_NOT_FOUND = exports.INTROSPECTION_QUERY = exports.isCapaGraphQLError = exports.inflate = exports.markGraphQLEntries = exports.markEditEntries = exports.ITEM_MAX_DEPTH = exports.itemPath = exports.isEditEntry = exports.fieldAttrs = exports.capaAttrs = exports.CAPA_EDIT = exports.serializeSelect = exports.resolveNextConfig = exports.releasePreviewBar = exports.LAYOUT_PAGE = exports.isCapaError = exports.createClient = exports.CapaError = void 0;
|
|
4
4
|
var client_1 = require("./client");
|
|
5
5
|
Object.defineProperty(exports, "CapaError", { enumerable: true, get: function () { return client_1.CapaError; } });
|
|
6
6
|
Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return client_1.createClient; } });
|
|
7
7
|
Object.defineProperty(exports, "isCapaError", { enumerable: true, get: function () { return client_1.isCapaError; } });
|
|
8
8
|
Object.defineProperty(exports, "LAYOUT_PAGE", { enumerable: true, get: function () { return client_1.LAYOUT_PAGE; } });
|
|
9
|
+
Object.defineProperty(exports, "releasePreviewBar", { enumerable: true, get: function () { return client_1.releasePreviewBar; } });
|
|
9
10
|
Object.defineProperty(exports, "resolveNextConfig", { enumerable: true, get: function () { return client_1.resolveNextConfig; } });
|
|
10
11
|
Object.defineProperty(exports, "serializeSelect", { enumerable: true, get: function () { return client_1.serializeSelect; } });
|
|
11
12
|
var attrs_1 = require("./attrs");
|
|
@@ -14,6 +14,47 @@ type RelationValue = {
|
|
|
14
14
|
readonly __capaRelation: "one" | "many";
|
|
15
15
|
readonly __capaRelationTarget: unknown;
|
|
16
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* The brands `capa-codegen --shape api` writes, keyed by symbols only this
|
|
19
|
+
* module declares. Each property is optional and type-only, so the JSON the
|
|
20
|
+
* API sends is the type, and no other type can carry the key, by accident or
|
|
21
|
+
* by a field of the same name: a type without them reads exactly as it did
|
|
22
|
+
* before the api shape existed. The legacy shape's brand is the required
|
|
23
|
+
* `__capaRelation` and `__capaRelationTarget` pair, read as it always was.
|
|
24
|
+
*/
|
|
25
|
+
declare const capaRelationBrand: unique symbol;
|
|
26
|
+
declare const capaReferenceListBrand: unique symbol;
|
|
27
|
+
/** A relation of `--shape api`: `"one"` or `"many"`, and the model a select can expand it into. */
|
|
28
|
+
export interface CapaRelationBrand<K extends "one" | "many", T> {
|
|
29
|
+
readonly [capaRelationBrand]?: {
|
|
30
|
+
readonly kind: K;
|
|
31
|
+
readonly target: T;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** A relation list of `--shape api` into a model its module lacks: never null. */
|
|
35
|
+
export interface CapaReferenceListBrand {
|
|
36
|
+
readonly [capaReferenceListBrand]?: true;
|
|
37
|
+
}
|
|
38
|
+
/** `V` without null, unless that leaves nothing or an index signature that would hold any key. */
|
|
39
|
+
type Keyed<V> = [NonNullable<V>] extends [never] ? never : symbol extends keyof NonNullable<V> ? never : NonNullable<V>;
|
|
40
|
+
/** Whether `V` declares key `K` itself. Never for `never`, whose `keyof` is every key. */
|
|
41
|
+
type HasKey<V, K> = [Keyed<V>] extends [never] ? false : K extends keyof Keyed<V> ? true : false;
|
|
42
|
+
/** `"one"` or `"many"` for a type that carries `CapaRelationBrand`; never for any other. */
|
|
43
|
+
export type SymbolBrandKind<V> = HasKey<V, typeof capaRelationBrand> extends true ? Keyed<V> extends {
|
|
44
|
+
readonly [capaRelationBrand]?: {
|
|
45
|
+
readonly kind: infer K;
|
|
46
|
+
};
|
|
47
|
+
} ? [K] extends ["one" | "many"] ? K : never : never : never;
|
|
48
|
+
/** True for a type that carries `CapaReferenceListBrand`, and nothing else. */
|
|
49
|
+
export type IsMarkedReferenceList<V> = HasKey<V, typeof capaReferenceListBrand> extends true ? [V] extends [NonNullable<V>] ? true : false : false;
|
|
50
|
+
/** The model a relation's brand names: the legacy shape's required one, as before, or `CapaRelationBrand`'s. */
|
|
51
|
+
export type BrandTarget<V> = NonNullable<V> extends {
|
|
52
|
+
readonly __capaRelationTarget: infer R;
|
|
53
|
+
} ? R : [SymbolBrandKind<V>] extends [never] ? never : Keyed<V> extends {
|
|
54
|
+
readonly [capaRelationBrand]?: {
|
|
55
|
+
readonly target: infer R;
|
|
56
|
+
};
|
|
57
|
+
} ? R : never;
|
|
17
58
|
/**
|
|
18
59
|
* A field type that says nothing about the field: `unknown`, as every field
|
|
19
60
|
* of an untyped client's `Record<string, unknown>` is, or `any`, as a type
|
|
@@ -24,12 +65,10 @@ type UntypedKeys<T> = {
|
|
|
24
65
|
[K in StringKey<T>]-?: Untyped<T[K]> extends true ? K : never;
|
|
25
66
|
}[StringKey<T>];
|
|
26
67
|
type RelationKeys<T> = {
|
|
27
|
-
[K in StringKey<T>]-?: Untyped<T[K]> extends true ? never : NonNullable<T[K]> extends RelationValue ? K : never;
|
|
68
|
+
[K in StringKey<T>]-?: Untyped<T[K]> extends true ? never : NonNullable<T[K]> extends RelationValue ? K : [SymbolBrandKind<T[K]>] extends [never] ? never : K;
|
|
28
69
|
}[StringKey<T>];
|
|
29
70
|
type ScalarKeys<T> = Exclude<StringKey<T>, RelationKeys<T>>;
|
|
30
|
-
type RelationTarget<T> =
|
|
31
|
-
readonly __capaRelationTarget: infer R;
|
|
32
|
-
} ? R : never;
|
|
71
|
+
type RelationTarget<T> = BrandTarget<T>;
|
|
33
72
|
/**
|
|
34
73
|
* A system key by its `$` name. The API reads a plain name as the model's
|
|
35
74
|
* field first, so `$tags` is how a select or sort asks for the entry's own
|
package/dist/nextjs/index.d.ts
CHANGED
|
@@ -528,6 +528,11 @@ export declare function clearDraftLinkCookie(cookies: CookiesFn): Promise<void>;
|
|
|
528
528
|
* Given `cookies`, the draft cookie is re-set by `frameDraftCookie`: one hour,
|
|
529
529
|
* and sent inside the editor's frame.
|
|
530
530
|
*
|
|
531
|
+
* A RELEASE PREVIEW keeps its token in `RELEASE_COOKIE` for the visit's reads.
|
|
532
|
+
* It and the draft cookie end when the link expires, or after `maxAge`
|
|
533
|
+
* seconds, whichever is first; a link already expired lands on
|
|
534
|
+
* `?preview=expired`.
|
|
535
|
+
*
|
|
531
536
|
* A DRAFT LINK (Share draft in Capa) opens only given `cookies`, and never
|
|
532
537
|
* turns draft mode on. The route tells Capa it narrows (`preview()` with
|
|
533
538
|
* `draftLinks`), keeps the token in `DRAFT_LINK_COOKIE` until the link
|