@kanorio/sdk 1.1.0 → 1.3.0
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/README.md +70 -1
- package/dist/edge.d.ts +4 -3
- package/dist/edge.js +26 -2
- package/dist/forms.d.ts +44 -2
- package/dist/forms.js +27 -8
- package/dist/generated/delivery.d.ts +640 -24
- package/dist/generated/management.d.ts +2021 -247
- package/dist/http.d.ts +2 -2
- package/dist/http.js +2 -2
- package/dist/image.d.ts +107 -0
- package/dist/image.js +136 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/next.d.ts +72 -0
- package/dist/next.js +72 -0
- package/dist/paginate.d.ts +23 -0
- package/dist/paginate.js +28 -2
- package/dist/seo.d.ts +145 -1
- package/dist/seo.js +126 -1
- package/dist/server-key.d.ts +13 -3
- package/dist/server-key.js +24 -5
- package/dist/shared/api/delivery/cache-tags.d.ts +4 -2
- package/dist/shared/api/delivery/cache-tags.js +5 -1
- package/dist/shared/api/server-key.d.ts +8 -0
- package/dist/shared/api/server-key.js +8 -0
- package/dist/shared/types/forms-logic.js +7 -2
- package/dist/warn-once.d.ts +3 -0
- package/dist/warn-once.js +16 -0
- package/package.json +6 -1
package/dist/http.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* and reads the Deprecation and Sunset headers (RFC 9745, RFC 8594). No business logic.
|
|
4
4
|
*/
|
|
5
5
|
import { type ServerKeyOptions } from "./server-key.js";
|
|
6
|
-
export declare const KANORIO_SDK_VERSION = "1.
|
|
6
|
+
export declare const KANORIO_SDK_VERSION = "1.3.0";
|
|
7
7
|
export declare const SDK_VERSION_HEADER = "x-kanorio-sdk-version";
|
|
8
8
|
export interface ErrorEnvelope {
|
|
9
9
|
error: {
|
|
@@ -29,7 +29,7 @@ export declare class KanorioApiError extends Error {
|
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* The request never reached the API: Kanorio's edge answered with a challenge page (HTTP 403, HTML).
|
|
32
|
-
*
|
|
32
|
+
* API requests are not normally challenged; `message` says what to check (on a server, sending a server key avoids it).
|
|
33
33
|
*/
|
|
34
34
|
export declare class KanorioEdgeChallengeError extends KanorioApiError {
|
|
35
35
|
constructor(message: string);
|
package/dist/http.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* and reads the Deprecation and Sunset headers (RFC 9745, RFC 8594). No business logic.
|
|
4
4
|
*/
|
|
5
5
|
import { EDGE_CHALLENGE_CODE, edgeChallengeMessage, isEdgeChallenge, serverKeyRequest } from "./server-key.js";
|
|
6
|
-
export const KANORIO_SDK_VERSION = "1.
|
|
6
|
+
export const KANORIO_SDK_VERSION = "1.3.0";
|
|
7
7
|
export const SDK_VERSION_HEADER = "x-kanorio-sdk-version";
|
|
8
8
|
export function isErrorEnvelope(value) {
|
|
9
9
|
if (typeof value !== "object" || value === null)
|
|
@@ -36,7 +36,7 @@ export class KanorioApiError extends Error {
|
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
38
|
* The request never reached the API: Kanorio's edge answered with a challenge page (HTTP 403, HTML).
|
|
39
|
-
*
|
|
39
|
+
* API requests are not normally challenged; `message` says what to check (on a server, sending a server key avoids it).
|
|
40
40
|
*/
|
|
41
41
|
export class KanorioEdgeChallengeError extends KanorioApiError {
|
|
42
42
|
constructor(message) {
|
package/dist/image.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Image helpers for a front end you host yourself: turn a field value into an image, and an image URL into the size you need.
|
|
3
|
+
*
|
|
4
|
+
* Kanorio serves images from `img.kanorio.com`. The URL of an image in the `assets` map delivers the original size (its `width` and `height`).
|
|
5
|
+
* Replace the last path segment with a size (`w=800,fit=scale-down`) and the edge resizes the image; any width works.
|
|
6
|
+
* `url` and `srcSet` do that replacement for you and keep the author's crop: a URL that is already cropped or padded to a shape
|
|
7
|
+
* (such as `fieldData.share_image`) stays that shape at the new width. They return other URLs (an external image, a file) unchanged.
|
|
8
|
+
* Give `url` the image from `resolve` rather than its URL and a crop keeps the focal point the owner set in the media library.
|
|
9
|
+
* See "Filtering, fields and expand" in the developer documentation for the asset fields.
|
|
10
|
+
*/
|
|
11
|
+
/** One entry of the `assets` map in a Delivery API response (the fields these helpers read) */
|
|
12
|
+
export interface ImageAssetLike {
|
|
13
|
+
url: string;
|
|
14
|
+
width?: number | null;
|
|
15
|
+
height?: number | null;
|
|
16
|
+
alt?: string | null;
|
|
17
|
+
focus?: ImageFocus | null;
|
|
18
|
+
placeholder?: string | null;
|
|
19
|
+
edgeColor?: string | null;
|
|
20
|
+
}
|
|
21
|
+
/** The `assets` map of a Delivery API response: asset id to asset */
|
|
22
|
+
export type ImageAssetMapLike = Readonly<Record<string, ImageAssetLike | undefined>>;
|
|
23
|
+
export interface ResolvedImage {
|
|
24
|
+
url: string;
|
|
25
|
+
/** Pixel size of the original; null when unknown (an external URL, an SVG) */
|
|
26
|
+
width: number | null;
|
|
27
|
+
height: number | null;
|
|
28
|
+
/** The `alt` you passed (the item's `imageAlt` for this image), otherwise the asset's; null when there is none */
|
|
29
|
+
alt: string | null;
|
|
30
|
+
/**
|
|
31
|
+
* Focal point as 0 to 1 coordinates; null when unset or centered. Pass the resolved image to `url` and a crop keeps it in frame;
|
|
32
|
+
* when CSS crops (`object-fit: cover`), use it for `object-position`
|
|
33
|
+
*/
|
|
34
|
+
focus: ImageFocus | null;
|
|
35
|
+
/** Tiny blurred preview as a data URL, for a blur-up `<img>` or Next.js `blurDataURL`; null when there is none */
|
|
36
|
+
placeholder: string | null;
|
|
37
|
+
/** The solid color shared by all four edges, for the background behind `object-fit: contain`; null when there is none */
|
|
38
|
+
edgeColor: string | null;
|
|
39
|
+
}
|
|
40
|
+
export type ImageFit = "scale-down" | "contain" | "cover" | "crop" | "pad";
|
|
41
|
+
/** A focal point as 0 to 1 coordinates on the original image: `x` from the left edge, `y` from the top */
|
|
42
|
+
export interface ImageFocus {
|
|
43
|
+
x: number;
|
|
44
|
+
y: number;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The image `url` and `srcSet` work on: an image URL from the API, or an image from `resolveImage` (or an `assets` entry),
|
|
48
|
+
* whose `focus` is then kept in frame when the URL crops
|
|
49
|
+
*/
|
|
50
|
+
export type ImageSource = string | {
|
|
51
|
+
url: string;
|
|
52
|
+
focus?: ImageFocus | null;
|
|
53
|
+
};
|
|
54
|
+
export interface ImageUrlOptions {
|
|
55
|
+
/** Output width in pixels */
|
|
56
|
+
width: number;
|
|
57
|
+
/** Output height in pixels. With a height, `fit` defaults to `cover` (crop to fill); without one, to `scale-down` (never enlarge) */
|
|
58
|
+
height?: number;
|
|
59
|
+
fit?: ImageFit;
|
|
60
|
+
/**
|
|
61
|
+
* The point to keep in frame when the image is cropped (a `height` with fit `cover` or `crop`), as 0 to 1 coordinates.
|
|
62
|
+
* Defaults to the `focus` of `src` when you pass a resolved image; `null` crops around the center. Not used for a URL that is
|
|
63
|
+
* already framed (one with a crop or a focal point of its own, such as `share_image`) or when nothing is cropped.
|
|
64
|
+
*/
|
|
65
|
+
focus?: ImageFocus | null;
|
|
66
|
+
}
|
|
67
|
+
export interface ResolveImageOptions {
|
|
68
|
+
/**
|
|
69
|
+
* Alt text written for this image where the item uses it: `item.imageAlt[fieldId]?.[assetId]`.
|
|
70
|
+
* It wins over the asset's alt from the `assets` map; leave it out, or pass null or an empty string, to use the asset's.
|
|
71
|
+
*/
|
|
72
|
+
alt?: string | null;
|
|
73
|
+
}
|
|
74
|
+
export interface ImageSrcSetOptions {
|
|
75
|
+
/** Widths to offer, in pixels. Default: 400, 800, 1200, 1600 */
|
|
76
|
+
widths?: readonly number[];
|
|
77
|
+
}
|
|
78
|
+
/** Widths `srcSet` offers by default: phones up to a desktop article column on a high-density screen */
|
|
79
|
+
export declare const DEFAULT_IMAGE_WIDTHS: readonly number[];
|
|
80
|
+
/** True when the URL is an image Kanorio can resize (not an external URL, not an SVG or a file) */
|
|
81
|
+
export declare function isResizableImageUrl(src: string | null | undefined): src is string;
|
|
82
|
+
/**
|
|
83
|
+
* Resolve an image field value: an asset id is looked up in `assets`, a full URL is returned as is.
|
|
84
|
+
* Pass the item's `imageAlt` for this image as `alt` so the alt written where the item uses it wins over the asset's default:
|
|
85
|
+
* `resolveImage(item.fieldData.cover, assets, { alt: item.imageAlt.cover?.[item.fieldData.cover] })`.
|
|
86
|
+
* Returns `undefined` for an empty value and for an asset id that is not in the map (the file was removed).
|
|
87
|
+
*/
|
|
88
|
+
export declare function resolveImage(value: string | null | undefined, assets: ImageAssetMapLike | null | undefined, options?: ResolveImageOptions): ResolvedImage | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* The URL of an image at a given size. `src` is an image URL from the API (`assets[id].url`, `share_image.url`), or the image
|
|
91
|
+
* `resolveImage` returns: then a crop keeps its focal point in frame (`options.focus` overrides it).
|
|
92
|
+
* The crop in `src` is kept (the area the author framed, the focal point, the padding color); a cropped or padded `src` keeps its shape
|
|
93
|
+
* when you give only a width. A URL Kanorio cannot resize is returned unchanged, so it is safe to call on any image URL.
|
|
94
|
+
*/
|
|
95
|
+
export declare function imageUrl(src: ImageSource, options: ImageUrlOptions): string;
|
|
96
|
+
/**
|
|
97
|
+
* A `srcset` value offering the image at several widths (never enlarged), for `<img srcset>` together with `sizes`.
|
|
98
|
+
* `src` is an image URL or a resolved image, as for `imageUrl`.
|
|
99
|
+
* Returns `undefined` for a URL Kanorio cannot resize: leave `srcset` off and use `src` alone.
|
|
100
|
+
*/
|
|
101
|
+
export declare function imageSrcSet(src: ImageSource, options?: ImageSrcSetOptions): string | undefined;
|
|
102
|
+
export declare const image: {
|
|
103
|
+
readonly resolve: typeof resolveImage;
|
|
104
|
+
readonly url: typeof imageUrl;
|
|
105
|
+
readonly srcSet: typeof imageSrcSet;
|
|
106
|
+
readonly isResizable: typeof isResizableImageUrl;
|
|
107
|
+
};
|
package/dist/image.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Image helpers for a front end you host yourself: turn a field value into an image, and an image URL into the size you need.
|
|
3
|
+
*
|
|
4
|
+
* Kanorio serves images from `img.kanorio.com`. The URL of an image in the `assets` map delivers the original size (its `width` and `height`).
|
|
5
|
+
* Replace the last path segment with a size (`w=800,fit=scale-down`) and the edge resizes the image; any width works.
|
|
6
|
+
* `url` and `srcSet` do that replacement for you and keep the author's crop: a URL that is already cropped or padded to a shape
|
|
7
|
+
* (such as `fieldData.share_image`) stays that shape at the new width. They return other URLs (an external image, a file) unchanged.
|
|
8
|
+
* Give `url` the image from `resolve` rather than its URL and a crop keeps the focal point the owner set in the media library.
|
|
9
|
+
* See "Filtering, fields and expand" in the developer documentation for the asset fields.
|
|
10
|
+
*/
|
|
11
|
+
/** Widths `srcSet` offers by default: phones up to a desktop article column on a high-density screen */
|
|
12
|
+
export const DEFAULT_IMAGE_WIDTHS = [400, 800, 1200, 1600];
|
|
13
|
+
const ABSOLUTE_URL = /^https?:\/\//i;
|
|
14
|
+
/**
|
|
15
|
+
* A Kanorio image URL, split before its last path segment (the variant: `public`, or a size such as `w=800,fit=scale-down`).
|
|
16
|
+
* Both hosts Kanorio has served images from are recognized.
|
|
17
|
+
*/
|
|
18
|
+
const KANORIO_IMAGE_URL = /^(https:\/\/(?:[a-z0-9-]+\.kanorio\.com\/cdn-cgi\/imagedelivery|imagedelivery\.net)\/[^/?#]+\/[^/?#]+)\/([^/?#]+)$/i;
|
|
19
|
+
/** Fits that produce an exact output shape: a URL with one of them (and `w` and `h`) is a cropped or padded image */
|
|
20
|
+
const FIXED_SHAPE_FITS = new Set(["crop", "cover", "pad"]);
|
|
21
|
+
/** The options in the last path segment (`trim=0;120;0;80,w=1200,h=630,fit=crop`); empty for a named variant such as `public` */
|
|
22
|
+
function variantOptions(variant) {
|
|
23
|
+
const options = new Map();
|
|
24
|
+
if (!variant.includes("="))
|
|
25
|
+
return options;
|
|
26
|
+
for (const part of variant.split(",")) {
|
|
27
|
+
const at = part.indexOf("=");
|
|
28
|
+
if (at > 0)
|
|
29
|
+
options.set(part.slice(0, at), part.slice(at + 1));
|
|
30
|
+
}
|
|
31
|
+
return options;
|
|
32
|
+
}
|
|
33
|
+
/** True when the URL is an image Kanorio can resize (not an external URL, not an SVG or a file) */
|
|
34
|
+
export function isResizableImageUrl(src) {
|
|
35
|
+
return typeof src === "string" && KANORIO_IMAGE_URL.test(src.trim());
|
|
36
|
+
}
|
|
37
|
+
function positiveInt(value) {
|
|
38
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.round(value) : null;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Resolve an image field value: an asset id is looked up in `assets`, a full URL is returned as is.
|
|
42
|
+
* Pass the item's `imageAlt` for this image as `alt` so the alt written where the item uses it wins over the asset's default:
|
|
43
|
+
* `resolveImage(item.fieldData.cover, assets, { alt: item.imageAlt.cover?.[item.fieldData.cover] })`.
|
|
44
|
+
* Returns `undefined` for an empty value and for an asset id that is not in the map (the file was removed).
|
|
45
|
+
*/
|
|
46
|
+
export function resolveImage(value, assets, options = {}) {
|
|
47
|
+
const trimmed = typeof value === "string" ? value.trim() : "";
|
|
48
|
+
if (!trimmed)
|
|
49
|
+
return undefined;
|
|
50
|
+
const writtenAlt = typeof options.alt === "string" && options.alt.trim() ? options.alt.trim() : null;
|
|
51
|
+
if (ABSOLUTE_URL.test(trimmed)) {
|
|
52
|
+
return { url: trimmed, width: null, height: null, alt: writtenAlt, focus: null, placeholder: null, edgeColor: null };
|
|
53
|
+
}
|
|
54
|
+
const asset = assets?.[trimmed];
|
|
55
|
+
if (!asset?.url)
|
|
56
|
+
return undefined;
|
|
57
|
+
return {
|
|
58
|
+
url: asset.url,
|
|
59
|
+
width: asset.width ?? null,
|
|
60
|
+
height: asset.height ?? null,
|
|
61
|
+
alt: writtenAlt ?? asset.alt ?? null,
|
|
62
|
+
focus: asset.focus ?? null,
|
|
63
|
+
placeholder: asset.placeholder ?? null,
|
|
64
|
+
edgeColor: asset.edgeColor ?? null,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** `x`×`y` for the `gravity` option, or null for a point that is not usable or is the center (where a crop goes anyway) */
|
|
68
|
+
function gravityOf(focus) {
|
|
69
|
+
if (!focus || !Number.isFinite(focus.x) || !Number.isFinite(focus.y))
|
|
70
|
+
return null;
|
|
71
|
+
const coordinate = (value) => Math.round(Math.min(1, Math.max(0, value)) * 1000) / 1000;
|
|
72
|
+
const x = coordinate(focus.x);
|
|
73
|
+
const y = coordinate(focus.y);
|
|
74
|
+
return x === 0.5 && y === 0.5 ? null : `${x}x${y}`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The URL of an image at a given size. `src` is an image URL from the API (`assets[id].url`, `share_image.url`), or the image
|
|
78
|
+
* `resolveImage` returns: then a crop keeps its focal point in frame (`options.focus` overrides it).
|
|
79
|
+
* The crop in `src` is kept (the area the author framed, the focal point, the padding color); a cropped or padded `src` keeps its shape
|
|
80
|
+
* when you give only a width. A URL Kanorio cannot resize is returned unchanged, so it is safe to call on any image URL.
|
|
81
|
+
*/
|
|
82
|
+
export function imageUrl(src, options) {
|
|
83
|
+
const url = typeof src === "string" ? src : src.url;
|
|
84
|
+
const match = KANORIO_IMAGE_URL.exec(url.trim());
|
|
85
|
+
const width = positiveInt(options.width);
|
|
86
|
+
if (!match?.[1] || !width)
|
|
87
|
+
return url;
|
|
88
|
+
const current = variantOptions(match[2] ?? "");
|
|
89
|
+
let height = positiveInt(options.height);
|
|
90
|
+
let fit = options.fit;
|
|
91
|
+
const currentFit = current.get("fit") ?? "";
|
|
92
|
+
const currentWidth = Number(current.get("w"));
|
|
93
|
+
const currentHeight = Number(current.get("h"));
|
|
94
|
+
const fixedShape = FIXED_SHAPE_FITS.has(currentFit) && currentWidth > 0 && currentHeight > 0;
|
|
95
|
+
if (!height && !fit && fixedShape) {
|
|
96
|
+
height = Math.max(1, Math.round((width * currentHeight) / currentWidth));
|
|
97
|
+
fit = currentFit;
|
|
98
|
+
}
|
|
99
|
+
fit ??= height ? "cover" : "scale-down";
|
|
100
|
+
const trim = current.get("trim");
|
|
101
|
+
// A framed URL (cropped or padded to a shape, cut to an area, or carrying a focal point) keeps the crop it has:
|
|
102
|
+
// the owner's focus is a point on the original, and a centered crop the author framed would move toward it
|
|
103
|
+
const framed = fixedShape || Boolean(trim) || current.has("gravity");
|
|
104
|
+
const focus = options.focus !== undefined ? options.focus : typeof src === "string" ? null : (src.focus ?? null);
|
|
105
|
+
const crops = Boolean(height) && (fit === "cover" || fit === "crop");
|
|
106
|
+
const gravity = current.get("gravity") ?? (crops && !framed ? gravityOf(focus) : null);
|
|
107
|
+
const background = current.get("background");
|
|
108
|
+
const parts = [
|
|
109
|
+
...(trim ? [`trim=${trim}`] : []),
|
|
110
|
+
`w=${width}`,
|
|
111
|
+
...(height ? [`h=${height}`] : []),
|
|
112
|
+
`fit=${fit}`,
|
|
113
|
+
...(gravity ? [`gravity=${gravity}`] : []),
|
|
114
|
+
...(background ? [`background=${background}`] : []),
|
|
115
|
+
];
|
|
116
|
+
return `${match[1]}/${parts.join(",")}`;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* A `srcset` value offering the image at several widths (never enlarged), for `<img srcset>` together with `sizes`.
|
|
120
|
+
* `src` is an image URL or a resolved image, as for `imageUrl`.
|
|
121
|
+
* Returns `undefined` for a URL Kanorio cannot resize: leave `srcset` off and use `src` alone.
|
|
122
|
+
*/
|
|
123
|
+
export function imageSrcSet(src, options = {}) {
|
|
124
|
+
if (!isResizableImageUrl(typeof src === "string" ? src : src.url))
|
|
125
|
+
return undefined;
|
|
126
|
+
const widths = [...new Set((options.widths ?? DEFAULT_IMAGE_WIDTHS).map((w) => positiveInt(w)).filter((w) => w !== null))].sort((a, b) => a - b);
|
|
127
|
+
if (widths.length === 0)
|
|
128
|
+
return undefined;
|
|
129
|
+
return widths.map((width) => `${imageUrl(src, { width })} ${width}w`).join(", ");
|
|
130
|
+
}
|
|
131
|
+
export const image = {
|
|
132
|
+
resolve: resolveImage,
|
|
133
|
+
url: imageUrl,
|
|
134
|
+
srcSet: imageSrcSet,
|
|
135
|
+
isResizable: isResizableImageUrl,
|
|
136
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
export { KanorioApiError, KanorioEdgeChallengeError, KANORIO_SDK_VERSION, SDK_VERSION_HEADER, type DeprecationNotice, type HttpClientOptions, type HttpClient, } from "./http.js";
|
|
2
2
|
export { visitorIpFromRequest, SERVER_KEY_HEADER, SERVER_KEY_VISITOR_IP_HEADER, EDGE_CHALLENGE_CODE, type ServerKeyOptions, type VisitorIpSource, type VisitorIpOptions, } from "./server-key.js";
|
|
3
3
|
export type { TypedClient, TypedRequest, SuccessResponseOf } from "./typed.js";
|
|
4
|
-
export { paginate, listAll, type ListPage } from "./paginate.js";
|
|
4
|
+
export { paginate, paginatePages, listAll, listAllWithAssets, type ListPage, type ListPageWithAssets, type ListWithAssets, } from "./paginate.js";
|
|
5
5
|
export { createManagementClient, DEFAULT_MANAGEMENT_BASE_URL, type ManagementClient, type ManagementClientOptions, type ManagementPaths, } from "./management.js";
|
|
6
6
|
export { createDeliveryClient, deliveryBaseUrl, parseDeliveryCacheTags, frameworkCacheTags, DELIVERY_CACHE_TAGS_HEADER, DELIVERY_PREVIEW_HEADER, type DeliveryClient, type DeliveryClientOptions, type DeliveryPaths, } from "./delivery.js";
|
|
7
7
|
export { createCustomerClient, createCustomerContentClient, customerBaseUrl, readStoredCustomerToken, writeStoredCustomerToken, CUSTOMER_TOKEN_STORAGE_KEY, type CustomerClient, type CustomerClientOptions, type CustomerContentClientLike, type CustomerPaths, type CustomerTokenSource, } from "./customer.js";
|
|
8
8
|
export { buildHostedLoginUrl, completeHostedCustomerLogin, createPkcePair, exchangeHostedLoginCode, refreshCustomerToken, revokeCustomerToken, startHostedCustomerLogin, type CompleteHostedLoginOptions, type HostedLoginGrant, type HostedLoginUrlParams, type StartHostedLoginOptions, } from "./customer-oauth.js";
|
|
9
|
-
export { seo, seoRewrites, fetchDeliveryJsonLd, fetchSitemapEntries, createSeoHelpers, listingRobots, type ListingKey, type ListingState, type SeoRewriteFramework, type NextSeoRewriteRule, type SeoRewritesOptions, type DeliveryJsonLdResult, type SitemapEntry, type SitemapEntriesResult, type SeoHelpers, } from "./seo.js";
|
|
9
|
+
export { seo, seoRewrites, fetchDeliveryJsonLd, fetchSitemapEntries, createSeoHelpers, listingRobots, seoMetadata, shareImageOf, type ItemSeoFields, type PageSeoFields, type SeoItemInput, type SeoPageInput, type SeoInput, type SeoMetadataOptions, type SeoMetadata, type ShareImage, type ShareImageSource, type ListingKey, type ListingState, type SeoRewriteFramework, type NextSeoRewriteRule, type SeoRewritesOptions, type DeliveryJsonLdResult, type SitemapEntry, type SitemapEntriesResult, type SeoHelpers, } from "./seo.js";
|
|
10
|
+
export { image, resolveImage, imageUrl, imageSrcSet, isResizableImageUrl, DEFAULT_IMAGE_WIDTHS, type ImageAssetLike, type ImageAssetMapLike, type ResolvedImage, type ImageFit, type ImageFocus, type ImageSource, type ImageUrlOptions, type ImageSrcSetOptions, type ResolveImageOptions, } from "./image.js";
|
|
10
11
|
export { cacheTag, localizeCacheTag, localizeCacheTags, prefixCacheTags, responseCacheTags, eventCacheTags, DEFAULT_CACHE_TAG_PREFIX, CACHE_TAG_LOCALE_SEPARATOR, DELIVERY_CACHE_TAG_LIMIT, type TranslationCacheScope, } from "./shared/api/delivery/cache-tags.js";
|
|
11
12
|
export { locales, buildAlternates, requestLocaleOf, localizedPath, languageSwitcher, resolveRequestLocale, withLocalePrefix, localeRootPath, localePrefixMap, localePrefixOf, localeOfPrefix, hreflangOf, buildSiteAlternates, sitemapAlternates, type BuildAlternatesOptions, type PageAlternates, type HreflangLink, type SiteLocaleMeta, type LocaleRoute, type RequestLocaleResult, type SiteAlternates, } from "./locales.js";
|
|
12
13
|
export { revalidate, createRevalidateHandler, verifyWebhookSignature, computeWebhookSignature, buildWebhookSignatureHeader, parseWebhookSignatureHeader, extractWebhookCacheTags, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_DELIVERY_HEADER, WEBHOOK_EVENT_HEADER, DEFAULT_WEBHOOK_TOLERANCE_SECONDS, type VerifyWebhookSignatureOptions, type ParsedWebhookSignature, type WebhookEnvelope, type RevalidateHandlerOptions, } from "./revalidate.js";
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
export { KanorioApiError, KanorioEdgeChallengeError, KANORIO_SDK_VERSION, SDK_VERSION_HEADER, } from "./http.js";
|
|
2
2
|
export { visitorIpFromRequest, SERVER_KEY_HEADER, SERVER_KEY_VISITOR_IP_HEADER, EDGE_CHALLENGE_CODE, } from "./server-key.js";
|
|
3
|
-
export { paginate, listAll } from "./paginate.js";
|
|
3
|
+
export { paginate, paginatePages, listAll, listAllWithAssets, } from "./paginate.js";
|
|
4
4
|
export { createManagementClient, DEFAULT_MANAGEMENT_BASE_URL, } from "./management.js";
|
|
5
5
|
export { createDeliveryClient, deliveryBaseUrl, parseDeliveryCacheTags, frameworkCacheTags, DELIVERY_CACHE_TAGS_HEADER, DELIVERY_PREVIEW_HEADER, } from "./delivery.js";
|
|
6
6
|
export { createCustomerClient, createCustomerContentClient, customerBaseUrl, readStoredCustomerToken, writeStoredCustomerToken, CUSTOMER_TOKEN_STORAGE_KEY, } from "./customer.js";
|
|
7
7
|
export { buildHostedLoginUrl, completeHostedCustomerLogin, createPkcePair, exchangeHostedLoginCode, refreshCustomerToken, revokeCustomerToken, startHostedCustomerLogin, } from "./customer-oauth.js";
|
|
8
|
-
export { seo, seoRewrites, fetchDeliveryJsonLd, fetchSitemapEntries, createSeoHelpers, listingRobots, } from "./seo.js";
|
|
8
|
+
export { seo, seoRewrites, fetchDeliveryJsonLd, fetchSitemapEntries, createSeoHelpers, listingRobots, seoMetadata, shareImageOf, } from "./seo.js";
|
|
9
|
+
export { image, resolveImage, imageUrl, imageSrcSet, isResizableImageUrl, DEFAULT_IMAGE_WIDTHS, } from "./image.js";
|
|
9
10
|
export { cacheTag, localizeCacheTag, localizeCacheTags, prefixCacheTags, responseCacheTags, eventCacheTags, DEFAULT_CACHE_TAG_PREFIX, CACHE_TAG_LOCALE_SEPARATOR, DELIVERY_CACHE_TAG_LIMIT, } from "./shared/api/delivery/cache-tags.js";
|
|
10
11
|
export { locales, buildAlternates, requestLocaleOf, localizedPath, languageSwitcher, resolveRequestLocale, withLocalePrefix, localeRootPath, localePrefixMap, localePrefixOf, localeOfPrefix, hreflangOf, buildSiteAlternates, sitemapAlternates, } from "./locales.js";
|
|
11
12
|
export { revalidate, createRevalidateHandler, verifyWebhookSignature, computeWebhookSignature, buildWebhookSignatureHeader, parseWebhookSignatureHeader, extractWebhookCacheTags, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_DELIVERY_HEADER, WEBHOOK_EVENT_HEADER, DEFAULT_WEBHOOK_TOLERANCE_SECONDS, } from "./revalidate.js";
|
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js adapters. They have no dependency on `next`: the return values match the shapes Next.js expects
|
|
3
|
+
* (`Metadata` from `generateMetadata`, an image `loader`, `images.remotePatterns`), so they work with the Next.js version you have.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* // app/blog/[slug]/page.tsx
|
|
7
|
+
* import { seoMetadata } from "@kanorio/sdk";
|
|
8
|
+
* import { toNextMetadata } from "@kanorio/sdk/next";
|
|
9
|
+
*
|
|
10
|
+
* export async function generateMetadata({ params }) {
|
|
11
|
+
* const data = await loadPost((await params).slug);
|
|
12
|
+
* return data ? toNextMetadata(seoMetadata(data.item, { siteUrl: SITE_URL, site })) : {};
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
import type { SeoMetadata } from "./seo.js";
|
|
17
|
+
/** The part of Next.js `Metadata` these helpers fill in. Assignable to `Metadata` from `next` */
|
|
18
|
+
export interface NextMetadataLike {
|
|
19
|
+
title?: string;
|
|
20
|
+
description?: string;
|
|
21
|
+
keywords?: string[];
|
|
22
|
+
alternates?: {
|
|
23
|
+
canonical?: string;
|
|
24
|
+
languages?: Record<string, string>;
|
|
25
|
+
};
|
|
26
|
+
robots: {
|
|
27
|
+
index: boolean;
|
|
28
|
+
follow: boolean;
|
|
29
|
+
};
|
|
30
|
+
openGraph: {
|
|
31
|
+
title?: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
url?: string;
|
|
34
|
+
images?: Array<{
|
|
35
|
+
url: string;
|
|
36
|
+
alt?: string;
|
|
37
|
+
width?: number;
|
|
38
|
+
height?: number;
|
|
39
|
+
}>;
|
|
40
|
+
};
|
|
41
|
+
twitter: {
|
|
42
|
+
card: "summary" | "summary_large_image";
|
|
43
|
+
title?: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
images?: string[];
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Turn `seoMetadata(...)` into the object `generateMetadata` returns. The title has no site name: add it with `title.template` in the root layout,
|
|
50
|
+
* and set `metadataBase` there when `canonical` is a path.
|
|
51
|
+
*/
|
|
52
|
+
export declare function toNextMetadata(metadata: SeoMetadata): NextMetadataLike;
|
|
53
|
+
/**
|
|
54
|
+
* An image loader for `next/image`: Kanorio's edge resizes the image, so Next.js does not have to.
|
|
55
|
+
* Use it per image (`<Image loader={kanorioImageLoader} ... />`) or for the whole app (`images.loaderFile` pointing at a file that
|
|
56
|
+
* default-exports it). Images Kanorio cannot resize (external URLs, SVG) are returned unchanged.
|
|
57
|
+
*/
|
|
58
|
+
export declare function kanorioImageLoader({ src, width }: {
|
|
59
|
+
src: string;
|
|
60
|
+
width: number;
|
|
61
|
+
quality?: number;
|
|
62
|
+
}): string;
|
|
63
|
+
/**
|
|
64
|
+
* `images.remotePatterns` entries for Kanorio's image host, for `next.config` when you use `next/image` without the loader:
|
|
65
|
+
* `images: { remotePatterns: kanorioRemotePatterns }`. It is a plain array because Next.js types `remotePatterns` as one;
|
|
66
|
+
* to add hosts of your own, spread it into a new array (`[...kanorioRemotePatterns, ...yours]`) rather than pushing to it.
|
|
67
|
+
*/
|
|
68
|
+
export declare const kanorioRemotePatterns: Array<{
|
|
69
|
+
protocol: "https";
|
|
70
|
+
hostname: string;
|
|
71
|
+
pathname: string;
|
|
72
|
+
}>;
|
package/dist/next.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js adapters. They have no dependency on `next`: the return values match the shapes Next.js expects
|
|
3
|
+
* (`Metadata` from `generateMetadata`, an image `loader`, `images.remotePatterns`), so they work with the Next.js version you have.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* // app/blog/[slug]/page.tsx
|
|
7
|
+
* import { seoMetadata } from "@kanorio/sdk";
|
|
8
|
+
* import { toNextMetadata } from "@kanorio/sdk/next";
|
|
9
|
+
*
|
|
10
|
+
* export async function generateMetadata({ params }) {
|
|
11
|
+
* const data = await loadPost((await params).slug);
|
|
12
|
+
* return data ? toNextMetadata(seoMetadata(data.item, { siteUrl: SITE_URL, site })) : {};
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
import { imageUrl } from "./image.js";
|
|
17
|
+
/**
|
|
18
|
+
* Turn `seoMetadata(...)` into the object `generateMetadata` returns. The title has no site name: add it with `title.template` in the root layout,
|
|
19
|
+
* and set `metadataBase` there when `canonical` is a path.
|
|
20
|
+
*/
|
|
21
|
+
export function toNextMetadata(metadata) {
|
|
22
|
+
const { openGraph } = metadata;
|
|
23
|
+
const hasLanguages = Object.keys(metadata.languages).length > 0;
|
|
24
|
+
const image = openGraph.image;
|
|
25
|
+
return {
|
|
26
|
+
...(metadata.title ? { title: metadata.title } : {}),
|
|
27
|
+
...(metadata.description ? { description: metadata.description } : {}),
|
|
28
|
+
...(metadata.keywords ? { keywords: metadata.keywords } : {}),
|
|
29
|
+
...(metadata.canonical || hasLanguages
|
|
30
|
+
? { alternates: { ...(metadata.canonical ? { canonical: metadata.canonical } : {}), ...(hasLanguages ? { languages: metadata.languages } : {}) } }
|
|
31
|
+
: {}),
|
|
32
|
+
robots: { index: metadata.robots.index, follow: metadata.robots.follow },
|
|
33
|
+
openGraph: {
|
|
34
|
+
...(openGraph.title ? { title: openGraph.title } : {}),
|
|
35
|
+
...(openGraph.description ? { description: openGraph.description } : {}),
|
|
36
|
+
...(metadata.canonical ? { url: metadata.canonical } : {}),
|
|
37
|
+
...(image
|
|
38
|
+
? {
|
|
39
|
+
images: [
|
|
40
|
+
{
|
|
41
|
+
url: image.url,
|
|
42
|
+
...(image.alt ? { alt: image.alt } : {}),
|
|
43
|
+
...(image.width && image.height ? { width: image.width, height: image.height } : {}),
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
}
|
|
47
|
+
: {}),
|
|
48
|
+
},
|
|
49
|
+
twitter: {
|
|
50
|
+
card: metadata.twitterCard,
|
|
51
|
+
...(openGraph.title ? { title: openGraph.title } : {}),
|
|
52
|
+
...(openGraph.description ? { description: openGraph.description } : {}),
|
|
53
|
+
...(image ? { images: [image.url] } : {}),
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* An image loader for `next/image`: Kanorio's edge resizes the image, so Next.js does not have to.
|
|
59
|
+
* Use it per image (`<Image loader={kanorioImageLoader} ... />`) or for the whole app (`images.loaderFile` pointing at a file that
|
|
60
|
+
* default-exports it). Images Kanorio cannot resize (external URLs, SVG) are returned unchanged.
|
|
61
|
+
*/
|
|
62
|
+
export function kanorioImageLoader({ src, width }) {
|
|
63
|
+
return imageUrl(src, { width });
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* `images.remotePatterns` entries for Kanorio's image host, for `next.config` when you use `next/image` without the loader:
|
|
67
|
+
* `images: { remotePatterns: kanorioRemotePatterns }`. It is a plain array because Next.js types `remotePatterns` as one;
|
|
68
|
+
* to add hosts of your own, spread it into a new array (`[...kanorioRemotePatterns, ...yours]`) rather than pushing to it.
|
|
69
|
+
*/
|
|
70
|
+
export const kanorioRemotePatterns = [
|
|
71
|
+
{ protocol: "https", hostname: "img.kanorio.com", pathname: "/**" },
|
|
72
|
+
];
|
package/dist/paginate.d.ts
CHANGED
|
@@ -7,12 +7,35 @@
|
|
|
7
7
|
* console.log(post.slug);
|
|
8
8
|
* }
|
|
9
9
|
* ```
|
|
10
|
+
*
|
|
11
|
+
* Content Delivery API lists also carry a top-level `assets` map that resolves the media on that page only.
|
|
12
|
+
* `paginatePages` yields each page whole, and `listAllWithAssets` returns every item together with every page's `assets` merged.
|
|
10
13
|
*/
|
|
11
14
|
export interface ListPage<T> {
|
|
12
15
|
data: T[];
|
|
13
16
|
hasMore: boolean;
|
|
14
17
|
nextCursor: string | null;
|
|
15
18
|
}
|
|
19
|
+
/** A list page that resolves its media in a top-level `assets` map (Content Delivery API lists) */
|
|
20
|
+
export interface ListPageWithAssets<T, A = unknown> extends ListPage<T> {
|
|
21
|
+
assets?: Readonly<Record<string, A>> | null;
|
|
22
|
+
}
|
|
23
|
+
/** Every item of a list, with the `assets` of all its pages in one map */
|
|
24
|
+
export interface ListWithAssets<T, A = unknown> {
|
|
25
|
+
data: T[];
|
|
26
|
+
assets: Record<string, A>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Follows `nextCursor` and yields each page as the API returned it (`data`, `assets`, `access`, ...).
|
|
30
|
+
* Use it when you need more than the items, such as the `assets` map that resolves the images on that page.
|
|
31
|
+
*/
|
|
32
|
+
export declare function paginatePages<P extends ListPage<unknown>>(fetchPage: (cursor: string | undefined) => Promise<P>): AsyncGenerator<P, void, undefined>;
|
|
33
|
+
/** Follows `nextCursor` and yields item by item. Each page's `assets` is dropped: use {@link paginatePages} or {@link listAllWithAssets} when you resolve images */
|
|
16
34
|
export declare function paginate<T>(fetchPage: (cursor: string | undefined) => Promise<ListPage<T>>): AsyncGenerator<T, void, undefined>;
|
|
17
35
|
/** Fetches every page and returns an array (for small lists; use {@link paginate} to process large ones item by item) */
|
|
18
36
|
export declare function listAll<T>(fetchPage: (cursor: string | undefined) => Promise<ListPage<T>>): Promise<T[]>;
|
|
37
|
+
/**
|
|
38
|
+
* Fetches every page and returns the items with the `assets` of every page merged into one map, so an image on any item
|
|
39
|
+
* resolves from the result (`image.resolve(item.fieldData.cover, result.assets)`). For small lists, as with {@link listAll}.
|
|
40
|
+
*/
|
|
41
|
+
export declare function listAllWithAssets<T, A = unknown>(fetchPage: (cursor: string | undefined) => Promise<ListPageWithAssets<T, A>>): Promise<ListWithAssets<T, A>>;
|
package/dist/paginate.js
CHANGED
|
@@ -7,17 +7,29 @@
|
|
|
7
7
|
* console.log(post.slug);
|
|
8
8
|
* }
|
|
9
9
|
* ```
|
|
10
|
+
*
|
|
11
|
+
* Content Delivery API lists also carry a top-level `assets` map that resolves the media on that page only.
|
|
12
|
+
* `paginatePages` yields each page whole, and `listAllWithAssets` returns every item together with every page's `assets` merged.
|
|
10
13
|
*/
|
|
11
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Follows `nextCursor` and yields each page as the API returned it (`data`, `assets`, `access`, ...).
|
|
16
|
+
* Use it when you need more than the items, such as the `assets` map that resolves the images on that page.
|
|
17
|
+
*/
|
|
18
|
+
export async function* paginatePages(fetchPage) {
|
|
12
19
|
let cursor;
|
|
13
20
|
for (;;) {
|
|
14
21
|
const page = await fetchPage(cursor);
|
|
15
|
-
yield
|
|
22
|
+
yield page;
|
|
16
23
|
if (!page.hasMore || !page.nextCursor)
|
|
17
24
|
return;
|
|
18
25
|
cursor = page.nextCursor;
|
|
19
26
|
}
|
|
20
27
|
}
|
|
28
|
+
/** Follows `nextCursor` and yields item by item. Each page's `assets` is dropped: use {@link paginatePages} or {@link listAllWithAssets} when you resolve images */
|
|
29
|
+
export async function* paginate(fetchPage) {
|
|
30
|
+
for await (const page of paginatePages(fetchPage))
|
|
31
|
+
yield* page.data;
|
|
32
|
+
}
|
|
21
33
|
/** Fetches every page and returns an array (for small lists; use {@link paginate} to process large ones item by item) */
|
|
22
34
|
export async function listAll(fetchPage) {
|
|
23
35
|
const out = [];
|
|
@@ -25,3 +37,17 @@ export async function listAll(fetchPage) {
|
|
|
25
37
|
out.push(item);
|
|
26
38
|
return out;
|
|
27
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Fetches every page and returns the items with the `assets` of every page merged into one map, so an image on any item
|
|
42
|
+
* resolves from the result (`image.resolve(item.fieldData.cover, result.assets)`). For small lists, as with {@link listAll}.
|
|
43
|
+
*/
|
|
44
|
+
export async function listAllWithAssets(fetchPage) {
|
|
45
|
+
const data = [];
|
|
46
|
+
const assets = {};
|
|
47
|
+
for await (const page of paginatePages(fetchPage)) {
|
|
48
|
+
data.push(...page.data);
|
|
49
|
+
if (page.assets)
|
|
50
|
+
Object.assign(assets, page.assets);
|
|
51
|
+
}
|
|
52
|
+
return { data, assets };
|
|
53
|
+
}
|