@capacms/sdk 1.0.0-next.1 → 1.0.0-next.11
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 +487 -0
- package/README.md +1810 -183
- package/bin/capa-codegen.js +192 -5
- package/bin/capa.js +235 -0
- package/bin/graphql-project.js +142 -0
- package/bin/project-env.js +58 -0
- package/dist/client.d.ts +89 -4
- package/dist/client.js +40 -5
- package/dist/codegen.d.ts +55 -0
- package/dist/codegen.js +320 -39
- package/dist/config.d.ts +5 -36
- package/dist/config.js +47 -1
- package/dist/esm/image/index.d.ts +187 -0
- package/dist/esm/image/index.js +353 -0
- package/dist/esm/image/shared-params.generated.d.ts +314 -0
- package/dist/esm/image/shared-params.generated.js +680 -0
- package/dist/esm/nextjs/image-loader.d.ts +60 -0
- package/dist/esm/nextjs/image-loader.js +67 -0
- package/dist/esm/nextjs/overlay.d.ts +30 -0
- package/dist/esm/nextjs/overlay.js +75 -0
- package/dist/esm/overlay/index.d.ts +32 -0
- package/dist/esm/overlay/index.js +576 -0
- package/dist/esm/overlay/protocol.d.ts +187 -0
- package/dist/esm/overlay/protocol.js +240 -0
- package/dist/esm/package.json +4 -0
- package/dist/graphql-codegen.d.ts +117 -0
- package/dist/graphql-codegen.js +705 -0
- package/dist/http.js +1 -1
- package/dist/image/index.d.ts +187 -0
- package/dist/image/index.js +362 -0
- package/dist/image/shared-params.generated.d.ts +314 -0
- package/dist/image/shared-params.generated.js +700 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -1
- package/dist/next/attrs.d.ts +84 -10
- package/dist/next/attrs.js +119 -2
- package/dist/next/client.d.ts +176 -32
- package/dist/next/client.js +212 -90
- package/dist/next/entry-fields.d.ts +203 -0
- package/dist/next/entry-fields.js +2 -0
- package/dist/next/errors.d.ts +136 -0
- package/dist/next/errors.js +214 -0
- package/dist/next/field-names.d.ts +37 -0
- package/dist/next/field-names.js +145 -0
- package/dist/next/graphql/build.d.ts +27 -0
- package/dist/next/graphql/build.js +98 -0
- package/dist/next/graphql/documents.d.ts +67 -0
- package/dist/next/graphql/documents.js +35 -0
- package/dist/next/graphql/edit-mode.d.ts +16 -0
- package/dist/next/graphql/edit-mode.js +93 -0
- package/dist/next/graphql/filter-values.d.ts +34 -0
- package/dist/next/graphql/filter-values.js +96 -0
- package/dist/next/graphql/introspection.d.ts +89 -0
- package/dist/next/graphql/introspection.js +102 -0
- package/dist/next/graphql/plan.d.ts +115 -0
- package/dist/next/graphql/plan.js +531 -0
- package/dist/next/graphql/request.d.ts +228 -0
- package/dist/next/graphql/request.js +283 -0
- package/dist/next/graphql/rest.d.ts +66 -0
- package/dist/next/graphql/rest.js +502 -0
- package/dist/next/graphql/selection.d.ts +55 -0
- package/dist/next/graphql/selection.js +212 -0
- package/dist/next/graphql/sha256.d.ts +13 -0
- package/dist/next/graphql/sha256.js +86 -0
- package/dist/next/graphql/summary.d.ts +83 -0
- package/dist/next/graphql/summary.js +151 -0
- package/dist/next/graphql/tree-layout.d.ts +36 -0
- package/dist/next/graphql/tree-layout.js +20 -0
- package/dist/next/graphql/tree.d.ts +171 -0
- package/dist/next/graphql/tree.js +258 -0
- package/dist/next/graphql/typed.d.ts +261 -0
- package/dist/next/graphql/typed.js +146 -0
- package/dist/next/index.d.ts +30 -5
- package/dist/next/index.js +32 -1
- package/dist/next/inflate.d.ts +51 -0
- package/dist/next/inflate.js +243 -0
- package/dist/next/key-family.d.ts +31 -0
- package/dist/next/key-family.js +66 -0
- package/dist/next/select-types.d.ts +58 -5
- package/dist/next/system-keys.d.ts +27 -0
- package/dist/next/system-keys.js +42 -0
- package/dist/nextjs/image-loader.d.ts +60 -0
- package/dist/nextjs/image-loader.js +71 -0
- package/dist/nextjs/index.d.ts +484 -5
- package/dist/nextjs/index.js +688 -6
- package/dist/nextjs/overlay.d.ts +30 -0
- package/dist/nextjs/overlay.js +78 -0
- package/dist/overlay/index.d.ts +14 -2
- package/dist/overlay/index.js +282 -43
- package/dist/overlay/protocol.d.ts +98 -2
- package/dist/overlay/protocol.js +151 -4
- package/dist/webhooks.d.ts +8 -0
- package/package.json +63 -15
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@capacms/sdk/nextjs/image-loader`: a next/image loader that lets Capa's CDN
|
|
3
|
+
* resize, in place of Next's own image optimization.
|
|
4
|
+
*
|
|
5
|
+
* Its own entry point, apart from `@capacms/sdk/nextjs`, because next/image
|
|
6
|
+
* runs the loader in the browser: `images.loaderFile` puts this module in
|
|
7
|
+
* every page that shows an image, and this entry carries the image rules and
|
|
8
|
+
* nothing else. `@capacms/sdk/nextjs` exports the same functions for server
|
|
9
|
+
* code.
|
|
10
|
+
*
|
|
11
|
+
* ```js
|
|
12
|
+
* // next.config.mjs
|
|
13
|
+
* export default { images: { loader: "custom", loaderFile: "./capa-image-loader.js" } };
|
|
14
|
+
*
|
|
15
|
+
* // capa-image-loader.js
|
|
16
|
+
* "use client";
|
|
17
|
+
* import { createCapaImageLoader } from "@capacms/sdk/nextjs/image-loader";
|
|
18
|
+
* export default createCapaImageLoader({ format: "webp" });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { type ImageFormat } from "../image/index.js";
|
|
22
|
+
/** What next/image passes a loader. Declared here so this module does not import `next`. */
|
|
23
|
+
export interface CapaImageLoaderProps {
|
|
24
|
+
src: string;
|
|
25
|
+
width: number;
|
|
26
|
+
quality?: number;
|
|
27
|
+
}
|
|
28
|
+
/** A next/image loader. */
|
|
29
|
+
export type CapaImageLoader = (props: CapaImageLoaderProps) => string;
|
|
30
|
+
/** What every image through the loader gets, unless next/image's own props say otherwise. */
|
|
31
|
+
export interface CapaImageLoaderOptions {
|
|
32
|
+
/**
|
|
33
|
+
* The format to encode. Left out, each image keeps its own. `auto` picks AVIF
|
|
34
|
+
* or WebP from the browser's `Accept` header; through the CDN it currently
|
|
35
|
+
* returns JPEG, so use `webp` until that is fixed.
|
|
36
|
+
*/
|
|
37
|
+
format?: ImageFormat;
|
|
38
|
+
/** Quality when an `<Image>` gives none, 1 to 100. Left out, Capa's default, 80. */
|
|
39
|
+
quality?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A next/image loader with fixed options. A `src` that is a full Capa URL
|
|
43
|
+
* (`https://cdn.capacms.com/files/...`, or `https://api.capacms.com/files/...`,
|
|
44
|
+
* which is built on the CDN host) is resized to the width next/image asks for.
|
|
45
|
+
* Any other `src` (a file in `public/`, a bare file name, another host) is
|
|
46
|
+
* returned unchanged.
|
|
47
|
+
*
|
|
48
|
+
* The loader never throws, since a throw would fail the render of the whole
|
|
49
|
+
* page: a `src` or prop the CDN would refuse (`dpr=2` past the 4096-pixel
|
|
50
|
+
* edge, `quality=abc` in the src, `quality={0}`) gets the `src` back unchanged.
|
|
51
|
+
* The options given here are checked once, when the loader is made, so a bad
|
|
52
|
+
* one fails at build time rather than on every image.
|
|
53
|
+
*/
|
|
54
|
+
export declare function createCapaImageLoader(options?: CapaImageLoaderOptions): CapaImageLoader;
|
|
55
|
+
/**
|
|
56
|
+
* The next/image loader with no options: each image keeps its own format, and
|
|
57
|
+
* its quality is the `<Image>`'s `quality` prop or Capa's default.
|
|
58
|
+
*/
|
|
59
|
+
export declare const capaImageLoader: CapaImageLoader;
|
|
60
|
+
export default capaImageLoader;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@capacms/sdk/nextjs/image-loader`: a next/image loader that lets Capa's CDN
|
|
3
|
+
* resize, in place of Next's own image optimization.
|
|
4
|
+
*
|
|
5
|
+
* Its own entry point, apart from `@capacms/sdk/nextjs`, because next/image
|
|
6
|
+
* runs the loader in the browser: `images.loaderFile` puts this module in
|
|
7
|
+
* every page that shows an image, and this entry carries the image rules and
|
|
8
|
+
* nothing else. `@capacms/sdk/nextjs` exports the same functions for server
|
|
9
|
+
* code.
|
|
10
|
+
*
|
|
11
|
+
* ```js
|
|
12
|
+
* // next.config.mjs
|
|
13
|
+
* export default { images: { loader: "custom", loaderFile: "./capa-image-loader.js" } };
|
|
14
|
+
*
|
|
15
|
+
* // capa-image-loader.js
|
|
16
|
+
* "use client";
|
|
17
|
+
* import { createCapaImageLoader } from "@capacms/sdk/nextjs/image-loader";
|
|
18
|
+
* export default createCapaImageLoader({ format: "webp" });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { imageUrl, isCapaImageUrl, MAX_OUTPUT_EDGE } from "../image/index.js";
|
|
22
|
+
/**
|
|
23
|
+
* A next/image loader with fixed options. A `src` that is a full Capa URL
|
|
24
|
+
* (`https://cdn.capacms.com/files/...`, or `https://api.capacms.com/files/...`,
|
|
25
|
+
* which is built on the CDN host) is resized to the width next/image asks for.
|
|
26
|
+
* Any other `src` (a file in `public/`, a bare file name, another host) is
|
|
27
|
+
* returned unchanged.
|
|
28
|
+
*
|
|
29
|
+
* The loader never throws, since a throw would fail the render of the whole
|
|
30
|
+
* page: a `src` or prop the CDN would refuse (`dpr=2` past the 4096-pixel
|
|
31
|
+
* edge, `quality=abc` in the src, `quality={0}`) gets the `src` back unchanged.
|
|
32
|
+
* The options given here are checked once, when the loader is made, so a bad
|
|
33
|
+
* one fails at build time rather than on every image.
|
|
34
|
+
*/
|
|
35
|
+
export function createCapaImageLoader(options = {}) {
|
|
36
|
+
const { format, quality } = options;
|
|
37
|
+
try {
|
|
38
|
+
imageUrl("x", { format, quality });
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
throw new TypeError(String(error.message).replace("imageUrl:", "createCapaImageLoader:"));
|
|
42
|
+
}
|
|
43
|
+
return (props) => {
|
|
44
|
+
// Everything inside the try, the props' own destructuring included: a call
|
|
45
|
+
// with no props object gives back no src rather than throwing.
|
|
46
|
+
try {
|
|
47
|
+
const { src, width, quality: asked } = props;
|
|
48
|
+
if (!isCapaImageUrl(src))
|
|
49
|
+
return src;
|
|
50
|
+
return imageUrl(src, {
|
|
51
|
+
// A width past Capa's edge asks for the edge, which keeps the image resized.
|
|
52
|
+
width: Math.min(width, MAX_OUTPUT_EDGE),
|
|
53
|
+
quality: asked ?? quality,
|
|
54
|
+
format,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return props?.src;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The next/image loader with no options: each image keeps its own format, and
|
|
64
|
+
* its quality is the `<Image>`'s `quality` prop or Capa's default.
|
|
65
|
+
*/
|
|
66
|
+
export const capaImageLoader = createCapaImageLoader();
|
|
67
|
+
export default capaImageLoader;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface CapaOverlayProps {
|
|
2
|
+
/** The Capa admin origins allowed to drive the overlay. */
|
|
3
|
+
adminOrigins: string[];
|
|
4
|
+
/**
|
|
5
|
+
* How a save shows. `"in-place"` (the default) re-renders the draft with
|
|
6
|
+
* `router.refresh()`, and reloads the page only if that has not landed
|
|
7
|
+
* within `refreshTimeoutMs`. `"reload"` reloads the page on every save.
|
|
8
|
+
* The scroll position is kept either way.
|
|
9
|
+
*/
|
|
10
|
+
refresh?: "in-place" | "reload";
|
|
11
|
+
/** How long an in-place refresh may take before the page reloads instead. 10 seconds. */
|
|
12
|
+
refreshTimeoutMs?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The refresh runs in a transition, so `isPending` says when the new draft
|
|
16
|
+
* has been committed to the screen, and the overlay settles the refresh
|
|
17
|
+
* then: it puts the scroll position back, and a refresh that has not
|
|
18
|
+
* committed within `refreshTimeoutMs` reloads.
|
|
19
|
+
*
|
|
20
|
+
* While the transition is pending the overlay makes a no-op state update
|
|
21
|
+
* every `REFRESH_NUDGE_MS`. React 19.2 under Next 15.5 can leave a draft
|
|
22
|
+
* refresh suspended after every part of its streamed response has arrived:
|
|
23
|
+
* the page suspends inside an already visible Suspense boundary (a root
|
|
24
|
+
* `loading.tsx` makes one around every page), and the signal that its data
|
|
25
|
+
* is ready is lost, so nothing commits until some other state update. Any
|
|
26
|
+
* state update makes React retry the suspended render, which then completes.
|
|
27
|
+
* A refresh that commits on its own stops the nudges at once. Draft mode
|
|
28
|
+
* only: a visitor never runs this component.
|
|
29
|
+
*/
|
|
30
|
+
export declare function CapaOverlay({ adminOrigins, refresh, refreshTimeoutMs }: CapaOverlayProps): null;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
/**
|
|
3
|
+
* `<CapaOverlay />`: Capa's live preview overlay as one Next.js client
|
|
4
|
+
* component (M6).
|
|
5
|
+
*
|
|
6
|
+
* // app/layout.tsx (a server component)
|
|
7
|
+
* import { CapaOverlay } from "@capacms/sdk/nextjs/overlay";
|
|
8
|
+
* {edit ? <CapaOverlay adminOrigins={["https://app.capacms.com"]} /> : null}
|
|
9
|
+
*
|
|
10
|
+
* Render it only in edit mode (`editMode()` from `@capacms/sdk/nextjs`), so a
|
|
11
|
+
* visitor never downloads it. Inside the Capa editor it outlines the focused
|
|
12
|
+
* field, reports clicks back, and on a save re-renders the page with
|
|
13
|
+
* `router.refresh()`, keeping the scroll position. Outside a Capa frame it does
|
|
14
|
+
* nothing at all.
|
|
15
|
+
*
|
|
16
|
+
* Its own entry point, apart from `@capacms/sdk/nextjs`, because it imports
|
|
17
|
+
* `react` and `next/navigation` and is a client module: the server helpers must
|
|
18
|
+
* stay free of both. A bundler that imports it gets an ES module, so it sees
|
|
19
|
+
* that only `useRouter` is used from `next/navigation` and leaves the chunks a
|
|
20
|
+
* visitor loads as they were.
|
|
21
|
+
*/
|
|
22
|
+
import { useEffect, useRef, useState, useTransition } from "react";
|
|
23
|
+
import { useRouter } from "next/navigation";
|
|
24
|
+
import { startOverlay } from "../overlay/index.js";
|
|
25
|
+
/**
|
|
26
|
+
* While a refresh is pending, the overlay updates its own unused state this
|
|
27
|
+
* often. See `CapaOverlay` for why.
|
|
28
|
+
*/
|
|
29
|
+
const REFRESH_NUDGE_MS = 300;
|
|
30
|
+
/**
|
|
31
|
+
* The refresh runs in a transition, so `isPending` says when the new draft
|
|
32
|
+
* has been committed to the screen, and the overlay settles the refresh
|
|
33
|
+
* then: it puts the scroll position back, and a refresh that has not
|
|
34
|
+
* committed within `refreshTimeoutMs` reloads.
|
|
35
|
+
*
|
|
36
|
+
* While the transition is pending the overlay makes a no-op state update
|
|
37
|
+
* every `REFRESH_NUDGE_MS`. React 19.2 under Next 15.5 can leave a draft
|
|
38
|
+
* refresh suspended after every part of its streamed response has arrived:
|
|
39
|
+
* the page suspends inside an already visible Suspense boundary (a root
|
|
40
|
+
* `loading.tsx` makes one around every page), and the signal that its data
|
|
41
|
+
* is ready is lost, so nothing commits until some other state update. Any
|
|
42
|
+
* state update makes React retry the suspended render, which then completes.
|
|
43
|
+
* A refresh that commits on its own stops the nudges at once. Draft mode
|
|
44
|
+
* only: a visitor never runs this component.
|
|
45
|
+
*/
|
|
46
|
+
export function CapaOverlay({ adminOrigins, refresh = "in-place", refreshTimeoutMs }) {
|
|
47
|
+
const router = useRouter();
|
|
48
|
+
const [refreshing, startRefresh] = useTransition();
|
|
49
|
+
const [, nudge] = useState(0);
|
|
50
|
+
/** One resolver per refresh waiting for its transition to commit. */
|
|
51
|
+
const waiting = useRef([]);
|
|
52
|
+
// A string, so a new array with the same origins does not restart it.
|
|
53
|
+
const origins = adminOrigins.join(",");
|
|
54
|
+
useEffect(() => startOverlay({
|
|
55
|
+
adminOrigins: origins.split(",").filter(Boolean),
|
|
56
|
+
onRefresh: refresh === "reload"
|
|
57
|
+
? undefined
|
|
58
|
+
: () => new Promise((resolve) => {
|
|
59
|
+
waiting.current.push(resolve);
|
|
60
|
+
startRefresh(() => router.refresh());
|
|
61
|
+
}),
|
|
62
|
+
refreshTimeoutMs,
|
|
63
|
+
}), [origins, router, refresh, refreshTimeoutMs]);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
if (!refreshing) {
|
|
66
|
+
// Committed: every refresh started before now is on screen.
|
|
67
|
+
for (const settle of waiting.current.splice(0))
|
|
68
|
+
settle();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const timer = window.setInterval(() => nudge((n) => n + 1), REFRESH_NUDGE_MS);
|
|
72
|
+
return () => window.clearInterval(timer);
|
|
73
|
+
}, [refreshing]);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export { acceptMessage, hoverMessage, normaliseOrigins, patchedText, pickCentred, readyMessage, scrollEdge, selectMessage, textAround, visibleMessage, ADMIN_SOURCE, PATCH_MAX_FIELDS, PATCH_MAX_LENGTH, PROTOCOL_VERSION, SITE_FEATURES, SITE_SOURCE, } from "./protocol.js";
|
|
2
|
+
export type { AdminMessage, MessageLike, PatchField, SiteMessage, TaggedBox } from "./protocol.js";
|
|
3
|
+
export interface OverlayOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The Capa admin origins allowed to drive this page, for example
|
|
6
|
+
* `["https://app.capacms.com"]`. A message from anywhere else is ignored.
|
|
7
|
+
*/
|
|
8
|
+
adminOrigins: string[];
|
|
9
|
+
/**
|
|
10
|
+
* How to re-render the draft after the editor saves. `router.refresh()` in a
|
|
11
|
+
* Next app. Without it the page reloads. Scroll position is kept either way.
|
|
12
|
+
*
|
|
13
|
+
* Return a promise that settles once the new draft is on screen, and the
|
|
14
|
+
* overlay waits for it: a promise that rejects, or is still pending after
|
|
15
|
+
* `refreshTimeoutMs`, falls back to a reload, which keeps the scroll
|
|
16
|
+
* position too. A function that returns nothing counts as done at once.
|
|
17
|
+
*/
|
|
18
|
+
onRefresh?: () => void | Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* How long `onRefresh`'s promise may stay pending before the page reloads
|
|
21
|
+
* instead. 10 seconds.
|
|
22
|
+
*/
|
|
23
|
+
refreshTimeoutMs?: number;
|
|
24
|
+
}
|
|
25
|
+
/** How long an in-place refresh may take before the page reloads instead. */
|
|
26
|
+
export declare const REFRESH_TIMEOUT_MS = 10000;
|
|
27
|
+
/**
|
|
28
|
+
* Start the overlay. Returns a disposer that removes every listener and the
|
|
29
|
+
* drawing layer. Calling it again while it runs updates the options and returns
|
|
30
|
+
* the same disposer, so a React effect that runs twice does not listen twice.
|
|
31
|
+
*/
|
|
32
|
+
export declare function startOverlay(options: OverlayOptions): () => void;
|