@12-apps/marketplace 1.2.0 → 1.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.
Files changed (69) hide show
  1. package/ADOPTING.md +76 -4
  2. package/README.md +163 -3
  3. package/dist/99food/index.d.ts +79 -2
  4. package/dist/99food/index.js +117 -2
  5. package/dist/99food/index.js.map +1 -1
  6. package/dist/adapter-B8QtQ2RA.d.ts +612 -0
  7. package/dist/{chunk-CRYFYDQ2.js → chunk-DPCZVN2C.js} +69 -5
  8. package/dist/chunk-DPCZVN2C.js.map +1 -0
  9. package/dist/{chunk-D2X7TOR6.js → chunk-EBAIZQPB.js} +841 -86
  10. package/dist/chunk-EBAIZQPB.js.map +1 -0
  11. package/dist/chunk-M2V4RFMA.js +23 -0
  12. package/dist/chunk-M2V4RFMA.js.map +1 -0
  13. package/dist/{chunk-24RQZPSV.js → chunk-SZRZTLVU.js} +1 -1
  14. package/dist/chunk-SZRZTLVU.js.map +1 -0
  15. package/dist/{engine-D-a4mttc.d.ts → engine-o5cm54vC.d.ts} +462 -2
  16. package/dist/hono/index.d.ts +2 -2
  17. package/dist/hono/index.js +2 -2
  18. package/dist/ifood/index.d.ts +22 -2
  19. package/dist/ifood/index.js +396 -56
  20. package/dist/ifood/index.js.map +1 -1
  21. package/dist/index.d.ts +30 -4
  22. package/dist/index.js +445 -9
  23. package/dist/index.js.map +1 -1
  24. package/dist/manifest/server.d.ts +5 -2
  25. package/dist/manifest/server.js +2 -2
  26. package/dist/rappi/index.d.ts +15 -2
  27. package/dist/rappi/index.js +62 -2
  28. package/dist/rappi/index.js.map +1 -1
  29. package/dist/server/index.d.ts +25 -10
  30. package/dist/server/index.js +4 -2
  31. package/package.json +1 -1
  32. package/prisma/marketplace.prisma +158 -1
  33. package/prisma/migrations/20261113120000_marketplace_catalogue/migration.sql +97 -0
  34. package/prisma/migrations/20261113230000_marketplace_store_pause/migration.sql +70 -0
  35. package/src/99food/catalogue.ts +117 -0
  36. package/src/99food/index.ts +14 -0
  37. package/src/99food/outbound.ts +3 -3
  38. package/src/99food/pause.ts +56 -0
  39. package/src/core/adapter.ts +20 -1
  40. package/src/core/catalogue-category.ts +119 -0
  41. package/src/core/catalogue-ids.ts +23 -0
  42. package/src/core/catalogue-job.ts +332 -0
  43. package/src/core/catalogue-port.ts +173 -0
  44. package/src/core/catalogue-store.ts +172 -0
  45. package/src/core/catalogue-sweep.ts +43 -0
  46. package/src/core/catalogue.ts +226 -0
  47. package/src/core/engine.ts +83 -0
  48. package/src/core/outbound-sweep.ts +7 -2
  49. package/src/core/pause-job.ts +381 -0
  50. package/src/core/pause-port.ts +110 -0
  51. package/src/core/pause-retired.ts +139 -0
  52. package/src/core/pause-store.ts +151 -0
  53. package/src/core/pause-sweep.ts +148 -0
  54. package/src/core/pause.ts +316 -0
  55. package/src/core/ports.ts +47 -0
  56. package/src/ifood/catalogue.ts +273 -0
  57. package/src/ifood/index.ts +8 -0
  58. package/src/ifood/outbound.ts +9 -6
  59. package/src/ifood/pause.ts +257 -0
  60. package/src/index.ts +64 -0
  61. package/src/rappi/index.ts +17 -0
  62. package/src/rappi/outbound.ts +2 -2
  63. package/src/rappi/pause.ts +98 -0
  64. package/src/server/index.ts +1 -0
  65. package/src/server/jobs.ts +100 -10
  66. package/dist/adapter-_iuSxvVD.d.ts +0 -319
  67. package/dist/chunk-24RQZPSV.js.map +0 -1
  68. package/dist/chunk-CRYFYDQ2.js.map +0 -1
  69. package/dist/chunk-D2X7TOR6.js.map +0 -1
@@ -0,0 +1,612 @@
1
+ /**
2
+ * The catalogue half of the adapter port (FUT-4768): a store turns one of its
3
+ * products ON or OFF per connected marketplace, with an optional price for
4
+ * that marketplace, and the engine keeps the marketplace's catalogue in step.
5
+ *
6
+ * Marketplaces differ in exactly one way that shapes the engine: who moves the
7
+ * menu.
8
+ *
9
+ * - **push** — the store's software writes each item to the marketplace's API
10
+ * (iFood's Catalog API v2.0). The adapter publishes and withdraws ONE item.
11
+ * - **pull** — the marketplace reads the menu from the store's software (Open
12
+ * Delivery's `GET /v1/merchant`, which 99Food implements). The adapter turns
13
+ * the published items into the marketplace's menu document, which the HOST
14
+ * serves, and tells the marketplace the menu changed.
15
+ *
16
+ * A marketplace whose adapter states no `catalogue` cannot be published to,
17
+ * and the host says so (`engine.catalogueMode(provider)` is null). Everything
18
+ * else — the publication record and its states, the job, its retries, the
19
+ * sweep, the price rule — is the engine's and does not change with the
20
+ * marketplace.
21
+ */
22
+ /** One product as it goes out to a marketplace. The host maps its product into this shape. */
23
+ interface CatalogueItem {
24
+ name: string;
25
+ description: string | null;
26
+ /**
27
+ * An absolute https URL of the product's photo, or null. It must CHANGE when
28
+ * the photo changes (a versioned URL: a content hash or an upload id in it):
29
+ * a marketplace that holds an uploaded copy reuses it while the URL is the
30
+ * same, so new bytes at an old URL are never sent.
31
+ */
32
+ imageUrl: string | null;
33
+ /** The name of the host category the product sits in; the adapter finds or creates it. */
34
+ categoryName: string;
35
+ /** The store's own price, in the smallest currency unit. */
36
+ priceCents: number;
37
+ }
38
+ /**
39
+ * What the adapter is handed to publish: the host's item with `priceCents`
40
+ * already the price for THIS marketplace (the connection's own price when the
41
+ * store set one, else the store's), and the engine's stable reference.
42
+ */
43
+ interface CatalogueItemOut extends CatalogueItem {
44
+ /**
45
+ * The engine's stable id for this (connection, product) publication. The
46
+ * same across retries and re-publishes, so an adapter that must invent the
47
+ * marketplace's id (iFood wants a UUID it does not issue) derives it from
48
+ * this and a lost answer never creates a second item.
49
+ */
50
+ ref: string;
51
+ /** The host's product id: the item's external code on the marketplace. */
52
+ productId: string;
53
+ /** The marketplace's id from the last publish, or null before the first. */
54
+ externalId: string | null;
55
+ /**
56
+ * The photo this marketplace already holds for the item, from an earlier
57
+ * publish: the adapter's own reference (iFood's `imagePath`) and the
58
+ * `imageUrl` it was made from. Reuse it while `sourceUrl` is still the
59
+ * item's `imageUrl` rather than uploading again; null when none is held.
60
+ */
61
+ storedImage: CatalogueStoredImage | null;
62
+ }
63
+ /** A photo a marketplace holds: the adapter's reference to it, and the URL it was made from. */
64
+ interface CatalogueStoredImage {
65
+ ref: string;
66
+ sourceUrl: string;
67
+ }
68
+ /**
69
+ * Something a publish that SUCCEEDED could not do, for the host to say:
70
+ * the photo was too large for the marketplace, or it refused the photo.
71
+ */
72
+ type CatalogueWarningCode = "IMAGE_TOO_LARGE" | "IMAGE_REFUSED";
73
+ /** What the adapter is handed to withdraw. */
74
+ type CatalogueItemRef = Pick<CatalogueItemOut, "ref" | "productId" | "externalId">;
75
+ /** An image's bytes, for a marketplace that takes an upload rather than a URL. */
76
+ interface CatalogueImage {
77
+ contentType: string;
78
+ /** The file, base64-encoded, no data-URL prefix. */
79
+ base64: string;
80
+ }
81
+ /** An outbound call's answer when it is not a success: refused (final) or retry. */
82
+ type CatalogueFailure = Exclude<OutboundResult, {
83
+ kind: "accepted";
84
+ }>;
85
+ /** What one catalogue call is handed: the outbound context plus the store's merchant id and the image loader. */
86
+ interface CatalogueCallContext extends AdapterCallContext {
87
+ /** The connection's merchant id on the marketplace. */
88
+ externalMerchantId: string;
89
+ /** The host's image loader: the bytes behind an item's `imageUrl`, or null when it cannot load them. */
90
+ loadImage(url: string): Promise<CatalogueImage | null>;
91
+ /**
92
+ * The marketplace's id for the host category `name`: the one the engine
93
+ * stored for this connection, else `resolve()` (the adapter's find-or-create)
94
+ * run by ONE publish at a time and stored. A second publish into a category
95
+ * being created is answered `retry`, so a new category is created once.
96
+ */
97
+ category(name: string, resolve: () => Promise<{
98
+ id: string;
99
+ } | CatalogueFailure>): Promise<{
100
+ id: string;
101
+ } | CatalogueFailure>;
102
+ /**
103
+ * Forget the id stored for the host category `name` (the marketplace no
104
+ * longer has it: the store deleted it on the marketplace's portal), so the
105
+ * next `category` finds or creates it again.
106
+ */
107
+ forgetCategory(name: string): Promise<void>;
108
+ }
109
+ /** A publish's answer: the marketplace's id for the item, or the refusal and retry shapes of every outbound call. */
110
+ type CataloguePublishResult = {
111
+ kind: "published";
112
+ externalId: string;
113
+ /**
114
+ * OPTIONAL. The SKU this marketplace's orders will carry for the item,
115
+ * when it is not the host's product id (the item's external code). The
116
+ * engine maps it to the product, so its orders arrive mapped.
117
+ */
118
+ sku?: string;
119
+ /**
120
+ * OPTIONAL. The photo the marketplace now holds (stored for the next
121
+ * publish to reuse), null when it holds none; absent leaves the stored one.
122
+ */
123
+ image?: CatalogueStoredImage | null;
124
+ /** OPTIONAL. What the publish could not do although it succeeded. */
125
+ warning?: CatalogueWarningCode;
126
+ } | CatalogueFailure;
127
+ /** A marketplace the store's software writes to. */
128
+ interface PushCatalogue {
129
+ readonly mode: "push";
130
+ /** Create or replace ONE item in the marketplace's catalogue, available for sale. Idempotent by `item.ref`. */
131
+ publishItem(ctx: CatalogueCallContext, item: CatalogueItemOut): Promise<CataloguePublishResult>;
132
+ /** Take ONE item out of the marketplace's catalogue. An item already absent is `accepted`. */
133
+ withdrawItem(ctx: CatalogueCallContext, item: CatalogueItemRef): Promise<OutboundResult>;
134
+ }
135
+ /** One published item, as a pull marketplace's menu document lists it. */
136
+ type CatalogueMenuEntry = CatalogueItemOut;
137
+ /** What the host states about the menu a pull marketplace reads: its words and money, never the engine's. */
138
+ interface CatalogueMenuOptions {
139
+ /** The menu's display name, in the store's language. */
140
+ menuName: string;
141
+ /** ISO 4217 code of the prices. */
142
+ currency: string;
143
+ }
144
+ /** A marketplace that reads the menu from the store's software. */
145
+ interface PullCatalogue {
146
+ readonly mode: "pull";
147
+ /**
148
+ * The menu part of the document the marketplace pulls, built from every item
149
+ * published to this connection. Pure and offline: the host serves it, merged
150
+ * into whatever else that document carries (the store's own details).
151
+ */
152
+ menuDocument(input: {
153
+ externalMerchantId: string;
154
+ entries: readonly CatalogueMenuEntry[];
155
+ options: CatalogueMenuOptions;
156
+ }): unknown;
157
+ /** Tell the marketplace the menu changed, so it pulls it again. */
158
+ notifyMenuChanged(ctx: CatalogueCallContext): Promise<OutboundResult>;
159
+ }
160
+ /** The catalogue capability an adapter states, or leaves out. */
161
+ type MarketplaceCatalogue = PushCatalogue | PullCatalogue;
162
+
163
+ /**
164
+ * The adapter port's OPTIONAL availability capability: closing a store on a
165
+ * marketplace for a while, and opening it again (FUT-4851).
166
+ *
167
+ * The owner's rules (2026-11-12), translated:
168
+ *
169
+ * 1. "When the kitchen is paused, the store is paused on every connected
170
+ * marketplace. The interruption carries the kitchen's estimated reopening
171
+ * time as its end, is renewed while the kitchen stays paused, and is
172
+ * deleted the moment the pause is over. The store is never closed on a
173
+ * marketplace longer than the kitchen's own pause."
174
+ * 2. The debounce is the host's; the engine receives pause(until) and resume.
175
+ * 3. "Never reopen a store the owner closed by hand on the marketplace, and
176
+ * never close one beyond what the kitchen's pause asked": the engine only
177
+ * deletes what IT created.
178
+ *
179
+ * Marketplaces close a store in one of two shapes, stated by the adapter as
180
+ * {@link MarketplaceAvailability.kind}:
181
+ *
182
+ * - `interruption`: the marketplace keeps a closure with its OWN id and an
183
+ * END (iFood's interruptions). The engine stores the id and deletes only
184
+ * that id; a closure the owner made by hand is another id and is never
185
+ * touched. If every call after the pause is lost, the marketplace still
186
+ * opens the store at the end it was given.
187
+ * - `switch`: a store-wide on/off with no id and no end (Rappi's store
188
+ * availability, Open Delivery's `merchantStatus`). The engine turns a store
189
+ * on only if its own record says it turned it off, and turns it on again by
190
+ * itself once the `until` it was given has passed without a renewal. What a
191
+ * switch cannot tell apart: an owner who ALSO closed the store by hand while
192
+ * the kitchen's pause stood is reopened with it. That is why a switch
193
+ * adapter offers the capability only when its host opts in.
194
+ */
195
+ /** What a pause asks of one marketplace. */
196
+ interface StorePauseRequest {
197
+ /** The store on the marketplace: the connection's `externalMerchantId`. */
198
+ merchantId: string;
199
+ /** When the store should open again on its own: the kitchen's estimated reopening time. */
200
+ until: Date;
201
+ /** The host's words for the closure, shown to the merchant where the marketplace shows one. */
202
+ reason: string;
203
+ /**
204
+ * A stable key for THIS closure, the same on every retry of it. An adapter
205
+ * that can name its own closure sends it; one that can list closures finds
206
+ * the closure a lost answer already created, by the key or, where the
207
+ * marketplace assigns its own id, by the end and the reason it carries, so
208
+ * a retry never creates a second one.
209
+ */
210
+ key: string;
211
+ /** The store's IANA time zone, for a marketplace that reads times as the store's wall clock. */
212
+ timeZone: string;
213
+ /**
214
+ * OPTIONAL. The marketplace ids the engine already holds (the current
215
+ * closure, the replaced ones it is about to delete): a closure found again
216
+ * for this create is never one of them.
217
+ */
218
+ keep?: readonly string[];
219
+ }
220
+ /** The closure the engine holds on one marketplace, as a resume or an extend names it. */
221
+ interface StorePauseRef {
222
+ merchantId: string;
223
+ /** The marketplace's own id for the closure; null for a switch, or when the create's answer was lost. */
224
+ externalId: string | null;
225
+ /** The key the closure was created under; null once nothing of ours is in doubt. */
226
+ key: string | null;
227
+ /**
228
+ * What the create under `key` carried, for a marketplace that names a
229
+ * closure with its own id: the closure whose end and description are these
230
+ * is the one a lost answer created. Null with no key in doubt.
231
+ */
232
+ sent: {
233
+ until: Date;
234
+ reason: string;
235
+ timeZone: string;
236
+ } | null;
237
+ /**
238
+ * OPTIONAL. The marketplace ids the engine already holds (the current
239
+ * closure, the replaced ones): a closure found under `key` is never one of
240
+ * them, so deleting a lost create can never delete the closure that stands.
241
+ */
242
+ keep?: readonly string[];
243
+ }
244
+ /** One pause or extend's answer. */
245
+ type StorePauseResult =
246
+ /** The store is closed on the marketplace until `until`. `externalId` is null for a switch. */
247
+ {
248
+ kind: "paused";
249
+ externalId: string | null;
250
+ until: Date;
251
+ } | Exclude<OutboundResult, {
252
+ kind: "accepted";
253
+ }>;
254
+ /** A marketplace's way of closing a store for a while. */
255
+ interface MarketplaceAvailability {
256
+ /** How the marketplace holds a closure: an `interruption` with its own id and end, or a store-wide `switch`. */
257
+ readonly kind: "interruption" | "switch";
258
+ /**
259
+ * Close the store until `request.until`. Idempotent under `request.key`
260
+ * where the marketplace allows it (a stable id, or a list before the create).
261
+ */
262
+ pause(ctx: AdapterCallContext, request: StorePauseRequest): Promise<StorePauseResult>;
263
+ /**
264
+ * OPTIONAL. Move the end of the closure the engine holds. Absent: the engine
265
+ * creates a new closure with the later end and then deletes the old one, so
266
+ * the store is never open in between.
267
+ */
268
+ extend?(ctx: AdapterCallContext, ref: StorePauseRef, request: StorePauseRequest): Promise<StorePauseResult>;
269
+ /**
270
+ * Open the store: delete THIS closure, and nothing else. A closure already
271
+ * gone (it ended, or a person removed it) is `accepted`: there is nothing of
272
+ * ours left to delete.
273
+ */
274
+ resume(ctx: AdapterCallContext, ref: StorePauseRef): Promise<OutboundResult>;
275
+ }
276
+
277
+ /**
278
+ * The canonical shape every marketplace adapter normalizes INTO.
279
+ *
280
+ * This type is the seam. The engine below it knows no marketplace by name: a
281
+ * marketplace is an adapter registered with the engine (`./adapter`), and the
282
+ * provider id is whatever that adapter says it is. A second marketplace is a
283
+ * second adapter, never a second pipeline, a second acceptance clock or a
284
+ * second idea of what an order is.
285
+ *
286
+ * Deliberately POORER than any marketplace's own payload. Everything a store
287
+ * operates on is here; everything else stays in the inbox row's raw `payload`,
288
+ * which is where a normalization bug is reconstructed from.
289
+ */
290
+ /**
291
+ * A marketplace's id, as its adapter declares it (`"IFOOD"`). Uppercase
292
+ * letters, digits and underscores; the migration's CHECK holds the same shape.
293
+ * A string rather than a closed union, because the set of marketplaces is the
294
+ * set of registered adapters, and a union would make a new one a code change
295
+ * in the engine.
296
+ */
297
+ type MarketplaceProvider = string;
298
+ /** The shape a provider id must have. Shared by the registry and the CHECK. */
299
+ declare const PROVIDER_ID_PATTERN: RegExp;
300
+ /** One line of an incoming order, before it is resolved against the catalog. */
301
+ interface NormalizedOrderLine {
302
+ /** The marketplace's own SKU — the key the SKU map is matched on. */
303
+ externalSku: string;
304
+ /** What the marketplace called it. Snapshotted, and the fallback line text. */
305
+ name: string;
306
+ quantity: number;
307
+ /**
308
+ * Unit price in the smallest currency unit AS THE MARKETPLACE STATES IT. A
309
+ * marketplace's menu carries its own prices, and the buyer's receipt is the
310
+ * marketplace's one: re-pricing from the host's catalog would produce a
311
+ * total nobody was charged.
312
+ */
313
+ unitPriceCents: number;
314
+ /** The buyer's note on this line, when they left one. */
315
+ note: string | null;
316
+ }
317
+ /** The buyer, as much of them as the marketplace discloses. */
318
+ interface NormalizedBuyer {
319
+ /** The marketplace's own stable id for this customer, when it gives one. */
320
+ externalId: string | null;
321
+ name: string | null;
322
+ phone: string | null;
323
+ }
324
+ /** Where a delivery goes, as much of it as the marketplace states. */
325
+ interface NormalizedAddress {
326
+ street: string;
327
+ number: string;
328
+ neighborhood: string;
329
+ complement: string | null;
330
+ /** The sentence that finds the door. */
331
+ reference: string | null;
332
+ postalCode: string | null;
333
+ city: string | null;
334
+ state: string | null;
335
+ }
336
+ /** How the order reaches the buyer: brought to the door, or collected. */
337
+ type MarketplaceOrderType = "DELIVERY" | "TAKEOUT";
338
+ /** WHO carries a delivery: the merchant's own courier, or the marketplace's fleet. */
339
+ type MarketplaceDeliveredBy = "MERCHANT" | "MARKETPLACE";
340
+ /** An incoming marketplace order, normalized. */
341
+ interface NormalizedMarketplaceOrder {
342
+ provider: MarketplaceProvider;
343
+ /** The marketplace's order id — THE idempotency key for the whole pipeline. */
344
+ externalOrderId: string;
345
+ /** The short code the courier and the buyer say out loud. */
346
+ externalDisplayId: string | null;
347
+ buyer: NormalizedBuyer;
348
+ lines: NormalizedOrderLine[];
349
+ /**
350
+ * What the buyer paid, gross. NOT derived from the lines when the
351
+ * marketplace states it: its total carries fees, promotions and rounding the
352
+ * line sum does not, and the number that must match the receipt is this one.
353
+ */
354
+ totalCents: number;
355
+ /** The marketplace's cut, when it states one. NULL is "did not say", not 0. */
356
+ commissionCents: number | null;
357
+ /** The marketplace's own promise to the buyer. */
358
+ deliveryDueAt: Date | null;
359
+ /** When the marketplace says the order was placed. */
360
+ placedAt: Date;
361
+ /** Null when the payload does not say. */
362
+ orderType?: MarketplaceOrderType | null;
363
+ /** Null on a take-out and when unstated. */
364
+ deliveredBy?: MarketplaceDeliveredBy | null;
365
+ /** Null on a take-out and when unstated. */
366
+ deliveryAddress?: NormalizedAddress | null;
367
+ /**
368
+ * OPTIONAL. The merchant the order names, when its body says (Open
369
+ * Delivery's `merchant.id`). A fetched order naming another merchant than
370
+ * the connection's is refused.
371
+ */
372
+ externalMerchantId?: string | null;
373
+ }
374
+ /**
375
+ * What an event says about the STORE's own cancellation request: `CONFIRMED`
376
+ * when the marketplace cancelled the order (a pending request is answered),
377
+ * `FAILED` when it refused the request.
378
+ */
379
+ type CancellationOutcome = "CONFIRMED" | "FAILED";
380
+ /** What an adapter makes of one inbound delivery. */
381
+ interface NormalizedEvent {
382
+ provider: MarketplaceProvider;
383
+ /** The delivery's own id — the inbox dedup key, paired with the provider. */
384
+ eventId: string;
385
+ /** The provider's event type verbatim, kept for the inbox row and the log. */
386
+ eventType: string;
387
+ /** The merchant this event names, used to attribute it to a tenant. */
388
+ externalMerchantId: string | null;
389
+ externalOrderId: string | null;
390
+ /**
391
+ * The order this event carries, or null when the event is one the engine
392
+ * understands and does not ingest. A null here is an IGNORED inbox row (or a
393
+ * cancellation, below), never a failure.
394
+ */
395
+ order: NormalizedMarketplaceOrder | null;
396
+ /** The marketplace has CANCELLED the order. */
397
+ cancelled: boolean;
398
+ /**
399
+ * The answer to the store's own cancellation request, when this event is
400
+ * one. A `cancelled: true` event reads as `CONFIRMED` when this is absent.
401
+ */
402
+ cancellationOutcome?: CancellationOutcome | null;
403
+ /**
404
+ * OPTIONAL. This event announces an order whose body the marketplace serves
405
+ * separately (Open Delivery's `CREATED` carries only the order's id): the
406
+ * engine fetches it through the adapter's `fetchOrder`, in the
407
+ * `marketplace.fetch-order` job, never inside the request that delivered
408
+ * the event. Ignored when `order` is present.
409
+ */
410
+ orderToFetch?: boolean;
411
+ }
412
+
413
+ /**
414
+ * The adapter port: everything one marketplace is, behind one shape.
415
+ *
416
+ * The owner's rule, which this file exists to make true: "the integration
417
+ * engine must be the same for all integrations: iFood, Rappi, 99. Only an
418
+ * adapter is made for each one, to connect to the base engine."
419
+ *
420
+ * A marketplace differs from the next in five ways that matter to a store:
421
+ * how its deliveries look and are signed (`normalize`, `readSignature`,
422
+ * `verifySignature`), what it lets a store answer through its API (`confirm`,
423
+ * `requestCancellation`), which reasons a refusal may carry
424
+ * (`cancellationReasons`), and where a person goes when the API cannot do it
425
+ * (`appLink`). Everything else — the inbox, the clock, the SKU map, the
426
+ * outbound bookkeeping and its retries — is the engine's and does not change
427
+ * with the marketplace.
428
+ *
429
+ * What a marketplace CAN do is stated by its adapter as
430
+ * {@link MarketplaceCapabilities} and read by the engine and the host. It is
431
+ * never inferred from the provider's id: a branch on the id outside an adapter
432
+ * would make the next marketplace a code change instead of an adapter.
433
+ *
434
+ * The HTTP client is a host port ({@link MarketplaceHttp}), handed to every
435
+ * outbound call. An adapter never reaches the network on its own, so tests
436
+ * answer the seam and nothing else, and the host owns the timeout, the proxy
437
+ * and the CA.
438
+ */
439
+ /** A marketplace's credentials, as the merchant supplies them. Sealed at rest. */
440
+ interface MarketplaceCredentials {
441
+ clientId: string;
442
+ clientSecret: string;
443
+ /**
444
+ * The shared secret a delivery's signature is verified against. Separate
445
+ * from the OAuth pair: a merchant rotating their webhook secret has not
446
+ * re-onboarded.
447
+ */
448
+ webhookSecret: string | null;
449
+ }
450
+ /** What the host's HTTP client takes. */
451
+ interface MarketplaceHttpRequest {
452
+ /**
453
+ * `PATCH` since FUT-4768: iFood's catalogue sets an item's status with it.
454
+ * `DELETE` since FUT-4851: iFood removes an interruption with it.
455
+ */
456
+ method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
457
+ url: string;
458
+ headers: Record<string, string>;
459
+ body?: string;
460
+ }
461
+ /** What the host's HTTP client answers: any status, never a throw for a 4xx or 5xx. */
462
+ interface MarketplaceHttpResponse {
463
+ status: number;
464
+ body: string;
465
+ }
466
+ /** The host's HTTP client. It throws only when no answer arrived (a timeout, a network error). */
467
+ type MarketplaceHttp = (request: MarketplaceHttpRequest) => Promise<MarketplaceHttpResponse>;
468
+ /** What a marketplace can do, stated by its adapter. */
469
+ interface MarketplaceCapabilities {
470
+ /** Accepting is answered through the API. */
471
+ apiConfirm: boolean;
472
+ /** A cancellation can be requested through the API at all. */
473
+ apiCancel: boolean;
474
+ /** …and still once the order was accepted. */
475
+ cancelAfterAccept: boolean;
476
+ /** The cancellation's outcome arrives later as an event rather than in the answer. */
477
+ asyncCancellationOutcome: boolean;
478
+ /** `appLink` names this order, rather than only the marketplace's portal. */
479
+ perOrderAppLink: boolean;
480
+ /** Confirming states a preparation time. */
481
+ confirmTakesPreparationTime: boolean;
482
+ /**
483
+ * OPTIONAL. How long the marketplace takes, at most, to answer a store's
484
+ * cancellation request, in milliseconds: past it the outbound sweep fails
485
+ * the request as TIMEOUT. Absent: only the 24-hour give-up applies.
486
+ */
487
+ cancellationAnswerWindowMs?: number;
488
+ }
489
+ /** One reason a store may give for refusing or cancelling an order. */
490
+ interface CancellationReason {
491
+ /** The marketplace's own code, sent verbatim. */
492
+ code: string;
493
+ /** The label the host was configured with for this code (a named pack). */
494
+ label: string;
495
+ /** This reason means "an item is unavailable": the host preselects it when stock is short. */
496
+ itemUnavailable: boolean;
497
+ /** The marketplace requires the offending SKUs with this reason. */
498
+ needsItems: boolean;
499
+ }
500
+ /** The order an outbound call is about, as the engine knows it. */
501
+ interface OutboundOrderRef {
502
+ externalOrderId: string;
503
+ externalDisplayId: string | null;
504
+ /** The host's own id for the order. */
505
+ hostOrderCode: string;
506
+ /** When the engine recorded the order. */
507
+ placedAt: Date;
508
+ }
509
+ /** A per-engine token cache an adapter reads and fills. Never persisted. */
510
+ type AdapterTokenCache = Map<string, {
511
+ token: string;
512
+ expiresAt: number;
513
+ }>;
514
+ /** What one outbound call is handed. */
515
+ interface AdapterCallContext {
516
+ http: MarketplaceHttp;
517
+ credentials: MarketplaceCredentials;
518
+ environment: "SANDBOX" | "PRODUCTION";
519
+ /** Written to no row, log or audit entry: memory only, per process. */
520
+ tokens: AdapterTokenCache;
521
+ now: Date;
522
+ }
523
+ /** One outbound call's answer, in the three shapes the engine acts on. */
524
+ type OutboundResult =
525
+ /** 2xx. For a marketplace whose outcome is asynchronous, "the request was taken". */
526
+ {
527
+ kind: "accepted";
528
+ }
529
+ /** A final 4xx (not 408/425/429): never retried. */
530
+ | {
531
+ kind: "refused";
532
+ status: number;
533
+ message: string;
534
+ }
535
+ /** 408, 425, 429, 5xx, a timeout, a network error: retried until the last attempt. */
536
+ | {
537
+ kind: "retry";
538
+ reason: string;
539
+ };
540
+ /**
541
+ * One order-body fetch's answer: the order, normalized, or the same refusal and
542
+ * retry shapes an outbound call answers with (a 404 for an expired order is a
543
+ * refusal; a 5xx, a timeout or a 429 is a retry).
544
+ */
545
+ type FetchOrderResult = {
546
+ kind: "order";
547
+ order: NormalizedMarketplaceOrder;
548
+ } | Exclude<OutboundResult, {
549
+ kind: "accepted";
550
+ }>;
551
+ /** What a cancellation request carries. */
552
+ interface CancellationRequestInput {
553
+ reason: CancellationReason;
554
+ /** Free text from the store; empty means "the reason's own label". */
555
+ text: string;
556
+ mode: "MANUAL" | "AUTO";
557
+ offendingSkus: readonly string[];
558
+ }
559
+ /** One marketplace, in and out. */
560
+ interface MarketplaceAdapter {
561
+ readonly provider: MarketplaceProvider;
562
+ readonly capabilities: MarketplaceCapabilities;
563
+ /** Inbound: one delivery into the canonical shape, or null when it carries no order id. */
564
+ normalize(payload: unknown): NormalizedEvent | null;
565
+ /** Which header carries this marketplace's signature, read off the request. */
566
+ readSignature(header: (name: string) => string | null): string | null;
567
+ /** Is this delivery's signature the one this store's secret would produce? */
568
+ verifySignature(rawBody: string, signature: string | null, secret: string | null): boolean;
569
+ /** The marketplace's documented reasons, static and offline: a dialog never waits on the network. */
570
+ cancellationReasons(): readonly CancellationReason[];
571
+ /** Tell the marketplace the store accepted the order. */
572
+ confirm(ctx: AdapterCallContext, order: OutboundOrderRef, input: {
573
+ preparationMinutes: number | null;
574
+ }): Promise<OutboundResult>;
575
+ /** Ask the marketplace to cancel the order, with one of its own reasons. */
576
+ requestCancellation(ctx: AdapterCallContext, order: OutboundOrderRef, input: CancellationRequestInput): Promise<OutboundResult>;
577
+ /**
578
+ * OPTIONAL. Fetch the order an event announced with `orderToFetch` (a
579
+ * marketplace whose events carry no order body). Called only from the
580
+ * `marketplace.fetch-order` job. An adapter whose events always carry the
581
+ * order (iFood) leaves it out.
582
+ */
583
+ fetchOrder?(ctx: AdapterCallContext, order: {
584
+ externalOrderId: string;
585
+ }): Promise<FetchOrderResult>;
586
+ /**
587
+ * OPTIONAL. Publishing the store's products to this marketplace's catalogue
588
+ * (FUT-4768): `push` (the adapter writes each item) or `pull` (the
589
+ * marketplace reads a menu document the host serves). Absent: the
590
+ * marketplace cannot be published to, and the host says so.
591
+ */
592
+ readonly catalogue?: MarketplaceCatalogue;
593
+ /**
594
+ * OPTIONAL. Closing the store on this marketplace for a while, and opening
595
+ * it again (FUT-4851). Absent: the marketplace cannot be paused through an
596
+ * API, and the host says so.
597
+ */
598
+ readonly availability?: MarketplaceAvailability;
599
+ /** Where "open it on the marketplace" goes: the order when `perOrderAppLink`, else the portal. Always https. */
600
+ appLink(order: Pick<OutboundOrderRef, "externalOrderId" | "externalDisplayId">): string;
601
+ }
602
+ /**
603
+ * Read one HTTP answer the way the engine acts on it. Shared by every adapter
604
+ * whose API speaks plain status codes, so the rule is one rule rather than one
605
+ * per marketplace: 2xx accepted; a 4xx other than 408/425/429 refused and
606
+ * final; everything else (408, 425, 429, 5xx, anything unexpected) retried.
607
+ */
608
+ declare function outboundResultOf(response: MarketplaceHttpResponse): OutboundResult;
609
+ /** A call that never got an answer is a retry, with the reason a log can read. */
610
+ declare function answered(call: () => Promise<OutboundResult>): Promise<OutboundResult>;
611
+
612
+ export { type AdapterCallContext as A, type MarketplaceHttpResponse as B, type CatalogueMenuEntry as C, type MarketplaceOrderType as D, type NormalizedAddress as E, type FetchOrderResult as F, type NormalizedBuyer as G, type NormalizedOrderLine as H, type OutboundOrderRef as I, PROVIDER_ID_PATTERN as J, answered as K, outboundResultOf as L, type MarketplaceAvailability as M, type NormalizedEvent as N, type OutboundResult as O, type PullCatalogue as P, type StorePauseRequest as S, type NormalizedMarketplaceOrder as a, type CatalogueMenuOptions as b, type MarketplaceAdapter as c, type PushCatalogue as d, type CatalogueItemOut as e, type StorePauseResult as f, type StorePauseRef as g, type MarketplaceProvider as h, type CancellationReason as i, type AdapterTokenCache as j, type CancellationOutcome as k, type CancellationRequestInput as l, type CatalogueCallContext as m, type CatalogueFailure as n, type CatalogueImage as o, type CatalogueItem as p, type CatalogueItemRef as q, type CataloguePublishResult as r, type CatalogueStoredImage as s, type CatalogueWarningCode as t, type MarketplaceCapabilities as u, type MarketplaceCatalogue as v, type MarketplaceCredentials as w, type MarketplaceDeliveredBy as x, type MarketplaceHttp as y, type MarketplaceHttpRequest as z };