@cloudexp-io/laudro-sdk 0.0.0-stage → 0.4.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 +21 -0
- package/README.md +54 -2
- package/dist/chunk-4ADLD5ZW.js +845 -0
- package/dist/chunk-6J7GJOQI.js +18 -0
- package/dist/chunk-6LTB3GRQ.js +357 -0
- package/dist/core.d.ts +1684 -0
- package/dist/core.js +178 -0
- package/dist/forms.d.ts +37 -0
- package/dist/forms.js +62 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +180 -0
- package/dist/next.d.ts +263 -0
- package/dist/next.js +180 -0
- package/dist/retry-BnQHOsTh.d.ts +409 -0
- package/package.json +52 -3
|
@@ -0,0 +1,845 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Allergen,
|
|
3
|
+
BookingLocation,
|
|
4
|
+
BookingRequestCreate,
|
|
5
|
+
BookingResponse,
|
|
6
|
+
ContactCreate,
|
|
7
|
+
DietTag,
|
|
8
|
+
EventPackage,
|
|
9
|
+
EventPackagesResponse,
|
|
10
|
+
EventRequestCreate,
|
|
11
|
+
IsoDate,
|
|
12
|
+
IsoDateTime,
|
|
13
|
+
IsoWeek,
|
|
14
|
+
Lang,
|
|
15
|
+
LangFallback,
|
|
16
|
+
LangQuery,
|
|
17
|
+
LocalTime,
|
|
18
|
+
Location,
|
|
19
|
+
LocationsResponse,
|
|
20
|
+
Media,
|
|
21
|
+
Money,
|
|
22
|
+
Problem,
|
|
23
|
+
ProblemFieldError,
|
|
24
|
+
RequestCreated,
|
|
25
|
+
Site,
|
|
26
|
+
TrackEvent,
|
|
27
|
+
Uuid,
|
|
28
|
+
WeeklyHours,
|
|
29
|
+
isRetryableIdempotentWrite,
|
|
30
|
+
isRetryableRead,
|
|
31
|
+
isRetryableWrite,
|
|
32
|
+
withRetry
|
|
33
|
+
} from "./chunk-6LTB3GRQ.js";
|
|
34
|
+
|
|
35
|
+
// ../contracts/src/hours.ts
|
|
36
|
+
import { z } from "zod";
|
|
37
|
+
var HoursKind = z.enum(["open", "kitchen", "booking", "lunch"]);
|
|
38
|
+
var HOURS_MAX_DAYS = 62;
|
|
39
|
+
var HoursQuery = z.object({
|
|
40
|
+
from: IsoDate.describe("First date to return."),
|
|
41
|
+
to: IsoDate.describe("Last date to return, at most 62 days from `from`."),
|
|
42
|
+
location_id: Uuid.optional().describe("Only this location. Default: the main location."),
|
|
43
|
+
lang: Lang.default("et").describe("Language of the text.")
|
|
44
|
+
});
|
|
45
|
+
var HoursInterval = z.object({
|
|
46
|
+
opens: LocalTime,
|
|
47
|
+
closes: LocalTime,
|
|
48
|
+
opens_at: IsoDateTime,
|
|
49
|
+
closes_at: IsoDateTime
|
|
50
|
+
});
|
|
51
|
+
var KindHours = z.object({
|
|
52
|
+
closed: z.boolean(),
|
|
53
|
+
intervals: z.array(HoursInterval),
|
|
54
|
+
/** True when a special-hours row decided this date. */
|
|
55
|
+
special: z.boolean(),
|
|
56
|
+
note: z.string().nullable()
|
|
57
|
+
});
|
|
58
|
+
var HoursDay = z.object({
|
|
59
|
+
date: IsoDate,
|
|
60
|
+
/** ISO weekday, 1 = Monday. */
|
|
61
|
+
weekday: z.int().min(1).max(7),
|
|
62
|
+
/** Public holiday name in the asked language, or null. */
|
|
63
|
+
holiday: z.string().nullable(),
|
|
64
|
+
/** Each kind separately. Null when the location has no hours of that kind at all. */
|
|
65
|
+
open: KindHours.nullable(),
|
|
66
|
+
kitchen: KindHours.nullable(),
|
|
67
|
+
booking: KindHours.nullable(),
|
|
68
|
+
lunch: KindHours.nullable()
|
|
69
|
+
});
|
|
70
|
+
var LocationHours = z.object({
|
|
71
|
+
location_id: Uuid,
|
|
72
|
+
timezone: z.string(),
|
|
73
|
+
days: z.array(HoursDay)
|
|
74
|
+
});
|
|
75
|
+
var HoursResponse = z.object({
|
|
76
|
+
from: IsoDate.describe("First date returned."),
|
|
77
|
+
to: IsoDate.describe("Last date returned."),
|
|
78
|
+
locations: z.array(LocationHours).describe("Hours for each location."),
|
|
79
|
+
lang: Lang.describe("Language of the text in this response.")
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// ../contracts/src/menus.ts
|
|
83
|
+
import { z as z2 } from "zod";
|
|
84
|
+
var MenuKind = z2.enum(["main", "drinks", "lunch", "kids", "event"]);
|
|
85
|
+
var MenusQuery = z2.object({
|
|
86
|
+
lang: Lang.default("et").describe("Language of the text."),
|
|
87
|
+
kind: MenuKind.optional().describe("Only menus of this kind.")
|
|
88
|
+
});
|
|
89
|
+
var MenuItemVariant = z2.object({
|
|
90
|
+
id: Uuid,
|
|
91
|
+
name: z2.string(),
|
|
92
|
+
price: Money.nullable(),
|
|
93
|
+
lang_fallback: LangFallback
|
|
94
|
+
});
|
|
95
|
+
var MenuItem = z2.object({
|
|
96
|
+
id: Uuid,
|
|
97
|
+
name: z2.string(),
|
|
98
|
+
description: z2.string().nullable(),
|
|
99
|
+
/** A short line under the item name, e.g. "Miso · Noodles · Tofu". */
|
|
100
|
+
tagline: z2.string().nullable(),
|
|
101
|
+
/** The ingredient list as free text for guests. Not a source of allergen data; use `allergens`. */
|
|
102
|
+
ingredients: z2.string().nullable(),
|
|
103
|
+
price: Money.nullable(),
|
|
104
|
+
/** True while `sold_out_until` is today or later (restaurant time). */
|
|
105
|
+
sold_out: z2.boolean(),
|
|
106
|
+
sold_out_until: IsoDate.nullable(),
|
|
107
|
+
image: Media.nullable(),
|
|
108
|
+
allergens: z2.array(Allergen),
|
|
109
|
+
tags: z2.array(DietTag),
|
|
110
|
+
variants: z2.array(MenuItemVariant),
|
|
111
|
+
lang_fallback: LangFallback
|
|
112
|
+
});
|
|
113
|
+
var MenuSection = z2.object({
|
|
114
|
+
id: Uuid,
|
|
115
|
+
name: z2.string(),
|
|
116
|
+
description: z2.string().nullable(),
|
|
117
|
+
items: z2.array(MenuItem),
|
|
118
|
+
lang_fallback: LangFallback
|
|
119
|
+
});
|
|
120
|
+
var Menu = z2.object({
|
|
121
|
+
id: Uuid,
|
|
122
|
+
name: z2.string(),
|
|
123
|
+
description: z2.string().nullable(),
|
|
124
|
+
kind: MenuKind,
|
|
125
|
+
sections: z2.array(MenuSection),
|
|
126
|
+
lang_fallback: LangFallback
|
|
127
|
+
});
|
|
128
|
+
var MenusResponse = z2.object({
|
|
129
|
+
menus: z2.array(Menu).describe("Published menus."),
|
|
130
|
+
lang: Lang.describe("Language of the text in this response.")
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// ../contracts/src/lunch.ts
|
|
134
|
+
import { z as z3 } from "zod";
|
|
135
|
+
var LunchQuery = z3.object({
|
|
136
|
+
date: IsoDate.optional().describe("One day. Default: today."),
|
|
137
|
+
week: IsoWeek.optional().describe("One ISO week, for example 2026-W47."),
|
|
138
|
+
location_id: Uuid.optional().describe("Only this location. Default: the main location."),
|
|
139
|
+
lang: Lang.default("et").describe("Language of the text.")
|
|
140
|
+
}).refine((q) => !(q.date && q.week), { message: "Use date or week, not both", path: ["week"] });
|
|
141
|
+
var LunchItem = z3.object({
|
|
142
|
+
id: Uuid,
|
|
143
|
+
name: z3.string(),
|
|
144
|
+
description: z3.string().nullable(),
|
|
145
|
+
price: Money.nullable(),
|
|
146
|
+
allergens: z3.array(Allergen),
|
|
147
|
+
tags: z3.array(DietTag),
|
|
148
|
+
/** Sold out today: show it on the lunch board, keep it listed. */
|
|
149
|
+
sold_out: z3.boolean().default(false),
|
|
150
|
+
lang_fallback: LangFallback
|
|
151
|
+
});
|
|
152
|
+
var LunchOffer = z3.object({
|
|
153
|
+
id: Uuid,
|
|
154
|
+
location_id: Uuid,
|
|
155
|
+
date: IsoDate,
|
|
156
|
+
/** `draft` only appears in preview mode. */
|
|
157
|
+
status: z3.enum(["live", "draft"]),
|
|
158
|
+
set_price: Money.nullable(),
|
|
159
|
+
served_from: LocalTime.nullable(),
|
|
160
|
+
served_to: LocalTime.nullable(),
|
|
161
|
+
note: z3.string().nullable(),
|
|
162
|
+
items: z3.array(LunchItem),
|
|
163
|
+
lang_fallback: LangFallback
|
|
164
|
+
});
|
|
165
|
+
var LunchDay = z3.object({
|
|
166
|
+
date: IsoDate,
|
|
167
|
+
offer: LunchOffer.nullable()
|
|
168
|
+
});
|
|
169
|
+
var LunchDayResponse = z3.object({
|
|
170
|
+
date: IsoDate.describe("The day asked for."),
|
|
171
|
+
location_id: Uuid.nullable().describe("Location of the offer, or null."),
|
|
172
|
+
offer: LunchOffer.nullable().describe("The lunch offer, or null when there is none."),
|
|
173
|
+
/** Next date after `date` with an offer, or null. */
|
|
174
|
+
next_date: IsoDate.nullable(),
|
|
175
|
+
lang: Lang.describe("Language of the text in this response.")
|
|
176
|
+
});
|
|
177
|
+
var LunchWeekResponse = z3.object({
|
|
178
|
+
week: IsoWeek.describe("The ISO week asked for."),
|
|
179
|
+
location_id: Uuid.nullable().describe("Location of the offers, or null."),
|
|
180
|
+
days: z3.array(LunchDay).describe("Monday to Sunday, each with its offer or null."),
|
|
181
|
+
/** Next date after the week with an offer, or null. */
|
|
182
|
+
next_date: IsoDate.nullable(),
|
|
183
|
+
lang: Lang.describe("Language of the text in this response.")
|
|
184
|
+
});
|
|
185
|
+
var LunchResponse = z3.union([LunchDayResponse, LunchWeekResponse]);
|
|
186
|
+
|
|
187
|
+
// ../contracts/src/pages.ts
|
|
188
|
+
import { z as z4 } from "zod";
|
|
189
|
+
var PageKey = z4.string().regex(/^[a-z0-9][a-z0-9-]{0,62}$/, "Lowercase letters, digits, -");
|
|
190
|
+
var PageBlock = z4.looseObject({ type: z4.string() });
|
|
191
|
+
var Page = z4.object({
|
|
192
|
+
id: Uuid.describe("Page id."),
|
|
193
|
+
key: PageKey.describe("Page key, as in the path."),
|
|
194
|
+
title: z4.string().describe("Page title."),
|
|
195
|
+
blocks: z4.array(PageBlock).describe("Content blocks in display order."),
|
|
196
|
+
seo: z4.object({
|
|
197
|
+
title: z4.string().nullable(),
|
|
198
|
+
description: z4.string().nullable()
|
|
199
|
+
}),
|
|
200
|
+
lang: Lang.describe("Language of the text in this response."),
|
|
201
|
+
lang_fallback: LangFallback
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// ../contracts/src/webhook.ts
|
|
205
|
+
import { z as z5 } from "zod";
|
|
206
|
+
var CACHE_TAGS = {
|
|
207
|
+
site: "laudro:site",
|
|
208
|
+
menus: "laudro:menus",
|
|
209
|
+
lunch: "laudro:lunch",
|
|
210
|
+
hours: "laudro:hours",
|
|
211
|
+
packages: "laudro:packages",
|
|
212
|
+
booking: "laudro:booking",
|
|
213
|
+
page: (key) => `laudro:pages:${key}`
|
|
214
|
+
};
|
|
215
|
+
var CacheTag = z5.string().regex(/^(?:laudro|rb):[a-z]+(:[a-z0-9-]+)?$/);
|
|
216
|
+
var WebhookPayload = z5.object({
|
|
217
|
+
id: z5.string().startsWith("evt_"),
|
|
218
|
+
event: z5.literal("content.updated"),
|
|
219
|
+
site_id: Uuid,
|
|
220
|
+
tags: z5.array(CacheTag),
|
|
221
|
+
at: IsoDateTime
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// ../contracts/src/preview.ts
|
|
225
|
+
import { z as z6 } from "zod";
|
|
226
|
+
var PREVIEW_TOKEN_HEADER = "laudro-preview-token";
|
|
227
|
+
var LEGACY_PREVIEW_TOKEN_HEADER = "rb-preview-token";
|
|
228
|
+
var PreviewQuery = z6.object({ token: z6.string().min(1).max(2e3) });
|
|
229
|
+
var PreviewCheck = z6.object({
|
|
230
|
+
valid: z6.literal(true).describe("Always true. An invalid token gets 403."),
|
|
231
|
+
restaurant_id: Uuid.describe("Restaurant the token is for."),
|
|
232
|
+
/** Path to open on the site, e.g. /lunch. */
|
|
233
|
+
path: z6.string().describe("Path to open on the site."),
|
|
234
|
+
expires_at: IsoDateTime.describe("When the token stops working.")
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// ../contracts/src/openapi.ts
|
|
238
|
+
import { z as z7 } from "zod";
|
|
239
|
+
var HealthResponse = z7.object({
|
|
240
|
+
ok: z7.boolean(),
|
|
241
|
+
db: z7.boolean(),
|
|
242
|
+
worker_heartbeat_at: z7.iso.datetime().nullable()
|
|
243
|
+
});
|
|
244
|
+
var API_ROUTES = [
|
|
245
|
+
{
|
|
246
|
+
method: "get",
|
|
247
|
+
path: "/health",
|
|
248
|
+
operationId: "getHealth",
|
|
249
|
+
summary: "Liveness and DB check with the last worker heartbeat. Never cached.",
|
|
250
|
+
response: { status: 200, schema: HealthResponse },
|
|
251
|
+
extraResponses: { "503": { description: "Database unreachable", schema: HealthResponse } },
|
|
252
|
+
public: true
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
method: "get",
|
|
256
|
+
path: "/schema",
|
|
257
|
+
operationId: "getSchema",
|
|
258
|
+
summary: "This OpenAPI 3.1 document. Also served at /openapi.json.",
|
|
259
|
+
response: {
|
|
260
|
+
status: 200,
|
|
261
|
+
schema: z7.record(z7.string(), z7.unknown()),
|
|
262
|
+
description: "Returns the OpenAPI 3.1 document for this API."
|
|
263
|
+
},
|
|
264
|
+
public: true
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
method: "get",
|
|
268
|
+
path: "/site",
|
|
269
|
+
operationId: "getSite",
|
|
270
|
+
summary: "Restaurant name, logo, brand, social links, languages, booking mode, contact",
|
|
271
|
+
query: LangQuery,
|
|
272
|
+
response: { status: 200, schema: Site },
|
|
273
|
+
tags: ["laudro:site"]
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
method: "get",
|
|
277
|
+
path: "/locations",
|
|
278
|
+
operationId: "getLocations",
|
|
279
|
+
summary: "Locations with address, geo and phone",
|
|
280
|
+
query: LangQuery,
|
|
281
|
+
response: { status: 200, schema: LocationsResponse },
|
|
282
|
+
tags: ["laudro:site"]
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
method: "get",
|
|
286
|
+
path: "/hours",
|
|
287
|
+
operationId: "getHours",
|
|
288
|
+
summary: "Resolved hours per date (weekly and special merged), max 62 days, each kind separate",
|
|
289
|
+
query: HoursQuery,
|
|
290
|
+
response: { status: 200, schema: HoursResponse },
|
|
291
|
+
tags: ["laudro:hours"]
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
method: "get",
|
|
295
|
+
path: "/menus",
|
|
296
|
+
operationId: "getMenus",
|
|
297
|
+
summary: "Published menus with sections, items, variants, allergens and tags",
|
|
298
|
+
query: MenusQuery,
|
|
299
|
+
response: { status: 200, schema: MenusResponse },
|
|
300
|
+
tags: ["laudro:menus"]
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
method: "get",
|
|
304
|
+
path: "/lunch",
|
|
305
|
+
operationId: "getLunch",
|
|
306
|
+
summary: "Lunch offer for a date, or each day of an ISO week",
|
|
307
|
+
query: LunchQuery,
|
|
308
|
+
response: { status: 200, schema: z7.union([LunchDayResponse, LunchWeekResponse]) },
|
|
309
|
+
tags: ["laudro:lunch"]
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
method: "get",
|
|
313
|
+
path: "/pages/{key}",
|
|
314
|
+
operationId: "getPage",
|
|
315
|
+
summary: "Page blocks and SEO fields",
|
|
316
|
+
query: LangQuery,
|
|
317
|
+
pathParams: { key: z7.string() },
|
|
318
|
+
response: { status: 200, schema: Page },
|
|
319
|
+
tags: ["laudro:pages:{key}"]
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
method: "get",
|
|
323
|
+
path: "/events/packages",
|
|
324
|
+
operationId: "getEventPackages",
|
|
325
|
+
summary: "Published event packages",
|
|
326
|
+
query: LangQuery,
|
|
327
|
+
response: { status: 200, schema: EventPackagesResponse },
|
|
328
|
+
tags: ["laudro:packages"]
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
method: "get",
|
|
332
|
+
path: "/booking",
|
|
333
|
+
operationId: "getBooking",
|
|
334
|
+
summary: "Booking settings: mode, provider link, party limits, booking hours",
|
|
335
|
+
query: LangQuery,
|
|
336
|
+
response: { status: 200, schema: BookingResponse },
|
|
337
|
+
tags: ["laudro:booking"]
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
method: "get",
|
|
341
|
+
path: "/preview",
|
|
342
|
+
operationId: "checkPreview",
|
|
343
|
+
summary: "Check a preview token before turning on Draft Mode",
|
|
344
|
+
query: PreviewQuery,
|
|
345
|
+
response: { status: 200, schema: PreviewCheck }
|
|
346
|
+
},
|
|
347
|
+
{
|
|
348
|
+
method: "post",
|
|
349
|
+
path: "/booking-requests",
|
|
350
|
+
operationId: "createBookingRequest",
|
|
351
|
+
summary: "Send a booking request",
|
|
352
|
+
body: BookingRequestCreate,
|
|
353
|
+
response: { status: 200, schema: RequestCreated },
|
|
354
|
+
idempotent: true
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
method: "post",
|
|
358
|
+
path: "/event-requests",
|
|
359
|
+
operationId: "createEventRequest",
|
|
360
|
+
summary: "Send an event request",
|
|
361
|
+
body: EventRequestCreate,
|
|
362
|
+
response: { status: 200, schema: RequestCreated },
|
|
363
|
+
idempotent: true
|
|
364
|
+
},
|
|
365
|
+
{
|
|
366
|
+
method: "post",
|
|
367
|
+
path: "/contact",
|
|
368
|
+
operationId: "createContactMessage",
|
|
369
|
+
summary: "Send a contact message",
|
|
370
|
+
body: ContactCreate,
|
|
371
|
+
response: { status: 200, schema: RequestCreated },
|
|
372
|
+
idempotent: true
|
|
373
|
+
},
|
|
374
|
+
{
|
|
375
|
+
method: "post",
|
|
376
|
+
path: "/track",
|
|
377
|
+
operationId: "track",
|
|
378
|
+
summary: "Count one site event (daily counters, no personal data)",
|
|
379
|
+
body: TrackEvent,
|
|
380
|
+
response: { status: 204, schema: null }
|
|
381
|
+
}
|
|
382
|
+
];
|
|
383
|
+
var OUTPUT_COMPONENTS = {
|
|
384
|
+
Problem,
|
|
385
|
+
ProblemFieldError,
|
|
386
|
+
Money,
|
|
387
|
+
Media,
|
|
388
|
+
Allergen,
|
|
389
|
+
Site,
|
|
390
|
+
Location,
|
|
391
|
+
LocationsResponse,
|
|
392
|
+
HoursInterval,
|
|
393
|
+
KindHours,
|
|
394
|
+
HoursDay,
|
|
395
|
+
LocationHours,
|
|
396
|
+
HoursResponse,
|
|
397
|
+
MenuItemVariant,
|
|
398
|
+
MenuItem,
|
|
399
|
+
MenuSection,
|
|
400
|
+
Menu,
|
|
401
|
+
MenusResponse,
|
|
402
|
+
LunchItem,
|
|
403
|
+
LunchOffer,
|
|
404
|
+
LunchDay,
|
|
405
|
+
LunchDayResponse,
|
|
406
|
+
LunchWeekResponse,
|
|
407
|
+
PageBlock,
|
|
408
|
+
Page,
|
|
409
|
+
EventPackage,
|
|
410
|
+
EventPackagesResponse,
|
|
411
|
+
WeeklyHours,
|
|
412
|
+
BookingLocation,
|
|
413
|
+
BookingResponse,
|
|
414
|
+
RequestCreated,
|
|
415
|
+
PreviewCheck,
|
|
416
|
+
WebhookPayload
|
|
417
|
+
};
|
|
418
|
+
var INPUT_COMPONENTS = {
|
|
419
|
+
BookingRequestCreate,
|
|
420
|
+
EventRequestCreate,
|
|
421
|
+
ContactCreate,
|
|
422
|
+
TrackEvent
|
|
423
|
+
};
|
|
424
|
+
var ref = (id) => ({ $ref: `#/components/schemas/${id}` });
|
|
425
|
+
function componentSchemas(components, io) {
|
|
426
|
+
const registry = z7.registry();
|
|
427
|
+
for (const [id, schema] of Object.entries(components)) registry.add(schema, { id });
|
|
428
|
+
const out = z7.toJSONSchema(registry, {
|
|
429
|
+
io,
|
|
430
|
+
unrepresentable: "any",
|
|
431
|
+
uri: (id) => `#/components/schemas/${id}`
|
|
432
|
+
});
|
|
433
|
+
for (const s of Object.values(out.schemas)) {
|
|
434
|
+
delete s.$schema;
|
|
435
|
+
delete s.$id;
|
|
436
|
+
}
|
|
437
|
+
return out.schemas;
|
|
438
|
+
}
|
|
439
|
+
function idOf(schema, components) {
|
|
440
|
+
return Object.entries(components).find(([, s]) => s === schema)?.[0];
|
|
441
|
+
}
|
|
442
|
+
function inlineSchema(schema, io) {
|
|
443
|
+
const s = z7.toJSONSchema(schema, { io, unrepresentable: "any" });
|
|
444
|
+
delete s.$schema;
|
|
445
|
+
return s;
|
|
446
|
+
}
|
|
447
|
+
var problemResponse = (description) => ({
|
|
448
|
+
description,
|
|
449
|
+
content: { "application/problem+json": { schema: ref("Problem") } }
|
|
450
|
+
});
|
|
451
|
+
var IDEMPOTENCY_KEY_PARAMETER = {
|
|
452
|
+
name: "Idempotency-Key",
|
|
453
|
+
in: "header",
|
|
454
|
+
required: false,
|
|
455
|
+
description: "1 to 255 printable ASCII characters (a UUID works well). Keys are per site and last 24 hours. Repeating a key with the same body replays the stored response with the same status and the header `Idempotent-Replayed: true`. Repeating a key with a different body returns 422 (field `Idempotency-Key`, code `reused`). An invalid key returns 422 (code `invalid`). While the first request with the key is still running, a repeat returns 409 with `Retry-After`. A failed request (non-2xx) frees the key, so it can be retried.",
|
|
456
|
+
schema: { type: "string", minLength: 1, maxLength: 255, pattern: "^[\\x21-\\x7E]{1,255}$" }
|
|
457
|
+
};
|
|
458
|
+
function buildOpenApi(opts = {}) {
|
|
459
|
+
const schemas = {
|
|
460
|
+
...componentSchemas(OUTPUT_COMPONENTS, "output"),
|
|
461
|
+
...componentSchemas(INPUT_COMPONENTS, "input")
|
|
462
|
+
};
|
|
463
|
+
const paths = {};
|
|
464
|
+
for (const route of API_ROUTES) {
|
|
465
|
+
const parameters = [];
|
|
466
|
+
for (const [name, schema] of Object.entries(route.pathParams ?? {})) {
|
|
467
|
+
parameters.push({ name, in: "path", required: true, schema: inlineSchema(schema, "input") });
|
|
468
|
+
}
|
|
469
|
+
if (route.query) {
|
|
470
|
+
const q = inlineSchema(route.query, "input");
|
|
471
|
+
for (const [name, schema] of Object.entries(q.properties ?? {})) {
|
|
472
|
+
parameters.push({
|
|
473
|
+
name,
|
|
474
|
+
in: "query",
|
|
475
|
+
required: q.required?.includes(name) ?? false,
|
|
476
|
+
schema
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (route.method === "get" && !route.public) {
|
|
481
|
+
parameters.push({
|
|
482
|
+
name: "preview",
|
|
483
|
+
in: "query",
|
|
484
|
+
required: false,
|
|
485
|
+
description: "1 = include drafts. Needs a valid laudro-preview-token header (the old rb-preview-token still works).",
|
|
486
|
+
schema: { type: "string", enum: ["1"] }
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
const responses = {};
|
|
490
|
+
const r = route.response;
|
|
491
|
+
if (r.schema) {
|
|
492
|
+
const id = idOf(r.schema, OUTPUT_COMPONENTS);
|
|
493
|
+
responses[String(r.status)] = {
|
|
494
|
+
description: r.description ?? "OK",
|
|
495
|
+
content: {
|
|
496
|
+
"application/json": { schema: id ? ref(id) : inlineSchema(r.schema, "output") }
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
} else {
|
|
500
|
+
responses[String(r.status)] = { description: "No content" };
|
|
501
|
+
}
|
|
502
|
+
if (route.public) {
|
|
503
|
+
for (const [status, extra] of Object.entries(route.extraResponses ?? {})) {
|
|
504
|
+
responses[status] = extra.schema ? {
|
|
505
|
+
description: extra.description,
|
|
506
|
+
content: { "application/json": { schema: inlineSchema(extra.schema, "output") } }
|
|
507
|
+
} : { description: extra.description };
|
|
508
|
+
}
|
|
509
|
+
} else {
|
|
510
|
+
if (route.method === "get") responses["304"] = { description: "Not modified (ETag)" };
|
|
511
|
+
responses["400"] = problemResponse("Bad query or body");
|
|
512
|
+
responses["401"] = problemResponse("Missing or wrong site secret");
|
|
513
|
+
if (route.method === "post") responses["422"] = problemResponse("Field errors");
|
|
514
|
+
responses["429"] = problemResponse("Rate limited. See Retry-After.");
|
|
515
|
+
}
|
|
516
|
+
if (route.idempotent) {
|
|
517
|
+
parameters.push({ $ref: "#/components/parameters/IdempotencyKey" });
|
|
518
|
+
const ok = responses[String(r.status)];
|
|
519
|
+
ok.headers = {
|
|
520
|
+
"Idempotent-Replayed": {
|
|
521
|
+
description: "`true` when this response is a replay of an earlier request with the same Idempotency-Key.",
|
|
522
|
+
schema: { type: "string", enum: ["true"] }
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
responses["409"] = {
|
|
526
|
+
...problemResponse(
|
|
527
|
+
"A request with this Idempotency-Key is still in progress. See Retry-After."
|
|
528
|
+
),
|
|
529
|
+
headers: {
|
|
530
|
+
"Retry-After": {
|
|
531
|
+
description: "Seconds to wait before retrying.",
|
|
532
|
+
schema: { type: "integer" }
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
const op = {
|
|
538
|
+
operationId: route.operationId,
|
|
539
|
+
summary: route.summary,
|
|
540
|
+
parameters,
|
|
541
|
+
responses
|
|
542
|
+
};
|
|
543
|
+
if (route.public) op.security = [];
|
|
544
|
+
if (route.tags) op["x-cache-tags"] = route.tags;
|
|
545
|
+
if (route.body) {
|
|
546
|
+
const id = idOf(route.body, INPUT_COMPONENTS);
|
|
547
|
+
op.requestBody = {
|
|
548
|
+
required: true,
|
|
549
|
+
content: {
|
|
550
|
+
"application/json": { schema: id ? ref(id) : inlineSchema(route.body, "input") }
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
paths[route.path] ??= {};
|
|
555
|
+
paths[route.path][route.method] = op;
|
|
556
|
+
}
|
|
557
|
+
return {
|
|
558
|
+
openapi: "3.1.0",
|
|
559
|
+
info: {
|
|
560
|
+
title: "Laudro public API",
|
|
561
|
+
version: opts.version ?? "1.0.0",
|
|
562
|
+
description: "Server-to-server API for restaurant websites. `/v1` only grows: fields and endpoints are added, never renamed or removed."
|
|
563
|
+
},
|
|
564
|
+
servers: [{ url: opts.serverUrl ?? "https://api.example.com/v1" }],
|
|
565
|
+
security: [{ siteSecret: [] }],
|
|
566
|
+
components: {
|
|
567
|
+
securitySchemes: {
|
|
568
|
+
siteSecret: {
|
|
569
|
+
type: "http",
|
|
570
|
+
scheme: "bearer",
|
|
571
|
+
description: "Site secret (lds_live_..., rbs_live_... still works). Server env only, never in a browser."
|
|
572
|
+
}
|
|
573
|
+
},
|
|
574
|
+
parameters: { IdempotencyKey: IDEMPOTENCY_KEY_PARAMETER },
|
|
575
|
+
schemas
|
|
576
|
+
},
|
|
577
|
+
paths
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// ../contracts/src/cache-tags.ts
|
|
582
|
+
function expandCacheTags(tags) {
|
|
583
|
+
return [
|
|
584
|
+
...new Set(
|
|
585
|
+
tags.flatMap((tag) => {
|
|
586
|
+
const m = /^(?:rb|laudro):(.+)$/.exec(tag);
|
|
587
|
+
return m ? [`laudro:${m[1]}`, `rb:${m[1]}`] : [tag];
|
|
588
|
+
})
|
|
589
|
+
)
|
|
590
|
+
];
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// src/client.ts
|
|
594
|
+
var RbApiError = class extends Error {
|
|
595
|
+
constructor(status, problem) {
|
|
596
|
+
super(problem?.title ?? `Restaurant API error ${status}`);
|
|
597
|
+
this.status = status;
|
|
598
|
+
this.problem = problem;
|
|
599
|
+
this.name = "RbApiError";
|
|
600
|
+
}
|
|
601
|
+
status;
|
|
602
|
+
problem;
|
|
603
|
+
/** Field errors of a 422, keyed by field path. */
|
|
604
|
+
get fieldErrors() {
|
|
605
|
+
const out = {};
|
|
606
|
+
for (const e of this.problem?.errors ?? []) out[e.path] ??= e.message;
|
|
607
|
+
return out;
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
var DEFAULT_REVALIDATE_SECONDS = 3600;
|
|
611
|
+
function createClient(options) {
|
|
612
|
+
return build(options, null);
|
|
613
|
+
}
|
|
614
|
+
function build(options, previewToken) {
|
|
615
|
+
if (!options.apiUrl) throw new Error("createClient: apiUrl is required");
|
|
616
|
+
if (!options.secret) throw new Error("createClient: secret is required");
|
|
617
|
+
const base = options.apiUrl.replace(/\/+$/, "");
|
|
618
|
+
const doFetch = options.fetch ?? fetch;
|
|
619
|
+
const fallbackRevalidate = options.revalidate ?? DEFAULT_REVALIDATE_SECONDS;
|
|
620
|
+
async function request(path, opts = {}) {
|
|
621
|
+
const url = new URL(base + (path.startsWith("/") ? path : `/${path}`));
|
|
622
|
+
for (const [k, v] of Object.entries(opts.query ?? {})) {
|
|
623
|
+
if (v !== void 0) url.searchParams.set(k, String(v));
|
|
624
|
+
}
|
|
625
|
+
const method = opts.method ?? (opts.body === void 0 ? "GET" : "POST");
|
|
626
|
+
const headers = {
|
|
627
|
+
authorization: `Bearer ${options.secret}`,
|
|
628
|
+
accept: "application/json",
|
|
629
|
+
...opts.body === void 0 ? {} : { "content-type": "application/json" },
|
|
630
|
+
...opts.headers
|
|
631
|
+
};
|
|
632
|
+
const init = {
|
|
633
|
+
method,
|
|
634
|
+
headers,
|
|
635
|
+
body: opts.body === void 0 ? void 0 : JSON.stringify(opts.body)
|
|
636
|
+
};
|
|
637
|
+
if (method === "GET" && previewToken) {
|
|
638
|
+
url.searchParams.set("preview", "1");
|
|
639
|
+
headers[LEGACY_PREVIEW_TOKEN_HEADER] = previewToken;
|
|
640
|
+
init.cache = "no-store";
|
|
641
|
+
} else if (method === "GET" && (opts.tags || opts.revalidate !== void 0)) {
|
|
642
|
+
Object.assign(
|
|
643
|
+
init,
|
|
644
|
+
options.readInit?.({
|
|
645
|
+
path,
|
|
646
|
+
tags: opts.tags ?? [],
|
|
647
|
+
revalidate: opts.revalidate ?? fallbackRevalidate
|
|
648
|
+
})
|
|
649
|
+
);
|
|
650
|
+
} else if (method !== "GET") {
|
|
651
|
+
init.cache = "no-store";
|
|
652
|
+
}
|
|
653
|
+
const res = await withRetry(() => doFetch(url, init), {
|
|
654
|
+
...options.retry,
|
|
655
|
+
retryable: method === "GET" ? isRetryableRead : opts.idempotent ? isRetryableIdempotentWrite : isRetryableWrite,
|
|
656
|
+
retryNetwork: method === "GET" || opts.idempotent === true
|
|
657
|
+
});
|
|
658
|
+
if (res.status === 204) return void 0;
|
|
659
|
+
const text = await res.text();
|
|
660
|
+
let json;
|
|
661
|
+
try {
|
|
662
|
+
json = text ? JSON.parse(text) : null;
|
|
663
|
+
} catch {
|
|
664
|
+
json = null;
|
|
665
|
+
}
|
|
666
|
+
if (!res.ok) {
|
|
667
|
+
const parsed = Problem.safeParse(json);
|
|
668
|
+
throw new RbApiError(res.status, parsed.success ? parsed.data : null);
|
|
669
|
+
}
|
|
670
|
+
return json;
|
|
671
|
+
}
|
|
672
|
+
const read = (path, tags, query) => request(path, { query, tags });
|
|
673
|
+
function write(path, schema, body, o) {
|
|
674
|
+
const parsed = schema.parse(body);
|
|
675
|
+
return request(path, {
|
|
676
|
+
method: "POST",
|
|
677
|
+
body: parsed,
|
|
678
|
+
idempotent: true,
|
|
679
|
+
headers: {
|
|
680
|
+
"idempotency-key": o?.idempotencyKey ?? crypto.randomUUID(),
|
|
681
|
+
...o?.ip ? { "x-forwarded-for": o.ip } : {}
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
function lunch(q = {}) {
|
|
686
|
+
return read("/lunch", [CACHE_TAGS.lunch], q);
|
|
687
|
+
}
|
|
688
|
+
return {
|
|
689
|
+
request,
|
|
690
|
+
/** Restaurant name, logo, brand, social links, languages, booking mode, contact. */
|
|
691
|
+
site: (q = {}) => read("/site", [CACHE_TAGS.site], q),
|
|
692
|
+
locations: (q = {}) => read("/locations", [CACHE_TAGS.site], q),
|
|
693
|
+
/** Resolved hours per date, max 62 days. */
|
|
694
|
+
hours: (q) => read("/hours", [CACHE_TAGS.hours], q),
|
|
695
|
+
menus: (q = {}) => read("/menus", [CACHE_TAGS.menus], q),
|
|
696
|
+
/** `{ week }` returns each day of the ISO week; otherwise one day (default today). */
|
|
697
|
+
lunch,
|
|
698
|
+
page: (key, q = {}) => read(`/pages/${encodeURIComponent(key)}`, [CACHE_TAGS.page(key)], q),
|
|
699
|
+
packages: (q = {}) => read("/events/packages", [CACHE_TAGS.packages], q),
|
|
700
|
+
booking: (q = {}) => read("/booking", [CACHE_TAGS.booking], q),
|
|
701
|
+
/** Check a preview token (from `/api/laudro-preview?token=`) before turning on Draft Mode. */
|
|
702
|
+
checkPreview: (token) => request("/preview", { query: { token }, revalidate: 0 }),
|
|
703
|
+
bookingRequests: {
|
|
704
|
+
create: (body, o) => write("/booking-requests", BookingRequestCreate, body, o)
|
|
705
|
+
},
|
|
706
|
+
eventRequests: {
|
|
707
|
+
create: (body, o) => write("/event-requests", EventRequestCreate, body, o)
|
|
708
|
+
},
|
|
709
|
+
contact: {
|
|
710
|
+
create: (body, o) => write("/contact", ContactCreate, body, o)
|
|
711
|
+
},
|
|
712
|
+
/** Daily counter. Returns nothing (204). Never throws: analytics must not break a page. */
|
|
713
|
+
track: async (event, o) => {
|
|
714
|
+
const parsed = TrackEvent.safeParse(event);
|
|
715
|
+
if (!parsed.success) return;
|
|
716
|
+
try {
|
|
717
|
+
await request("/track", {
|
|
718
|
+
method: "POST",
|
|
719
|
+
body: parsed.data,
|
|
720
|
+
headers: o?.ip ? { "x-forwarded-for": o.ip } : void 0
|
|
721
|
+
});
|
|
722
|
+
} catch {
|
|
723
|
+
}
|
|
724
|
+
},
|
|
725
|
+
/**
|
|
726
|
+
* A client for Draft Mode: reads send `?preview=1` with the token and skip the cache, so
|
|
727
|
+
* drafts never land in the shared Next.js cache.
|
|
728
|
+
*/
|
|
729
|
+
withPreview: (token) => build(options, token)
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// src/form-token.ts
|
|
734
|
+
async function signFormToken(secret, now = /* @__PURE__ */ new Date()) {
|
|
735
|
+
if (!secret) throw new Error("signFormToken: secret is required");
|
|
736
|
+
const unix = Math.floor(now.getTime() / 1e3);
|
|
737
|
+
return `${unix}.${await hmacHex(secret, `rb-form:${unix}`)}`;
|
|
738
|
+
}
|
|
739
|
+
async function hmacHex(secret, data) {
|
|
740
|
+
const enc = new TextEncoder();
|
|
741
|
+
const key = await crypto.subtle.importKey(
|
|
742
|
+
"raw",
|
|
743
|
+
enc.encode(secret),
|
|
744
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
745
|
+
false,
|
|
746
|
+
["sign"]
|
|
747
|
+
);
|
|
748
|
+
const sig = await crypto.subtle.sign("HMAC", key, enc.encode(data));
|
|
749
|
+
return Array.from(new Uint8Array(sig), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// src/open-now.ts
|
|
753
|
+
function openNow(hours, now = /* @__PURE__ */ new Date(), kind = "open") {
|
|
754
|
+
const t = now.getTime();
|
|
755
|
+
let next = null;
|
|
756
|
+
for (const day of hours.days) {
|
|
757
|
+
const k = day[kind];
|
|
758
|
+
if (!k || k.closed) continue;
|
|
759
|
+
for (const iv of k.intervals) {
|
|
760
|
+
const opens = Date.parse(iv.opens_at);
|
|
761
|
+
const closes = Date.parse(iv.closes_at);
|
|
762
|
+
if (opens <= t && t < closes) {
|
|
763
|
+
return { open: true, closes_at: iv.closes_at, next_opens_at: null };
|
|
764
|
+
}
|
|
765
|
+
if (opens > t && (!next || opens < next.at)) next = { at: opens, iso: iv.opens_at };
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return { open: false, closes_at: null, next_opens_at: next?.iso ?? null };
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// src/webhook.ts
|
|
772
|
+
var WEBHOOK_SIGNATURE_HEADER = "laudro-signature";
|
|
773
|
+
var LEGACY_WEBHOOK_SIGNATURE_HEADER = "rb-signature";
|
|
774
|
+
var WEBHOOK_TOLERANCE_SECONDS = 300;
|
|
775
|
+
async function verifyWebhook(req, secret, now = /* @__PURE__ */ new Date()) {
|
|
776
|
+
const header = req.headers.get(WEBHOOK_SIGNATURE_HEADER) ?? req.headers.get(LEGACY_WEBHOOK_SIGNATURE_HEADER);
|
|
777
|
+
const body = await req.text();
|
|
778
|
+
if (!header) return { ok: false, reason: "missing_signature" };
|
|
779
|
+
const parts = Object.fromEntries(
|
|
780
|
+
header.split(",").map((p) => {
|
|
781
|
+
const i = p.indexOf("=");
|
|
782
|
+
return [p.slice(0, i).trim(), p.slice(i + 1).trim()];
|
|
783
|
+
})
|
|
784
|
+
);
|
|
785
|
+
const t = Number(parts.t);
|
|
786
|
+
const v1 = parts.v1;
|
|
787
|
+
if (!Number.isFinite(t) || !v1) return { ok: false, reason: "bad_signature" };
|
|
788
|
+
if (Math.abs(now.getTime() / 1e3 - t) > WEBHOOK_TOLERANCE_SECONDS) {
|
|
789
|
+
return { ok: false, reason: "expired" };
|
|
790
|
+
}
|
|
791
|
+
const expected = await hmacHex(secret, `${t}.${body}`);
|
|
792
|
+
return timingSafeEqualHex(expected, v1) ? { ok: true, body } : { ok: false, reason: "bad_signature" };
|
|
793
|
+
}
|
|
794
|
+
function timingSafeEqualHex(a, b) {
|
|
795
|
+
if (a.length !== b.length) return false;
|
|
796
|
+
let diff = 0;
|
|
797
|
+
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
798
|
+
return diff === 0;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
export {
|
|
802
|
+
HoursKind,
|
|
803
|
+
HOURS_MAX_DAYS,
|
|
804
|
+
HoursQuery,
|
|
805
|
+
HoursInterval,
|
|
806
|
+
KindHours,
|
|
807
|
+
HoursDay,
|
|
808
|
+
LocationHours,
|
|
809
|
+
HoursResponse,
|
|
810
|
+
MenuKind,
|
|
811
|
+
MenusQuery,
|
|
812
|
+
MenuItemVariant,
|
|
813
|
+
MenuItem,
|
|
814
|
+
MenuSection,
|
|
815
|
+
Menu,
|
|
816
|
+
MenusResponse,
|
|
817
|
+
LunchQuery,
|
|
818
|
+
LunchItem,
|
|
819
|
+
LunchOffer,
|
|
820
|
+
LunchDay,
|
|
821
|
+
LunchDayResponse,
|
|
822
|
+
LunchWeekResponse,
|
|
823
|
+
LunchResponse,
|
|
824
|
+
PageKey,
|
|
825
|
+
PageBlock,
|
|
826
|
+
Page,
|
|
827
|
+
CACHE_TAGS,
|
|
828
|
+
CacheTag,
|
|
829
|
+
WebhookPayload,
|
|
830
|
+
PREVIEW_TOKEN_HEADER,
|
|
831
|
+
LEGACY_PREVIEW_TOKEN_HEADER,
|
|
832
|
+
PreviewQuery,
|
|
833
|
+
PreviewCheck,
|
|
834
|
+
API_ROUTES,
|
|
835
|
+
buildOpenApi,
|
|
836
|
+
expandCacheTags,
|
|
837
|
+
RbApiError,
|
|
838
|
+
DEFAULT_REVALIDATE_SECONDS,
|
|
839
|
+
createClient,
|
|
840
|
+
signFormToken,
|
|
841
|
+
openNow,
|
|
842
|
+
WEBHOOK_SIGNATURE_HEADER,
|
|
843
|
+
WEBHOOK_TOLERANCE_SECONDS,
|
|
844
|
+
verifyWebhook
|
|
845
|
+
};
|