@cloudexp-io/laudro-site-kit 0.4.0 → 0.5.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 +1 -1
- package/dist/client.d.ts +9 -3
- package/dist/client.js +14 -5
- package/dist/index.d.ts +17 -2
- package/dist/index.js +21 -6
- package/dist/server.d.ts +5 -5
- package/dist/server.js +18 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ The package is public on npmjs.com. No token is needed.
|
|
|
16
16
|
| ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
17
17
|
| `@cloudexp-io/laudro-site-kit` | Plain functions: hours (`openStatus`, `groupWeekly`, `specialDays`), menus and allergens, lunch, prices (`formatMoney`), languages (`localePath`), booking helpers, JSON-LD, sitemap, robots and `buildLlmsTxt`. |
|
|
18
18
|
| `@cloudexp-io/laudro-site-kit/server` | Route handler factories: `createFormHandler`, `createFormTokenHandler`, `createTrackHandler`, `createRevalidateHandler`, `createPreviewHandler`. Imports `server-only`. |
|
|
19
|
-
| `@cloudexp-io/laudro-site-kit/client` | Hooks for client components: `
|
|
19
|
+
| `@cloudexp-io/laudro-site-kit/client` | Hooks for client components: `useLaudroForm`, `useFormToken`, `usePageView`. `useRbForm` still works but is deprecated. |
|
|
20
20
|
|
|
21
21
|
## Example
|
|
22
22
|
|
package/dist/client.d.ts
CHANGED
|
@@ -139,6 +139,8 @@ interface Dictionary {
|
|
|
139
139
|
phone: string;
|
|
140
140
|
};
|
|
141
141
|
invalid: string;
|
|
142
|
+
/** An email field that is not a valid address. */
|
|
143
|
+
invalidEmail: string;
|
|
142
144
|
rateLimited: string;
|
|
143
145
|
failed: string;
|
|
144
146
|
failedCall: (phone: string) => string;
|
|
@@ -154,6 +156,8 @@ interface Dictionary {
|
|
|
154
156
|
company: string;
|
|
155
157
|
message: string;
|
|
156
158
|
sendAnother: string;
|
|
159
|
+
/** "Send another" after a contact message. */
|
|
160
|
+
sendAnotherMessage: string;
|
|
157
161
|
};
|
|
158
162
|
}
|
|
159
163
|
|
|
@@ -178,7 +182,7 @@ interface ParseSchema {
|
|
|
178
182
|
};
|
|
179
183
|
};
|
|
180
184
|
}
|
|
181
|
-
interface
|
|
185
|
+
interface UseLaudroFormOptions {
|
|
182
186
|
action: string;
|
|
183
187
|
tokenUrl: string;
|
|
184
188
|
lang: SiteLang;
|
|
@@ -190,7 +194,7 @@ interface UseRbFormOptions {
|
|
|
190
194
|
}>["data"]) => void;
|
|
191
195
|
phone?: string | null;
|
|
192
196
|
}
|
|
193
|
-
declare function
|
|
197
|
+
declare function useLaudroForm(o: UseLaudroFormOptions): {
|
|
194
198
|
d: Dictionary;
|
|
195
199
|
status: FormStatus;
|
|
196
200
|
errors: Record<string, string>;
|
|
@@ -199,6 +203,8 @@ declare function useRbForm(o: UseRbFormOptions): {
|
|
|
199
203
|
reset: () => void;
|
|
200
204
|
formRef: react.RefObject<HTMLFormElement | null>;
|
|
201
205
|
};
|
|
206
|
+
/** @deprecated Use useLaudroForm. */
|
|
207
|
+
declare const useRbForm: typeof useLaudroForm;
|
|
202
208
|
|
|
203
209
|
/** Fire-and-forget counter to the site's own track route. No cookies, no ids. */
|
|
204
210
|
declare function sendTrack(name: TrackName, lang: SiteLang, opts?: {
|
|
@@ -212,5 +218,5 @@ declare function usePageView({ lang, url, name, }: {
|
|
|
212
218
|
name?: TrackName;
|
|
213
219
|
}): void;
|
|
214
220
|
|
|
215
|
-
export { sendTrack, useFormToken, usePageView, useRbForm };
|
|
221
|
+
export { sendTrack, useFormToken, useLaudroForm, usePageView, useRbForm };
|
|
216
222
|
export type { FormStatus };
|
package/dist/client.js
CHANGED
|
@@ -14,7 +14,7 @@ function clientFieldErrors(issues, body, form) {
|
|
|
14
14
|
if (!key || errs[key]) continue;
|
|
15
15
|
const empty = body[key] === void 0 || body[key] === "";
|
|
16
16
|
const short = issue.code === "too_small" && !empty && key in form.tooShort ? form.tooShort[key] : void 0;
|
|
17
|
-
errs[key] = empty ? form.required : short ?? form.invalid;
|
|
17
|
+
errs[key] = empty ? form.required : short ?? (key === "email" ? form.invalidEmail : form.invalid);
|
|
18
18
|
}
|
|
19
19
|
return errs;
|
|
20
20
|
}
|
|
@@ -24,7 +24,10 @@ function failureMessage(form, phone) {
|
|
|
24
24
|
function submitFailure(res, form, phone) {
|
|
25
25
|
if (res.status === 422 && Object.keys(res.fieldErrors).length > 0) {
|
|
26
26
|
const errors = {};
|
|
27
|
-
for (const key of Object.keys(res.fieldErrors))
|
|
27
|
+
for (const key of Object.keys(res.fieldErrors)) {
|
|
28
|
+
const field = key.split(".")[0] ?? key;
|
|
29
|
+
errors[field] = field === "email" ? form.invalidEmail : form.invalid;
|
|
30
|
+
}
|
|
28
31
|
return { errors, message: null };
|
|
29
32
|
}
|
|
30
33
|
if (res.status === 429) return { errors: {}, message: form.rateLimited };
|
|
@@ -125,6 +128,7 @@ var et = {
|
|
|
125
128
|
required: "Palun t\xE4ida see v\xE4li.",
|
|
126
129
|
tooShort: { phone: "Telefoninumber on liiga l\xFChike" },
|
|
127
130
|
invalid: "Palun kontrolli seda v\xE4lja.",
|
|
131
|
+
invalidEmail: "Palun sisesta korrektne e-posti aadress.",
|
|
128
132
|
rateLimited: "Liiga palju p\xE4ringuid. Proovi m\xF5ne minuti p\xE4rast uuesti.",
|
|
129
133
|
failed: "Saatmine eba\xF5nnestus. Proovi uuesti.",
|
|
130
134
|
failedCall: (p) => `Saatmine eba\xF5nnestus. Proovi uuesti v\xF5i helista ${p}.`,
|
|
@@ -138,7 +142,8 @@ var et = {
|
|
|
138
142
|
perPerson: "/ in",
|
|
139
143
|
company: "Ettev\xF5te",
|
|
140
144
|
message: "S\xF5num",
|
|
141
|
-
sendAnother: "Saada uus p\xE4ring"
|
|
145
|
+
sendAnother: "Saada uus p\xE4ring",
|
|
146
|
+
sendAnotherMessage: "Saada uus s\xF5num"
|
|
142
147
|
}
|
|
143
148
|
};
|
|
144
149
|
var en = {
|
|
@@ -234,6 +239,7 @@ var en = {
|
|
|
234
239
|
required: "Please fill in this field.",
|
|
235
240
|
tooShort: { phone: "Phone number is too short" },
|
|
236
241
|
invalid: "Please check this field.",
|
|
242
|
+
invalidEmail: "Please enter a valid email address.",
|
|
237
243
|
rateLimited: "Too many requests. Please try again in a few minutes.",
|
|
238
244
|
failed: "Sending failed. Please try again.",
|
|
239
245
|
failedCall: (p) => `Sending failed. Please try again or call ${p}.`,
|
|
@@ -247,7 +253,8 @@ var en = {
|
|
|
247
253
|
perPerson: "/ person",
|
|
248
254
|
company: "Company",
|
|
249
255
|
message: "Message",
|
|
250
|
-
sendAnother: "Send another request"
|
|
256
|
+
sendAnother: "Send another request",
|
|
257
|
+
sendAnotherMessage: "Send another message"
|
|
251
258
|
}
|
|
252
259
|
};
|
|
253
260
|
var DICTIONARIES = { et, en };
|
|
@@ -290,7 +297,7 @@ function useFormToken(url) {
|
|
|
290
297
|
return tok.t;
|
|
291
298
|
}, [url]);
|
|
292
299
|
}
|
|
293
|
-
function
|
|
300
|
+
function useLaudroForm(o) {
|
|
294
301
|
const d = dictionary(o.lang);
|
|
295
302
|
const getToken = useFormToken(o.tokenUrl);
|
|
296
303
|
const [status, setStatus] = useState("idle");
|
|
@@ -354,6 +361,7 @@ function focusFirst(form, errs) {
|
|
|
354
361
|
const el = form.elements.namedItem(first);
|
|
355
362
|
if (el && "focus" in el && typeof el.focus === "function") el.focus();
|
|
356
363
|
}
|
|
364
|
+
var useRbForm = useLaudroForm;
|
|
357
365
|
|
|
358
366
|
// src/client/track.ts
|
|
359
367
|
import { usePathname } from "next/navigation";
|
|
@@ -419,6 +427,7 @@ function usePageView({
|
|
|
419
427
|
export {
|
|
420
428
|
sendTrack,
|
|
421
429
|
useFormToken,
|
|
430
|
+
useLaudroForm,
|
|
422
431
|
usePageView,
|
|
423
432
|
useRbForm
|
|
424
433
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -629,6 +629,8 @@ interface Dictionary {
|
|
|
629
629
|
phone: string;
|
|
630
630
|
};
|
|
631
631
|
invalid: string;
|
|
632
|
+
/** An email field that is not a valid address. */
|
|
633
|
+
invalidEmail: string;
|
|
632
634
|
rateLimited: string;
|
|
633
635
|
failed: string;
|
|
634
636
|
failedCall: (phone: string) => string;
|
|
@@ -644,6 +646,8 @@ interface Dictionary {
|
|
|
644
646
|
company: string;
|
|
645
647
|
message: string;
|
|
646
648
|
sendAnother: string;
|
|
649
|
+
/** "Send another" after a contact message. */
|
|
650
|
+
sendAnotherMessage: string;
|
|
647
651
|
};
|
|
648
652
|
}
|
|
649
653
|
declare const DICTIONARIES: Record<SiteLang, Dictionary>;
|
|
@@ -746,10 +750,21 @@ declare function specialDaysFrom(hours: Pick<LocationHours, "days">, kind?: Hour
|
|
|
746
750
|
*/
|
|
747
751
|
declare function splitSpecialDays(specials: SpecialDay[], table: boolean): {
|
|
748
752
|
closedDates: string[];
|
|
753
|
+
/** The same closed days with their note (`null` when none). */
|
|
754
|
+
closedDays: {
|
|
755
|
+
date: string;
|
|
756
|
+
note: string | null;
|
|
757
|
+
}[];
|
|
749
758
|
listed: SpecialDay[];
|
|
750
759
|
};
|
|
751
|
-
/**
|
|
752
|
-
|
|
760
|
+
/**
|
|
761
|
+
* "24.12 ja 25.12 on suletud" for the closed dates (`YYYY-MM-DD`). A day given as
|
|
762
|
+
* `{ date, note }` shows its note: "24.12 (Jõulud) on suletud".
|
|
763
|
+
*/
|
|
764
|
+
declare function closedNoticeText(closedDates: (string | {
|
|
765
|
+
date: string;
|
|
766
|
+
note?: string | null;
|
|
767
|
+
})[], lang: SiteLang): string;
|
|
753
768
|
/** Does `today` (`YYYY-MM-DD`) fall on a weekday of the group? False when `today` is not given. */
|
|
754
769
|
declare function groupHasDay(group: Pick<WeeklyGroup, "from" | "to">, today?: string): boolean;
|
|
755
770
|
/**
|
package/dist/index.js
CHANGED
|
@@ -102,6 +102,7 @@ var et = {
|
|
|
102
102
|
required: "Palun t\xE4ida see v\xE4li.",
|
|
103
103
|
tooShort: { phone: "Telefoninumber on liiga l\xFChike" },
|
|
104
104
|
invalid: "Palun kontrolli seda v\xE4lja.",
|
|
105
|
+
invalidEmail: "Palun sisesta korrektne e-posti aadress.",
|
|
105
106
|
rateLimited: "Liiga palju p\xE4ringuid. Proovi m\xF5ne minuti p\xE4rast uuesti.",
|
|
106
107
|
failed: "Saatmine eba\xF5nnestus. Proovi uuesti.",
|
|
107
108
|
failedCall: (p) => `Saatmine eba\xF5nnestus. Proovi uuesti v\xF5i helista ${p}.`,
|
|
@@ -115,7 +116,8 @@ var et = {
|
|
|
115
116
|
perPerson: "/ in",
|
|
116
117
|
company: "Ettev\xF5te",
|
|
117
118
|
message: "S\xF5num",
|
|
118
|
-
sendAnother: "Saada uus p\xE4ring"
|
|
119
|
+
sendAnother: "Saada uus p\xE4ring",
|
|
120
|
+
sendAnotherMessage: "Saada uus s\xF5num"
|
|
119
121
|
}
|
|
120
122
|
};
|
|
121
123
|
var en = {
|
|
@@ -211,6 +213,7 @@ var en = {
|
|
|
211
213
|
required: "Please fill in this field.",
|
|
212
214
|
tooShort: { phone: "Phone number is too short" },
|
|
213
215
|
invalid: "Please check this field.",
|
|
216
|
+
invalidEmail: "Please enter a valid email address.",
|
|
214
217
|
rateLimited: "Too many requests. Please try again in a few minutes.",
|
|
215
218
|
failed: "Sending failed. Please try again.",
|
|
216
219
|
failedCall: (p) => `Sending failed. Please try again or call ${p}.`,
|
|
@@ -224,7 +227,8 @@ var en = {
|
|
|
224
227
|
perPerson: "/ person",
|
|
225
228
|
company: "Company",
|
|
226
229
|
message: "Message",
|
|
227
|
-
sendAnother: "Send another request"
|
|
230
|
+
sendAnother: "Send another request",
|
|
231
|
+
sendAnotherMessage: "Send another message"
|
|
228
232
|
}
|
|
229
233
|
};
|
|
230
234
|
var DICTIONARIES = { et, en };
|
|
@@ -384,13 +388,21 @@ function specialDaysFrom(hours, kind = "open", today) {
|
|
|
384
388
|
return specialDays(hours, kind).filter((s) => !today || s.date >= today);
|
|
385
389
|
}
|
|
386
390
|
function splitSpecialDays(specials, table) {
|
|
391
|
+
const closed = table ? specials.filter((x) => x.closed) : [];
|
|
387
392
|
return {
|
|
388
|
-
closedDates:
|
|
393
|
+
closedDates: closed.map((x) => x.date),
|
|
394
|
+
closedDays: closed.map((x) => ({ date: x.date, note: x.note?.trim() || null })),
|
|
389
395
|
listed: table ? specials.filter((x) => !x.closed) : specials
|
|
390
396
|
};
|
|
391
397
|
}
|
|
392
398
|
function closedNoticeText(closedDates, lang) {
|
|
393
|
-
return dictionary(lang).closedOn(
|
|
399
|
+
return dictionary(lang).closedOn(
|
|
400
|
+
closedDates.map((x) => {
|
|
401
|
+
if (typeof x === "string") return formatDayMonth(x);
|
|
402
|
+
const note = x.note?.trim();
|
|
403
|
+
return note ? `${formatDayMonth(x.date)} (${note})` : formatDayMonth(x.date);
|
|
404
|
+
})
|
|
405
|
+
);
|
|
394
406
|
}
|
|
395
407
|
function groupHasDay(group, today) {
|
|
396
408
|
return !!today && isoWeekday(today) >= group.from && isoWeekday(today) <= group.to;
|
|
@@ -864,7 +876,7 @@ function clientFieldErrors(issues, body, form) {
|
|
|
864
876
|
if (!key || errs[key]) continue;
|
|
865
877
|
const empty = body[key] === void 0 || body[key] === "";
|
|
866
878
|
const short = issue.code === "too_small" && !empty && key in form.tooShort ? form.tooShort[key] : void 0;
|
|
867
|
-
errs[key] = empty ? form.required : short ?? form.invalid;
|
|
879
|
+
errs[key] = empty ? form.required : short ?? (key === "email" ? form.invalidEmail : form.invalid);
|
|
868
880
|
}
|
|
869
881
|
return errs;
|
|
870
882
|
}
|
|
@@ -874,7 +886,10 @@ function failureMessage(form, phone) {
|
|
|
874
886
|
function submitFailure(res, form, phone) {
|
|
875
887
|
if (res.status === 422 && Object.keys(res.fieldErrors).length > 0) {
|
|
876
888
|
const errors = {};
|
|
877
|
-
for (const key of Object.keys(res.fieldErrors))
|
|
889
|
+
for (const key of Object.keys(res.fieldErrors)) {
|
|
890
|
+
const field = key.split(".")[0] ?? key;
|
|
891
|
+
errors[field] = field === "email" ? form.invalidEmail : form.invalid;
|
|
892
|
+
}
|
|
878
893
|
return { errors, message: null };
|
|
879
894
|
}
|
|
880
895
|
if (res.status === 429) return { errors: {}, message: form.rateLimited };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LaudroClient } from '@cloudexp-io/laudro-sdk/next';
|
|
2
2
|
|
|
3
3
|
interface RevalidateHandlerOptions {
|
|
4
4
|
/** `RB_WEBHOOK_SECRET`. Without it every call gets 503, so a missing env var is visible. */
|
|
@@ -27,7 +27,7 @@ declare function createRevalidateHandler(o: RevalidateHandlerOptions): (req: Req
|
|
|
27
27
|
type FormKind = "booking" | "event" | "contact";
|
|
28
28
|
interface FormHandlerOptions {
|
|
29
29
|
/** Returns the server client, or null when env vars are missing (503). */
|
|
30
|
-
client: () =>
|
|
30
|
+
client: () => LaudroClient | null;
|
|
31
31
|
kind: FormKind;
|
|
32
32
|
/** Max body size in bytes. Default 16 KB. */
|
|
33
33
|
maxBytes?: number;
|
|
@@ -44,7 +44,7 @@ interface FormHandlerOptions {
|
|
|
44
44
|
*/
|
|
45
45
|
declare function createFormHandler(o: FormHandlerOptions): (req: Request) => Promise<Response>;
|
|
46
46
|
interface TrackHandlerOptions {
|
|
47
|
-
client: () =>
|
|
47
|
+
client: () => LaudroClient | null;
|
|
48
48
|
}
|
|
49
49
|
/** Route handler for `/api/track`. Always 204, so a beacon never shows an error. */
|
|
50
50
|
declare function createTrackHandler(o: TrackHandlerOptions): (req: Request) => Promise<Response>;
|
|
@@ -60,7 +60,7 @@ declare function createFormTokenHandler(o: FormTokenHandlerOptions): () => Promi
|
|
|
60
60
|
|
|
61
61
|
declare const PREVIEW_COOKIE = "laudro_preview";
|
|
62
62
|
interface PreviewHandlerOptions {
|
|
63
|
-
client: () =>
|
|
63
|
+
client: () => LaudroClient | null;
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* Route handler for `/api/laudro-preview?token=...&path=/menu`. Checks the token with the API,
|
|
@@ -73,7 +73,7 @@ declare function createPreviewExitHandler(): (req: Request) => Promise<Response>
|
|
|
73
73
|
* The client to read with on a page: the preview client while Draft Mode is on, else `base`.
|
|
74
74
|
* Reads cookies only in Draft Mode, so normal pages stay static.
|
|
75
75
|
*/
|
|
76
|
-
declare function getPreviewAwareClient(base:
|
|
76
|
+
declare function getPreviewAwareClient(base: LaudroClient): Promise<LaudroClient>;
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* The visitor's IP as the site's host saw it. `x-real-ip` first (set by Vercel and most
|
package/dist/server.js
CHANGED
|
@@ -76,7 +76,11 @@ function createRevalidateHandler(o) {
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
// src/server/forms.ts
|
|
79
|
-
import {
|
|
79
|
+
import {
|
|
80
|
+
LaudroApiError,
|
|
81
|
+
signFormToken,
|
|
82
|
+
TrackEvent
|
|
83
|
+
} from "@cloudexp-io/laudro-sdk/next";
|
|
80
84
|
function isZodError(e) {
|
|
81
85
|
return !!e && typeof e === "object" && e.name === "ZodError" && Array.isArray(e.issues);
|
|
82
86
|
}
|
|
@@ -97,7 +101,18 @@ function createFormHandler(o) {
|
|
|
97
101
|
return problem(400, "Invalid JSON");
|
|
98
102
|
}
|
|
99
103
|
const ip = visitorIp(req);
|
|
100
|
-
const opts =
|
|
104
|
+
const opts = {
|
|
105
|
+
...ip ? { ip } : {},
|
|
106
|
+
// Demo sites only: the API says why it dropped the form. Logged, never sent to the browser.
|
|
107
|
+
onResponse: (h) => {
|
|
108
|
+
const m = /^dropped=([a-z_]+)$/.exec(h.get("laudro-debug") ?? "");
|
|
109
|
+
if (m) {
|
|
110
|
+
console.warn(
|
|
111
|
+
`[laudro] request dropped by spam check: ${m[1]} (demo sites only; see https://developers.laudro.app/docs/sdk#forms)`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
101
116
|
try {
|
|
102
117
|
const created = o.kind === "booking" ? await rb.bookingRequests.create(body, opts) : o.kind === "event" ? await rb.eventRequests.create(body, opts) : await rb.contact.create(body, opts);
|
|
103
118
|
return json({ id: created.id, status: created.status }, 201);
|
|
@@ -111,7 +126,7 @@ function createFormHandler(o) {
|
|
|
111
126
|
}))
|
|
112
127
|
});
|
|
113
128
|
}
|
|
114
|
-
if (e instanceof
|
|
129
|
+
if (e instanceof LaudroApiError) {
|
|
115
130
|
const status = e.status >= 400 && e.status < 600 ? e.status : 502;
|
|
116
131
|
if (e.problem)
|
|
117
132
|
return json(e.problem, status, { "content-type": "application/problem+json" });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudexp-io/laudro-site-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Logic and route handlers for restaurant sites on Next.js: hours, menus, lunch, pages, forms, JSON-LD, sitemap, llms.txt, revalidate. No components, no CSS.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@date-fns/tz": "^1.5.0",
|
|
36
36
|
"date-fns": "^4.4.0",
|
|
37
37
|
"server-only": "^0.0.1",
|
|
38
|
-
"@cloudexp-io/laudro-sdk": "0.
|
|
38
|
+
"@cloudexp-io/laudro-sdk": "0.5.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "^22.20.5",
|