@capacms/sdk 1.0.0-next.15 → 1.0.0-next.16
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 +22 -0
- package/README.md +47 -0
- package/dist/next/client.d.ts +7 -0
- package/dist/next/client.js +6 -1
- package/dist/nextjs/index.d.ts +55 -3
- package/dist/nextjs/index.js +55 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 1.0.0-next.16
|
|
6
|
+
|
|
7
|
+
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`.
|
|
8
|
+
|
|
9
|
+
- A shared draft link that does not open (revoked, expired, changed, made
|
|
10
|
+
up, or a preview route not given `cookies`) now lands on
|
|
11
|
+
`?preview=link-ended`, exported as `DRAFT_LINK_ENDED`, instead of the
|
|
12
|
+
editor's `?preview=expired`. The person holding a draft link has no Capa
|
|
13
|
+
account, so a site can word the answer for them. An editor's preview
|
|
14
|
+
token that does not open still lands on `?preview=expired`. A site that
|
|
15
|
+
shows a banner only for `expired` shows none for a dead draft link until
|
|
16
|
+
it handles `link-ended`.
|
|
17
|
+
- `draftLinkVisit({ headers })`, new in `@capacms/sdk/nextjs`, tells a page
|
|
18
|
+
whether its request is a visit of a draft link and whether the link is
|
|
19
|
+
still live: `null`, or `{ status: "active", path, expiresAt }`,
|
|
20
|
+
`{ status: "ended" }` or `{ status: "unknown" }`. It checks the token with
|
|
21
|
+
Capa once, as the preview route does, with the published key, and never
|
|
22
|
+
throws. Use it for a "Draft preview" bar on a draft-link visit.
|
|
23
|
+
- `preview(token, { checkOnly: true })` sends `Capa-Draft-Link-Check: 1`,
|
|
24
|
+
which changes nothing in the answer. `draftLinkVisit` sets it, so Capa's
|
|
25
|
+
logs tell a page asking about a link from the link being opened.
|
|
26
|
+
|
|
5
27
|
## 1.0.0-next.15
|
|
6
28
|
|
|
7
29
|
Published 2026-11-17. New since 1.0.0-next.14: `capa-codegen --shape api` (or `{ shape: "api" }` in code), an opt-in mode that writes types as `/api/` returns them: fields that can be null say so, required fields are no longer optional, enums are their options with a pk_ key, and media carry their real fields. Without the flag the output is byte for byte what it was.
|
package/README.md
CHANGED
|
@@ -2027,6 +2027,53 @@ every CSP it is sent, so if your site sends its own `frame-ancestors` or
|
|
|
2027
2027
|
`capaMiddleware` marks its edit-mode responses noindex too, and
|
|
2028
2028
|
`resolveEditRequest` returns the value as `robotsTag`.
|
|
2029
2029
|
|
|
2030
|
+
### Shared draft links
|
|
2031
|
+
|
|
2032
|
+
An editor can share a draft with someone who has no Capa account (Share draft
|
|
2033
|
+
in the preview pane). The link is `<preview base URL><path>?capa-preview=cap_draft_...`,
|
|
2034
|
+
and the same middleware and `createPreviewRoute({ draftMode, cookies })` open
|
|
2035
|
+
it. The route never turns draft mode on for it: it keeps the token in the
|
|
2036
|
+
`capa-draft-link` cookie for at most an hour, and lands on the page with the
|
|
2037
|
+
token gone from the address. That visit's reads through `getCapaClient` and
|
|
2038
|
+
`graphql()`, given `headers`, show the linked entry's draft and every other
|
|
2039
|
+
entry as published.
|
|
2040
|
+
|
|
2041
|
+
The person holding the link is not an editor, so word what they see for them:
|
|
2042
|
+
|
|
2043
|
+
- A draft link that does not open (revoked, expired, changed, or a route not
|
|
2044
|
+
given `cookies`) lands on `?preview=link-ended` (`DRAFT_LINK_ENDED`), never
|
|
2045
|
+
on the editor's `?preview=expired`. Capa answers every dead link the same
|
|
2046
|
+
way, so say it once: "This preview link is no longer active. Ask the person
|
|
2047
|
+
who shared it for a new one."
|
|
2048
|
+
- `draftLinkVisit({ headers })` says whether this request is a visit, and
|
|
2049
|
+
whether its link is still live: `null` with no visit, or `active` (with the
|
|
2050
|
+
page it opens and when it ends), `ended` or `unknown`. It asks Capa once.
|
|
2051
|
+
|
|
2052
|
+
```tsx
|
|
2053
|
+
// app/layout.tsx
|
|
2054
|
+
import { draftMode, headers } from "next/headers";
|
|
2055
|
+
import { draftLinkVisit } from "@capacms/sdk/nextjs";
|
|
2056
|
+
|
|
2057
|
+
export default async function RootLayout({ children }: { children: React.ReactNode }) {
|
|
2058
|
+
const draft = (await draftMode()).isEnabled;
|
|
2059
|
+
const visit = draft ? null : await draftLinkVisit({ headers });
|
|
2060
|
+
// visit?.status === "active": "Draft preview. You are seeing unpublished changes."
|
|
2061
|
+
// visit?.status === "ended": "This preview link is no longer active."
|
|
2062
|
+
// ...render the bar and {children}
|
|
2063
|
+
}
|
|
2064
|
+
```
|
|
2065
|
+
|
|
2066
|
+
Next keeps a layout as the visitor moves between pages, so a bar the root
|
|
2067
|
+
layout draws keeps the state it read on the first page. To show a revoke on
|
|
2068
|
+
the next page, as the content does, ask again after each page change: a
|
|
2069
|
+
route handler that returns `draftLinkVisit({ headers })`, fetched by the bar
|
|
2070
|
+
when the path changes, or a server component in each page.
|
|
2071
|
+
|
|
2072
|
+
Show the visitor a bar while a visit is active, as the editor's preview
|
|
2073
|
+
does, or they may take an unpublished page for the live one. Tell them to
|
|
2074
|
+
forward the link they were sent: the address bar holds the page's own
|
|
2075
|
+
address once the link opens, and that address is the published page.
|
|
2076
|
+
|
|
2030
2077
|
### Add preview to a live site without changing it
|
|
2031
2078
|
|
|
2032
2079
|
A live Next.js site that reads Capa with its own code keeps that code. Preview
|
package/dist/next/client.d.ts
CHANGED
|
@@ -118,6 +118,13 @@ export interface PreviewOptions extends CallOptions {
|
|
|
118
118
|
* route that would turn the whole site's draft mode on never opens one.
|
|
119
119
|
*/
|
|
120
120
|
draftLinks?: boolean;
|
|
121
|
+
/**
|
|
122
|
+
* True when the call only asks whether a draft-link visit's link is still
|
|
123
|
+
* live, as `draftLinkVisit` does, rather than opening it. Sent as
|
|
124
|
+
* `Capa-Draft-Link-Check: 1`. It changes nothing in the answer: Capa's logs
|
|
125
|
+
* then tell a page that asked about a link from a link being opened.
|
|
126
|
+
*/
|
|
127
|
+
checkOnly?: boolean;
|
|
121
128
|
}
|
|
122
129
|
/**
|
|
123
130
|
* One entry as `/api/entries` returns it: the system keys, and `fields`. A read
|
package/dist/next/client.js
CHANGED
|
@@ -542,7 +542,12 @@ function createClient(config) {
|
|
|
542
542
|
try {
|
|
543
543
|
const claim = (await request("/api/preview", query, options.signal, undefined, undefined,
|
|
544
544
|
// Only a caller that narrows a draft link opens one (PreviewOptions).
|
|
545
|
-
options.draftLinks === true
|
|
545
|
+
options.draftLinks === true || options.checkOnly === true
|
|
546
|
+
? {
|
|
547
|
+
...(options.draftLinks === true ? { "Capa-Draft-Links": "narrow" } : {}),
|
|
548
|
+
...(options.checkOnly === true ? { "Capa-Draft-Link-Check": "1" } : {}),
|
|
549
|
+
}
|
|
550
|
+
: undefined)).body.data;
|
|
546
551
|
// A release preview names releases, not an entry: the entry keys are
|
|
547
552
|
// filled so a site that reads them reads a string, never undefined.
|
|
548
553
|
// A link to a release that is done since carries its banner, for the
|
package/dist/nextjs/index.d.ts
CHANGED
|
@@ -194,6 +194,55 @@ export declare function editMode(input: {
|
|
|
194
194
|
export declare function draftLinkToken(input: {
|
|
195
195
|
headers: () => HeaderReader | Promise<HeaderReader>;
|
|
196
196
|
}): Promise<string | null>;
|
|
197
|
+
/**
|
|
198
|
+
* The `?preview=` value `createPreviewRoute` lands a DRAFT LINK on when it does
|
|
199
|
+
* not open: revoked, expired, changed, made up, another site's, or a route not
|
|
200
|
+
* given `cookies`. Capa gives one answer for all of them, so a site cannot tell
|
|
201
|
+
* them apart either. An editor's preview token that does not open still lands
|
|
202
|
+
* on `?preview=expired`. The person holding a draft link has no Capa account,
|
|
203
|
+
* so word it for them, for example "This preview link is no longer active. Ask
|
|
204
|
+
* the person who shared it for a new one."
|
|
205
|
+
*/
|
|
206
|
+
export declare const DRAFT_LINK_ENDED = "link-ended";
|
|
207
|
+
/**
|
|
208
|
+
* What a draft-link visit is now, from `draftLinkVisit`:
|
|
209
|
+
*
|
|
210
|
+
* - `active`: the link still opens. `path` is the page it opens, or null when
|
|
211
|
+
* Capa can no longer resolve one; `expiresAt` is when the link itself ends.
|
|
212
|
+
* - `ended`: the link no longer opens (revoked, expired, its entry deleted).
|
|
213
|
+
* The visit's reads already show what is published.
|
|
214
|
+
* - `unknown`: Capa could not be asked. Show nothing rather than guess.
|
|
215
|
+
*/
|
|
216
|
+
export type DraftLinkVisit = {
|
|
217
|
+
status: "active";
|
|
218
|
+
path: string | null;
|
|
219
|
+
expiresAt: string;
|
|
220
|
+
} | {
|
|
221
|
+
status: "ended";
|
|
222
|
+
} | {
|
|
223
|
+
status: "unknown";
|
|
224
|
+
};
|
|
225
|
+
/**
|
|
226
|
+
* Whether this request is a visit of a draft link, and whether that link is
|
|
227
|
+
* still live: null with no draft-link cookie, otherwise its state, asked of
|
|
228
|
+
* Capa (`preview()` with `draftLinks` and `checkOnly`, one request). For a
|
|
229
|
+
* banner that tells the visitor they are looking at an unpublished draft, in
|
|
230
|
+
* a server component or a route handler:
|
|
231
|
+
*
|
|
232
|
+
* import { draftMode, headers } from "next/headers";
|
|
233
|
+
* const visit = (await draftMode()).isEnabled ? null : await draftLinkVisit({ headers });
|
|
234
|
+
* // visit?.status === "active": "Draft preview. You are seeing unpublished changes."
|
|
235
|
+
* // visit?.status === "ended": "This preview link is no longer active."
|
|
236
|
+
*
|
|
237
|
+
* Under draft mode the reads are the editor's, so ask only outside it. The
|
|
238
|
+
* token is checked with the published key (`CAPA_KEY`), as
|
|
239
|
+
* `createPreviewRoute` checks it, unless `client` gives another. It never
|
|
240
|
+
* throws: Capa unreachable, no key, or `headers` throwing is `unknown`.
|
|
241
|
+
*/
|
|
242
|
+
export declare function draftLinkVisit(input: {
|
|
243
|
+
headers: () => HeaderReader | Promise<HeaderReader>;
|
|
244
|
+
client?: () => Pick<CapaNextClient, "preview">;
|
|
245
|
+
}): Promise<DraftLinkVisit | null>;
|
|
197
246
|
export interface EditRequest {
|
|
198
247
|
/** True when the page must render in edit mode. */
|
|
199
248
|
edit: boolean;
|
|
@@ -521,7 +570,8 @@ export declare function clearDraftLinkCookie(cookies: CookiesFn): Promise<void>;
|
|
|
521
570
|
*
|
|
522
571
|
* Checks the token with Capa (the site never holds the signing key), turns
|
|
523
572
|
* draft mode on and lands on the entry's page. A bad or expired token lands on
|
|
524
|
-
* the page without draft mode and `?preview=expired
|
|
573
|
+
* the page without draft mode and `?preview=expired`, or `?preview=link-ended`
|
|
574
|
+
* for a draft link (`DRAFT_LINK_ENDED`); Capa unreachable gives
|
|
525
575
|
* `?preview=unavailable`. The token is checked with any key the site holds,
|
|
526
576
|
* its legacy key included (`getPublishedClient`, from `CAPA_KEY`).
|
|
527
577
|
*
|
|
@@ -540,8 +590,10 @@ export declare function clearDraftLinkCookie(cookies: CookiesFn): Promise<void>;
|
|
|
540
590
|
* mode the browser had, and lands on the linked page. `onEnable` does not run
|
|
541
591
|
* for it. That visit's reads through `getCapaClient` and `graphql()` show the
|
|
542
592
|
* linked entry's draft and every other entry as published, and stop showing
|
|
543
|
-
* the draft once the link is revoked or expires.
|
|
544
|
-
*
|
|
593
|
+
* the draft once the link is revoked or expires. A draft link that does not
|
|
594
|
+
* open lands on `?preview=link-ended` (`DRAFT_LINK_ENDED`), never on the
|
|
595
|
+
* editor's `expired`. Without `cookies`, Capa answers every draft link that
|
|
596
|
+
* way. `draftLinkVisit` tells a page whether the visit's link is still live.
|
|
545
597
|
*
|
|
546
598
|
* With no `redirect`, the route answers
|
|
547
599
|
* with its own 307, marked `X-Robots-Tag: noindex, nofollow`,
|
package/dist/nextjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RELEASE_COOKIE = exports.DRAFT_COOKIE_MAX_AGE = exports.DRAFT_ROBOTS_TAG = exports.CAPA_ADMIN_ORIGIN = exports.DEFAULT_API_VERSION = exports.CAPA_ENV_ALIASES = exports.CAPA_ENV = exports.EDIT_CACHE_CONTROL = exports.DRAFT_LINK_COOKIE = exports.DRAFT_COOKIE = exports.EDIT_HEADER = exports.VIEW_PARAM = exports.PREVIEW_PARAM = exports.EDIT_PARAM = exports.withCapaWriteKeyGuard = exports.scanNextBuildForWriteKeys = exports.scanForWriteKeys = exports.CapaWriteKeyInBundleError = exports.assertNoWriteKeysInBundle = exports.assertNoWriteKeysInBuild = exports.createCapaImageLoader = exports.capaImageLoader = exports.LAYOUT_PAGE = exports.gql = exports.MEDIA_TAG = exports.GRAPHQL_TAG = void 0;
|
|
3
|
+
exports.RELEASE_COOKIE = exports.DRAFT_COOKIE_MAX_AGE = exports.DRAFT_ROBOTS_TAG = exports.CAPA_ADMIN_ORIGIN = exports.DEFAULT_API_VERSION = exports.CAPA_ENV_ALIASES = exports.CAPA_ENV = exports.DRAFT_LINK_ENDED = exports.EDIT_CACHE_CONTROL = exports.DRAFT_LINK_COOKIE = exports.DRAFT_COOKIE = exports.EDIT_HEADER = exports.VIEW_PARAM = exports.PREVIEW_PARAM = exports.EDIT_PARAM = exports.withCapaWriteKeyGuard = exports.scanNextBuildForWriteKeys = exports.scanForWriteKeys = exports.CapaWriteKeyInBundleError = exports.assertNoWriteKeysInBundle = exports.assertNoWriteKeysInBuild = exports.createCapaImageLoader = exports.capaImageLoader = exports.LAYOUT_PAGE = exports.gql = exports.MEDIA_TAG = exports.GRAPHQL_TAG = void 0;
|
|
4
4
|
exports.withCache = withCache;
|
|
5
5
|
exports.modelTag = modelTag;
|
|
6
6
|
exports.tagsFor = tagsFor;
|
|
@@ -11,6 +11,7 @@ exports.preview = preview;
|
|
|
11
11
|
exports.pagesFor = pagesFor;
|
|
12
12
|
exports.editMode = editMode;
|
|
13
13
|
exports.draftLinkToken = draftLinkToken;
|
|
14
|
+
exports.draftLinkVisit = draftLinkVisit;
|
|
14
15
|
exports.resolveEditRequest = resolveEditRequest;
|
|
15
16
|
exports.getPublishedClient = getPublishedClient;
|
|
16
17
|
exports.getCapaClient = getCapaClient;
|
|
@@ -406,6 +407,48 @@ function draftLinkIn(headers) {
|
|
|
406
407
|
async function draftLinkToken(input) {
|
|
407
408
|
return draftLinkIn(await input.headers());
|
|
408
409
|
}
|
|
410
|
+
/**
|
|
411
|
+
* The `?preview=` value `createPreviewRoute` lands a DRAFT LINK on when it does
|
|
412
|
+
* not open: revoked, expired, changed, made up, another site's, or a route not
|
|
413
|
+
* given `cookies`. Capa gives one answer for all of them, so a site cannot tell
|
|
414
|
+
* them apart either. An editor's preview token that does not open still lands
|
|
415
|
+
* on `?preview=expired`. The person holding a draft link has no Capa account,
|
|
416
|
+
* so word it for them, for example "This preview link is no longer active. Ask
|
|
417
|
+
* the person who shared it for a new one."
|
|
418
|
+
*/
|
|
419
|
+
exports.DRAFT_LINK_ENDED = "link-ended";
|
|
420
|
+
/**
|
|
421
|
+
* Whether this request is a visit of a draft link, and whether that link is
|
|
422
|
+
* still live: null with no draft-link cookie, otherwise its state, asked of
|
|
423
|
+
* Capa (`preview()` with `draftLinks` and `checkOnly`, one request). For a
|
|
424
|
+
* banner that tells the visitor they are looking at an unpublished draft, in
|
|
425
|
+
* a server component or a route handler:
|
|
426
|
+
*
|
|
427
|
+
* import { draftMode, headers } from "next/headers";
|
|
428
|
+
* const visit = (await draftMode()).isEnabled ? null : await draftLinkVisit({ headers });
|
|
429
|
+
* // visit?.status === "active": "Draft preview. You are seeing unpublished changes."
|
|
430
|
+
* // visit?.status === "ended": "This preview link is no longer active."
|
|
431
|
+
*
|
|
432
|
+
* Under draft mode the reads are the editor's, so ask only outside it. The
|
|
433
|
+
* token is checked with the published key (`CAPA_KEY`), as
|
|
434
|
+
* `createPreviewRoute` checks it, unless `client` gives another. It never
|
|
435
|
+
* throws: Capa unreachable, no key, or `headers` throwing is `unknown`.
|
|
436
|
+
*/
|
|
437
|
+
async function draftLinkVisit(input) {
|
|
438
|
+
let claim;
|
|
439
|
+
try {
|
|
440
|
+
const token = draftLinkIn(await input.headers());
|
|
441
|
+
if (token === null)
|
|
442
|
+
return null;
|
|
443
|
+
claim = await (input.client ?? getPublishedClient)().preview(token, { draftLinks: true, checkOnly: true });
|
|
444
|
+
}
|
|
445
|
+
catch {
|
|
446
|
+
return { status: "unknown" };
|
|
447
|
+
}
|
|
448
|
+
if (claim === null || claim.draftLink !== true)
|
|
449
|
+
return { status: "ended" };
|
|
450
|
+
return { status: "active", path: claim.path ?? null, expiresAt: claim.expiresAt };
|
|
451
|
+
}
|
|
409
452
|
/** The settings with the draft link added, when there is one. */
|
|
410
453
|
function withDraftLink(settings, link) {
|
|
411
454
|
return link === null ? settings : { ...settings, draftLink: link };
|
|
@@ -943,7 +986,8 @@ function routeRedirect(location, headers) {
|
|
|
943
986
|
*
|
|
944
987
|
* Checks the token with Capa (the site never holds the signing key), turns
|
|
945
988
|
* draft mode on and lands on the entry's page. A bad or expired token lands on
|
|
946
|
-
* the page without draft mode and `?preview=expired
|
|
989
|
+
* the page without draft mode and `?preview=expired`, or `?preview=link-ended`
|
|
990
|
+
* for a draft link (`DRAFT_LINK_ENDED`); Capa unreachable gives
|
|
947
991
|
* `?preview=unavailable`. The token is checked with any key the site holds,
|
|
948
992
|
* its legacy key included (`getPublishedClient`, from `CAPA_KEY`).
|
|
949
993
|
*
|
|
@@ -962,8 +1006,10 @@ function routeRedirect(location, headers) {
|
|
|
962
1006
|
* mode the browser had, and lands on the linked page. `onEnable` does not run
|
|
963
1007
|
* for it. That visit's reads through `getCapaClient` and `graphql()` show the
|
|
964
1008
|
* linked entry's draft and every other entry as published, and stop showing
|
|
965
|
-
* the draft once the link is revoked or expires.
|
|
966
|
-
*
|
|
1009
|
+
* the draft once the link is revoked or expires. A draft link that does not
|
|
1010
|
+
* open lands on `?preview=link-ended` (`DRAFT_LINK_ENDED`), never on the
|
|
1011
|
+
* editor's `expired`. Without `cookies`, Capa answers every draft link that
|
|
1012
|
+
* way. `draftLinkVisit` tells a page whether the visit's link is still live.
|
|
967
1013
|
*
|
|
968
1014
|
* With no `redirect`, the route answers
|
|
969
1015
|
* with its own 307, marked `X-Robots-Tag: noindex, nofollow`,
|
|
@@ -1011,7 +1057,11 @@ function createPreviewRoute(input) {
|
|
|
1011
1057
|
await clearDraftLinkCookie(input.cookies);
|
|
1012
1058
|
await clearReleaseCookie(input.cookies);
|
|
1013
1059
|
}
|
|
1014
|
-
|
|
1060
|
+
// A draft link's holder has no Capa account: the site words its answer
|
|
1061
|
+
// for them (DRAFT_LINK_ENDED). Known by the token's shape, since Capa
|
|
1062
|
+
// answers a dead link exactly as it answers a forged one.
|
|
1063
|
+
const ended = isLink || token.startsWith(DRAFT_LINK_PREFIX) ? exports.DRAFT_LINK_ENDED : "expired";
|
|
1064
|
+
return go(`${path}?preview=${failed ? "unavailable" : ended}`);
|
|
1015
1065
|
}
|
|
1016
1066
|
if (isLink) {
|
|
1017
1067
|
// The link's own page, and no draft mode: the visit's reads send the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capacms/sdk",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.16",
|
|
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",
|