@kanorio/sdk 1.0.0 → 1.2.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 +41 -2
- package/dist/customer-oauth.d.ts +21 -21
- package/dist/customer-oauth.js +12 -12
- package/dist/customer.d.ts +22 -22
- package/dist/customer.js +20 -11
- package/dist/delivery.d.ts +9 -9
- package/dist/delivery.js +6 -6
- package/dist/edge.d.ts +25 -21
- package/dist/edge.js +36 -20
- package/dist/forms.d.ts +33 -14
- package/dist/forms.js +20 -14
- package/dist/generated/delivery.d.ts +564 -13
- package/dist/generated/management.d.ts +790 -81
- package/dist/http.d.ts +13 -13
- package/dist/http.js +8 -8
- package/dist/image.d.ts +77 -0
- package/dist/image.js +78 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -3
- package/dist/locales.d.ts +22 -22
- package/dist/locales.js +12 -12
- package/dist/management.d.ts +5 -5
- package/dist/management.js +3 -3
- package/dist/next.d.ts +66 -0
- package/dist/next.js +58 -0
- package/dist/paginate.d.ts +3 -3
- package/dist/paginate.js +3 -3
- package/dist/preview.d.ts +13 -13
- package/dist/preview.js +11 -11
- package/dist/protection.d.ts +18 -18
- package/dist/protection.js +11 -11
- package/dist/revalidate.d.ts +22 -22
- package/dist/revalidate.js +14 -14
- package/dist/seo.d.ts +137 -21
- package/dist/seo.js +113 -21
- package/dist/server-key.d.ts +2 -2
- package/dist/server-key.js +2 -2
- package/dist/shared/api/delivery/cache-tags.d.ts +41 -39
- package/dist/shared/api/delivery/cache-tags.js +36 -34
- package/dist/shared/api/server-key.d.ts +32 -28
- package/dist/shared/api/server-key.js +41 -33
- package/dist/shared/business-registry/attribution.d.ts +5 -5
- package/dist/shared/business-registry/attribution.js +4 -4
- package/dist/shared/business-registry/define.d.ts +1 -1
- package/dist/shared/business-registry/define.js +1 -1
- package/dist/shared/business-registry/formats/eu-vat.js +4 -4
- package/dist/shared/business-registry/formats/hk-br.d.ts +2 -2
- package/dist/shared/business-registry/formats/hk-br.js +4 -4
- package/dist/shared/business-registry/formats/jp-trn.d.ts +3 -3
- package/dist/shared/business-registry/formats/jp-trn.js +4 -4
- package/dist/shared/business-registry/formats/kr-brn.d.ts +2 -2
- package/dist/shared/business-registry/formats/kr-brn.js +3 -3
- package/dist/shared/business-registry/formats/my.d.ts +3 -3
- package/dist/shared/business-registry/formats/my.js +4 -4
- package/dist/shared/business-registry/formats/sg-uen.js +4 -4
- package/dist/shared/business-registry/formats/tw-vat.d.ts +5 -5
- package/dist/shared/business-registry/formats/tw-vat.js +6 -6
- package/dist/shared/business-registry/index.js +1 -1
- package/dist/shared/business-registry/lookup-wire.d.ts +9 -9
- package/dist/shared/business-registry/lookup-wire.js +4 -4
- package/dist/shared/business-registry/normalize.d.ts +3 -3
- package/dist/shared/business-registry/normalize.js +4 -4
- package/dist/shared/business-registry/registry.d.ts +9 -9
- package/dist/shared/business-registry/registry.js +13 -13
- package/dist/shared/business-registry/types.d.ts +20 -20
- package/dist/shared/business-registry/types.js +6 -6
- package/dist/shared/localization/locale-path.d.ts +18 -18
- package/dist/shared/localization/locale-path.js +18 -18
- package/dist/shared/localization/request-locale.d.ts +16 -16
- package/dist/shared/localization/request-locale.js +13 -13
- package/dist/shared/seo/alternates.d.ts +26 -26
- package/dist/shared/seo/alternates.js +18 -18
- package/dist/shared/types/forms-logic.d.ts +28 -28
- package/dist/shared/types/forms-logic.js +27 -27
- package/dist/shared/types/number-input.d.ts +2 -2
- package/dist/shared/types/number-input.js +3 -3
- package/dist/shared/utils/redirect-matching.d.ts +33 -24
- package/dist/shared/utils/redirect-matching.js +73 -40
- package/dist/typed.d.ts +5 -5
- package/dist/typed.js +3 -3
- package/package.json +15 -10
package/dist/http.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* The fetch core shared by the three clients: builds the URL, sends headers, parses JSON, turns the error envelope into `KanorioApiError`
|
|
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.2.0";
|
|
7
7
|
export declare const SDK_VERSION_HEADER = "x-kanorio-sdk-version";
|
|
8
8
|
export interface ErrorEnvelope {
|
|
9
9
|
error: {
|
|
@@ -17,14 +17,14 @@ export declare class KanorioApiError extends Error {
|
|
|
17
17
|
readonly code: string;
|
|
18
18
|
readonly status: number;
|
|
19
19
|
readonly details?: unknown | undefined;
|
|
20
|
-
/** 429
|
|
20
|
+
/** Suggested wait in seconds on 429 and 503 (the `Retry-After` header or `details.retryAfter`) */
|
|
21
21
|
readonly retryAfter?: number | undefined;
|
|
22
|
-
/**
|
|
22
|
+
/** Release time attached to an access decision (`details.availableAt` of `NOT_RELEASED`) */
|
|
23
23
|
readonly availableAt?: string | undefined;
|
|
24
24
|
constructor(code: string, status: number, message: string, details?: unknown | undefined,
|
|
25
|
-
/** 429
|
|
25
|
+
/** Suggested wait in seconds on 429 and 503 (the `Retry-After` header or `details.retryAfter`) */
|
|
26
26
|
retryAfter?: number | undefined,
|
|
27
|
-
/**
|
|
27
|
+
/** Release time attached to an access decision (`details.availableAt` of `NOT_RELEASED`) */
|
|
28
28
|
availableAt?: string | undefined);
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
@@ -48,15 +48,15 @@ export type Query = Record<string, QueryValue | QueryValue[]>;
|
|
|
48
48
|
export declare function buildUrl(baseUrl: string, path: string, query?: Query): string;
|
|
49
49
|
export interface HttpClientOptions extends ServerKeyOptions {
|
|
50
50
|
baseUrl: string;
|
|
51
|
-
/**
|
|
51
|
+
/** Uses the global `fetch` when omitted */
|
|
52
52
|
fetch?: typeof fetch;
|
|
53
|
-
/**
|
|
53
|
+
/** Headers added to every request (may be a function, for dynamic tokens) */
|
|
54
54
|
headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
|
|
55
|
-
/**
|
|
55
|
+
/** Timeout in milliseconds; none when omitted */
|
|
56
56
|
timeoutMs?: number;
|
|
57
|
-
/**
|
|
57
|
+
/** Called when the server announces a deprecation (`console.warn` once by default) */
|
|
58
58
|
onDeprecation?: (notice: DeprecationNotice) => void;
|
|
59
|
-
/**
|
|
59
|
+
/** User agent string (Node); ignored in browsers */
|
|
60
60
|
userAgent?: string;
|
|
61
61
|
}
|
|
62
62
|
export interface RequestOptions {
|
|
@@ -67,7 +67,7 @@ export interface RequestOptions {
|
|
|
67
67
|
}
|
|
68
68
|
export interface HttpClient {
|
|
69
69
|
request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
|
|
70
|
-
/**
|
|
70
|
+
/** The full response (headers and status), without throwing */
|
|
71
71
|
raw(method: string, path: string, options?: RequestOptions): Promise<Response>;
|
|
72
72
|
}
|
|
73
73
|
export declare function createHttpClient(options: HttpClientOptions): HttpClient;
|
package/dist/http.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* The fetch core shared by the three clients: builds the URL, sends headers, parses JSON, turns the error envelope into `KanorioApiError`
|
|
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.2.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)
|
|
@@ -21,9 +21,9 @@ export class KanorioApiError extends Error {
|
|
|
21
21
|
retryAfter;
|
|
22
22
|
availableAt;
|
|
23
23
|
constructor(code, status, message, details,
|
|
24
|
-
/** 429
|
|
24
|
+
/** Suggested wait in seconds on 429 and 503 (the `Retry-After` header or `details.retryAfter`) */
|
|
25
25
|
retryAfter,
|
|
26
|
-
/**
|
|
26
|
+
/** Release time attached to an access decision (`details.availableAt` of `NOT_RELEASED`) */
|
|
27
27
|
availableAt) {
|
|
28
28
|
super(message);
|
|
29
29
|
this.code = code;
|
|
@@ -48,7 +48,7 @@ export function toApiError(status, body, headers) {
|
|
|
48
48
|
const retryHeader = headers.get("retry-after");
|
|
49
49
|
const retryAfter = retryHeader ? Number.parseInt(retryHeader, 10) : undefined;
|
|
50
50
|
if (isErrorEnvelope(body)) {
|
|
51
|
-
//
|
|
51
|
+
// The three APIs share one error envelope: extra fields are in details
|
|
52
52
|
const details = body.error.details;
|
|
53
53
|
const detailRetry = typeof details?.retryAfter === "number" ? details.retryAfter : undefined;
|
|
54
54
|
const availableAt = typeof details?.availableAt === "string" ? details.availableAt : undefined;
|
|
@@ -110,7 +110,7 @@ export function createHttpClient(options) {
|
|
|
110
110
|
body = JSON.stringify(req.body);
|
|
111
111
|
}
|
|
112
112
|
const url = buildUrl(options.baseUrl, path, req.query);
|
|
113
|
-
//
|
|
113
|
+
// Server key: server side only, https only, redirects not followed (custom headers are not stripped on cross-origin redirects)
|
|
114
114
|
const viaServerKey = await serverKeyRequest(options, url);
|
|
115
115
|
if (viaServerKey)
|
|
116
116
|
for (const [name, value] of Object.entries(viaServerKey.headers))
|
|
@@ -135,7 +135,7 @@ export function createHttpClient(options) {
|
|
|
135
135
|
}
|
|
136
136
|
async function request(method, path, req = {}) {
|
|
137
137
|
const res = await raw(method, path, req);
|
|
138
|
-
//
|
|
138
|
+
// The edge's challenge page is not an API response: return an error that says what to do next instead of "HTTP 403" and a block of HTML
|
|
139
139
|
if (isEdgeChallenge(res))
|
|
140
140
|
throw new KanorioEdgeChallengeError(edgeChallengeMessage(Boolean(options.serverKey)));
|
|
141
141
|
const notice = parseDeprecation(res.headers, method, path);
|
package/dist/image.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
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 ends in `/public` (the original).
|
|
5
|
+
* Replace that last 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. They return other URLs (an external image, a file) unchanged.
|
|
7
|
+
* See "Filtering, fields and expand" in the developer documentation for the asset fields.
|
|
8
|
+
*/
|
|
9
|
+
/** One entry of the `assets` map in a Delivery API response (the fields these helpers read) */
|
|
10
|
+
export interface ImageAssetLike {
|
|
11
|
+
url: string;
|
|
12
|
+
width?: number | null;
|
|
13
|
+
height?: number | null;
|
|
14
|
+
alt?: string | null;
|
|
15
|
+
focus?: {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
} | null;
|
|
19
|
+
placeholder?: string | null;
|
|
20
|
+
edgeColor?: string | null;
|
|
21
|
+
}
|
|
22
|
+
/** The `assets` map of a Delivery API response: asset id to asset */
|
|
23
|
+
export type ImageAssetMapLike = Readonly<Record<string, ImageAssetLike | undefined>>;
|
|
24
|
+
export interface ResolvedImage {
|
|
25
|
+
url: string;
|
|
26
|
+
/** Pixel size of the original; null when unknown (an external URL, an SVG) */
|
|
27
|
+
width: number | null;
|
|
28
|
+
height: number | null;
|
|
29
|
+
/** Alt text written where the image is used, otherwise the asset's own; null when there is none */
|
|
30
|
+
alt: string | null;
|
|
31
|
+
/** Focal point as 0 to 1 coordinates, for `object-position` when you crop; null when unset or centered */
|
|
32
|
+
focus: {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
} | null;
|
|
36
|
+
/** Tiny blurred preview as a data URL, for a blur-up `<img>` or Next.js `blurDataURL`; null when there is none */
|
|
37
|
+
placeholder: string | null;
|
|
38
|
+
/** The solid color shared by all four edges, for the background behind `object-fit: contain`; null when there is none */
|
|
39
|
+
edgeColor: string | null;
|
|
40
|
+
}
|
|
41
|
+
export type ImageFit = "scale-down" | "contain" | "cover" | "crop" | "pad";
|
|
42
|
+
export interface ImageUrlOptions {
|
|
43
|
+
/** Output width in pixels */
|
|
44
|
+
width: number;
|
|
45
|
+
/** Output height in pixels. With a height, `fit` defaults to `cover` (crop to fill); without one, to `scale-down` (never enlarge) */
|
|
46
|
+
height?: number;
|
|
47
|
+
fit?: ImageFit;
|
|
48
|
+
}
|
|
49
|
+
export interface ImageSrcSetOptions {
|
|
50
|
+
/** Widths to offer, in pixels. Default: 400, 800, 1200, 1600 */
|
|
51
|
+
widths?: readonly number[];
|
|
52
|
+
}
|
|
53
|
+
/** Widths `srcSet` offers by default: phones up to a desktop article column on a high-density screen */
|
|
54
|
+
export declare const DEFAULT_IMAGE_WIDTHS: readonly number[];
|
|
55
|
+
/** True when the URL is an image Kanorio can resize (not an external URL, not an SVG or a file) */
|
|
56
|
+
export declare function isResizableImageUrl(src: string | null | undefined): src is string;
|
|
57
|
+
/**
|
|
58
|
+
* Resolve an image field value: an asset id is looked up in `assets`, a full URL is returned as is.
|
|
59
|
+
* Returns `undefined` for an empty value and for an asset id that is not in the map (the file was removed).
|
|
60
|
+
*/
|
|
61
|
+
export declare function resolveImage(value: string | null | undefined, assets: ImageAssetMapLike | null | undefined): ResolvedImage | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* The URL of an image at a given size. `src` is an image URL from the API (`assets[id].url`, or `resolveImage(...).url`).
|
|
64
|
+
* A URL Kanorio cannot resize is returned unchanged, so it is safe to call on any image URL.
|
|
65
|
+
*/
|
|
66
|
+
export declare function imageUrl(src: string, options: ImageUrlOptions): string;
|
|
67
|
+
/**
|
|
68
|
+
* A `srcset` value offering the image at several widths (never enlarged), for `<img srcset>` together with `sizes`.
|
|
69
|
+
* Returns `undefined` for a URL Kanorio cannot resize: leave `srcset` off and use `src` alone.
|
|
70
|
+
*/
|
|
71
|
+
export declare function imageSrcSet(src: string, options?: ImageSrcSetOptions): string | undefined;
|
|
72
|
+
export declare const image: {
|
|
73
|
+
readonly resolve: typeof resolveImage;
|
|
74
|
+
readonly url: typeof imageUrl;
|
|
75
|
+
readonly srcSet: typeof imageSrcSet;
|
|
76
|
+
readonly isResizable: typeof isResizableImageUrl;
|
|
77
|
+
};
|
package/dist/image.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
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 ends in `/public` (the original).
|
|
5
|
+
* Replace that last 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. They return other URLs (an external image, a file) unchanged.
|
|
7
|
+
* See "Filtering, fields and expand" in the developer documentation for the asset fields.
|
|
8
|
+
*/
|
|
9
|
+
/** Widths `srcSet` offers by default: phones up to a desktop article column on a high-density screen */
|
|
10
|
+
export const DEFAULT_IMAGE_WIDTHS = [400, 800, 1200, 1600];
|
|
11
|
+
const ABSOLUTE_URL = /^https?:\/\//i;
|
|
12
|
+
/**
|
|
13
|
+
* A Kanorio image URL, split before its last path segment (the variant: `public`, or a size such as `w=800,fit=scale-down`).
|
|
14
|
+
* Both hosts Kanorio has served images from are recognized.
|
|
15
|
+
*/
|
|
16
|
+
const KANORIO_IMAGE_URL = /^(https:\/\/(?:[a-z0-9-]+\.kanorio\.com\/cdn-cgi\/imagedelivery|imagedelivery\.net)\/[^/?#]+\/[^/?#]+)\/[^/?#]+$/i;
|
|
17
|
+
/** True when the URL is an image Kanorio can resize (not an external URL, not an SVG or a file) */
|
|
18
|
+
export function isResizableImageUrl(src) {
|
|
19
|
+
return typeof src === "string" && KANORIO_IMAGE_URL.test(src.trim());
|
|
20
|
+
}
|
|
21
|
+
function positiveInt(value) {
|
|
22
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.round(value) : null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Resolve an image field value: an asset id is looked up in `assets`, a full URL is returned as is.
|
|
26
|
+
* Returns `undefined` for an empty value and for an asset id that is not in the map (the file was removed).
|
|
27
|
+
*/
|
|
28
|
+
export function resolveImage(value, assets) {
|
|
29
|
+
const trimmed = typeof value === "string" ? value.trim() : "";
|
|
30
|
+
if (!trimmed)
|
|
31
|
+
return undefined;
|
|
32
|
+
if (ABSOLUTE_URL.test(trimmed)) {
|
|
33
|
+
return { url: trimmed, width: null, height: null, alt: null, focus: null, placeholder: null, edgeColor: null };
|
|
34
|
+
}
|
|
35
|
+
const asset = assets?.[trimmed];
|
|
36
|
+
if (!asset?.url)
|
|
37
|
+
return undefined;
|
|
38
|
+
return {
|
|
39
|
+
url: asset.url,
|
|
40
|
+
width: asset.width ?? null,
|
|
41
|
+
height: asset.height ?? null,
|
|
42
|
+
alt: asset.alt ?? null,
|
|
43
|
+
focus: asset.focus ?? null,
|
|
44
|
+
placeholder: asset.placeholder ?? null,
|
|
45
|
+
edgeColor: asset.edgeColor ?? null,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The URL of an image at a given size. `src` is an image URL from the API (`assets[id].url`, or `resolveImage(...).url`).
|
|
50
|
+
* A URL Kanorio cannot resize is returned unchanged, so it is safe to call on any image URL.
|
|
51
|
+
*/
|
|
52
|
+
export function imageUrl(src, options) {
|
|
53
|
+
const match = KANORIO_IMAGE_URL.exec(src.trim());
|
|
54
|
+
const width = positiveInt(options.width);
|
|
55
|
+
if (!match || !width)
|
|
56
|
+
return src;
|
|
57
|
+
const height = positiveInt(options.height);
|
|
58
|
+
const fit = options.fit ?? (height ? "cover" : "scale-down");
|
|
59
|
+
return `${match[1]}/w=${width}${height ? `,h=${height}` : ""},fit=${fit}`;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A `srcset` value offering the image at several widths (never enlarged), for `<img srcset>` together with `sizes`.
|
|
63
|
+
* Returns `undefined` for a URL Kanorio cannot resize: leave `srcset` off and use `src` alone.
|
|
64
|
+
*/
|
|
65
|
+
export function imageSrcSet(src, options = {}) {
|
|
66
|
+
if (!isResizableImageUrl(src))
|
|
67
|
+
return undefined;
|
|
68
|
+
const widths = [...new Set((options.widths ?? DEFAULT_IMAGE_WIDTHS).map((w) => positiveInt(w)).filter((w) => w !== null))].sort((a, b) => a - b);
|
|
69
|
+
if (widths.length === 0)
|
|
70
|
+
return undefined;
|
|
71
|
+
return widths.map((width) => `${imageUrl(src, { width })} ${width}w`).join(", ");
|
|
72
|
+
}
|
|
73
|
+
export const image = {
|
|
74
|
+
resolve: resolveImage,
|
|
75
|
+
url: imageUrl,
|
|
76
|
+
srcSet: imageSrcSet,
|
|
77
|
+
isResizable: isResizableImageUrl,
|
|
78
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,8 @@ export { createManagementClient, DEFAULT_MANAGEMENT_BASE_URL, type ManagementCli
|
|
|
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 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 ImageUrlOptions, type ImageSrcSetOptions, } 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
|
@@ -5,13 +5,14 @@ export { createManagementClient, DEFAULT_MANAGEMENT_BASE_URL, } from "./manageme
|
|
|
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";
|
|
12
13
|
export { forms, createFormsHelpers, protectionFromChallenge, uploadProtectionFromChallenge, } from "./forms.js";
|
|
13
14
|
export { edge, createKanorioMiddleware, classifyEdgeUserAgent, isEdgeBotBlocked, EDGE_BOT_LIST, CRAWLER_LIST_VERSION, } from "./edge.js";
|
|
14
15
|
export { preview, previewHeaders, tokenFromCookieHeader, isValidPreviewTokenFormat, sanitizePreviewRedirectPath, createPreviewRouteHandler, PREVIEW_TOKEN_PREFIX, PREVIEW_COOKIE_NAME, } from "./preview.js";
|
|
15
|
-
//
|
|
16
|
-
//
|
|
16
|
+
// Bot protection (shared by forms and customer sign-in): for forms, `protectionFromChallenge(formId, challenge, renderedAt)` is above;
|
|
17
|
+
// for sign-in (`GET /customer/auth/protection`), use `solveProtectionChallenge(challenge, { context, renderedAt })`
|
|
17
18
|
export { protectionFromChallenge as solveProtectionChallenge, solveAltchaChallenge, } from "./protection.js";
|
package/dist/locales.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Locale routing and hreflang for multilingual sites (the same behavior as hosted sites).
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* -
|
|
6
|
-
* - `resolveRequestLocale`
|
|
7
|
-
* - `buildAlternates`
|
|
8
|
-
*
|
|
4
|
+
* The same pure functions Kanorio's hosted sites use (`src/shared/**` is copied from Kanorio's shared code by `pnpm --filter @kanorio/sdk gen`):
|
|
5
|
+
* - The primary locale has no prefix (`/about`) and secondary locales use a subdirectory (`/en/about`); the prefix is the `pathPrefix` the owner saved (`locales[]` in `GET /api/v1/site`)
|
|
6
|
+
* - `resolveRequestLocale` reads the first path segment; the primary locale code used as a prefix (`/zh-hant/about`) must redirect 301 to the unprefixed path
|
|
7
|
+
* - `buildAlternates` works out canonical and hreflang: each locale version is self-canonical, only locales with a translation of this page are listed, and x-default points to the primary locale;
|
|
8
|
+
* an untranslated fallback page's canonical points to the primary locale URL and it emits no hreflang
|
|
9
9
|
*/
|
|
10
10
|
import { type LocaleRoute } from "./shared/localization/locale-path.js";
|
|
11
11
|
import { type RequestLocaleResult } from "./shared/localization/request-locale.js";
|
|
@@ -13,27 +13,27 @@ import { hreflangEntries, type SiteAlternates } from "./shared/seo/alternates.js
|
|
|
13
13
|
export { localeOfPrefix, localePrefixMap, localePrefixOf, languageOfLocale, type LocaleRoute } from "./shared/localization/locale-path.js";
|
|
14
14
|
export { resolveRequestLocale, withLocalePrefix, localeRootPath, type RequestLocaleInput, type RequestLocaleResult } from "./shared/localization/request-locale.js";
|
|
15
15
|
export { buildSiteAlternates, hreflangEntries, hreflangOf, localizedSiteUrl, sitemapAlternates, type SiteAlternates, type SiteAlternatesInput, type LocalePrefixes } from "./shared/seo/alternates.js";
|
|
16
|
-
/** `GET /api/v1/site`
|
|
16
|
+
/** The locale-related fields of the `GET /api/v1/site` response */
|
|
17
17
|
export interface SiteLocaleMeta {
|
|
18
18
|
primaryLocale?: string | null;
|
|
19
19
|
locales?: readonly LocaleRoute[] | null;
|
|
20
20
|
}
|
|
21
21
|
export interface BuildAlternatesOptions {
|
|
22
|
-
/**
|
|
22
|
+
/** The external front end's public URL, without a trailing slash (`https://www.example.com`) */
|
|
23
23
|
siteUrl: string;
|
|
24
|
-
/** `GET /api/v1/site`
|
|
24
|
+
/** The `GET /api/v1/site` response (`primaryLocale`, `locales[]`) */
|
|
25
25
|
site: SiteLocaleMeta;
|
|
26
|
-
/**
|
|
26
|
+
/** Path in the primary locale's space (no prefix): `/about`, `/blog/hello`, the home page `/` */
|
|
27
27
|
path: string;
|
|
28
|
-
/**
|
|
28
|
+
/** The locale in the URL (`locale` from `resolveRequestLocale`); omit for the primary locale */
|
|
29
29
|
requestLocale?: string | null;
|
|
30
|
-
/**
|
|
30
|
+
/** The locale the content was actually served in (`resolvedLocale` of the Delivery response); omit to use `requestLocale` */
|
|
31
31
|
resolvedLocale?: string | null;
|
|
32
|
-
/**
|
|
32
|
+
/** Secondary locales with a published translation of this page (`availableLocales` of the Delivery response); `"all"` means every live locale has one (list pages) */
|
|
33
33
|
availableLocales?: readonly string[] | "all" | null;
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
35
|
+
* This page's path in every live locale (no prefix; `localePaths` of the Delivery response). Each locale can have its own translated slug:
|
|
36
|
+
* when given, it is used to build each locale's URL; otherwise every locale uses `path`
|
|
37
37
|
*/
|
|
38
38
|
localePaths?: Readonly<Record<string, string>> | null;
|
|
39
39
|
}
|
|
@@ -43,21 +43,21 @@ export interface HreflangLink {
|
|
|
43
43
|
href: string;
|
|
44
44
|
}
|
|
45
45
|
export interface PageAlternates extends SiteAlternates {
|
|
46
|
-
/** `<link rel="alternate" hreflang="
|
|
46
|
+
/** `<link rel="alternate" hreflang="..." href="...">`; empty for fallback pages and single-language sites */
|
|
47
47
|
links: HreflangLink[];
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
50
|
+
* A page's canonical and hreflang (`canonical` and `languages` fit Next.js `metadata.alternates` directly).
|
|
51
|
+
* Single-language sites get only canonical; a fallback page (a secondary locale was requested but the page has no translation) points canonical to the primary locale URL and emits no hreflang.
|
|
52
52
|
*/
|
|
53
53
|
export declare function buildAlternates(options: BuildAlternatesOptions): PageAlternates;
|
|
54
|
-
/**
|
|
54
|
+
/** The request locale for an external front end: the prefix is read from the first path segment; always the primary locale when `site` has no locale data (single-language sites) */
|
|
55
55
|
export declare function requestLocaleOf(pathname: string, site: SiteLocaleMeta): RequestLocaleResult;
|
|
56
|
-
/**
|
|
56
|
+
/** This page's path in a locale (the primary locale has no prefix); for a language switcher */
|
|
57
57
|
export declare function localizedPath(pathname: string, locale: string, site: SiteLocaleMeta): string;
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
59
|
+
* Language switcher: this page's URL in every live locale (primary first, then in `locales[]` order).
|
|
60
|
+
* With `localePaths` (from the Delivery response), each locale uses its own path (translated slugs); otherwise `path` is placed under each locale's prefix.
|
|
61
61
|
*/
|
|
62
62
|
export declare function languageSwitcher(options: {
|
|
63
63
|
siteUrl: string;
|
package/dist/locales.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Locale routing and hreflang for multilingual sites (the same behavior as hosted sites).
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* -
|
|
6
|
-
* - `resolveRequestLocale`
|
|
7
|
-
* - `buildAlternates`
|
|
8
|
-
*
|
|
4
|
+
* The same pure functions Kanorio's hosted sites use (`src/shared/**` is copied from Kanorio's shared code by `pnpm --filter @kanorio/sdk gen`):
|
|
5
|
+
* - The primary locale has no prefix (`/about`) and secondary locales use a subdirectory (`/en/about`); the prefix is the `pathPrefix` the owner saved (`locales[]` in `GET /api/v1/site`)
|
|
6
|
+
* - `resolveRequestLocale` reads the first path segment; the primary locale code used as a prefix (`/zh-hant/about`) must redirect 301 to the unprefixed path
|
|
7
|
+
* - `buildAlternates` works out canonical and hreflang: each locale version is self-canonical, only locales with a translation of this page are listed, and x-default points to the primary locale;
|
|
8
|
+
* an untranslated fallback page's canonical points to the primary locale URL and it emits no hreflang
|
|
9
9
|
*/
|
|
10
10
|
import { localePrefixMap, localePrefixOf } from "./shared/localization/locale-path.js";
|
|
11
11
|
import { resolveRequestLocale, withLocalePrefix } from "./shared/localization/request-locale.js";
|
|
@@ -14,8 +14,8 @@ export { localeOfPrefix, localePrefixMap, localePrefixOf, languageOfLocale } fro
|
|
|
14
14
|
export { resolveRequestLocale, withLocalePrefix, localeRootPath } from "./shared/localization/request-locale.js";
|
|
15
15
|
export { buildSiteAlternates, hreflangEntries, hreflangOf, localizedSiteUrl, sitemapAlternates } from "./shared/seo/alternates.js";
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
17
|
+
* A page's canonical and hreflang (`canonical` and `languages` fit Next.js `metadata.alternates` directly).
|
|
18
|
+
* Single-language sites get only canonical; a fallback page (a secondary locale was requested but the page has no translation) points canonical to the primary locale URL and emits no hreflang.
|
|
19
19
|
*/
|
|
20
20
|
export function buildAlternates(options) {
|
|
21
21
|
const primaryLocale = options.site.primaryLocale ?? null;
|
|
@@ -42,7 +42,7 @@ export function buildAlternates(options) {
|
|
|
42
42
|
const links = Object.entries(alternates.languages).map(([hreflang, href]) => ({ rel: "alternate", hreflang, href }));
|
|
43
43
|
return { ...alternates, links };
|
|
44
44
|
}
|
|
45
|
-
/**
|
|
45
|
+
/** The request locale for an external front end: the prefix is read from the first path segment; always the primary locale when `site` has no locale data (single-language sites) */
|
|
46
46
|
export function requestLocaleOf(pathname, site) {
|
|
47
47
|
const primaryLocale = site.primaryLocale ?? "";
|
|
48
48
|
const routes = site.locales ?? [];
|
|
@@ -50,14 +50,14 @@ export function requestLocaleOf(pathname, site) {
|
|
|
50
50
|
return { locale: primaryLocale, pathname: pathname || "/", prefixed: false, redirectTo: null };
|
|
51
51
|
return resolveRequestLocale({ pathname, primaryLocale, locales: routes });
|
|
52
52
|
}
|
|
53
|
-
/**
|
|
53
|
+
/** This page's path in a locale (the primary locale has no prefix); for a language switcher */
|
|
54
54
|
export function localizedPath(pathname, locale, site) {
|
|
55
55
|
const primaryLocale = site.primaryLocale ?? locale;
|
|
56
56
|
return withLocalePrefix(pathname, locale, primaryLocale, site.locales ?? []);
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
59
|
+
* Language switcher: this page's URL in every live locale (primary first, then in `locales[]` order).
|
|
60
|
+
* With `localePaths` (from the Delivery response), each locale uses its own path (translated slugs); otherwise `path` is placed under each locale's prefix.
|
|
61
61
|
*/
|
|
62
62
|
export function languageSwitcher(options) {
|
|
63
63
|
const primaryLocale = options.site.primaryLocale ?? null;
|
package/dist/management.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Management API
|
|
3
|
-
* MCP
|
|
4
|
-
*
|
|
2
|
+
* Management API (`https://app.kanorio.com/api/v1`, Bearer = a personal access token `knr_pat_` or a service account token `knr_sa_`; an OAuth access token `knr_at_` works when it was issued for the Management API, as `kanorio login` does).
|
|
3
|
+
* MCP (`https://app.kanorio.com/api/mcp`) is another way to reach the same API and shares the same operation registry.
|
|
4
|
+
* Types are generated by openapi-typescript from the Management API's OpenAPI document (a snapshot of `GET /api/v1/openapi.json`).
|
|
5
5
|
*/
|
|
6
6
|
import type { paths as ManagementPaths } from "./generated/management.js";
|
|
7
7
|
import { type HttpClientOptions } from "./http.js";
|
|
@@ -9,9 +9,9 @@ import { type TypedClient } from "./typed.js";
|
|
|
9
9
|
export type { ManagementPaths };
|
|
10
10
|
export type ManagementClient = TypedClient<ManagementPaths>;
|
|
11
11
|
export interface ManagementClientOptions extends Omit<HttpClientOptions, "baseUrl" | "headers"> {
|
|
12
|
-
/** `knr_pat_
|
|
12
|
+
/** `knr_pat_...` or `knr_sa_...` (or an OAuth `knr_at_...` issued for the Management API); may be a function, for rotation */
|
|
13
13
|
token: string | (() => string | Promise<string>);
|
|
14
|
-
/**
|
|
14
|
+
/** Defaults to {@link DEFAULT_MANAGEMENT_BASE_URL} */
|
|
15
15
|
baseUrl?: string;
|
|
16
16
|
headers?: HeadersInit;
|
|
17
17
|
}
|
package/dist/management.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Management API
|
|
3
|
-
* MCP
|
|
4
|
-
*
|
|
2
|
+
* Management API (`https://app.kanorio.com/api/v1`, Bearer = a personal access token `knr_pat_` or a service account token `knr_sa_`; an OAuth access token `knr_at_` works when it was issued for the Management API, as `kanorio login` does).
|
|
3
|
+
* MCP (`https://app.kanorio.com/api/mcp`) is another way to reach the same API and shares the same operation registry.
|
|
4
|
+
* Types are generated by openapi-typescript from the Management API's OpenAPI document (a snapshot of `GET /api/v1/openapi.json`).
|
|
5
5
|
*/
|
|
6
6
|
import { createHttpClient } from "./http.js";
|
|
7
7
|
import { createTypedClient } from "./typed.js";
|
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
}>;
|
|
38
|
+
};
|
|
39
|
+
twitter: {
|
|
40
|
+
card: "summary" | "summary_large_image";
|
|
41
|
+
title?: string;
|
|
42
|
+
description?: string;
|
|
43
|
+
images?: string[];
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Turn `seoMetadata(...)` into the object `generateMetadata` returns. The title has no site name: add it with `title.template` in the root layout,
|
|
48
|
+
* and set `metadataBase` there when `canonical` is a path.
|
|
49
|
+
*/
|
|
50
|
+
export declare function toNextMetadata(metadata: SeoMetadata): NextMetadataLike;
|
|
51
|
+
/**
|
|
52
|
+
* An image loader for `next/image`: Kanorio's edge resizes the image, so Next.js does not have to.
|
|
53
|
+
* Use it per image (`<Image loader={kanorioImageLoader} ... />`) or for the whole app (`images.loaderFile` pointing at a file that
|
|
54
|
+
* default-exports it). Images Kanorio cannot resize (external URLs, SVG) are returned unchanged.
|
|
55
|
+
*/
|
|
56
|
+
export declare function kanorioImageLoader({ src, width }: {
|
|
57
|
+
src: string;
|
|
58
|
+
width: number;
|
|
59
|
+
quality?: number;
|
|
60
|
+
}): string;
|
|
61
|
+
/** `images.remotePatterns` entries for Kanorio's image host, for `next.config` when you use `next/image` without the loader */
|
|
62
|
+
export declare const kanorioRemotePatterns: ReadonlyArray<{
|
|
63
|
+
protocol: "https";
|
|
64
|
+
hostname: string;
|
|
65
|
+
pathname: string;
|
|
66
|
+
}>;
|
package/dist/next.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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 ? { images: [{ url: image.url, ...(image.alt ? { alt: image.alt } : {}) }] } : {}),
|
|
38
|
+
},
|
|
39
|
+
twitter: {
|
|
40
|
+
card: metadata.twitterCard,
|
|
41
|
+
...(openGraph.title ? { title: openGraph.title } : {}),
|
|
42
|
+
...(openGraph.description ? { description: openGraph.description } : {}),
|
|
43
|
+
...(image ? { images: [image.url] } : {}),
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* An image loader for `next/image`: Kanorio's edge resizes the image, so Next.js does not have to.
|
|
49
|
+
* Use it per image (`<Image loader={kanorioImageLoader} ... />`) or for the whole app (`images.loaderFile` pointing at a file that
|
|
50
|
+
* default-exports it). Images Kanorio cannot resize (external URLs, SVG) are returned unchanged.
|
|
51
|
+
*/
|
|
52
|
+
export function kanorioImageLoader({ src, width }) {
|
|
53
|
+
return imageUrl(src, { width });
|
|
54
|
+
}
|
|
55
|
+
/** `images.remotePatterns` entries for Kanorio's image host, for `next.config` when you use `next/image` without the loader */
|
|
56
|
+
export const kanorioRemotePatterns = [
|
|
57
|
+
{ protocol: "https", hostname: "img.kanorio.com", pathname: "/**" },
|
|
58
|
+
];
|