@lynkow/astro 0.0.0-stage → 0.1.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/LICENSE +29 -0
- package/README.md +390 -2
- package/dist/chunk-67VKUBOP.js +40 -0
- package/dist/chunk-67VKUBOP.js.map +1 -0
- package/dist/chunk-7ZL7TFBK.js +28 -0
- package/dist/chunk-7ZL7TFBK.js.map +1 -0
- package/dist/chunk-DQXJDZWU.js +9 -0
- package/dist/chunk-DQXJDZWU.js.map +1 -0
- package/dist/chunk-OGQ25G7G.js +329 -0
- package/dist/chunk-OGQ25G7G.js.map +1 -0
- package/dist/chunk-USZSRPT5.js +119 -0
- package/dist/chunk-USZSRPT5.js.map +1 -0
- package/dist/client/index.d.ts +60 -0
- package/dist/client/index.js +59 -0
- package/dist/client/index.js.map +1 -0
- package/dist/clients-DLlMBN4c.d.ts +10 -0
- package/dist/config-BVCvQ8Wx.d.ts +66 -0
- package/dist/index.d.ts +353 -0
- package/dist/index.js +395 -0
- package/dist/index.js.map +1 -0
- package/dist/integration.d.ts +37 -0
- package/dist/integration.js +51 -0
- package/dist/integration.js.map +1 -0
- package/dist/lynkow-cache-BKZFJRTI.d.ts +150 -0
- package/dist/middleware/index.d.ts +187 -0
- package/dist/middleware/index.js +582 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/next-headers-3VG6LM4M.js +3 -0
- package/dist/next-headers-3VG6LM4M.js.map +1 -0
- package/dist/next-navigation-VNM5XXCX.js +7 -0
- package/dist/next-navigation-VNM5XXCX.js.map +1 -0
- package/dist/react/index.d.ts +134 -0
- package/dist/react/index.js +128 -0
- package/dist/react/index.js.map +1 -0
- package/dist/server/index.d.ts +493 -0
- package/dist/server/index.js +989 -0
- package/dist/server/index.js.map +1 -0
- package/dist/site-locales-tmcu43n2.d.ts +80 -0
- package/package.json +91 -4
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { writableVisualEditorStore } from '../chunk-67VKUBOP.js';
|
|
2
|
+
export { visualEditorStore } from '../chunk-67VKUBOP.js';
|
|
3
|
+
import { initVisualEditor } from 'lynkow/visual-editor';
|
|
4
|
+
export { DEFAULT_CMS_ORIGIN, PREVIEW_PARAM } from 'lynkow/visual-editor';
|
|
5
|
+
|
|
6
|
+
var RUNNING = /* @__PURE__ */ Symbol.for("@lynkow/astro/visual-editor-running");
|
|
7
|
+
function patchField(root, blockSlug, fieldKey, data) {
|
|
8
|
+
if (!Object.prototype.hasOwnProperty.call(data, fieldKey)) return 0;
|
|
9
|
+
const raw = data[fieldKey];
|
|
10
|
+
if (raw !== null && typeof raw !== "string" && typeof raw !== "number") return 0;
|
|
11
|
+
const value = raw === null ? "" : String(raw);
|
|
12
|
+
let written = 0;
|
|
13
|
+
for (const block of root.querySelectorAll("[data-lynkow-block]")) {
|
|
14
|
+
if (block.getAttribute("data-lynkow-block") !== blockSlug) continue;
|
|
15
|
+
for (const field of block.querySelectorAll("[data-lynkow-field]")) {
|
|
16
|
+
if (field.getAttribute("data-lynkow-field") !== fieldKey) continue;
|
|
17
|
+
if (field.closest("[data-lynkow-block]") !== block) continue;
|
|
18
|
+
if (field.closest("astro-slot, astro-island")?.localName === "astro-island") continue;
|
|
19
|
+
if (field.getAttribute("data-lynkow-type") === "richtext") {
|
|
20
|
+
if (field.innerHTML !== value) field.innerHTML = value;
|
|
21
|
+
} else if (field.textContent !== value) {
|
|
22
|
+
field.textContent = value;
|
|
23
|
+
}
|
|
24
|
+
written++;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return written;
|
|
28
|
+
}
|
|
29
|
+
function startVisualEditor(options = {}) {
|
|
30
|
+
if (typeof window === "undefined" || typeof document === "undefined") return () => {
|
|
31
|
+
};
|
|
32
|
+
const global = globalThis;
|
|
33
|
+
const running = global[RUNNING];
|
|
34
|
+
if (running) return running;
|
|
35
|
+
const instance = initVisualEditor("cmsOrigin" in options ? { cmsOrigin: options.cmsOrigin } : {});
|
|
36
|
+
const store = writableVisualEditorStore;
|
|
37
|
+
let unsubscribe = () => {
|
|
38
|
+
};
|
|
39
|
+
if (instance.isPreviewMode()) {
|
|
40
|
+
store.setPreviewMode(true);
|
|
41
|
+
unsubscribe = instance.onDataChange((blockSlug, fieldKey, _fieldPath, _value, allData) => {
|
|
42
|
+
patchField(document, blockSlug, fieldKey, allData);
|
|
43
|
+
store.setBlockData(blockSlug, allData);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const stop = () => {
|
|
47
|
+
if (global[RUNNING] !== stop) return;
|
|
48
|
+
global[RUNNING] = void 0;
|
|
49
|
+
unsubscribe();
|
|
50
|
+
instance.destroy();
|
|
51
|
+
store.reset();
|
|
52
|
+
};
|
|
53
|
+
global[RUNNING] = stop;
|
|
54
|
+
return stop;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { startVisualEditor };
|
|
58
|
+
//# sourceMappingURL=index.js.map
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/visual-editor.ts"],"names":[],"mappings":";;;;;AAaA,IAAM,OAAA,mBAAU,MAAA,CAAO,GAAA,CAAI,qCAAqC,CAAA;AAuBzD,SAAS,UAAA,CACd,IAAA,EACA,SAAA,EACA,QAAA,EACA,IAAA,EACQ;AACR,EAAA,IAAI,CAAC,OAAO,SAAA,CAAU,cAAA,CAAe,KAAK,IAAA,EAAM,QAAQ,GAAG,OAAO,CAAA;AAClE,EAAA,MAAM,GAAA,GAAM,KAAK,QAAQ,CAAA;AACzB,EAAA,IAAI,GAAA,KAAQ,QAAQ,OAAO,GAAA,KAAQ,YAAY,OAAO,GAAA,KAAQ,UAAU,OAAO,CAAA;AAC/E,EAAA,MAAM,KAAA,GAAQ,GAAA,KAAQ,IAAA,GAAO,EAAA,GAAK,OAAO,GAAG,CAAA;AAE5C,EAAA,IAAI,OAAA,GAAU,CAAA;AACd,EAAA,KAAA,MAAW,KAAA,IAAS,IAAA,CAAK,gBAAA,CAAiB,qBAAqB,CAAA,EAAG;AAChE,IAAA,IAAI,KAAA,CAAM,YAAA,CAAa,mBAAmB,CAAA,KAAM,SAAA,EAAW;AAC3D,IAAA,KAAA,MAAW,KAAA,IAAS,KAAA,CAAM,gBAAA,CAAiB,qBAAqB,CAAA,EAAG;AACjE,MAAA,IAAI,KAAA,CAAM,YAAA,CAAa,mBAAmB,CAAA,KAAM,QAAA,EAAU;AAC1D,MAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,qBAAqB,CAAA,KAAM,KAAA,EAAO;AAGpD,MAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,0BAA0B,CAAA,EAAG,cAAc,cAAA,EAAgB;AAG7E,MAAA,IAAI,KAAA,CAAM,YAAA,CAAa,kBAAkB,CAAA,KAAM,UAAA,EAAY;AACzD,QAAA,IAAI,KAAA,CAAM,SAAA,KAAc,KAAA,EAAO,KAAA,CAAM,SAAA,GAAY,KAAA;AAAA,MACnD,CAAA,MAAA,IAAW,KAAA,CAAM,WAAA,KAAgB,KAAA,EAAO;AACtC,QAAA,KAAA,CAAM,WAAA,GAAc,KAAA;AAAA,MACtB;AACA,MAAA,OAAA,EAAA;AAAA,IACF;AAAA,EACF;AACA,EAAA,OAAO,OAAA;AACT;AAuBO,SAAS,iBAAA,CAAkB,OAAA,GAAoC,EAAC,EAAe;AACpF,EAAA,IAAI,OAAO,MAAA,KAAW,WAAA,IAAe,OAAO,QAAA,KAAa,WAAA,SAAoB,MAAM;AAAA,EAAC,CAAA;AACpF,EAAA,MAAM,MAAA,GAAS,UAAA;AACf,EAAA,MAAM,OAAA,GAAU,OAAO,OAAO,CAAA;AAC9B,EAAA,IAAI,SAAS,OAAO,OAAA;AAIpB,EAAA,MAAM,QAAA,GAAW,gBAAA,CAAiB,WAAA,IAAe,OAAA,GAAU,EAAE,WAAW,OAAA,CAAQ,SAAA,EAAU,GAAI,EAAE,CAAA;AAChG,EAAA,MAAM,KAAA,GAAQ,yBAAA;AACd,EAAA,IAAI,cAAc,MAAM;AAAA,EAAC,CAAA;AAEzB,EAAA,IAAI,QAAA,CAAS,eAAc,EAAG;AAC5B,IAAA,KAAA,CAAM,eAAe,IAAI,CAAA;AACzB,IAAA,WAAA,GAAc,SAAS,YAAA,CAAa,CAAC,WAAW,QAAA,EAAU,UAAA,EAAY,QAAQ,OAAA,KAAY;AACxF,MAAA,UAAA,CAAW,QAAA,EAAU,SAAA,EAAW,QAAA,EAAU,OAAO,CAAA;AACjD,MAAA,KAAA,CAAM,YAAA,CAAa,WAAW,OAAO,CAAA;AAAA,IACvC,CAAC,CAAA;AAAA,EACH;AAEA,EAAA,MAAM,OAAO,MAAM;AACjB,IAAA,IAAI,MAAA,CAAO,OAAO,CAAA,KAAM,IAAA,EAAM;AAC9B,IAAA,MAAA,CAAO,OAAO,CAAA,GAAI,MAAA;AAClB,IAAA,WAAA,EAAY;AACZ,IAAA,QAAA,CAAS,OAAA,EAAQ;AACjB,IAAA,KAAA,CAAM,KAAA,EAAM;AAAA,EACd,CAAA;AACA,EAAA,MAAA,CAAO,OAAO,CAAA,GAAI,IAAA;AAClB,EAAA,OAAO,IAAA;AACT","file":"index.js","sourcesContent":["import { initVisualEditor } from 'lynkow/visual-editor'\nimport { writableVisualEditorStore } from '../visual-editor/store.js'\n\n/** Options of {@link startVisualEditor}. */\nexport interface StartVisualEditorOptions {\n /**\n * Origin of the Lynkow dashboard that frames the site. Omit it to use the\n * Lynkow dashboard; give the middleware the same value, or neither one. An\n * empty value is refused by the SDK, which then does not start.\n */\n cmsOrigin?: string\n}\n\nconst RUNNING = Symbol.for('@lynkow/astro/visual-editor-running')\n\ntype Global = Record<symbol, (() => void) | undefined>\n\n/**\n * Writes the value the editor sent for `fieldKey` into every element marked\n * with that field inside a block marked `blockSlug`, and returns how many it\n * wrote.\n *\n * - A `richtext` field (`data-lynkow-type=\"richtext\"`) gets the value as HTML,\n * any other its text, as the React `Editable` renders them.\n * - Only a flat key holding a string or a number is written (`null` empties\n * the element). A repeater row has no element to write to.\n * - A field inside a nested block belongs to that block, and a field an\n * `astro-island` renders is left to the island, which renders the same value\n * from the store when it is hydrated. Static content passed into the island\n * (inside its `astro-slot`) is never re-rendered by it, so it is written.\n * An island that remounts that content puts back the HTML Astro captured\n * when it hydrated, so the value written is lost until the field changes\n * again.\n * - An element already holding the value is not touched, so the caret of an\n * editor typing in it stays where it is.\n */\nexport function patchField(\n root: ParentNode,\n blockSlug: string,\n fieldKey: string,\n data: Record<string, unknown>\n): number {\n if (!Object.prototype.hasOwnProperty.call(data, fieldKey)) return 0\n const raw = data[fieldKey]\n if (raw !== null && typeof raw !== 'string' && typeof raw !== 'number') return 0\n const value = raw === null ? '' : String(raw)\n\n let written = 0\n for (const block of root.querySelectorAll('[data-lynkow-block]')) {\n if (block.getAttribute('data-lynkow-block') !== blockSlug) continue\n for (const field of block.querySelectorAll('[data-lynkow-field]')) {\n if (field.getAttribute('data-lynkow-field') !== fieldKey) continue\n if (field.closest('[data-lynkow-block]') !== block) continue\n // The nearest island boundary decides: an island renders its own subtree,\n // not the static HTML of its slot.\n if (field.closest('astro-slot, astro-island')?.localName === 'astro-island') continue\n // Same trust boundary as the React `Editable`: the SDK accepts these\n // messages from the dashboard's origin only.\n if (field.getAttribute('data-lynkow-type') === 'richtext') {\n if (field.innerHTML !== value) field.innerHTML = value\n } else if (field.textContent !== value) {\n field.textContent = value\n }\n written++\n }\n }\n return written\n}\n\n/**\n * Starts the Lynkow visual editor on this page, once, and returns the function\n * that stops it. Call it from a `<script>` of the base layout:\n *\n * ```astro\n * <script>\n * import { startVisualEditor } from '@lynkow/astro/client'\n * startVisualEditor()\n * </script>\n * ```\n *\n * Outside the Lynkow dashboard's preview frame it does nothing. Inside it, the\n * SDK's editor scans the page for the `data-lynkow-block` and\n * `data-lynkow-field` markers, and every change an editor makes is written\n * into the static HTML in place (see {@link patchField}) and published to the\n * store the React hooks of `@lynkow/astro/react` read, so a hydrated island\n * renders it too.\n *\n * A second call while it runs starts nothing and returns the same stop\n * function. On the server it does nothing.\n */\nexport function startVisualEditor(options: StartVisualEditorOptions = {}): () => void {\n if (typeof window === 'undefined' || typeof document === 'undefined') return () => {}\n const global = globalThis as unknown as Global\n const running = global[RUNNING]\n if (running) return running\n\n // Passed as given: the SDK tells an absent origin, which takes the default,\n // from an empty one, which it refuses.\n const instance = initVisualEditor('cmsOrigin' in options ? { cmsOrigin: options.cmsOrigin } : {})\n const store = writableVisualEditorStore\n let unsubscribe = () => {}\n\n if (instance.isPreviewMode()) {\n store.setPreviewMode(true)\n unsubscribe = instance.onDataChange((blockSlug, fieldKey, _fieldPath, _value, allData) => {\n patchField(document, blockSlug, fieldKey, allData)\n store.setBlockData(blockSlug, allData)\n })\n }\n\n const stop = () => {\n if (global[RUNNING] !== stop) return\n global[RUNNING] = undefined\n unsubscribe()\n instance.destroy()\n store.reset()\n }\n global[RUNNING] = stop\n return stop\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Options of the client {@link LynkowClients} returns. */
|
|
2
|
+
interface ClientOptions {
|
|
3
|
+
/**
|
|
4
|
+
* `false` returns a client that sends each read once instead of retrying a
|
|
5
|
+
* 429 or a 503. Defaults to `true`.
|
|
6
|
+
*/
|
|
7
|
+
retry?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type { ClientOptions as C };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The configuration a Lynkow site hands this adapter, and the one function
|
|
3
|
+
* that validates it.
|
|
4
|
+
*
|
|
5
|
+
* Nothing in this package reads `process.env`. Next inlines `NEXT_PUBLIC_*`
|
|
6
|
+
* variables at build time by textual substitution, so only a literal
|
|
7
|
+
* `process.env.NEXT_PUBLIC_X` written in the application's own source reaches
|
|
8
|
+
* the browser bundle, and a library cannot spell out names an application
|
|
9
|
+
* chose. The application reads its environment and passes the values here.
|
|
10
|
+
*
|
|
11
|
+
* Everything in the resolved configuration is public, because it reaches
|
|
12
|
+
* client bundles. A server secret, such as the webhook signing secret, never
|
|
13
|
+
* belongs in it.
|
|
14
|
+
*/
|
|
15
|
+
/** What an application passes to {@link resolveConfig}, typically straight from its environment. */
|
|
16
|
+
interface LynkowNextConfigInput {
|
|
17
|
+
/** The site's UUID, shown in the Lynkow admin and returned by the MCP `list_sites` tool. */
|
|
18
|
+
siteId: string | undefined;
|
|
19
|
+
/** The public origin of the site, such as `https://www.example.com`. No path, query or fragment. */
|
|
20
|
+
siteUrl: string | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* A static override of the locales the site serves, as an array or a
|
|
23
|
+
* comma-separated list, in the order they should be advertised. Leave it out,
|
|
24
|
+
* or empty, to read the site's enabled locales at runtime from the source
|
|
25
|
+
* created by `createSiteLocales`. When given, it wins over that source.
|
|
26
|
+
*/
|
|
27
|
+
locales?: string | readonly string[] | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* A static override of the default locale. With `locales`, it must be one of
|
|
30
|
+
* them and defaults to the first. Without them, it is kept as given, and when
|
|
31
|
+
* it is left out too the runtime source supplies the site's default locale.
|
|
32
|
+
*/
|
|
33
|
+
defaultLocale?: string | undefined;
|
|
34
|
+
/** The Lynkow API origin. Defaults to `https://api.lynkow.com`. */
|
|
35
|
+
apiUrl?: string | undefined;
|
|
36
|
+
/** The site's publishable key (`lkw_pk_...`), which storefront reads need. */
|
|
37
|
+
publishableKey?: string | undefined;
|
|
38
|
+
}
|
|
39
|
+
/** A validated configuration, frozen. Every field is safe to ship to the browser. */
|
|
40
|
+
interface LynkowNextConfig {
|
|
41
|
+
readonly siteId: string;
|
|
42
|
+
/** The site's origin, with no trailing slash, so `${siteUrl}${path}` is always well formed. */
|
|
43
|
+
readonly siteUrl: string;
|
|
44
|
+
/** The API origin, with no trailing slash. */
|
|
45
|
+
readonly apiUrl: string;
|
|
46
|
+
readonly publishableKey: string | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* The static locale list, in its configured case, or empty when none was
|
|
49
|
+
* given: the runtime source then supplies the live list. URLs carry locales
|
|
50
|
+
* lowercased.
|
|
51
|
+
*/
|
|
52
|
+
readonly locales: readonly string[];
|
|
53
|
+
/**
|
|
54
|
+
* The static default locale, in its configured case: one of `locales` when a
|
|
55
|
+
* list was given. Empty when neither was given, the runtime source then
|
|
56
|
+
* supplying the live one.
|
|
57
|
+
*/
|
|
58
|
+
readonly defaultLocale: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Names to use for each field in error messages, such as the environment
|
|
62
|
+
* variable the application reads it from.
|
|
63
|
+
*/
|
|
64
|
+
type LynkowNextConfigLabels = Partial<Record<keyof LynkowNextConfigInput, string>>;
|
|
65
|
+
|
|
66
|
+
export type { LynkowNextConfigInput as L, LynkowNextConfigLabels as a, LynkowNextConfig as b };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { L as LynkowNextConfigInput, a as LynkowNextConfigLabels, b as LynkowNextConfig } from './config-BVCvQ8Wx.js';
|
|
2
|
+
import { S as SiteLocalesOptions, a as SiteLocales } from './site-locales-tmcu43n2.js';
|
|
3
|
+
export { b as SiteLocalesDetection, c as SiteLocalesSnapshot } from './site-locales-tmcu43n2.js';
|
|
4
|
+
import { L as LynkowCache } from './lynkow-cache-BKZFJRTI.js';
|
|
5
|
+
export { C as CacheEntry, a as CachePolicy, b as CacheStore, c as LynkowCacheOptions, M as MemoryStoreOptions, d as cached, e as createLynkowCache, f as createMemoryStore, l as lynkowCache, u as uncached } from './lynkow-cache-BKZFJRTI.js';
|
|
6
|
+
import { ClientConfig, Client, AnalyticsEventType } from 'lynkow';
|
|
7
|
+
export { AnalyticsEventType } from 'lynkow';
|
|
8
|
+
import { C as ClientOptions } from './clients-DLlMBN4c.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Validates a configuration and fills in its defaults: `siteId` (a UUID) and
|
|
12
|
+
* `siteUrl` (an origin) are required, `apiUrl` defaults to the Lynkow API, and
|
|
13
|
+
* `locales` with `defaultLocale` are an optional static override of the site's
|
|
14
|
+
* languages, which are otherwise read at runtime through `createSiteLocales`.
|
|
15
|
+
*
|
|
16
|
+
* The adapter reads no environment variable: the application reads its own,
|
|
17
|
+
* with literal `import.meta.env.PUBLIC_*` accesses Vite can inline, and passes
|
|
18
|
+
* the values here. Everything in the result is public, since it reaches the
|
|
19
|
+
* browser. A server secret never belongs in it.
|
|
20
|
+
*
|
|
21
|
+
* @param input - The values, typically read from the application's environment
|
|
22
|
+
* @param labels - Optional names for the fields in error messages, such as
|
|
23
|
+
* `{ siteId: 'PUBLIC_LYNKOW_SITE_ID' }`
|
|
24
|
+
* @throws {TypeError} Listing every problem found, one per line
|
|
25
|
+
* @example
|
|
26
|
+
* export const config = resolveConfig(
|
|
27
|
+
* {
|
|
28
|
+
* siteId: import.meta.env.PUBLIC_LYNKOW_SITE_ID,
|
|
29
|
+
* siteUrl: import.meta.env.PUBLIC_SITE_URL,
|
|
30
|
+
* locales: import.meta.env.PUBLIC_LOCALES,
|
|
31
|
+
* },
|
|
32
|
+
* { siteId: 'PUBLIC_LYNKOW_SITE_ID', siteUrl: 'PUBLIC_SITE_URL', locales: 'PUBLIC_LOCALES' }
|
|
33
|
+
* )
|
|
34
|
+
*/
|
|
35
|
+
declare function resolveConfig(input: LynkowNextConfigInput, labels?: LynkowNextConfigLabels): LynkowNextConfig;
|
|
36
|
+
/**
|
|
37
|
+
* A runtime source of the site's languages: the locales it serves, its default
|
|
38
|
+
* locale, each locale's writing direction and its detection settings, read from
|
|
39
|
+
* the site's configuration and kept for `ttlMs` (a minute by default), so a
|
|
40
|
+
* language enabled in the admin reaches the site without a rebuild.
|
|
41
|
+
*
|
|
42
|
+
* Declare it before the clients it reads the site through, and pass it to
|
|
43
|
+
* them; the arrow reads `lynkow` only when the source is first asked, so the
|
|
44
|
+
* two are wired without a cycle.
|
|
45
|
+
*
|
|
46
|
+
* @throws {RangeError} When `ttlMs` is not a finite number of at least 0.
|
|
47
|
+
* @example
|
|
48
|
+
* export const siteLocales = createSiteLocales({ client: (locale) => lynkow(locale) })
|
|
49
|
+
* export const lynkow = createClients(config, { siteLocales })
|
|
50
|
+
*/
|
|
51
|
+
declare function createSiteLocales(options: SiteLocalesOptions): SiteLocales;
|
|
52
|
+
|
|
53
|
+
/** An href that names its own scheme (`https:`, `mailto:`), or a protocol-relative one. */
|
|
54
|
+
declare const ABSOLUTE_HREF: RegExp;
|
|
55
|
+
/**
|
|
56
|
+
* The one place a public path gets its locale, bound to a configuration.
|
|
57
|
+
*
|
|
58
|
+
* Every localized path of a site goes through {@link LocalePaths.localePath}
|
|
59
|
+
* or {@link LocalePaths.fromApiPath}. That is what keeps every localized path
|
|
60
|
+
* consistent with the locale rule: a path built by hand as `/${locale}/...`
|
|
61
|
+
* puts a segment in front of a default-locale path that is served without
|
|
62
|
+
* one.
|
|
63
|
+
*
|
|
64
|
+
* The locales and the default locale are read at each use: from the static
|
|
65
|
+
* list of the configuration when it has one, otherwise from the runtime
|
|
66
|
+
* source given to {@link createLocalePaths}, so the helpers follow the site's
|
|
67
|
+
* language settings as they change.
|
|
68
|
+
*/
|
|
69
|
+
interface LocalePaths {
|
|
70
|
+
/**
|
|
71
|
+
* The configured locales: the configuration's static list when it has one,
|
|
72
|
+
* otherwise the runtime source's current list.
|
|
73
|
+
*/
|
|
74
|
+
readonly locales: readonly string[];
|
|
75
|
+
/**
|
|
76
|
+
* The default locale: the configuration's when it has a static list,
|
|
77
|
+
* otherwise the runtime source's current one.
|
|
78
|
+
*/
|
|
79
|
+
readonly defaultLocale: string;
|
|
80
|
+
/** The configured spelling of a locale, matched case-insensitively, or null. */
|
|
81
|
+
toLocale(value: string | null | undefined): string | null;
|
|
82
|
+
/** Whether a value names a configured locale, in any case. */
|
|
83
|
+
isLocale(value: string | null | undefined): value is string;
|
|
84
|
+
/**
|
|
85
|
+
* The locale a path belongs to. A path whose first segment names a locale
|
|
86
|
+
* belongs to it. Any other path belongs to the default locale, which is
|
|
87
|
+
* served unprefixed. That includes paths that are not pages at all, such as
|
|
88
|
+
* `/_next/...`, so a caller that routes requests filters those first.
|
|
89
|
+
*/
|
|
90
|
+
localeFromPath(pathname: string): string | null;
|
|
91
|
+
/**
|
|
92
|
+
* The public path of `path` in `locale`: `/fr/blog`, or `/blog` for the
|
|
93
|
+
* default locale, which is never prefixed. The locale segment is written
|
|
94
|
+
* lowercased, as the API writes it. A query or fragment is kept. A leading
|
|
95
|
+
* run of slashes or backslashes collapses to one, so `//evil.example` stays a
|
|
96
|
+
* path once a default locale is stripped. Dot segments are left as written:
|
|
97
|
+
* `/.//evil.example` resolves to `//evil.example` in `new URL()`, so a caller
|
|
98
|
+
* that resolves the result checks the resolved path, as the proxy does.
|
|
99
|
+
*
|
|
100
|
+
* A default-locale path whose first segment names another configured locale
|
|
101
|
+
* keeps its locale segment: `/en/fr/x` rather than `/fr/x`, which would be
|
|
102
|
+
* read as a French page. A first segment that names no configured locale
|
|
103
|
+
* does not trigger it, so `/de/x` stays as it is on a site without `de`.
|
|
104
|
+
*
|
|
105
|
+
* @throws {RangeError} When `locale` is not a configured locale.
|
|
106
|
+
*/
|
|
107
|
+
localePath(locale: string, path?: string): string;
|
|
108
|
+
/**
|
|
109
|
+
* The public path of a path the API returned.
|
|
110
|
+
*
|
|
111
|
+
* The API serves the site's default locale unprefixed and prefixes a path
|
|
112
|
+
* in any other locale with that locale (`/fr/blog/article`), which is kept.
|
|
113
|
+
* A path with no locale segment, a default-locale path or any path on a
|
|
114
|
+
* single-locale site, belongs to `locale`, so pass the locale of the row the
|
|
115
|
+
* path came from. A path that still carries the default locale's own prefix
|
|
116
|
+
* (`/en/blog/post`, a stored or older form) is mapped to its unprefixed URL
|
|
117
|
+
* (`/blog/post`). Anything that is not a root-relative path is returned
|
|
118
|
+
* unchanged.
|
|
119
|
+
*
|
|
120
|
+
* The API never returns a protocol-relative URL, so a leading run of slashes
|
|
121
|
+
* or backslashes collapses to one before the locale is read: on a
|
|
122
|
+
* single-locale site, a blog prefix of `/evil.example` makes an article path
|
|
123
|
+
* `//evil.example/post`, which stays `/evil.example/post` on this site.
|
|
124
|
+
*/
|
|
125
|
+
fromApiPath(path: string, locale: string): string;
|
|
126
|
+
/**
|
|
127
|
+
* An href from CMS content, made to lead to `locale`.
|
|
128
|
+
*
|
|
129
|
+
* Navigation blocks store locale-agnostic hrefs (`/blog`, `/contact`), which
|
|
130
|
+
* is right for an editor, so the locale is added at render: without it every
|
|
131
|
+
* internal link goes through a redirect. An href that already names a locale
|
|
132
|
+
* keeps it, normalized like {@link fromApiPath}. Absolute URLs, `mailto:` and
|
|
133
|
+
* `tel:` links, anchors, query-only and relative hrefs are left untouched.
|
|
134
|
+
*/
|
|
135
|
+
localizeHref(href: string, locale: string): string;
|
|
136
|
+
/**
|
|
137
|
+
* The locale a link leads to on this site, or null when it leads elsewhere:
|
|
138
|
+
* another origin, a `mailto:`, an anchor, a relative path. An absolute URL
|
|
139
|
+
* on the site's own origin counts as this site, which catches an editor's
|
|
140
|
+
* pasted `https://<site>/fr/...`.
|
|
141
|
+
*/
|
|
142
|
+
hrefLocale(href: string): string | null;
|
|
143
|
+
/**
|
|
144
|
+
* The best match between an `Accept-Language` header and the configured
|
|
145
|
+
* locales, falling back to the default locale rather than to a 404. A
|
|
146
|
+
* language the header marks `q=0` is one the browser refuses, so it is
|
|
147
|
+
* never the match.
|
|
148
|
+
*/
|
|
149
|
+
negotiateLocale(acceptLanguage: string | null | undefined): string;
|
|
150
|
+
/** `path` on the site's origin. */
|
|
151
|
+
absoluteUrl(path: string): string;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Binds the locale path helpers to a configuration from {@link resolveConfig}.
|
|
155
|
+
*
|
|
156
|
+
* `source` supplies the locales and the default locale at runtime, through
|
|
157
|
+
* {@link SiteLocales.current}, which must have been loaded once with
|
|
158
|
+
* {@link SiteLocales.get}. A configuration with a non-empty static locale list
|
|
159
|
+
* wins: its list and default locale are used and the source is never read.
|
|
160
|
+
* The default locale is always served unprefixed.
|
|
161
|
+
*
|
|
162
|
+
* @throws {Error} When the configuration has no static locale list and no
|
|
163
|
+
* source is given, since nothing would supply the locales.
|
|
164
|
+
*/
|
|
165
|
+
declare function createLocalePaths(config: LynkowNextConfig, source?: SiteLocales): LocalePaths;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The caching strategy of a Lynkow site on Next, in one file.
|
|
169
|
+
*
|
|
170
|
+
* Next 15 changed `fetch()` to be uncached by default, so nothing here is
|
|
171
|
+
* inherited: a read that does not pass one of these option bags hits the Lynkow
|
|
172
|
+
* API on every single request. Every data read in this app therefore goes
|
|
173
|
+
* through `cached()`. The durations are defaults tuned for a content site; a
|
|
174
|
+
* site that wants others passes its own number to `cached()`.
|
|
175
|
+
*
|
|
176
|
+
* Two mechanisms, deliberately combined:
|
|
177
|
+
*
|
|
178
|
+
* - `revalidate` is the ceiling. Even with no webhook wired, content is never
|
|
179
|
+
* more than this many seconds stale.
|
|
180
|
+
* - `tags` are the floor. The revalidation webhook calls `revalidateTag()` so
|
|
181
|
+
* an edit in the admin appears in seconds rather than minutes.
|
|
182
|
+
*
|
|
183
|
+
* A tag is only useful if the webhook actually sends the matching event, so the
|
|
184
|
+
* names here and the ones the revalidation handler maps MUST stay in step.
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
/** Seconds. Tuned for a content site: rarely-changing structure lives longer. */
|
|
188
|
+
declare const REVALIDATE$1: {
|
|
189
|
+
/** Site config and global blocks: header, footer, branding. */
|
|
190
|
+
readonly site: 3600;
|
|
191
|
+
/** Structural pages driven by site blocks. */
|
|
192
|
+
readonly page: 300;
|
|
193
|
+
/** Blog articles, categories, tags. */
|
|
194
|
+
readonly blog: 300;
|
|
195
|
+
/** Approved reviews. */
|
|
196
|
+
readonly reviews: 900;
|
|
197
|
+
/** Form schemas. */
|
|
198
|
+
readonly forms: 3600;
|
|
199
|
+
/** The cookie consent banner configuration. */
|
|
200
|
+
readonly consent: 3600;
|
|
201
|
+
/** sitemap.xml, robots.txt, llms.txt. */
|
|
202
|
+
readonly seo: 3600;
|
|
203
|
+
/** The path table used for static generation and redirect matching. */
|
|
204
|
+
readonly paths: 600;
|
|
205
|
+
/**
|
|
206
|
+
* One search result page.
|
|
207
|
+
*
|
|
208
|
+
* Short on purpose. Next keys its fetch cache on the full URL, so every
|
|
209
|
+
* distinct query is its own entry and a long ceiling would pin a large,
|
|
210
|
+
* unbounded set of them. The tag is what actually keeps results honest: a
|
|
211
|
+
* publish evicts every cached query at once.
|
|
212
|
+
*/
|
|
213
|
+
readonly search: 60;
|
|
214
|
+
};
|
|
215
|
+
declare const TAGS$1: {
|
|
216
|
+
readonly site: "lynkow:site";
|
|
217
|
+
readonly pages: "lynkow:pages";
|
|
218
|
+
readonly page: (slug: string) => string;
|
|
219
|
+
readonly contents: "lynkow:contents";
|
|
220
|
+
readonly content: (slug: string) => string;
|
|
221
|
+
readonly categories: "lynkow:categories";
|
|
222
|
+
readonly tags: "lynkow:tags";
|
|
223
|
+
readonly reviews: "lynkow:reviews";
|
|
224
|
+
readonly forms: "lynkow:forms";
|
|
225
|
+
readonly form: (slug: string) => string;
|
|
226
|
+
readonly consent: "lynkow:consent";
|
|
227
|
+
readonly seo: "lynkow:seo";
|
|
228
|
+
readonly paths: "lynkow:paths";
|
|
229
|
+
readonly search: "lynkow:search";
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The default freshness ceilings of each kind of read, in seconds, for
|
|
234
|
+
* `cached(tags, revalidate)`: the same values `@lynkow/next` uses. A site that
|
|
235
|
+
* wants others passes its own number.
|
|
236
|
+
*/
|
|
237
|
+
declare const REVALIDATE: typeof REVALIDATE$1;
|
|
238
|
+
/**
|
|
239
|
+
* The cache tags the revalidation endpoint evicts for each webhook event. A
|
|
240
|
+
* read tagged with one of them is evicted when that content changes, so the
|
|
241
|
+
* names here and those the endpoint maps always stay in step.
|
|
242
|
+
*/
|
|
243
|
+
declare const TAGS: typeof TAGS$1;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Options of {@link createClients}, applied to every client it creates.
|
|
247
|
+
*
|
|
248
|
+
* Declared here in full rather than extending the shared options type, so the
|
|
249
|
+
* published declarations name no type that only a hashed chunk exports.
|
|
250
|
+
*/
|
|
251
|
+
interface CreateClientsOptions {
|
|
252
|
+
/**
|
|
253
|
+
* The SDK's browser content enhancements, passed as they are to
|
|
254
|
+
* `createClient({ enhancements })`. Set `{ activateScripts: false }` when the
|
|
255
|
+
* page renders CMS HTML on the server: the browser has already run that
|
|
256
|
+
* HTML's inline scripts on page load. Only a client created in the browser
|
|
257
|
+
* uses it.
|
|
258
|
+
*/
|
|
259
|
+
enhancements?: ClientConfig['enhancements'];
|
|
260
|
+
/**
|
|
261
|
+
* The site's languages read at runtime, from `createSiteLocales`. When the
|
|
262
|
+
* configuration has no static locale list, a locale is checked against the
|
|
263
|
+
* languages this source last loaded, and a call with no locale gets the
|
|
264
|
+
* site's default. Ignored when the configuration has a static list.
|
|
265
|
+
*/
|
|
266
|
+
siteLocales?: SiteLocales;
|
|
267
|
+
/** The data cache the reads carrying `cached()` go through. Defaults to `lynkowCache`. */
|
|
268
|
+
cache?: LynkowCache;
|
|
269
|
+
}
|
|
270
|
+
/** Returns the Lynkow client of a locale, the default one when none is given. */
|
|
271
|
+
type LynkowClients = (locale?: string, options?: ClientOptions) => Client;
|
|
272
|
+
/**
|
|
273
|
+
* One Lynkow client per locale, reused across requests, whose reads go through
|
|
274
|
+
* the Astro data cache when they carry `cached()` options.
|
|
275
|
+
*
|
|
276
|
+
* A read passed `cached(tags, revalidate)`, alone or spread into its own
|
|
277
|
+
* options, is served from the cache: keyed on the client's locale, the
|
|
278
|
+
* service and method, and the read's arguments (request headers included),
|
|
279
|
+
* evicted by its tags, fresh for `revalidate` seconds. The hint is removed
|
|
280
|
+
* before the SDK sees the options. A read without it, or with `uncached()`,
|
|
281
|
+
* goes to the API every time. A fresh cache hit never calls the SDK method,
|
|
282
|
+
* so a side effect of the method is skipped too: `site.getConfig` served from
|
|
283
|
+
* the cache does not refresh the client's `availableLocales`. A stale hit
|
|
284
|
+
* calls it in the background, so its side effect lands after that read has
|
|
285
|
+
* returned.
|
|
286
|
+
*
|
|
287
|
+
* The SDK's own cache stays off, and every other rule of these clients is the
|
|
288
|
+
* one `@lynkow/next` applies: FOR READS THAT ARE THE SAME FOR EVERY VISITOR.
|
|
289
|
+
* The clients are shared by every request the process serves, so a buyer's
|
|
290
|
+
* cart or session never goes through them: use `createBuyerClient` from
|
|
291
|
+
* `@lynkow/astro/server`, whose reads are never cached. A locale outside the
|
|
292
|
+
* site's languages throws a `RangeError`; await the site's languages before a
|
|
293
|
+
* locale taken from a URL reaches this function.
|
|
294
|
+
*
|
|
295
|
+
* ```ts
|
|
296
|
+
* const siteLocales = createSiteLocales({ client: (locale) => lynkow(locale) })
|
|
297
|
+
* export const lynkow = createClients(config, { siteLocales })
|
|
298
|
+
* ```
|
|
299
|
+
*
|
|
300
|
+
* @throws {RangeError} From the returned function, when a locale is given that
|
|
301
|
+
* is not a well-formed language tag, or names none of the site's languages
|
|
302
|
+
* once they are known.
|
|
303
|
+
*/
|
|
304
|
+
declare function createClients(config: LynkowNextConfig, { cache, ...options }?: CreateClientsOptions): LynkowClients;
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Sends one custom event through the tracker already on the page.
|
|
308
|
+
*
|
|
309
|
+
* `type` is a CLOSED enum of mechanical interactions, the SDK's
|
|
310
|
+
* `AnalyticsEventType`: there is no free-form event name. A value outside it is
|
|
311
|
+
* a compile error rather than a silently dropped row, since the collector
|
|
312
|
+
* answers `204` to a payload it rejects.
|
|
313
|
+
*
|
|
314
|
+
* Returns `false` when no tracker has loaded, which is never an error: on an
|
|
315
|
+
* `opt-in` site none loads before the visitor accepts analytics. `true` means
|
|
316
|
+
* the event was handed to the tracker, which adds the session id, the visitor
|
|
317
|
+
* id and the timestamp, and drops the event itself when the visitor refused on
|
|
318
|
+
* an `opt-in` or `opt-out` site.
|
|
319
|
+
*
|
|
320
|
+
* The tracker already records pageviews, scroll, clicks, form events and Core
|
|
321
|
+
* Web Vitals. Reach for this only for an interaction that escapes it, such as a
|
|
322
|
+
* form submitted through `fetch` without a real `submit` event.
|
|
323
|
+
*/
|
|
324
|
+
declare function trackEvent(type: AnalyticsEventType, data?: Record<string, unknown>): boolean;
|
|
325
|
+
|
|
326
|
+
/** What {@link imageUrl} takes. */
|
|
327
|
+
interface ImageUrlParams {
|
|
328
|
+
src: string;
|
|
329
|
+
/** The width to resize to, in pixels. */
|
|
330
|
+
width: number;
|
|
331
|
+
/** The encoding quality, 1 to 100. Defaults to 80. */
|
|
332
|
+
quality?: number;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* The URL of a Lynkow image resized on the Lynkow CDN to `width`, never
|
|
336
|
+
* upscaled, in the best format the browser accepts. A relative URL (a file in
|
|
337
|
+
* `public/`) or a URL that is not Lynkow media comes back unchanged.
|
|
338
|
+
*/
|
|
339
|
+
declare function imageUrl({ src, width, quality }: ImageUrlParams): string;
|
|
340
|
+
/**
|
|
341
|
+
* A `srcset` for `src`, one resized candidate per width, ascending, for an
|
|
342
|
+
* `<img srcset sizes>` that lets the browser pick. Empty when `src` is not
|
|
343
|
+
* Lynkow media, since every candidate would then be the same file.
|
|
344
|
+
*
|
|
345
|
+
* @throws {RangeError} When a width is not a positive integer.
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* <img src={imageUrl({ src, width: 1280 })} srcset={imageSrcSet(src, [640, 960, 1280])} sizes="100vw" alt="" />
|
|
349
|
+
*/
|
|
350
|
+
declare function imageSrcSet(src: string, widths: readonly number[], quality?: number): string;
|
|
351
|
+
|
|
352
|
+
export { ABSOLUTE_HREF, ClientOptions, LynkowCache, LynkowNextConfig as LynkowConfig, LynkowNextConfigInput as LynkowConfigInput, LynkowNextConfigLabels as LynkowConfigLabels, REVALIDATE, SiteLocales, SiteLocalesOptions, TAGS, createClients, createLocalePaths, createSiteLocales, imageSrcSet, imageUrl, resolveConfig, trackEvent };
|
|
353
|
+
export type { CreateClientsOptions, ImageUrlParams, LocalePaths, LynkowClients };
|