@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
package/dist/http.js
CHANGED
|
@@ -10,7 +10,7 @@ class CapaError extends Error {
|
|
|
10
10
|
status;
|
|
11
11
|
path;
|
|
12
12
|
constructor(status, path, body) {
|
|
13
|
-
super(`Capa API ${status} on ${path}${body ?
|
|
13
|
+
super(`Capa API ${status} on ${path}${body ? `: ${body.slice(0, 200)}` : ""}`);
|
|
14
14
|
this.name = "CapaError";
|
|
15
15
|
this.status = status;
|
|
16
16
|
this.path = path;
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@capacms/sdk/image`: URLs for Capa's image CDN, for any framework.
|
|
3
|
+
*
|
|
4
|
+
* Capa serves every upload at `https://cdn.capacms.com/files/<key>` and
|
|
5
|
+
* resizes it by query: `width`, `height`, `fit`, `quality`, `dpr`, `blur` and
|
|
6
|
+
* `format`. The API answers a URL whose query is spelled any other way than
|
|
7
|
+
* its canonical spelling with a 301 the edge keeps for a year, so every URL
|
|
8
|
+
* built here is already canonical. The spelling is not decided here: it comes
|
|
9
|
+
* from the rules the API serves by (`canonicalQuery` and `parseImageParams`,
|
|
10
|
+
* compiled in from @capa/shared at build time), so the two cannot drift.
|
|
11
|
+
*
|
|
12
|
+
* Four rules beside the API's:
|
|
13
|
+
* - Nothing is enlarged. When the image's own width is known (the `/api/`
|
|
14
|
+
* media shape carries it), a request for more pixels than it has is brought
|
|
15
|
+
* down to its size, keeping the requested shape.
|
|
16
|
+
* - `format=auto` is only sent when asked for.
|
|
17
|
+
* - A URL that is not a Capa image comes back unchanged.
|
|
18
|
+
* - A crop to a shape (`fit: "cover"` with a width and a height) of a media
|
|
19
|
+
* value that carries an editor's focal point or crop names them in the URL
|
|
20
|
+
* (`fp`, `rect`), so moving the focal point gives a new URL rather than a
|
|
21
|
+
* copy cached under the old one. A media value without them, and a URL or
|
|
22
|
+
* a key without `fp` or `rect`, builds exactly the URL it always did.
|
|
23
|
+
*
|
|
24
|
+
* A stored URL on `api.capacms.com/files/` (older uploads) names the same file
|
|
25
|
+
* as the CDN: the CDN fetches from that host. Built URLs go to the CDN host,
|
|
26
|
+
* where a resized copy is cached at the edge rather than made again by the
|
|
27
|
+
* origin on every request.
|
|
28
|
+
*/
|
|
29
|
+
import { MAX_OUTPUT_EDGE, type CropRect, type FocalPoint, type ImageFit, type ImageFormat } from "./shared-params.generated.js";
|
|
30
|
+
export type { ImageFit, ImageFormat };
|
|
31
|
+
export { MAX_OUTPUT_EDGE };
|
|
32
|
+
/**
|
|
33
|
+
* The spot in an image an editor marked as the one that matters most, as
|
|
34
|
+
* fractions of the image from 0 to 1: `x` from the left, `y` from the top.
|
|
35
|
+
*/
|
|
36
|
+
export type ImageFocalPoint = FocalPoint;
|
|
37
|
+
/** The part of an image an editor chose to show, as fractions of the image from 0 to 1. */
|
|
38
|
+
export type ImageCrop = CropRect;
|
|
39
|
+
/** Where Capa serves files. A media `url` from the API starts with this and `/files/`. */
|
|
40
|
+
export declare const CAPA_CDN_ORIGIN = "https://cdn.capacms.com";
|
|
41
|
+
/**
|
|
42
|
+
* True when `src` is a full URL of a file on Capa: `https://cdn.capacms.com/files/...`
|
|
43
|
+
* or `https://api.capacms.com/files/...`. A key, a path or another host is not.
|
|
44
|
+
* Use it to mark the other images `unoptimized` in next/image.
|
|
45
|
+
*/
|
|
46
|
+
export declare function isCapaImageUrl(src: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* A media value: the `/api/` shape `{ id, url, alt, type, width, height }`, the
|
|
49
|
+
* legacy client's `CapaImage`, or anything else with a `url`. `width` and
|
|
50
|
+
* `height` are the image's own pixel size, used so it is never enlarged.
|
|
51
|
+
*/
|
|
52
|
+
export interface ImageLike {
|
|
53
|
+
url: string | null;
|
|
54
|
+
width?: number | null;
|
|
55
|
+
height?: number | null;
|
|
56
|
+
/** The file's kind. Anything but `image` (or a missing kind) is returned unchanged. */
|
|
57
|
+
type?: string | null;
|
|
58
|
+
/**
|
|
59
|
+
* The editor's focal point, where `/api/` sends one. A crop to a shape
|
|
60
|
+
* (`fit: "cover"` with a width and a height) centres on it, and its URL
|
|
61
|
+
* names it as `fp`.
|
|
62
|
+
*/
|
|
63
|
+
focalPoint?: ImageFocalPoint | null;
|
|
64
|
+
/** The editor's crop, where `/api/` sends one. A crop to a shape stays inside it, and its URL names it as `rect`. */
|
|
65
|
+
crop?: ImageCrop | null;
|
|
66
|
+
}
|
|
67
|
+
/** A media value, a full CDN URL, or a file key (`sunns_1790971326210_d54fed020806.jpg`). */
|
|
68
|
+
export type ImageSource = string | ImageLike;
|
|
69
|
+
/** What Capa's CDN can do to an image. Every one is optional, and a default is left out of the URL. */
|
|
70
|
+
export interface ImageOptions {
|
|
71
|
+
/** Width in CSS pixels; with `dpr`, the image is `width × dpr` pixels wide. */
|
|
72
|
+
width?: number | null;
|
|
73
|
+
/** Height in CSS pixels. */
|
|
74
|
+
height?: number | null;
|
|
75
|
+
/** How the image meets a box given by both `width` and `height`: `inside` (the default), `cover` (crop) or `contain` (pad). */
|
|
76
|
+
fit?: ImageFit;
|
|
77
|
+
/** 1 to 100; Capa's default is 80. Ignored for png. */
|
|
78
|
+
quality?: number;
|
|
79
|
+
/** Device pixel ratio, 1 to 3. */
|
|
80
|
+
dpr?: number;
|
|
81
|
+
/** A blurred placeholder `blur × blur` pixels square, 1 to 256. */
|
|
82
|
+
blur?: number;
|
|
83
|
+
/**
|
|
84
|
+
* The format to encode. Left out, the image keeps its own format. `auto`
|
|
85
|
+
* picks AVIF or WebP from the browser's `Accept` header; through the CDN it
|
|
86
|
+
* currently returns JPEG, so use `webp` until that is fixed.
|
|
87
|
+
*/
|
|
88
|
+
format?: ImageFormat;
|
|
89
|
+
/**
|
|
90
|
+
* The focal point for a crop to a shape (`fit: "cover"` with a width and a
|
|
91
|
+
* height), in place of the media value's. Other requests have nothing to
|
|
92
|
+
* centre, so it is left out of their URL.
|
|
93
|
+
*/
|
|
94
|
+
focalPoint?: ImageFocalPoint | null;
|
|
95
|
+
/** A crop, in place of the media value's. It applies to any resize, and on its own crops the image. */
|
|
96
|
+
crop?: ImageCrop | null;
|
|
97
|
+
}
|
|
98
|
+
/** The options `srcSet` takes: each candidate's width comes from its list, and its height from `aspectRatio`. */
|
|
99
|
+
export interface SrcSetOptions {
|
|
100
|
+
/** Width over height. Given, each candidate is `width` by `width / aspectRatio`; use it with `fit: "cover"` to crop. */
|
|
101
|
+
aspectRatio?: number;
|
|
102
|
+
fit?: ImageFit;
|
|
103
|
+
quality?: number;
|
|
104
|
+
format?: ImageFormat;
|
|
105
|
+
/** As `imageUrl`'s: applies to each candidate cropped to a shape (`aspectRatio` with `fit: "cover"`). */
|
|
106
|
+
focalPoint?: ImageFocalPoint | null;
|
|
107
|
+
/** As `imageUrl`'s: applies to every candidate. */
|
|
108
|
+
crop?: ImageCrop | null;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The URL of a Capa image, resized as `options` say, spelled exactly as Capa's
|
|
112
|
+
* CDN serves it without a redirect.
|
|
113
|
+
*
|
|
114
|
+
* `image` is a media value (`entry.fields.hero`), a CDN URL, or a file key.
|
|
115
|
+
* Gives `undefined` when there is no image (`null`, or a media value whose
|
|
116
|
+
* `url` is null or empty, which is how the legacy API sends an unset image),
|
|
117
|
+
* so a media value always gives `string | undefined`; a string always gives a
|
|
118
|
+
* string. Gives back unchanged a URL that is not a Capa image (another host, a
|
|
119
|
+
* site path) or a media value that is not an image. Throws a `TypeError` for
|
|
120
|
+
* an option Capa would refuse with a 400.
|
|
121
|
+
*
|
|
122
|
+
* ```ts
|
|
123
|
+
* imageUrl(entry.fields.hero, { width: 1200, format: "webp" })
|
|
124
|
+
* // https://cdn.capacms.com/files/<key>?format=webp&width=1200
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export declare function imageUrl(image: string, options?: ImageOptions): string;
|
|
128
|
+
export declare function imageUrl(image: ImageSource | null | undefined, options?: ImageOptions): string | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* A `srcset` for a Capa image: one candidate per width, smallest first, each
|
|
131
|
+
* as `<url> <width>w`. Widths past the image's own width (when known) are
|
|
132
|
+
* dropped in favour of one candidate at its own width.
|
|
133
|
+
*
|
|
134
|
+
* Gives `undefined` when there is nothing to resize: no image, or one that is
|
|
135
|
+
* not Capa's. Throws a `TypeError` for a width list that is empty or not whole
|
|
136
|
+
* pixels, or an option Capa would refuse.
|
|
137
|
+
*
|
|
138
|
+
* ```ts
|
|
139
|
+
* srcSet(entry.fields.hero, [640, 960, 1280, 1920], { format: "webp" })
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export declare function srcSet(image: ImageSource | null | undefined, widths: readonly number[], options?: SrcSetOptions): string | undefined;
|
|
143
|
+
/**
|
|
144
|
+
* A `sizes` attribute from min-width breakpoints, widest first so the first
|
|
145
|
+
* match is the right one, then the fallback.
|
|
146
|
+
*
|
|
147
|
+
* ```ts
|
|
148
|
+
* sizes({ 768: "50vw", 1280: "33vw" })
|
|
149
|
+
* // (min-width: 1280px) 33vw, (min-width: 768px) 50vw, 100vw
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function sizes(breakpoints: Readonly<Record<number, string>>, fallback?: string): string;
|
|
153
|
+
/**
|
|
154
|
+
* What `/api/` sends of an image's placeholders, where Capa makes them: a
|
|
155
|
+
* tiny blurred copy as a data URL, and its colours.
|
|
156
|
+
*/
|
|
157
|
+
export interface PlaceholderLike {
|
|
158
|
+
blurDataURL?: string | null;
|
|
159
|
+
averageColor?: string | null;
|
|
160
|
+
dominantColor?: string | null;
|
|
161
|
+
}
|
|
162
|
+
/** next/image's `placeholder` and `blurDataURL` props, to spread onto an `<Image>`. */
|
|
163
|
+
export type ImagePlaceholder = {
|
|
164
|
+
placeholder: "blur";
|
|
165
|
+
blurDataURL: string;
|
|
166
|
+
} | {
|
|
167
|
+
placeholder: "empty";
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* next/image's placeholder props for a media value: `placeholder="blur"` with
|
|
171
|
+
* its `blurDataURL` when it has one, and `placeholder="empty"` otherwise, so
|
|
172
|
+
* the result can always be spread:
|
|
173
|
+
*
|
|
174
|
+
* ```tsx
|
|
175
|
+
* <Image src={imageUrl(hero)} alt={hero.alt ?? ""} width={1200} height={800} {...imagePlaceholder(hero)} />
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
export declare function imagePlaceholder(image: PlaceholderLike | null | undefined): ImagePlaceholder;
|
|
179
|
+
/**
|
|
180
|
+
* A colour to paint an image's space with while it loads, as `#rrggbb`: its
|
|
181
|
+
* average colour, else its dominant one, else undefined.
|
|
182
|
+
*
|
|
183
|
+
* ```tsx
|
|
184
|
+
* <div style={{ backgroundColor: placeholderColor(hero) }}>...</div>
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export declare function placeholderColor(image: PlaceholderLike | null | undefined): string | undefined;
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CAPA_CDN_ORIGIN = exports.MAX_OUTPUT_EDGE = void 0;
|
|
4
|
+
exports.isCapaImageUrl = isCapaImageUrl;
|
|
5
|
+
exports.imageUrl = imageUrl;
|
|
6
|
+
exports.srcSet = srcSet;
|
|
7
|
+
exports.sizes = sizes;
|
|
8
|
+
exports.imagePlaceholder = imagePlaceholder;
|
|
9
|
+
exports.placeholderColor = placeholderColor;
|
|
10
|
+
/**
|
|
11
|
+
* `@capacms/sdk/image`: URLs for Capa's image CDN, for any framework.
|
|
12
|
+
*
|
|
13
|
+
* Capa serves every upload at `https://cdn.capacms.com/files/<key>` and
|
|
14
|
+
* resizes it by query: `width`, `height`, `fit`, `quality`, `dpr`, `blur` and
|
|
15
|
+
* `format`. The API answers a URL whose query is spelled any other way than
|
|
16
|
+
* its canonical spelling with a 301 the edge keeps for a year, so every URL
|
|
17
|
+
* built here is already canonical. The spelling is not decided here: it comes
|
|
18
|
+
* from the rules the API serves by (`canonicalQuery` and `parseImageParams`,
|
|
19
|
+
* compiled in from @capa/shared at build time), so the two cannot drift.
|
|
20
|
+
*
|
|
21
|
+
* Four rules beside the API's:
|
|
22
|
+
* - Nothing is enlarged. When the image's own width is known (the `/api/`
|
|
23
|
+
* media shape carries it), a request for more pixels than it has is brought
|
|
24
|
+
* down to its size, keeping the requested shape.
|
|
25
|
+
* - `format=auto` is only sent when asked for.
|
|
26
|
+
* - A URL that is not a Capa image comes back unchanged.
|
|
27
|
+
* - A crop to a shape (`fit: "cover"` with a width and a height) of a media
|
|
28
|
+
* value that carries an editor's focal point or crop names them in the URL
|
|
29
|
+
* (`fp`, `rect`), so moving the focal point gives a new URL rather than a
|
|
30
|
+
* copy cached under the old one. A media value without them, and a URL or
|
|
31
|
+
* a key without `fp` or `rect`, builds exactly the URL it always did.
|
|
32
|
+
*
|
|
33
|
+
* A stored URL on `api.capacms.com/files/` (older uploads) names the same file
|
|
34
|
+
* as the CDN: the CDN fetches from that host. Built URLs go to the CDN host,
|
|
35
|
+
* where a resized copy is cached at the edge rather than made again by the
|
|
36
|
+
* origin on every request.
|
|
37
|
+
*/
|
|
38
|
+
const shared_params_generated_js_1 = require("./shared-params.generated.js");
|
|
39
|
+
Object.defineProperty(exports, "MAX_OUTPUT_EDGE", { enumerable: true, get: function () { return shared_params_generated_js_1.MAX_OUTPUT_EDGE; } });
|
|
40
|
+
/** Where Capa serves files. A media `url` from the API starts with this and `/files/`. */
|
|
41
|
+
exports.CAPA_CDN_ORIGIN = "https://cdn.capacms.com";
|
|
42
|
+
/**
|
|
43
|
+
* The API's own host, which some stored URLs name (`https://api.capacms.com/files/<key>`).
|
|
44
|
+
* It is the CDN's origin, with nothing in front of it, so the same key is the
|
|
45
|
+
* same file on both; a URL built here always names the CDN.
|
|
46
|
+
*/
|
|
47
|
+
const CAPA_API_ORIGIN = "https://api.capacms.com";
|
|
48
|
+
const CAPA_ORIGINS = new Set([exports.CAPA_CDN_ORIGIN, CAPA_API_ORIGIN]);
|
|
49
|
+
const FILES_PATH = "/files/";
|
|
50
|
+
/** A full `https://` URL of a file on Capa's CDN or API host, as a URL, or null. */
|
|
51
|
+
function capaFileUrl(text) {
|
|
52
|
+
let url;
|
|
53
|
+
try {
|
|
54
|
+
url = new URL(text);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
if (!CAPA_ORIGINS.has(url.origin) || !url.pathname.startsWith(FILES_PATH) || url.pathname.length === FILES_PATH.length)
|
|
60
|
+
return null;
|
|
61
|
+
// The same path, query and fragment on the CDN host.
|
|
62
|
+
return url.origin === exports.CAPA_CDN_ORIGIN ? url : new URL(`${url.pathname}${url.search}${url.hash}`, exports.CAPA_CDN_ORIGIN);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* True when `src` is a full URL of a file on Capa: `https://cdn.capacms.com/files/...`
|
|
66
|
+
* or `https://api.capacms.com/files/...`. A key, a path or another host is not.
|
|
67
|
+
* Use it to mark the other images `unoptimized` in next/image.
|
|
68
|
+
*/
|
|
69
|
+
function isCapaImageUrl(src) {
|
|
70
|
+
return typeof src === "string" && /^https:\/\//i.test(src) && capaFileUrl(src) !== null;
|
|
71
|
+
}
|
|
72
|
+
const OPTION_KEYS = ["width", "height", "fit", "quality", "dpr", "blur", "format"];
|
|
73
|
+
function positive(value) {
|
|
74
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
75
|
+
}
|
|
76
|
+
/** True when `text` names a scheme (`https:`, `data:`), so it is a URL rather than a file key. */
|
|
77
|
+
function hasScheme(text) {
|
|
78
|
+
return /^[a-z][a-z0-9+.-]*:/i.test(text);
|
|
79
|
+
}
|
|
80
|
+
/** What a media value says about itself: its size, and an editor's focal point and crop when they are well formed. */
|
|
81
|
+
function known(image) {
|
|
82
|
+
const out = { intrinsicWidth: positive(image?.width), intrinsicHeight: positive(image?.height) };
|
|
83
|
+
const focalPoint = image ? (0, shared_params_generated_js_1.readFocalPoint)(image.focalPoint) : null;
|
|
84
|
+
const crop = image ? (0, shared_params_generated_js_1.readCrop)(image.crop) : null;
|
|
85
|
+
if (focalPoint)
|
|
86
|
+
out.focalPoint = focalPoint;
|
|
87
|
+
if (crop)
|
|
88
|
+
out.crop = crop;
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
/** A string or `url` as a Capa image, or why not. */
|
|
92
|
+
function resolveUrl(text, image) {
|
|
93
|
+
if (hasScheme(text)) {
|
|
94
|
+
const url = capaFileUrl(text);
|
|
95
|
+
if (url === null)
|
|
96
|
+
return { unchanged: text };
|
|
97
|
+
return { target: { url, ...known(image) } };
|
|
98
|
+
}
|
|
99
|
+
// A path (`/logo.png`, `//host/x`) is the site's own or another host's.
|
|
100
|
+
if (text === "" || text.startsWith("/"))
|
|
101
|
+
return { unchanged: text };
|
|
102
|
+
// A file key, which may hold folders: each segment is encoded on its own.
|
|
103
|
+
const path = text.split("/").map(encodeURIComponent).join("/");
|
|
104
|
+
return { target: { url: new URL(`${exports.CAPA_CDN_ORIGIN}${FILES_PATH}${path}`), ...known(image) } };
|
|
105
|
+
}
|
|
106
|
+
function resolve(image) {
|
|
107
|
+
if (image === null || image === undefined)
|
|
108
|
+
return null;
|
|
109
|
+
if (typeof image === "string")
|
|
110
|
+
return resolveUrl(image);
|
|
111
|
+
if (typeof image !== "object" || typeof image.url !== "string" || image.url === "")
|
|
112
|
+
return null;
|
|
113
|
+
if (typeof image.type === "string" && image.type !== "image" && !image.type.startsWith("image/"))
|
|
114
|
+
return { unchanged: image.url };
|
|
115
|
+
return resolveUrl(image.url, image);
|
|
116
|
+
}
|
|
117
|
+
/** A number from a query value, when it is one. */
|
|
118
|
+
function numeric(value) {
|
|
119
|
+
if (typeof value !== "string" || !/^\d+(\.\d+)?$/.test(value.trim()))
|
|
120
|
+
return undefined;
|
|
121
|
+
return positive(Number(value));
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Brings a request that would enlarge the image down to the image's own size.
|
|
125
|
+
*
|
|
126
|
+
* The scale the request puts on the image's content is worked out for its
|
|
127
|
+
* fit. `inside` and `contain` fit the image within the box, so the smaller of
|
|
128
|
+
* the two ratios rules and both sizes must be known; `cover` fills the box, so
|
|
129
|
+
* the larger rules, and with only the width known the width's ratio is a floor
|
|
130
|
+
* on it. Above 1, the box is divided by that scale, which keeps its shape and
|
|
131
|
+
* makes the content its own size. Nothing changes when the scale cannot be
|
|
132
|
+
* worked out. Blur is left alone: its size comes from `blur`, not the box.
|
|
133
|
+
*/
|
|
134
|
+
function capToImage(query, target) {
|
|
135
|
+
const W = target.intrinsicWidth;
|
|
136
|
+
const H = target.intrinsicHeight;
|
|
137
|
+
if ((W === undefined && H === undefined) || query.blur !== undefined)
|
|
138
|
+
return;
|
|
139
|
+
const width = numeric(query.width);
|
|
140
|
+
const height = numeric(query.height);
|
|
141
|
+
const dpr = numeric(query.dpr) ?? 1;
|
|
142
|
+
const fit = typeof query.fit === "string" ? query.fit.trim().toLowerCase() : "inside";
|
|
143
|
+
const sw = width !== undefined && W !== undefined ? (width * dpr) / W : undefined;
|
|
144
|
+
const sh = height !== undefined && H !== undefined ? (height * dpr) / H : undefined;
|
|
145
|
+
let scale;
|
|
146
|
+
if (width !== undefined && height === undefined)
|
|
147
|
+
scale = sw;
|
|
148
|
+
else if (height !== undefined && width === undefined)
|
|
149
|
+
scale = sh;
|
|
150
|
+
else if (width !== undefined && height !== undefined) {
|
|
151
|
+
if (fit === "cover")
|
|
152
|
+
scale = sh === undefined ? sw : sw === undefined ? sh : Math.max(sw, sh);
|
|
153
|
+
else
|
|
154
|
+
scale = sw === undefined || sh === undefined ? undefined : Math.min(sw, sh);
|
|
155
|
+
}
|
|
156
|
+
if (scale === undefined || scale <= 1)
|
|
157
|
+
return;
|
|
158
|
+
// The epsilon keeps a division that is exact on paper (1600 / (4/3)) from flooring to one less.
|
|
159
|
+
const down = (n) => String(Math.max(1, Math.floor(n / scale + 1e-9)));
|
|
160
|
+
if (width !== undefined)
|
|
161
|
+
query.width = down(width);
|
|
162
|
+
if (height !== undefined)
|
|
163
|
+
query.height = down(height);
|
|
164
|
+
}
|
|
165
|
+
function fail(fn, message) {
|
|
166
|
+
throw new TypeError(`@capacms/sdk: ${fn}: ${message}`);
|
|
167
|
+
}
|
|
168
|
+
/** The canonical URL for a Capa image, and the pixel width it asks for. */
|
|
169
|
+
function build(fn, target, options) {
|
|
170
|
+
const { url } = target;
|
|
171
|
+
const search = url.search.slice(1);
|
|
172
|
+
// The URL's own image parameters, as Fastify would hand them to the API.
|
|
173
|
+
const query = {};
|
|
174
|
+
for (const [key, value] of new URLSearchParams(search)) {
|
|
175
|
+
if (!shared_params_generated_js_1.LEGACY_PARAMS.has(key) && !shared_params_generated_js_1.NEW_PARAMS.has(key))
|
|
176
|
+
continue;
|
|
177
|
+
const prior = query[key];
|
|
178
|
+
query[key] = prior === undefined ? value : [].concat(prior, value);
|
|
179
|
+
}
|
|
180
|
+
// The options win over them.
|
|
181
|
+
for (const key of OPTION_KEYS) {
|
|
182
|
+
const value = options[key];
|
|
183
|
+
if (value !== undefined && value !== null)
|
|
184
|
+
query[key] = String(value);
|
|
185
|
+
}
|
|
186
|
+
capToImage(query, target);
|
|
187
|
+
const parsed = (0, shared_params_generated_js_1.parseImageParams)(query);
|
|
188
|
+
if (!parsed.ok)
|
|
189
|
+
fail(fn, parsed.rejection.error);
|
|
190
|
+
const focal = focalParams(fn, target, options, parsed.params, search);
|
|
191
|
+
// With no focal point or crop to name, the URL is spelled as it always was:
|
|
192
|
+
// an `fp` or `rect` already in it is an unknown key, kept as it is.
|
|
193
|
+
const canonical = focal === null ? (0, shared_params_generated_js_1.canonicalQuery)(search, parsed.params) : (0, shared_params_generated_js_1.canonicalQuery)(search, focal, { focal: true });
|
|
194
|
+
const href = `${url.origin}${url.pathname}${canonical ? `?${canonical}` : ""}${url.hash}`;
|
|
195
|
+
return { href, width: parsed.params.width };
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* The parameters with the focal point and crop this URL names, or null when
|
|
199
|
+
* it names none of its own making.
|
|
200
|
+
*
|
|
201
|
+
* Capa's CDN crops a request to a shape (`fit=cover` with a width and a
|
|
202
|
+
* height) around the focal point and inside the crop an editor set on the
|
|
203
|
+
* file, whether the URL names them or not. Named in the URL, a moved focal
|
|
204
|
+
* point is a new URL, so no browser or edge keeps the old crop under it. A
|
|
205
|
+
* crop given as an option applies to any resize, as the CDN applies a `rect`
|
|
206
|
+
* it is sent. Options win over the media value, which wins over an `fp` or
|
|
207
|
+
* `rect` the URL already had. With none of them, null: the URL is spelled as
|
|
208
|
+
* it always was.
|
|
209
|
+
*/
|
|
210
|
+
function focalParams(fn, target, options, params, search) {
|
|
211
|
+
let askedFocal;
|
|
212
|
+
if (options.focalPoint !== undefined && options.focalPoint !== null) {
|
|
213
|
+
const read = (0, shared_params_generated_js_1.readFocalPoint)(options.focalPoint);
|
|
214
|
+
if (read === null)
|
|
215
|
+
fail(fn, "`focalPoint` must be { x, y }, numbers from 0 to 1");
|
|
216
|
+
askedFocal = read;
|
|
217
|
+
}
|
|
218
|
+
let askedCrop;
|
|
219
|
+
if (options.crop !== undefined && options.crop !== null) {
|
|
220
|
+
const read = (0, shared_params_generated_js_1.readCrop)(options.crop);
|
|
221
|
+
if (read === null)
|
|
222
|
+
fail(fn, "`crop` must be { x, y, width, height }, numbers from 0 to 1 with a width and a height above 0, inside the image");
|
|
223
|
+
askedCrop = read;
|
|
224
|
+
}
|
|
225
|
+
const cover = params.fit === "cover" && params.width !== undefined && params.height !== undefined && params.blur === undefined;
|
|
226
|
+
const focalPoint = cover ? askedFocal ?? target.focalPoint : undefined;
|
|
227
|
+
const rect = askedCrop ?? (cover ? target.crop : undefined);
|
|
228
|
+
// What the URL already said (a URL this function built, handed back to it,
|
|
229
|
+
// as next/image hands its loader a `src`), kept where nothing here replaces
|
|
230
|
+
// it, and spelled as the CDN spells it. One the CDN would refuse stays an
|
|
231
|
+
// unknown key, as it is, unless something here replaces it.
|
|
232
|
+
const own = {};
|
|
233
|
+
for (const [key, value] of new URLSearchParams(search))
|
|
234
|
+
if (shared_params_generated_js_1.FOCAL_PARAMS.has(key) && !(key in own))
|
|
235
|
+
own[key] = value;
|
|
236
|
+
const ownFocal = (0, shared_params_generated_js_1.parseFocalParams)(own);
|
|
237
|
+
const kept = ownFocal.ok ? ownFocal : {};
|
|
238
|
+
const fp = focalPoint ?? kept.focalPoint;
|
|
239
|
+
const r = rect ?? kept.rect;
|
|
240
|
+
if (fp === undefined && r === undefined)
|
|
241
|
+
return null;
|
|
242
|
+
const out = { ...params };
|
|
243
|
+
if (fp !== undefined)
|
|
244
|
+
out.focalPoint = fp;
|
|
245
|
+
if (r !== undefined)
|
|
246
|
+
out.rect = r;
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
function imageUrl(image, options = {}) {
|
|
250
|
+
const resolved = resolve(image);
|
|
251
|
+
if (resolved === null)
|
|
252
|
+
return undefined;
|
|
253
|
+
if ("unchanged" in resolved)
|
|
254
|
+
return resolved.unchanged;
|
|
255
|
+
return build("imageUrl", resolved.target, options).href;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* A `srcset` for a Capa image: one candidate per width, smallest first, each
|
|
259
|
+
* as `<url> <width>w`. Widths past the image's own width (when known) are
|
|
260
|
+
* dropped in favour of one candidate at its own width.
|
|
261
|
+
*
|
|
262
|
+
* Gives `undefined` when there is nothing to resize: no image, or one that is
|
|
263
|
+
* not Capa's. Throws a `TypeError` for a width list that is empty or not whole
|
|
264
|
+
* pixels, or an option Capa would refuse.
|
|
265
|
+
*
|
|
266
|
+
* ```ts
|
|
267
|
+
* srcSet(entry.fields.hero, [640, 960, 1280, 1920], { format: "webp" })
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
function srcSet(image, widths, options = {}) {
|
|
271
|
+
if (!Array.isArray(widths) || widths.length === 0 || !widths.every((w) => Number.isSafeInteger(w) && w > 0)) {
|
|
272
|
+
fail("srcSet", `widths must be a list of whole pixel widths, such as [640, 1280]. Got ${JSON.stringify(widths)}.`);
|
|
273
|
+
}
|
|
274
|
+
const loose = options;
|
|
275
|
+
if (loose.width !== undefined)
|
|
276
|
+
fail("srcSet", "srcSet takes its widths as the second argument, not a `width` option.");
|
|
277
|
+
if (loose.height !== undefined)
|
|
278
|
+
fail("srcSet", "give `aspectRatio` (width / height) rather than `height`; each candidate's height follows its width.");
|
|
279
|
+
if (loose.dpr !== undefined || loose.blur !== undefined) {
|
|
280
|
+
fail("srcSet", "`dpr` and `blur` do not apply to a srcset: the browser picks a candidate by its w descriptor.");
|
|
281
|
+
}
|
|
282
|
+
const { aspectRatio } = options;
|
|
283
|
+
if (aspectRatio !== undefined && positive(aspectRatio) === undefined) {
|
|
284
|
+
fail("srcSet", `\`aspectRatio\` must be a positive number (width / height). Got ${JSON.stringify(aspectRatio)}.`);
|
|
285
|
+
}
|
|
286
|
+
const resolved = resolve(image);
|
|
287
|
+
if (resolved === null || "unchanged" in resolved)
|
|
288
|
+
return undefined;
|
|
289
|
+
const seen = new Set();
|
|
290
|
+
const candidates = [];
|
|
291
|
+
for (const width of [...new Set(widths)].sort((a, b) => a - b)) {
|
|
292
|
+
const height = aspectRatio === undefined ? undefined : Math.max(1, Math.round(width / aspectRatio));
|
|
293
|
+
const { href, width: built } = build("srcSet", resolved.target, {
|
|
294
|
+
width,
|
|
295
|
+
height,
|
|
296
|
+
fit: options.fit,
|
|
297
|
+
quality: options.quality,
|
|
298
|
+
format: options.format,
|
|
299
|
+
focalPoint: options.focalPoint,
|
|
300
|
+
crop: options.crop,
|
|
301
|
+
});
|
|
302
|
+
if (seen.has(href))
|
|
303
|
+
continue;
|
|
304
|
+
seen.add(href);
|
|
305
|
+
candidates.push(`${href} ${built ?? width}w`);
|
|
306
|
+
}
|
|
307
|
+
return candidates.join(", ");
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* A `sizes` attribute from min-width breakpoints, widest first so the first
|
|
311
|
+
* match is the right one, then the fallback.
|
|
312
|
+
*
|
|
313
|
+
* ```ts
|
|
314
|
+
* sizes({ 768: "50vw", 1280: "33vw" })
|
|
315
|
+
* // (min-width: 1280px) 33vw, (min-width: 768px) 50vw, 100vw
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
318
|
+
function sizes(breakpoints, fallback = "100vw") {
|
|
319
|
+
const rules = [];
|
|
320
|
+
for (const [key, size] of Object.entries(breakpoints)) {
|
|
321
|
+
const px = Number(key);
|
|
322
|
+
if (!/^\d+$/.test(key) || px <= 0)
|
|
323
|
+
fail("sizes", `breakpoints are min-widths in pixels, such as { 768: "50vw" }. Got ${JSON.stringify(key)}.`);
|
|
324
|
+
if (typeof size !== "string" || size.trim() === "")
|
|
325
|
+
fail("sizes", `the size for ${key}px must be a length, such as "50vw". Got ${JSON.stringify(size)}.`);
|
|
326
|
+
rules.push([px, size.trim()]);
|
|
327
|
+
}
|
|
328
|
+
if (typeof fallback !== "string" || fallback.trim() === "")
|
|
329
|
+
fail("sizes", `the fallback must be a length, such as "100vw". Got ${JSON.stringify(fallback)}.`);
|
|
330
|
+
rules.sort(([a], [b]) => b - a);
|
|
331
|
+
return [...rules.map(([px, size]) => `(min-width: ${px}px) ${size}`), fallback.trim()].join(", ");
|
|
332
|
+
}
|
|
333
|
+
const BLUR_DATA_URL = /^data:image\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/]+={0,2}$/i;
|
|
334
|
+
const HEX_COLOUR = /^#[0-9a-f]{6}$/i;
|
|
335
|
+
/**
|
|
336
|
+
* next/image's placeholder props for a media value: `placeholder="blur"` with
|
|
337
|
+
* its `blurDataURL` when it has one, and `placeholder="empty"` otherwise, so
|
|
338
|
+
* the result can always be spread:
|
|
339
|
+
*
|
|
340
|
+
* ```tsx
|
|
341
|
+
* <Image src={imageUrl(hero)} alt={hero.alt ?? ""} width={1200} height={800} {...imagePlaceholder(hero)} />
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
344
|
+
function imagePlaceholder(image) {
|
|
345
|
+
const blur = image?.blurDataURL;
|
|
346
|
+
return typeof blur === "string" && BLUR_DATA_URL.test(blur) ? { placeholder: "blur", blurDataURL: blur } : { placeholder: "empty" };
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* A colour to paint an image's space with while it loads, as `#rrggbb`: its
|
|
350
|
+
* average colour, else its dominant one, else undefined.
|
|
351
|
+
*
|
|
352
|
+
* ```tsx
|
|
353
|
+
* <div style={{ backgroundColor: placeholderColor(hero) }}>...</div>
|
|
354
|
+
* ```
|
|
355
|
+
*/
|
|
356
|
+
function placeholderColor(image) {
|
|
357
|
+
for (const colour of [image?.averageColor, image?.dominantColor]) {
|
|
358
|
+
if (typeof colour === "string" && HEX_COLOUR.test(colour))
|
|
359
|
+
return colour.toLowerCase();
|
|
360
|
+
}
|
|
361
|
+
return undefined;
|
|
362
|
+
}
|