@c1m-public/channel-client 0.5.0 → 0.8.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/dist/types.d.ts CHANGED
@@ -232,6 +232,320 @@ export interface BlogPost {
232
232
  export interface BlogPostResponse {
233
233
  post: BlogPost;
234
234
  }
235
+ export interface GivingEntry {
236
+ id: string;
237
+ workspace_id: string;
238
+ slug: string;
239
+ title: string;
240
+ subtitle?: string;
241
+ variant: string;
242
+ default_amount_cents: number;
243
+ suggested_amounts_cents?: number[];
244
+ allow_custom_amount: boolean;
245
+ cta_label?: string;
246
+ is_active: boolean;
247
+ created_at: string;
248
+ updated_at: string;
249
+ }
250
+ export interface GivingIntent {
251
+ id: string;
252
+ workspace_id: string;
253
+ status: string;
254
+ variant: string;
255
+ title: string;
256
+ description?: string;
257
+ currency: string;
258
+ amount_cents: number;
259
+ is_recurring: boolean;
260
+ is_anonymous: boolean;
261
+ display_name?: string;
262
+ email?: string;
263
+ message?: string;
264
+ session_id?: string;
265
+ recurring_agreement_id?: string;
266
+ source_invoice_id?: string;
267
+ created_at: string;
268
+ updated_at: string;
269
+ refunded_at?: string;
270
+ }
271
+ export interface StartGivingInput {
272
+ variant?: string;
273
+ title?: string;
274
+ description?: string;
275
+ amount_cents: number;
276
+ currency?: string;
277
+ is_recurring?: boolean;
278
+ is_anonymous?: boolean;
279
+ display_name?: string;
280
+ email?: string;
281
+ message?: string;
282
+ success_url?: string;
283
+ cancel_url?: string;
284
+ }
285
+ export interface StartGivingResponse {
286
+ giving: GivingIntent;
287
+ session: {
288
+ session_id: string;
289
+ url: string;
290
+ payment_intent_id?: string;
291
+ };
292
+ }
293
+ export interface PrintPerPagePricing {
294
+ bw_cents: number;
295
+ colour_cents: number;
296
+ }
297
+ export interface PrintPaperSize {
298
+ key: string;
299
+ label: string;
300
+ surcharge_cents: number;
301
+ enabled: boolean;
302
+ }
303
+ export interface PrintPricingOption {
304
+ key: string;
305
+ label: string;
306
+ type: string;
307
+ surcharge_cents: number;
308
+ }
309
+ export interface PrintPricing {
310
+ base_fee_cents: number;
311
+ per_page: PrintPerPagePricing;
312
+ paper_sizes: PrintPaperSize[];
313
+ options: PrintPricingOption[];
314
+ min_total_cents: number;
315
+ }
316
+ export interface PrintBranding {
317
+ display_name?: string;
318
+ logo_url?: string;
319
+ accent?: string;
320
+ instructions?: string;
321
+ }
322
+ export interface PrintIntakeLimits {
323
+ max_files: number;
324
+ max_file_bytes: number;
325
+ max_total_bytes: number;
326
+ allowed_extensions: string[];
327
+ }
328
+ export interface PrintSettingsResponse {
329
+ accepting: boolean;
330
+ currency: string;
331
+ retention_days: number;
332
+ pricing: PrintPricing;
333
+ branding: PrintBranding;
334
+ intake_limits: PrintIntakeLimits;
335
+ }
336
+ export interface PrintOptionsSelection {
337
+ paper_size: string;
338
+ colour: boolean;
339
+ pages: number;
340
+ copies: number;
341
+ extras?: Record<string, boolean>;
342
+ }
343
+ export interface PrintPriceLine {
344
+ label: string;
345
+ amount_cents: number;
346
+ }
347
+ export interface PrintPriceBreakdown {
348
+ lines: PrintPriceLine[];
349
+ total_cents: number;
350
+ currency: string;
351
+ }
352
+ export interface PrintQuoteResponse {
353
+ price_cents: number;
354
+ currency: string;
355
+ breakdown: PrintPriceBreakdown;
356
+ }
357
+ export interface PrintIntakeInput {
358
+ name: string;
359
+ email?: string;
360
+ phone?: string;
361
+ notes?: string;
362
+ options: PrintOptionsSelection;
363
+ files: Array<{
364
+ blob: Blob;
365
+ filename: string;
366
+ }>;
367
+ }
368
+ export interface PrintJobFile {
369
+ id: string;
370
+ job_id: string;
371
+ file_name: string;
372
+ mime_type: string;
373
+ size_bytes: number;
374
+ page_count?: number;
375
+ sort_order: number;
376
+ created_at: string;
377
+ }
378
+ export interface PrintJob {
379
+ id: string;
380
+ workspace_id: string;
381
+ reference: string;
382
+ customer_name: string;
383
+ customer_email?: string;
384
+ customer_phone?: string;
385
+ status: string;
386
+ payment_status: string;
387
+ payment_method: string;
388
+ options: PrintOptionsSelection;
389
+ price_cents: number;
390
+ currency: string;
391
+ price_breakdown: PrintPriceBreakdown;
392
+ notes?: string;
393
+ file_count?: number;
394
+ version: number;
395
+ files?: PrintJobFile[];
396
+ created_at: string;
397
+ updated_at: string;
398
+ }
399
+ export interface DiningAvailabilityRule {
400
+ days?: string[];
401
+ start?: string;
402
+ end?: string;
403
+ starts_on?: string;
404
+ ends_on?: string;
405
+ }
406
+ export interface DiningAvailability {
407
+ timezone?: string;
408
+ rules?: DiningAvailabilityRule[];
409
+ is_available_now: boolean;
410
+ next_change_at?: string;
411
+ }
412
+ export interface DiningItemSize {
413
+ id: string;
414
+ name: string;
415
+ price_cents: number;
416
+ is_default: boolean;
417
+ sort_order: number;
418
+ is_active: boolean;
419
+ }
420
+ export interface DiningModifier {
421
+ id: string;
422
+ name: string;
423
+ description?: string;
424
+ price_delta_cents: number;
425
+ is_default: boolean;
426
+ sort_order: number;
427
+ is_active: boolean;
428
+ }
429
+ export interface DiningModifierGroup {
430
+ id: string;
431
+ name: string;
432
+ description?: string;
433
+ selection_type: string;
434
+ is_required: boolean;
435
+ min_selections: number;
436
+ max_selections?: number;
437
+ sort_order: number;
438
+ is_active: boolean;
439
+ modifiers: DiningModifier[];
440
+ }
441
+ export interface DiningOfferBadge {
442
+ offer_id: string;
443
+ label: string;
444
+ type: string;
445
+ is_featured: boolean;
446
+ display_priority: number;
447
+ }
448
+ export interface DiningComboChoice {
449
+ id: string;
450
+ item: {
451
+ id: string;
452
+ slug: string;
453
+ name: string;
454
+ description?: string;
455
+ base_price_cents: number;
456
+ image_url?: string;
457
+ image_alt: string;
458
+ badges: string[];
459
+ status: string;
460
+ dietary_flags: string[];
461
+ allergens: string[];
462
+ calories?: number;
463
+ prep_time_mins?: number;
464
+ spice_level?: number;
465
+ kitchen_station?: string;
466
+ sizes: DiningItemSize[];
467
+ modifier_groups: DiningModifierGroup[];
468
+ };
469
+ label_override?: string;
470
+ description_override?: string;
471
+ price_delta_cents: number;
472
+ is_default: boolean;
473
+ sort_order: number;
474
+ is_active: boolean;
475
+ is_available: boolean;
476
+ }
477
+ export interface DiningComboGroup {
478
+ id: string;
479
+ name: string;
480
+ description?: string;
481
+ min_selections: number;
482
+ max_selections?: number;
483
+ allow_repeats: boolean;
484
+ sort_order: number;
485
+ is_active: boolean;
486
+ choices: DiningComboChoice[];
487
+ }
488
+ export interface DiningOffer {
489
+ id: string;
490
+ name: string;
491
+ badge_label?: string;
492
+ type: string;
493
+ code?: string;
494
+ is_featured: boolean;
495
+ display_priority: number;
496
+ image_url?: string;
497
+ availability?: DiningAvailability;
498
+ is_active: boolean;
499
+ }
500
+ export interface DiningMenuItem {
501
+ id: string;
502
+ slug: string;
503
+ name: string;
504
+ description?: string;
505
+ item_kind: string;
506
+ price_cents: number;
507
+ price_from_cents: number;
508
+ image_url?: string;
509
+ image_alt: string;
510
+ badges: string[];
511
+ is_featured: boolean;
512
+ is_available: boolean;
513
+ status: string;
514
+ dietary_flags: string[];
515
+ allergens: string[];
516
+ calories?: number;
517
+ prep_time_mins?: number;
518
+ spice_level?: number;
519
+ sizes: DiningItemSize[];
520
+ modifier_groups: DiningModifierGroup[];
521
+ offer_badges: DiningOfferBadge[];
522
+ combo_groups?: DiningComboGroup[];
523
+ }
524
+ export interface DiningMenu {
525
+ saas_app: string;
526
+ workspace_id: string;
527
+ board: {
528
+ id: string;
529
+ slug: string;
530
+ name: string;
531
+ description?: string;
532
+ currency: string;
533
+ applicable_order_types: string[];
534
+ availability?: DiningAvailability;
535
+ is_active: boolean;
536
+ };
537
+ sections: Array<{
538
+ id: string;
539
+ name: string;
540
+ description?: string;
541
+ sort_order: number;
542
+ availability?: DiningAvailability;
543
+ is_active: boolean;
544
+ items: DiningMenuItem[];
545
+ }>;
546
+ offers: DiningOffer[];
547
+ generated_at: string;
548
+ }
235
549
  /** The published theme, as served to a channel that owns its own routing and
236
550
  * therefore never calls `site.resolve` (which otherwise bundles it). */
237
551
  export interface ThemeResponse {
@@ -9,7 +9,7 @@
9
9
  * PII-free fields; fetch the detail back through the channel client. Data POSTed
10
10
  * to your server can never be reached by a GDPR erasure on ours.
11
11
  */
12
- export declare const CHANNEL_WEBHOOK_EVENT_TYPES: readonly ["order.created", "order.paid", "lead.created", "review.submitted", "page.published"];
12
+ export declare const CHANNEL_WEBHOOK_EVENT_TYPES: readonly ["order.created", "order.paid", "order.updated", "order.cancelled", "order.refunded", "lead.created", "review.submitted", "review.published", "page.published", "product.created", "product.updated", "product.deleted"];
13
13
  export type ChannelWebhookEventType = (typeof CHANNEL_WEBHOOK_EVENT_TYPES)[number];
14
14
  export interface OrderEventData {
15
15
  order_id: string;
@@ -35,18 +35,46 @@ export interface ReviewSubmittedEventData {
35
35
  source: string;
36
36
  submitted_at: string;
37
37
  }
38
+ /**
39
+ * Fired when a review clears moderation. Unlike `review.submitted` this one
40
+ * carries the review id — it is the event a reviews page revalidates on.
41
+ */
42
+ export interface ReviewPublishedEventData {
43
+ review_id: string;
44
+ rating: number;
45
+ source: string;
46
+ published_at?: string;
47
+ }
38
48
  export interface PagePublishedEventData {
39
49
  page_id: string;
40
50
  route_pattern: string;
41
51
  title: string;
42
52
  published_at?: string;
43
53
  }
54
+ /**
55
+ * The catalog-change signal. Thin by design: `slug` locates the cached product
56
+ * page, `status` says whether it is still publicly visible. Fetch price and
57
+ * stock back through the channel client — a webhook body is a cache-busting
58
+ * signal, not a data feed.
59
+ */
60
+ export interface ProductEventData {
61
+ product_id: string;
62
+ slug?: string;
63
+ status?: string;
64
+ }
44
65
  export interface ChannelWebhookEventDataMap {
45
66
  "order.created": OrderEventData;
46
67
  "order.paid": OrderEventData;
68
+ "order.updated": OrderEventData;
69
+ "order.cancelled": OrderEventData;
70
+ "order.refunded": OrderEventData;
47
71
  "lead.created": LeadCreatedEventData;
48
72
  "review.submitted": ReviewSubmittedEventData;
73
+ "review.published": ReviewPublishedEventData;
49
74
  "page.published": PagePublishedEventData;
75
+ "product.created": ProductEventData;
76
+ "product.updated": ProductEventData;
77
+ "product.deleted": ProductEventData;
50
78
  }
51
79
  export interface ChannelWebhookEvent<T extends ChannelWebhookEventType = ChannelWebhookEventType> {
52
80
  /** The originating event id. Delivery is AT-LEAST-ONCE — use this to dedupe. */
@@ -62,7 +90,19 @@ export interface ChannelWebhookEvent<T extends ChannelWebhookEventType = Channel
62
90
  export type AnyChannelWebhookEvent = {
63
91
  [T in ChannelWebhookEventType]: ChannelWebhookEvent<T>;
64
92
  }[ChannelWebhookEventType];
93
+ /** A valid signed envelope for an event introduced by a newer API version. */
94
+ export interface UnknownChannelWebhookEvent {
95
+ id: string;
96
+ type: string;
97
+ api_version: string;
98
+ occurred_at: string;
99
+ workspace_id: string;
100
+ channel_id?: string;
101
+ data: unknown;
102
+ }
103
+ export type ParsedChannelWebhookEvent = AnyChannelWebhookEvent | UnknownChannelWebhookEvent;
65
104
  export declare function isChannelWebhookEventType(value: string): value is ChannelWebhookEventType;
105
+ export declare function isKnownChannelWebhookEvent(event: ParsedChannelWebhookEvent): event is AnyChannelWebhookEvent;
66
106
  export type ChannelWebhookVerificationCode = "missing_signature" | "missing_timestamp" | "invalid_timestamp" | "timestamp_out_of_tolerance" | "invalid_signature" | "invalid_payload";
67
107
  export declare class ChannelWebhookVerificationError extends Error {
68
108
  readonly code: ChannelWebhookVerificationCode;
@@ -126,4 +166,4 @@ export interface ParseChannelWebhookEventOptions {
126
166
  *
127
167
  * @throws {ChannelWebhookVerificationError} if the request is not authentic.
128
168
  */
129
- export declare function parseChannelWebhookEvent(request: Request, options: ParseChannelWebhookEventOptions): Promise<AnyChannelWebhookEvent>;
169
+ export declare function parseChannelWebhookEvent(request: Request, options: ParseChannelWebhookEventOptions): Promise<ParsedChannelWebhookEvent>;
package/dist/webhooks.js CHANGED
@@ -12,13 +12,23 @@
12
12
  export const CHANNEL_WEBHOOK_EVENT_TYPES = [
13
13
  "order.created",
14
14
  "order.paid",
15
+ "order.updated",
16
+ "order.cancelled",
17
+ "order.refunded",
15
18
  "lead.created",
16
19
  "review.submitted",
20
+ "review.published",
17
21
  "page.published",
22
+ "product.created",
23
+ "product.updated",
24
+ "product.deleted",
18
25
  ];
19
26
  export function isChannelWebhookEventType(value) {
20
27
  return CHANNEL_WEBHOOK_EVENT_TYPES.includes(value);
21
28
  }
29
+ export function isKnownChannelWebhookEvent(event) {
30
+ return isChannelWebhookEventType(event.type);
31
+ }
22
32
  export class ChannelWebhookVerificationError extends Error {
23
33
  code;
24
34
  constructor(message, code) {
@@ -50,7 +60,7 @@ function timingSafeEqual(a, b) {
50
60
  return diff === 0;
51
61
  }
52
62
  function decodeHex(value) {
53
- if (value.length === 0 || value.length % 2 !== 0)
63
+ if (value.length === 0 || value.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(value))
54
64
  return null;
55
65
  const out = new Uint8Array(value.length / 2);
56
66
  for (let i = 0; i < out.length; i += 1) {
@@ -150,8 +160,57 @@ export async function parseChannelWebhookEvent(request, options) {
150
160
  catch {
151
161
  throw new ChannelWebhookVerificationError("body is not valid JSON", "invalid_payload");
152
162
  }
153
- if (!event || typeof event.id !== "string" || typeof event.type !== "string") {
163
+ if (!isEventEnvelope(event)) {
154
164
  throw new ChannelWebhookVerificationError("body is not a channel event", "invalid_payload");
155
165
  }
166
+ if (!isChannelWebhookEventType(event.type))
167
+ return event;
168
+ if (!isKnownEventData(event.type, event.data)) {
169
+ throw new ChannelWebhookVerificationError("body contains invalid event data", "invalid_payload");
170
+ }
156
171
  return event;
157
172
  }
173
+ function isRecord(value) {
174
+ return typeof value === "object" && value !== null && !Array.isArray(value);
175
+ }
176
+ function isEventEnvelope(value) {
177
+ if (!isRecord(value) || !isRecord(value.data))
178
+ return false;
179
+ for (const key of ["id", "type", "api_version", "occurred_at", "workspace_id"]) {
180
+ if (typeof value[key] !== "string" || value[key].trim() === "")
181
+ return false;
182
+ }
183
+ return value.channel_id === undefined || typeof value.channel_id === "string";
184
+ }
185
+ function hasStrings(data, keys) {
186
+ return keys.every((key) => typeof data[key] === "string");
187
+ }
188
+ function isKnownEventData(type, value) {
189
+ if (!isRecord(value))
190
+ return false;
191
+ switch (type) {
192
+ case "order.created":
193
+ case "order.paid":
194
+ case "order.updated":
195
+ case "order.cancelled":
196
+ case "order.refunded":
197
+ return hasStrings(value, ["order_id", "order_number", "status", "payment_status", "currency", "created_at"]) &&
198
+ typeof value.total_cents === "number";
199
+ case "lead.created":
200
+ return hasStrings(value, ["lead_id", "source", "created_at"]);
201
+ case "review.submitted":
202
+ return hasStrings(value, ["source", "submitted_at"]) && typeof value.rating === "number";
203
+ case "review.published":
204
+ return hasStrings(value, ["review_id", "source"]) && typeof value.rating === "number" &&
205
+ (value.published_at === undefined || typeof value.published_at === "string");
206
+ case "page.published":
207
+ return hasStrings(value, ["page_id", "route_pattern", "title"]) &&
208
+ (value.published_at === undefined || typeof value.published_at === "string");
209
+ case "product.created":
210
+ case "product.updated":
211
+ case "product.deleted":
212
+ return typeof value.product_id === "string" &&
213
+ (value.slug === undefined || typeof value.slug === "string") &&
214
+ (value.status === undefined || typeof value.status === "string");
215
+ }
216
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@c1m-public/channel-client",
3
- "version": "0.5.0",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Typed server-side client for the MML headless Channel API",