@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,493 @@
|
|
|
1
|
+
import { AstroCookies, APIRoute } from 'astro';
|
|
2
|
+
import { Client, BaseRequestOptions, Page, Content, ResolvedCategory, Product, ProductCollection, ProductCategory } from 'lynkow';
|
|
3
|
+
import { LynkowConfig as LynkowNextConfig } from '../index.js';
|
|
4
|
+
import { SiteLocales } from '../index.js';
|
|
5
|
+
import { LynkowCache } from '../index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The buyer session on the server: a Lynkow client created for one request and
|
|
9
|
+
* seeded from that request's cookies, and the helpers that write and clear
|
|
10
|
+
* those cookies.
|
|
11
|
+
*
|
|
12
|
+
* The SDK keeps a buyer's state on the client instance: the guest cart token
|
|
13
|
+
* and the signed-in customer's session token. The clients of `createClients`
|
|
14
|
+
* serve every request the process handles, so buyer state on one of them would
|
|
15
|
+
* hand one visitor's cart or session to the next. A buyer client belongs to one
|
|
16
|
+
* request and is dropped with it, and nothing in this module is kept between
|
|
17
|
+
* calls.
|
|
18
|
+
*/
|
|
19
|
+
/** The names of the two buyer cookies. */
|
|
20
|
+
declare const BUYER_COOKIE_NAMES: {
|
|
21
|
+
/** The guest cart token. */
|
|
22
|
+
readonly cart: "__Host-lynkow-cart";
|
|
23
|
+
/** The signed-in customer's session token. */
|
|
24
|
+
readonly customer: "__Host-lynkow-customer";
|
|
25
|
+
};
|
|
26
|
+
/** The two buyer tokens a request carries, each `null` when absent or unusable. */
|
|
27
|
+
interface BuyerTokens {
|
|
28
|
+
cartToken: string | null;
|
|
29
|
+
customerToken: string | null;
|
|
30
|
+
}
|
|
31
|
+
/** Options of {@link createBuyerClient}. */
|
|
32
|
+
interface BuyerClientOptions {
|
|
33
|
+
/**
|
|
34
|
+
* The locale the client reads in, typically the route's. Omitted, the API
|
|
35
|
+
* answers in the site's default locale.
|
|
36
|
+
*/
|
|
37
|
+
locale?: string;
|
|
38
|
+
/**
|
|
39
|
+
* The site's server key (`lkw_srv_...`), created on the Storefront settings
|
|
40
|
+
* page. A secret API token also works. With `clientIp`, it makes the API
|
|
41
|
+
* count this buyer, rather than your server, against its rate limits.
|
|
42
|
+
* Leave both unset to let the client forward them on its own from
|
|
43
|
+
* `LYNKOW_SERVER_KEY`. Setting either one turns that off for this client.
|
|
44
|
+
*/
|
|
45
|
+
serverKey?: string;
|
|
46
|
+
/**
|
|
47
|
+
* The buyer's IP address, read from a request header your host sets and
|
|
48
|
+
* overwrites. Sent only together with `serverKey`.
|
|
49
|
+
*/
|
|
50
|
+
clientIp?: string;
|
|
51
|
+
}
|
|
52
|
+
/** Options of {@link setCartCookie} and {@link setCustomerCookie}. */
|
|
53
|
+
interface BuyerCookieOptions {
|
|
54
|
+
/**
|
|
55
|
+
* When the token stops being valid, as a `Date` or epoch milliseconds. The
|
|
56
|
+
* cookie then lives until that moment, never longer than its 30-day ceiling.
|
|
57
|
+
* A moment already past deletes the cookie instead of writing the token.
|
|
58
|
+
*/
|
|
59
|
+
expiresAt?: Date | number;
|
|
60
|
+
}
|
|
61
|
+
/** Options of {@link signOut}. */
|
|
62
|
+
interface SignOutOptions {
|
|
63
|
+
/**
|
|
64
|
+
* `true` also revokes, through the API, every session this customer has open
|
|
65
|
+
* on any device. Omitted, signing out forgets the session on this site only
|
|
66
|
+
* and makes no request to the API, so the token stays valid on the API until
|
|
67
|
+
* it expires.
|
|
68
|
+
*/
|
|
69
|
+
everywhere?: boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The buyer session on an Astro server: a Lynkow client created for one
|
|
74
|
+
* request and seeded from that request's cookies, and the helpers that write
|
|
75
|
+
* and clear those cookies through `Astro.cookies` (or `context.cookies` in an
|
|
76
|
+
* endpoint, a middleware or an action).
|
|
77
|
+
*
|
|
78
|
+
* The cookie rules, the token checks and the IP forwarding are the ones of
|
|
79
|
+
* `@lynkow/next`, shared with it: both buyer cookies are `__Host-`, `Secure`,
|
|
80
|
+
* `HttpOnly`, `SameSite=Lax`, and live 30 days at most.
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Reads the buyer's cart token and customer token from `Astro.cookies` or the
|
|
85
|
+
* incoming `Request`. A value that is empty, longer than 4096 characters, or
|
|
86
|
+
* holds anything but visible ASCII without `,` and `;` is treated as absent.
|
|
87
|
+
*/
|
|
88
|
+
declare function readBuyerTokens(source: AstroCookies | Request): BuyerTokens;
|
|
89
|
+
/**
|
|
90
|
+
* Creates the Lynkow client of ONE buyer request, seeded with the cart token
|
|
91
|
+
* and the customer token read from the buyer cookies.
|
|
92
|
+
*
|
|
93
|
+
* Call it once per request, in a page, an endpoint or an action, and drop the
|
|
94
|
+
* client with the request: the SDK keeps the cart and the session on the
|
|
95
|
+
* instance, so a client shared between requests would hand one visitor's cart
|
|
96
|
+
* or session to the next. Its reads are never cached.
|
|
97
|
+
*
|
|
98
|
+
* With `LYNKOW_SERVER_KEY` set to the site's server key, the client forwards
|
|
99
|
+
* the buyer's IP address with it on every write and on the guest order read,
|
|
100
|
+
* read from the current request, which the middleware of
|
|
101
|
+
* `@lynkow/astro/middleware` (or `runWithRequest`) makes available. Passing
|
|
102
|
+
* `serverKey` or `clientIp` yourself replaces this for the client.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ---
|
|
106
|
+
* // src/pages/[locale]/cart.astro
|
|
107
|
+
* import { createBuyerClient } from '@lynkow/astro/server'
|
|
108
|
+
* const buyer = createBuyerClient(config, Astro.cookies, { locale })
|
|
109
|
+
* const cart = await buyer.cart.retrieve()
|
|
110
|
+
* ---
|
|
111
|
+
*/
|
|
112
|
+
declare function createBuyerClient(config: LynkowNextConfig, source: AstroCookies | Request, options?: BuyerClientOptions): Client;
|
|
113
|
+
/**
|
|
114
|
+
* Stores the guest cart token in the buyer's cart cookie, for 30 days at most.
|
|
115
|
+
* Call it from an endpoint, an action or a middleware, before the response
|
|
116
|
+
* starts.
|
|
117
|
+
*
|
|
118
|
+
* @throws {TypeError} When the token is empty, too long or not cookie-safe, or
|
|
119
|
+
* `expiresAt` is not a valid date. Nothing is written then.
|
|
120
|
+
*/
|
|
121
|
+
declare function setCartCookie(cookies: AstroCookies, token: string, options?: BuyerCookieOptions): void;
|
|
122
|
+
/**
|
|
123
|
+
* Stores the customer session token `customers.login()` returned in the
|
|
124
|
+
* buyer's customer cookie, for 30 days at most. The token is a bearer
|
|
125
|
+
* credential: it goes into this `HttpOnly` cookie and nowhere a page script
|
|
126
|
+
* can read it.
|
|
127
|
+
*
|
|
128
|
+
* @throws {TypeError} When the token is empty, too long or not cookie-safe, or
|
|
129
|
+
* `expiresAt` is not a valid date. Nothing is written then.
|
|
130
|
+
*/
|
|
131
|
+
declare function setCustomerCookie(cookies: AstroCookies, token: string, options?: BuyerCookieOptions): void;
|
|
132
|
+
/**
|
|
133
|
+
* Deletes both buyer cookies by overwriting them empty with `Max-Age=0` and
|
|
134
|
+
* the same `__Host-` attributes they were set with.
|
|
135
|
+
*/
|
|
136
|
+
declare function clearBuyerCookies(cookies: AstroCookies): void;
|
|
137
|
+
/**
|
|
138
|
+
* Signs the buyer out: `customers.logout()` on the request's buyer client,
|
|
139
|
+
* then both buyer cookies deleted and the cart forgotten on that client. With
|
|
140
|
+
* `{ everywhere: true }` it also revokes every session the customer has open,
|
|
141
|
+
* through the API. The cookies are deleted even when that call fails, and the
|
|
142
|
+
* failure is then rethrown.
|
|
143
|
+
*/
|
|
144
|
+
declare function signOut(client: Client, cookies: AstroCookies, options?: SignOutOptions): Promise<void>;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Buyer IP forwarding: the server key and the buyer's IP address a buyer
|
|
148
|
+
* client sends so the Lynkow API counts each buyer, rather than your server,
|
|
149
|
+
* against its rate limits.
|
|
150
|
+
*
|
|
151
|
+
* Both values are read on the server only, from the environment and the
|
|
152
|
+
* incoming request headers, and never reach a client bundle: this module is
|
|
153
|
+
* imported by `@lynkow/next/server` alone.
|
|
154
|
+
*/
|
|
155
|
+
/**
|
|
156
|
+
* What a buyer client forwards: both values, or neither. Spread it into the
|
|
157
|
+
* options of `createBuyerClient`.
|
|
158
|
+
*/
|
|
159
|
+
interface BuyerForwarding {
|
|
160
|
+
/** The site's server key, from `LYNKOW_SERVER_KEY`. */
|
|
161
|
+
serverKey?: string;
|
|
162
|
+
/** The buyer's IP address, read from the request headers. */
|
|
163
|
+
clientIp?: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The server key and the buyer's IP address to forward for one request, or
|
|
168
|
+
* an empty object when there is nothing to forward. Server only, never throws.
|
|
169
|
+
*
|
|
170
|
+
* `createBuyerClient` already does this on its own once `LYNKOW_SERVER_KEY` is
|
|
171
|
+
* set. Call it only from code that builds its own client with `createClient`
|
|
172
|
+
* from `lynkow`: `createClient({ siteId, ...resolveBuyerForwarding(request.headers) })`.
|
|
173
|
+
*
|
|
174
|
+
* `LYNKOW_SERVER_KEY` is the site's server key (`lkw_srv_...`); unset, nothing
|
|
175
|
+
* is forwarded. The address is the value of the one header
|
|
176
|
+
* `LYNKOW_CLIENT_IP_HEADER` names, when set, else the first public
|
|
177
|
+
* `X-Forwarded-For` entry from the right.
|
|
178
|
+
*
|
|
179
|
+
* @param headers - The incoming request headers, such as `Astro.request.headers`
|
|
180
|
+
*/
|
|
181
|
+
declare function resolveBuyerForwarding(headers: Pick<Headers, 'get'>): BuyerForwarding;
|
|
182
|
+
/**
|
|
183
|
+
* Checks that a mutating request was sent by a page of this site: its `Origin`
|
|
184
|
+
* must name the request's host (`X-Forwarded-Host`, else `Host`, else the URL's
|
|
185
|
+
* host), compared lowercased, port included. Astro checks the origin of form
|
|
186
|
+
* submissions and actions on its own when `security.checkOrigin` is on; an
|
|
187
|
+
* endpoint that changes a buyer's cart or account calls this at its top. Not on
|
|
188
|
+
* a GET, which often carries no `Origin`.
|
|
189
|
+
*
|
|
190
|
+
* @returns `null` when the request may proceed, otherwise a 403 response to
|
|
191
|
+
* return as it is. Never throws on request input.
|
|
192
|
+
* @example
|
|
193
|
+
* // src/pages/api/cart.ts
|
|
194
|
+
* export const POST: APIRoute = async ({ request }) => {
|
|
195
|
+
* const refused = assertSameOrigin(request)
|
|
196
|
+
* if (refused) return refused
|
|
197
|
+
* // ...
|
|
198
|
+
* }
|
|
199
|
+
*/
|
|
200
|
+
declare function assertSameOrigin(request: Request): Response | null;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The `<head>` data of a page, flat and typed, built from the metadata
|
|
204
|
+
* builders `@lynkow/next` shares with this package. An Astro layout renders it
|
|
205
|
+
* with plain tags; nothing here renders HTML.
|
|
206
|
+
*/
|
|
207
|
+
/** One hreflang alternate. */
|
|
208
|
+
interface HeadAlternate {
|
|
209
|
+
/** A configured locale, or `x-default`. */
|
|
210
|
+
hreflang: string;
|
|
211
|
+
href: string;
|
|
212
|
+
}
|
|
213
|
+
/** One Open Graph image. */
|
|
214
|
+
interface HeadImage {
|
|
215
|
+
url: string;
|
|
216
|
+
alt?: string;
|
|
217
|
+
}
|
|
218
|
+
/** The Open Graph `article:*` tags of a blog article. */
|
|
219
|
+
interface HeadArticle {
|
|
220
|
+
/** `article:published_time`, an ISO 8601 date. */
|
|
221
|
+
publishedTime?: string;
|
|
222
|
+
/** `article:modified_time`, an ISO 8601 date. */
|
|
223
|
+
modifiedTime?: string;
|
|
224
|
+
/** One `article:author` per name. */
|
|
225
|
+
authors: string[];
|
|
226
|
+
/** One `article:tag` per tag name. */
|
|
227
|
+
tags: string[];
|
|
228
|
+
}
|
|
229
|
+
/** The Open Graph tags of a page. */
|
|
230
|
+
interface HeadOpenGraph {
|
|
231
|
+
type?: string;
|
|
232
|
+
/** `og:locale`, in the `ll_CC` form Open Graph uses (`fr_FR`). */
|
|
233
|
+
locale?: string;
|
|
234
|
+
/** `og:locale:alternate`, in the same form. */
|
|
235
|
+
alternateLocales: string[];
|
|
236
|
+
url?: string;
|
|
237
|
+
siteName?: string;
|
|
238
|
+
title?: string;
|
|
239
|
+
description?: string;
|
|
240
|
+
images: HeadImage[];
|
|
241
|
+
/** The `article:*` tags, set by `articleHead` only. */
|
|
242
|
+
article?: HeadArticle;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* The Twitter card tags of a page. What the card leaves unset is taken from
|
|
246
|
+
* Open Graph, as Next does: title, description and images.
|
|
247
|
+
*/
|
|
248
|
+
interface HeadTwitter {
|
|
249
|
+
/** The card set by the editor, else `summary_large_image` with an image and `summary` without. */
|
|
250
|
+
card: string;
|
|
251
|
+
title?: string;
|
|
252
|
+
description?: string;
|
|
253
|
+
images: HeadImage[];
|
|
254
|
+
}
|
|
255
|
+
/** Everything a page's `<head>` needs for its SEO. */
|
|
256
|
+
interface HeadData {
|
|
257
|
+
/** The `<title>` text, before any layout template. */
|
|
258
|
+
title?: string;
|
|
259
|
+
/**
|
|
260
|
+
* `true` when {@link HeadData.title} is the editor's whole title, brand
|
|
261
|
+
* included: a layout must not append its `%s | Site` template to it.
|
|
262
|
+
*/
|
|
263
|
+
titleAbsolute: boolean;
|
|
264
|
+
description?: string;
|
|
265
|
+
/** The `keywords` meta, comma-separated. */
|
|
266
|
+
keywords?: string;
|
|
267
|
+
/** The canonical URL. */
|
|
268
|
+
canonical?: string;
|
|
269
|
+
/** The hreflang set, empty when the page advertises none. */
|
|
270
|
+
alternates: HeadAlternate[];
|
|
271
|
+
/** The `robots` meta, such as `noindex, nofollow`, or undefined to leave it out. */
|
|
272
|
+
robots?: string;
|
|
273
|
+
openGraph?: HeadOpenGraph;
|
|
274
|
+
twitter?: HeadTwitter;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** `Content-Type` of an XML text route, such as `sitemap.xml`. */
|
|
278
|
+
declare const XML = "application/xml; charset=utf-8";
|
|
279
|
+
/** `Content-Type` of a plain text route, such as `robots.txt` or `llms.txt`. */
|
|
280
|
+
declare const TEXT = "text/plain; charset=utf-8";
|
|
281
|
+
/**
|
|
282
|
+
* Reads one text file from Lynkow for a route. `options` carries the cache
|
|
283
|
+
* tags and duration; pass it to the SDK call. `locale` is the route's, which a
|
|
284
|
+
* localized file such as `llms.txt` passes explicitly, since the SDK reads the
|
|
285
|
+
* locale of that call from its options.
|
|
286
|
+
*/
|
|
287
|
+
type TextReader = (client: Client, options: BaseRequestOptions, locale: string) => Promise<string>;
|
|
288
|
+
/** Options of {@link createLynkowServer}. */
|
|
289
|
+
interface LynkowServerOptions {
|
|
290
|
+
config: LynkowNextConfig;
|
|
291
|
+
/** The client of a locale, typically the function `createClients(config)` returns. */
|
|
292
|
+
client: (locale: string) => Client;
|
|
293
|
+
/**
|
|
294
|
+
* The site's configured domain, which Lynkow builds the absolute URLs of
|
|
295
|
+
* its generated files from, or null when it has none. Read it from a cached
|
|
296
|
+
* call, such as the site configuration the layout already fetches.
|
|
297
|
+
*/
|
|
298
|
+
getSiteDomain: (locale: string) => Promise<string | null | undefined>;
|
|
299
|
+
/**
|
|
300
|
+
* The runtime source of the site's languages, from `createSiteLocales`.
|
|
301
|
+
* Without a static locale list, `textRoute` and `jsonLd` await it; the head
|
|
302
|
+
* builders and `rewriteOrigin`, which are synchronous, read its current
|
|
303
|
+
* value, so call them once it has loaded (after `await siteLocales.get()`).
|
|
304
|
+
*/
|
|
305
|
+
siteLocales?: SiteLocales;
|
|
306
|
+
}
|
|
307
|
+
/** The server helpers {@link createLynkowServer} returns. */
|
|
308
|
+
interface LynkowServer {
|
|
309
|
+
/**
|
|
310
|
+
* An Astro endpoint serving a text file Lynkow generates, such as
|
|
311
|
+
* `sitemap.xml`, `robots.txt` or `llms.txt`: the upstream read is cached
|
|
312
|
+
* under `TAGS.seo` and its URLs are put on their public form through
|
|
313
|
+
* {@link LynkowServer.rewriteOrigin}. Under a `[locale]` segment the route's
|
|
314
|
+
* locale is used; anywhere else, or for a locale that is not configured, the
|
|
315
|
+
* default one. A file the site does not produce (Lynkow answers NOT_FOUND)
|
|
316
|
+
* answers 404; any other error throws.
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* // src/pages/sitemap.xml.ts
|
|
320
|
+
* export const prerender = false
|
|
321
|
+
* export const GET = server.textRoute((client, options) => client.seo.sitemap(options), XML)
|
|
322
|
+
*/
|
|
323
|
+
textRoute(read: TextReader, contentType: string): APIRoute;
|
|
324
|
+
/**
|
|
325
|
+
* The payload of one `<script type="application/ld+json">` holding `nodes`
|
|
326
|
+
* as a single `@graph`, with URLs on this site's public form and every `<`
|
|
327
|
+
* escaped, or null when there are no nodes. Render it with `set:html`.
|
|
328
|
+
*/
|
|
329
|
+
jsonLd(nodes: object[] | null | undefined, locale: string): Promise<string | null>;
|
|
330
|
+
/**
|
|
331
|
+
* The head of a Lynkow page, from its SEO block: canonical URL, hreflang set
|
|
332
|
+
* from the page's own alternates, Open Graph and Twitter tags. The rules are
|
|
333
|
+
* those of `pageMetadata` in `@lynkow/next`.
|
|
334
|
+
*
|
|
335
|
+
* @param fallbackPath - The locale-agnostic path of the route, for the
|
|
336
|
+
* canonical URL when the page advertises no alternate for `locale`
|
|
337
|
+
*/
|
|
338
|
+
pageHead(page: Page, locale: string, fallbackPath: string): HeadData;
|
|
339
|
+
/**
|
|
340
|
+
* The head of a route the site owns rather than one Lynkow drives, such as
|
|
341
|
+
* `/reviews`: its hreflang set is every configured locale.
|
|
342
|
+
*
|
|
343
|
+
* @param path - The locale-agnostic path, with a leading slash
|
|
344
|
+
*/
|
|
345
|
+
routeHead(locale: string, path: string, title: string, description: string): HeadData;
|
|
346
|
+
/** The head of a blog article, from its SEO fields. */
|
|
347
|
+
articleHead(content: Content): HeadData;
|
|
348
|
+
/** The head of a blog category archive, from the category `paths.resolve()` returns. */
|
|
349
|
+
categoryHead(category: ResolvedCategory): HeadData;
|
|
350
|
+
/** The head of a product, from its SEO fields. */
|
|
351
|
+
productHead(product: Product): HeadData;
|
|
352
|
+
/** The head of a product collection, from its SEO fields. */
|
|
353
|
+
productCollectionHead(collection: ProductCollection): HeadData;
|
|
354
|
+
/** The head of a product category, from its SEO fields. */
|
|
355
|
+
productCategoryHead(category: ProductCategory): HeadData;
|
|
356
|
+
/**
|
|
357
|
+
* Puts the absolute URLs of a body the API generated on this site's public
|
|
358
|
+
* form: the site's domain moved to `siteUrl`, and the default locale's
|
|
359
|
+
* prefix removed. Synchronous, so with a runtime source it needs the
|
|
360
|
+
* source loaded first.
|
|
361
|
+
*/
|
|
362
|
+
rewriteOrigin(body: string, siteDomain: string | null | undefined): string;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* The server-side helpers of a Lynkow site on Astro, bound to one
|
|
366
|
+
* configuration: text routes, JSON-LD, and the head data of a page, an
|
|
367
|
+
* article, a category, a product, a product collection and a product category.
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* // src/lib/server.ts
|
|
371
|
+
* import { createLynkowServer } from '@lynkow/astro/server'
|
|
372
|
+
*
|
|
373
|
+
* export const server = createLynkowServer({
|
|
374
|
+
* config,
|
|
375
|
+
* client: (locale) => lynkow(locale),
|
|
376
|
+
* getSiteDomain: async (locale) => (await getSiteShell(locale)).domain,
|
|
377
|
+
* siteLocales,
|
|
378
|
+
* })
|
|
379
|
+
*/
|
|
380
|
+
declare function createLynkowServer({ config, client, getSiteDomain, siteLocales, }: LynkowServerOptions): LynkowServer;
|
|
381
|
+
|
|
382
|
+
/** Options of {@link createRevalidateEndpoint}. */
|
|
383
|
+
interface RevalidateEndpointOptions {
|
|
384
|
+
/**
|
|
385
|
+
* The webhook's signing secret, read from a server-only environment
|
|
386
|
+
* variable such as `LYNKOW_WEBHOOK_SECRET`. Absent or empty, the endpoint
|
|
387
|
+
* stays closed and answers 503.
|
|
388
|
+
*/
|
|
389
|
+
secret: string | undefined;
|
|
390
|
+
/**
|
|
391
|
+
* The runtime source of the site's languages, from `createSiteLocales`. A
|
|
392
|
+
* `site.updated` event marks it stale, unless it names the cookie consent
|
|
393
|
+
* settings as its only change. Pass the instance the rest of the site reads.
|
|
394
|
+
*/
|
|
395
|
+
siteLocales?: SiteLocales;
|
|
396
|
+
/** The data cache to evict from. Defaults to `lynkowCache`, the one `createClients` uses by default. */
|
|
397
|
+
cache?: LynkowCache;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* The `POST` endpoint of a revalidation webhook, for `src/pages/api/revalidate.ts`.
|
|
401
|
+
*
|
|
402
|
+
* Point a Lynkow webhook at it and give it the same secret. Each signed event
|
|
403
|
+
* evicts the cache tags it can invalidate, so an edit is live on the next
|
|
404
|
+
* request instead of waiting out the `REVALIDATE` ceiling. It is the handler
|
|
405
|
+
* of `@lynkow/next`, with the same checks: a missing secret answers 503, a
|
|
406
|
+
* missing, malformed, wrong or stale signature 401, a body over 5 MiB 413, a
|
|
407
|
+
* signed body that names no event 400, and an event the site does not read
|
|
408
|
+
* 200 with nothing evicted. It answers 500 when the cache store fails to
|
|
409
|
+
* evict, or the body cannot be read, so the delivery is retried.
|
|
410
|
+
*
|
|
411
|
+
* @example
|
|
412
|
+
* // src/pages/api/revalidate.ts
|
|
413
|
+
* import { createRevalidateEndpoint } from '@lynkow/astro/server'
|
|
414
|
+
* import { siteLocales } from '@/lib/lynkow'
|
|
415
|
+
*
|
|
416
|
+
* export const prerender = false
|
|
417
|
+
* export const POST = createRevalidateEndpoint({
|
|
418
|
+
* secret: process.env.LYNKOW_WEBHOOK_SECRET,
|
|
419
|
+
* siteLocales,
|
|
420
|
+
* })
|
|
421
|
+
*/
|
|
422
|
+
declare function createRevalidateEndpoint({ secret, siteLocales, cache, }: RevalidateEndpointOptions): APIRoute;
|
|
423
|
+
|
|
424
|
+
/** The envelope the Lynkow API POSTs to a webhook. */
|
|
425
|
+
interface WebhookEnvelope {
|
|
426
|
+
event: string;
|
|
427
|
+
timestamp: string;
|
|
428
|
+
data: Record<string, unknown>;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* The tags one delivered event invalidates, deduplicated.
|
|
432
|
+
*
|
|
433
|
+
* An unknown event returns an empty list rather than throwing. Lynkow adds
|
|
434
|
+
* events without asking its consumers, and a site that fails on an event it has
|
|
435
|
+
* never heard of turns a new CMS feature into an outage.
|
|
436
|
+
*/
|
|
437
|
+
declare function tagsForEvent(envelope: WebhookEnvelope): string[];
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Verify a Lynkow webhook: its HMAC first, then the freshness of the timestamp
|
|
441
|
+
* that HMAC covers.
|
|
442
|
+
*
|
|
443
|
+
* The API builds `{ event, timestamp, data }` once per delivery attempt, signs
|
|
444
|
+
* `JSON.stringify` of it with the webhook's secret, sends the digest as
|
|
445
|
+
* `X-Webhook-Signature: sha256=<hex>`, and POSTs that exact same string as the
|
|
446
|
+
* body (`webhook_service.ts`, `sendWebhook` and `generateSignature`). So the raw
|
|
447
|
+
* request text is verifiable byte for byte, and the `timestamp` inside it is
|
|
448
|
+
* authenticated.
|
|
449
|
+
*
|
|
450
|
+
* That is why the handler reads `await request.text()` and never `request.json()`:
|
|
451
|
+
* parsing and re-serialising would reorder no keys today, but it is not a
|
|
452
|
+
* property JSON guarantees, and a signature checked against a re-serialisation
|
|
453
|
+
* is a signature that fails for reasons nobody can reproduce.
|
|
454
|
+
*
|
|
455
|
+
* Returns `true` only when every check passes, and `false` for anything else,
|
|
456
|
+
* including an empty secret, a missing or malformed header, a body that is not
|
|
457
|
+
* JSON, and a stale or future timestamp. It never throws on request input, so a
|
|
458
|
+
* crafted header is a rejection rather than a 500. The result deliberately does
|
|
459
|
+
* not say which check failed.
|
|
460
|
+
*
|
|
461
|
+
* It is safe to call on its own: it does not rely on the caller having checked
|
|
462
|
+
* that a secret is configured.
|
|
463
|
+
*/
|
|
464
|
+
declare function verifySignature(rawBody: string, header: string | null, secret: string): boolean;
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Runs `fn` with `request` as the current request, which the buyer client
|
|
468
|
+
* reads to forward the buyer's IP address on its writes. The middleware of
|
|
469
|
+
* `@lynkow/astro/middleware` does it for every request; call it yourself only
|
|
470
|
+
* from a middleware of your own that does not use that one.
|
|
471
|
+
*
|
|
472
|
+
* `clientAddress` is the client's address as Astro resolved it, used by
|
|
473
|
+
* {@link headers} when the request carries no `X-Forwarded-For`. Without it,
|
|
474
|
+
* the address Astro's Node server recorded on the request is used, if any.
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* // src/middleware.ts
|
|
478
|
+
* export const onRequest = defineMiddleware((context, next) => {
|
|
479
|
+
* let address: string | undefined
|
|
480
|
+
* try {
|
|
481
|
+
* address = context.clientAddress || undefined
|
|
482
|
+
* } catch {
|
|
483
|
+
* address = undefined // the adapter provides none
|
|
484
|
+
* }
|
|
485
|
+
* return runWithRequest(context.request, () => next(), address)
|
|
486
|
+
* })
|
|
487
|
+
*/
|
|
488
|
+
declare function runWithRequest<T>(request: Request, fn: () => T, clientAddress?: string): T;
|
|
489
|
+
/** The request being served, or undefined outside one (a build, a script). */
|
|
490
|
+
declare function currentRequest(): Request | undefined;
|
|
491
|
+
|
|
492
|
+
export { BUYER_COOKIE_NAMES, TEXT, XML, assertSameOrigin, clearBuyerCookies, createBuyerClient, createLynkowServer, createRevalidateEndpoint, currentRequest, readBuyerTokens, resolveBuyerForwarding, runWithRequest, setCartCookie, setCustomerCookie, signOut, tagsForEvent, verifySignature };
|
|
493
|
+
export type { BuyerClientOptions, BuyerCookieOptions, BuyerForwarding, BuyerTokens, HeadAlternate, HeadArticle, HeadData, HeadImage, HeadOpenGraph, HeadTwitter, LynkowServer, LynkowServerOptions, RevalidateEndpointOptions, SignOutOptions, TextReader, WebhookEnvelope };
|