@12-apps/marketplace 1.4.0 → 1.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 +51 -1
- package/dist/99food/index.d.ts +1 -1
- package/dist/99food/index.js +1 -1
- package/dist/{adapter-B8QtQ2RA.d.ts → adapter-CCWMqt2g.d.ts} +150 -1
- package/dist/{chunk-EBAIZQPB.js → chunk-FKUEFVVA.js} +2 -1
- package/dist/{chunk-SZRZTLVU.js → chunk-RGMSD7S7.js} +1 -1
- package/dist/chunk-RGMSD7S7.js.map +1 -0
- package/dist/{chunk-DPCZVN2C.js → chunk-UONWZIBO.js} +2 -2
- package/dist/chunk-WN2EK4KX.js +15 -0
- package/dist/chunk-WN2EK4KX.js.map +1 -0
- package/dist/{engine-o5cm54vC.d.ts → engine-C3uq_BJd.d.ts} +11 -1
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +2 -2
- package/dist/ifood/index.d.ts +32 -2
- package/dist/ifood/index.js +286 -9
- package/dist/ifood/index.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +44 -2
- package/dist/index.js.map +1 -1
- package/dist/manifest/server.d.ts +2 -2
- package/dist/manifest/server.js +2 -2
- package/dist/rappi/index.d.ts +1 -1
- package/dist/rappi/index.js +1 -1
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.js +2 -2
- package/package.json +1 -1
- package/src/core/adapter.ts +7 -0
- package/src/core/catalogue-read-port.ts +151 -0
- package/src/core/catalogue-read.ts +51 -0
- package/src/core/engine.ts +9 -0
- package/src/ifood/catalogue-read.ts +187 -0
- package/src/ifood/catalogue-snapshot.ts +246 -0
- package/src/ifood/index.ts +5 -0
- package/src/index.ts +15 -0
- package/dist/chunk-SZRZTLVU.js.map +0 -1
- /package/dist/{chunk-EBAIZQPB.js.map → chunk-FKUEFVVA.js.map} +0 -0
- /package/dist/{chunk-DPCZVN2C.js.map → chunk-UONWZIBO.js.map} +0 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import {
|
|
2
|
+
centsFromMajor,
|
|
3
|
+
type CatalogueReadNotice,
|
|
4
|
+
type CatalogueReadWarning,
|
|
5
|
+
type CatalogueSnapshotItem,
|
|
6
|
+
type CatalogueSnapshotOption,
|
|
7
|
+
type CatalogueSnapshotOptionGroup,
|
|
8
|
+
} from "../core/catalogue-read-port";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* iFood's catalogue answer mapped into the engine's {@link CatalogueSnapshotItem}
|
|
12
|
+
* (FUT-951). Pure and offline: `./catalogue-read.ts` makes the calls.
|
|
13
|
+
*
|
|
14
|
+
* The shapes are the Catalog API v2.0's `PUT /items` payload, which the
|
|
15
|
+
* portal's Introduction shows whole (read 2026-11-16): an `item` (id, type,
|
|
16
|
+
* categoryId, status, `price { value }`, externalCode), `products` (id, name,
|
|
17
|
+
* description, externalCode, and `optionGroups: [{ id, min, max }]` linking a
|
|
18
|
+
* product to its groups), `optionGroups` (id, name, status, `optionIds`) and
|
|
19
|
+
* `options` (id, `productId` naming the product that holds its name, status,
|
|
20
|
+
* `price { value }`). The package README's "Catalogue reading" table marks
|
|
21
|
+
* each fact CONFIRMED with its URL or ESTIMATED. ESTIMATED here, and said
|
|
22
|
+
* beside each use:
|
|
23
|
+
* - that `GET /categories/{categoryId}/items` answers the same four lists
|
|
24
|
+
* (`items` plural) the PUT takes;
|
|
25
|
+
* - that `item.productId` names the item's product (the field the push sends);
|
|
26
|
+
* - that `products[].imagePath` holds the photo, a path under
|
|
27
|
+
* {@link IFOOD_IMAGE_BASE_URL}.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Where iFood serves a catalogue photo's `imagePath`. ESTIMATED: the Catalog
|
|
32
|
+
* pages read name `imagePath` but not its host; this is the host iFood's own
|
|
33
|
+
* app loads dish photos from. A path that is already an absolute https URL is
|
|
34
|
+
* used as it is.
|
|
35
|
+
*/
|
|
36
|
+
export const IFOOD_IMAGE_BASE_URL = "https://static-images.ifood.com.br/image/upload/t_high/pratos/";
|
|
37
|
+
|
|
38
|
+
type Json = Record<string, unknown>;
|
|
39
|
+
|
|
40
|
+
export const record = (value: unknown): Json | null =>
|
|
41
|
+
typeof value === "object" && value !== null && !Array.isArray(value) ? (value as Json) : null;
|
|
42
|
+
|
|
43
|
+
export const text = (value: unknown): string | null => (typeof value === "string" && value.trim() !== "" ? value.trim() : null);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The objects of a JSON array, and every other entry it held (a string, a
|
|
47
|
+
* number, a nested array), so the caller can SAY what it could not read rather
|
|
48
|
+
* than lose it. Not an array: no entries, nothing else.
|
|
49
|
+
*/
|
|
50
|
+
export function entriesOf(value: unknown): { entries: Json[]; others: unknown[] } {
|
|
51
|
+
const entries: Json[] = [];
|
|
52
|
+
const others: unknown[] = [];
|
|
53
|
+
for (const entry of Array.isArray(value) ? value : []) {
|
|
54
|
+
const object = record(entry);
|
|
55
|
+
if (object) entries.push(object);
|
|
56
|
+
else others.push(entry);
|
|
57
|
+
}
|
|
58
|
+
return { entries, others };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The lookup lists (`products`, `optionGroups`, `options`) keep only objects
|
|
63
|
+
* with an id: an entry is only ever reached through an id that names it, and a
|
|
64
|
+
* reference that finds nothing is warned about where it is made
|
|
65
|
+
* (OPTION_GROUP_UNREADABLE, OPTION_UNREADABLE, or the item's ITEM_UNREADABLE).
|
|
66
|
+
*/
|
|
67
|
+
const list = (value: unknown): Json[] => entriesOf(value).entries;
|
|
68
|
+
|
|
69
|
+
/** iFood's `status`: anything but UNAVAILABLE is on sale (an absent status included). */
|
|
70
|
+
export const availableOf = (status: unknown): boolean => status !== "UNAVAILABLE";
|
|
71
|
+
|
|
72
|
+
/** The four lists of one category's answer, each by id. */
|
|
73
|
+
interface IfoodCatalogueIndex {
|
|
74
|
+
products: Map<string, Json>;
|
|
75
|
+
optionGroups: Map<string, Json>;
|
|
76
|
+
options: Map<string, Json>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const byId = (entries: Json[]): Map<string, Json> =>
|
|
80
|
+
new Map(entries.flatMap((entry) => (text(entry.id) ? [[text(entry.id) as string, entry] as const] : [])));
|
|
81
|
+
|
|
82
|
+
export function indexOf(answer: Json): IfoodCatalogueIndex {
|
|
83
|
+
return { products: byId(list(answer.products)), optionGroups: byId(list(answer.optionGroups)), options: byId(list(answer.options)) };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const priceCentsOf = (price: unknown): number | null => centsFromMajor(record(price)?.value);
|
|
87
|
+
|
|
88
|
+
const warning = (code: CatalogueReadWarning["code"], subject: string | null, detail: unknown = null): CatalogueReadWarning => ({
|
|
89
|
+
code,
|
|
90
|
+
subject,
|
|
91
|
+
detail: detail === null || detail === undefined ? null : JSON.stringify(detail),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
/** The photo's absolute URL; a warning when a reference is there but cannot be resolved. */
|
|
95
|
+
export function photoOf(imagePath: unknown, subject: string | null): { url: string | null; warning?: CatalogueReadWarning } {
|
|
96
|
+
if (imagePath === undefined || imagePath === null || imagePath === "") return { url: null };
|
|
97
|
+
const path = text(imagePath);
|
|
98
|
+
if (path === null) return { url: null, warning: warning("PHOTO_UNREADABLE", subject, imagePath) };
|
|
99
|
+
if (/^https:\/\//i.test(path)) return { url: path };
|
|
100
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(path)) return { url: null, warning: warning("PHOTO_UNREADABLE", subject, path) };
|
|
101
|
+
return { url: `${IFOOD_IMAGE_BASE_URL}${urlSafePath(path.replace(/^\/+/, ""))}` };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The path as a URL path: kept as it is when it is already one (an encoded
|
|
106
|
+
* `%20` stays `%20`, never `%2520`); only a character a URL may not carry
|
|
107
|
+
* (a space, a quote, a non-ASCII letter) is encoded, and a `%` that starts no
|
|
108
|
+
* escape becomes `%25`.
|
|
109
|
+
*/
|
|
110
|
+
function urlSafePath(path: string): string {
|
|
111
|
+
return path
|
|
112
|
+
.replace(/%(?![0-9A-Fa-f]{2})/g, "%25")
|
|
113
|
+
.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@/%]/g, (character) => encodeURIComponent(character));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The words and photo of an item or option: its product's (`productId`), else its own. */
|
|
117
|
+
interface Described {
|
|
118
|
+
name: string | null;
|
|
119
|
+
description: string | null;
|
|
120
|
+
externalCode: string | null;
|
|
121
|
+
imagePath: unknown;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function describedBy(index: IfoodCatalogueIndex, own: Json): Described {
|
|
125
|
+
const product = index.products.get(text(own.productId) ?? "") ?? {};
|
|
126
|
+
return {
|
|
127
|
+
name: text(product.name) ?? text(own.name),
|
|
128
|
+
description: text(product.description) ?? text(own.description),
|
|
129
|
+
externalCode: text(own.externalCode) ?? text(product.externalCode),
|
|
130
|
+
imagePath: product.imagePath ?? own.imagePath,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The photo's URL, its warning (if any) pushed onto `warnings`. */
|
|
135
|
+
function photoInto(imagePath: unknown, subject: string | null, warnings: CatalogueReadWarning[]): string | null {
|
|
136
|
+
const photo = photoOf(imagePath, subject);
|
|
137
|
+
if (photo.warning) warnings.push(photo.warning);
|
|
138
|
+
return photo.url;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** One option of a group, or the warning that leaves it out. */
|
|
142
|
+
function optionOf(id: string, index: IfoodCatalogueIndex, warnings: CatalogueReadWarning[]): CatalogueSnapshotOption | null {
|
|
143
|
+
const option = index.options.get(id);
|
|
144
|
+
const described = option ? describedBy(index, option) : null;
|
|
145
|
+
if (!option || !described || described.name === null) {
|
|
146
|
+
warnings.push(warning("OPTION_UNREADABLE", id, option));
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const priceDeltaCents = priceCentsOf(option.price);
|
|
150
|
+
if (priceDeltaCents === null) warnings.push(warning("OPTION_PRICE_MISSING", id, option.price));
|
|
151
|
+
return {
|
|
152
|
+
externalId: id,
|
|
153
|
+
externalCode: described.externalCode,
|
|
154
|
+
name: described.name,
|
|
155
|
+
description: described.description,
|
|
156
|
+
priceDeltaCents,
|
|
157
|
+
photoUrl: photoInto(described.imagePath, id, warnings),
|
|
158
|
+
available: availableOf(option.status),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A group's bounds, when they make sense: whole numbers, 0 <= min <= max, max >= 1. */
|
|
163
|
+
function boundsOf(link: Json, group: Json): { min: number; max: number } | null {
|
|
164
|
+
const min = link.min ?? group.min ?? 0;
|
|
165
|
+
const max = link.max ?? group.max;
|
|
166
|
+
if (!Number.isInteger(min) || !Number.isInteger(max)) return null;
|
|
167
|
+
const [low, high] = [min as number, max as number];
|
|
168
|
+
return low >= 0 && high >= 1 && high >= low ? { min: low, max: high } : null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** A group's option ids; an entry that is not an id is an OPTION_UNREADABLE warning, never dropped. */
|
|
172
|
+
function idsOf(groupId: string, value: unknown, warnings: CatalogueReadWarning[]): string[] {
|
|
173
|
+
const ids: string[] = [];
|
|
174
|
+
for (const entry of Array.isArray(value) ? value : []) {
|
|
175
|
+
const id = text(entry);
|
|
176
|
+
if (id === null) warnings.push(warning("OPTION_UNREADABLE", groupId, entry));
|
|
177
|
+
else ids.push(id);
|
|
178
|
+
}
|
|
179
|
+
return ids;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** One group a product links to (`products[].optionGroups[]`), or the warning that leaves it out. */
|
|
183
|
+
function groupOf(link: Json, index: IfoodCatalogueIndex, warnings: CatalogueReadWarning[]): CatalogueSnapshotOptionGroup | null {
|
|
184
|
+
const id = text(link.id);
|
|
185
|
+
const group = id === null ? undefined : index.optionGroups.get(id);
|
|
186
|
+
const bounds = group ? boundsOf(link, group) : null;
|
|
187
|
+
const name = text(group?.name);
|
|
188
|
+
if (id === null || !group || name === null || bounds === null) {
|
|
189
|
+
warnings.push(warning("OPTION_GROUP_UNREADABLE", id, { link, group }));
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
const options = idsOf(id, group.optionIds, warnings).flatMap((optionId) => optionOf(optionId, index, warnings) ?? []);
|
|
193
|
+
if (options.length === 0) {
|
|
194
|
+
warnings.push(warning("OPTION_GROUP_UNREADABLE", id, "no readable option"));
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
return { externalId: id, externalCode: text(group.externalCode), name, ...bounds, available: availableOf(group.status), options };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The item's own warnings: a kind it maps only partly, and a missing price. */
|
|
201
|
+
function itemWarnings(raw: Json, priceCents: number | null): CatalogueReadWarning[] {
|
|
202
|
+
const warnings: CatalogueReadWarning[] = [];
|
|
203
|
+
const type = text(raw.type);
|
|
204
|
+
if (type !== null && type !== "DEFAULT") warnings.push(warning("ITEM_TYPE_UNSUPPORTED", null, type));
|
|
205
|
+
if (priceCents === null) warnings.push(warning("PRICE_MISSING", null, raw.price));
|
|
206
|
+
return warnings;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The group links of an item: on its product (CONFIRMED, the PUT example),
|
|
211
|
+
* else on the item itself. Links that are not objects, or a value that is not
|
|
212
|
+
* a list at all, are an OPTION_GROUP_UNREADABLE warning each.
|
|
213
|
+
*/
|
|
214
|
+
function linksOf(index: IfoodCatalogueIndex, raw: Json, warnings: CatalogueReadWarning[]): Json[] {
|
|
215
|
+
const product = index.products.get(text(raw.productId) ?? "");
|
|
216
|
+
const value = product?.optionGroups ?? raw.optionGroups;
|
|
217
|
+
if (value !== undefined && value !== null && !Array.isArray(value)) warnings.push(warning("OPTION_GROUP_UNREADABLE", null, value));
|
|
218
|
+
const { entries, others } = entriesOf(value);
|
|
219
|
+
for (const other of others) warnings.push(warning("OPTION_GROUP_UNREADABLE", null, other));
|
|
220
|
+
return entries;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** One item of a category, or the notice that says why it could not be read. */
|
|
224
|
+
export function itemOf(raw: Json, index: IfoodCatalogueIndex, categoryId: string): CatalogueSnapshotItem | CatalogueReadNotice {
|
|
225
|
+
const id = text(raw.id);
|
|
226
|
+
// ESTIMATED: `item.productId` names the product that holds the item's name, description and photo.
|
|
227
|
+
const described = describedBy(index, raw);
|
|
228
|
+
if (id === null || described.name === null) {
|
|
229
|
+
return { code: "ITEM_UNREADABLE", categoryExternalId: categoryId, externalId: id, detail: JSON.stringify(raw).slice(0, 300) };
|
|
230
|
+
}
|
|
231
|
+
const priceCents = priceCentsOf(raw.price);
|
|
232
|
+
const warnings = itemWarnings(raw, priceCents);
|
|
233
|
+
const photoUrl = photoInto(described.imagePath, null, warnings);
|
|
234
|
+
const optionGroups = linksOf(index, raw, warnings).flatMap((link) => groupOf(link, index, warnings) ?? []);
|
|
235
|
+
return {
|
|
236
|
+
externalId: id,
|
|
237
|
+
externalCode: described.externalCode,
|
|
238
|
+
name: described.name,
|
|
239
|
+
description: described.description,
|
|
240
|
+
priceCents,
|
|
241
|
+
photoUrl,
|
|
242
|
+
available: availableOf(raw.status),
|
|
243
|
+
optionGroups,
|
|
244
|
+
warnings,
|
|
245
|
+
};
|
|
246
|
+
}
|
package/src/ifood/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { CancellationReason, MarketplaceAdapter } from "../core/adapter";
|
|
2
2
|
import { IFOOD_REASON_CODES, type IfoodAdapterCopy, type IfoodReasonCode } from "./copy";
|
|
3
3
|
import { IFOOD_CATALOGUE } from "./catalogue";
|
|
4
|
+
import { IFOOD_CATALOGUE_READER } from "./catalogue-read";
|
|
4
5
|
import { normalizeIfoodEvent, verifyIfoodSignature } from "./normalize";
|
|
5
6
|
import { IFOOD_PORTAL_URL, ifoodConfirm, ifoodRequestCancellation } from "./outbound";
|
|
6
7
|
import { IFOOD_AVAILABILITY } from "./pause";
|
|
@@ -66,6 +67,8 @@ export function createIfoodAdapter(config: IfoodAdapterConfig): MarketplaceAdapt
|
|
|
66
67
|
cancellationReasons: () => reasons,
|
|
67
68
|
// The Catalog API v2.0: each item is written and paused through the API (FUT-4768).
|
|
68
69
|
catalogue: IFOOD_CATALOGUE,
|
|
70
|
+
// The same API read back: the menu the store keeps on iFood, for the host to import (FUT-951).
|
|
71
|
+
catalogueReader: IFOOD_CATALOGUE_READER,
|
|
69
72
|
confirm: (ctx, order) => ifoodConfirm(ctx, order),
|
|
70
73
|
requestCancellation: ifoodRequestCancellation,
|
|
71
74
|
// The Merchant API's interruptions: an id and an end per closure (FUT-4851).
|
|
@@ -79,4 +82,6 @@ export function createIfoodAdapter(config: IfoodAdapterConfig): MarketplaceAdapt
|
|
|
79
82
|
export { IFOOD_REASON_CODES, type IfoodAdapterCopy, type IfoodReasonCode } from "./copy";
|
|
80
83
|
export { normalizeIfoodEvent, verifyIfoodSignature } from "./normalize";
|
|
81
84
|
export { IFOOD_CATALOGUE, ifoodItemBody, ifoodItemId, ifoodProductId } from "./catalogue";
|
|
85
|
+
export { IFOOD_CATALOGUE_READER } from "./catalogue-read";
|
|
86
|
+
export { IFOOD_IMAGE_BASE_URL } from "./catalogue-snapshot";
|
|
82
87
|
export { IFOOD_AVAILABILITY, ifoodLocalTime, ifoodPause, ifoodResume } from "./pause";
|
package/src/index.ts
CHANGED
|
@@ -162,6 +162,21 @@ export type {
|
|
|
162
162
|
PullCatalogue,
|
|
163
163
|
PushCatalogue,
|
|
164
164
|
} from "./core/catalogue-port";
|
|
165
|
+
export { centsFromMajor } from "./core/catalogue-read-port";
|
|
166
|
+
export type {
|
|
167
|
+
CatalogueReadContext,
|
|
168
|
+
CatalogueReader,
|
|
169
|
+
CatalogueReadNotice,
|
|
170
|
+
CatalogueReadRefusalCode,
|
|
171
|
+
CatalogueReadResult,
|
|
172
|
+
CatalogueReadWarning,
|
|
173
|
+
CatalogueReadWarningCode,
|
|
174
|
+
CatalogueSnapshot,
|
|
175
|
+
CatalogueSnapshotCategory,
|
|
176
|
+
CatalogueSnapshotItem,
|
|
177
|
+
CatalogueSnapshotOption,
|
|
178
|
+
CatalogueSnapshotOptionGroup,
|
|
179
|
+
} from "./core/catalogue-read-port";
|
|
165
180
|
export type {
|
|
166
181
|
CatalogueFailedAction,
|
|
167
182
|
CatalogueFailureCode,
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/core/adapter.ts"],"sourcesContent":["import type { MarketplaceCatalogue } from \"./catalogue-port\";\nimport type { MarketplaceAvailability } from \"./pause-port\";\nimport type { MarketplaceProvider, NormalizedEvent, NormalizedMarketplaceOrder } from \"./types\";\n\n/**\n * The adapter port: everything one marketplace is, behind one shape.\n *\n * The owner's rule, which this file exists to make true: \"the integration\n * engine must be the same for all integrations: iFood, Rappi, 99. Only an\n * adapter is made for each one, to connect to the base engine.\"\n *\n * A marketplace differs from the next in five ways that matter to a store:\n * how its deliveries look and are signed (`normalize`, `readSignature`,\n * `verifySignature`), what it lets a store answer through its API (`confirm`,\n * `requestCancellation`), which reasons a refusal may carry\n * (`cancellationReasons`), and where a person goes when the API cannot do it\n * (`appLink`). Everything else — the inbox, the clock, the SKU map, the\n * outbound bookkeeping and its retries — is the engine's and does not change\n * with the marketplace.\n *\n * What a marketplace CAN do is stated by its adapter as\n * {@link MarketplaceCapabilities} and read by the engine and the host. It is\n * never inferred from the provider's id: a branch on the id outside an adapter\n * would make the next marketplace a code change instead of an adapter.\n *\n * The HTTP client is a host port ({@link MarketplaceHttp}), handed to every\n * outbound call. An adapter never reaches the network on its own, so tests\n * answer the seam and nothing else, and the host owns the timeout, the proxy\n * and the CA.\n */\n\n/** A marketplace's credentials, as the merchant supplies them. Sealed at rest. */\nexport interface MarketplaceCredentials {\n clientId: string;\n clientSecret: string;\n /**\n * The shared secret a delivery's signature is verified against. Separate\n * from the OAuth pair: a merchant rotating their webhook secret has not\n * re-onboarded.\n */\n webhookSecret: string | null;\n}\n\n/** What the host's HTTP client takes. */\nexport interface MarketplaceHttpRequest {\n /**\n * `PATCH` since FUT-4768: iFood's catalogue sets an item's status with it.\n * `DELETE` since FUT-4851: iFood removes an interruption with it.\n */\n method: \"GET\" | \"POST\" | \"PUT\" | \"PATCH\" | \"DELETE\";\n url: string;\n headers: Record<string, string>;\n body?: string;\n}\n\n/** What the host's HTTP client answers: any status, never a throw for a 4xx or 5xx. */\nexport interface MarketplaceHttpResponse {\n status: number;\n body: string;\n}\n\n/** The host's HTTP client. It throws only when no answer arrived (a timeout, a network error). */\nexport type MarketplaceHttp = (request: MarketplaceHttpRequest) => Promise<MarketplaceHttpResponse>;\n\n/** What a marketplace can do, stated by its adapter. */\nexport interface MarketplaceCapabilities {\n /** Accepting is answered through the API. */\n apiConfirm: boolean;\n /** A cancellation can be requested through the API at all. */\n apiCancel: boolean;\n /** …and still once the order was accepted. */\n cancelAfterAccept: boolean;\n /** The cancellation's outcome arrives later as an event rather than in the answer. */\n asyncCancellationOutcome: boolean;\n /** `appLink` names this order, rather than only the marketplace's portal. */\n perOrderAppLink: boolean;\n /** Confirming states a preparation time. */\n confirmTakesPreparationTime: boolean;\n /**\n * OPTIONAL. How long the marketplace takes, at most, to answer a store's\n * cancellation request, in milliseconds: past it the outbound sweep fails\n * the request as TIMEOUT. Absent: only the 24-hour give-up applies.\n */\n cancellationAnswerWindowMs?: number;\n}\n\n/** One reason a store may give for refusing or cancelling an order. */\nexport interface CancellationReason {\n /** The marketplace's own code, sent verbatim. */\n code: string;\n /** The label the host was configured with for this code (a named pack). */\n label: string;\n /** This reason means \"an item is unavailable\": the host preselects it when stock is short. */\n itemUnavailable: boolean;\n /** The marketplace requires the offending SKUs with this reason. */\n needsItems: boolean;\n}\n\n/** The order an outbound call is about, as the engine knows it. */\nexport interface OutboundOrderRef {\n externalOrderId: string;\n externalDisplayId: string | null;\n /** The host's own id for the order. */\n hostOrderCode: string;\n /** When the engine recorded the order. */\n placedAt: Date;\n}\n\n/** A per-engine token cache an adapter reads and fills. Never persisted. */\nexport type AdapterTokenCache = Map<string, { token: string; expiresAt: number }>;\n\n/** What one outbound call is handed. */\nexport interface AdapterCallContext {\n http: MarketplaceHttp;\n credentials: MarketplaceCredentials;\n environment: \"SANDBOX\" | \"PRODUCTION\";\n /** Written to no row, log or audit entry: memory only, per process. */\n tokens: AdapterTokenCache;\n now: Date;\n}\n\n/** One outbound call's answer, in the three shapes the engine acts on. */\nexport type OutboundResult =\n /** 2xx. For a marketplace whose outcome is asynchronous, \"the request was taken\". */\n | { kind: \"accepted\" }\n /** A final 4xx (not 408/425/429): never retried. */\n | { kind: \"refused\"; status: number; message: string }\n /** 408, 425, 429, 5xx, a timeout, a network error: retried until the last attempt. */\n | { kind: \"retry\"; reason: string };\n\n/**\n * One order-body fetch's answer: the order, normalized, or the same refusal and\n * retry shapes an outbound call answers with (a 404 for an expired order is a\n * refusal; a 5xx, a timeout or a 429 is a retry).\n */\nexport type FetchOrderResult =\n | { kind: \"order\"; order: NormalizedMarketplaceOrder }\n | Exclude<OutboundResult, { kind: \"accepted\" }>;\n\n/** What a cancellation request carries. */\nexport interface CancellationRequestInput {\n reason: CancellationReason;\n /** Free text from the store; empty means \"the reason's own label\". */\n text: string;\n mode: \"MANUAL\" | \"AUTO\";\n offendingSkus: readonly string[];\n}\n\n/** One marketplace, in and out. */\nexport interface MarketplaceAdapter {\n readonly provider: MarketplaceProvider;\n readonly capabilities: MarketplaceCapabilities;\n /** Inbound: one delivery into the canonical shape, or null when it carries no order id. */\n normalize(payload: unknown): NormalizedEvent | null;\n /** Which header carries this marketplace's signature, read off the request. */\n readSignature(header: (name: string) => string | null): string | null;\n /** Is this delivery's signature the one this store's secret would produce? */\n verifySignature(rawBody: string, signature: string | null, secret: string | null): boolean;\n /** The marketplace's documented reasons, static and offline: a dialog never waits on the network. */\n cancellationReasons(): readonly CancellationReason[];\n /** Tell the marketplace the store accepted the order. */\n confirm(\n ctx: AdapterCallContext,\n order: OutboundOrderRef,\n input: { preparationMinutes: number | null },\n ): Promise<OutboundResult>;\n /** Ask the marketplace to cancel the order, with one of its own reasons. */\n requestCancellation(\n ctx: AdapterCallContext,\n order: OutboundOrderRef,\n input: CancellationRequestInput,\n ): Promise<OutboundResult>;\n /**\n * OPTIONAL. Fetch the order an event announced with `orderToFetch` (a\n * marketplace whose events carry no order body). Called only from the\n * `marketplace.fetch-order` job. An adapter whose events always carry the\n * order (iFood) leaves it out.\n */\n fetchOrder?(ctx: AdapterCallContext, order: { externalOrderId: string }): Promise<FetchOrderResult>;\n /**\n * OPTIONAL. Publishing the store's products to this marketplace's catalogue\n * (FUT-4768): `push` (the adapter writes each item) or `pull` (the\n * marketplace reads a menu document the host serves). Absent: the\n * marketplace cannot be published to, and the host says so.\n */\n readonly catalogue?: MarketplaceCatalogue;\n /**\n * OPTIONAL. Closing the store on this marketplace for a while, and opening\n * it again (FUT-4851). Absent: the marketplace cannot be paused through an\n * API, and the host says so.\n */\n readonly availability?: MarketplaceAvailability;\n /** Where \"open it on the marketplace\" goes: the order when `perOrderAppLink`, else the portal. Always https. */\n appLink(order: Pick<OutboundOrderRef, \"externalOrderId\" | \"externalDisplayId\">): string;\n}\n\n/**\n * The 4xx statuses that are NOT a refusal: a request timeout (408), \"too\n * early\" (425) and a rate limit (429) say \"ask again later\" by HTTP's own\n * semantics, and a marketplace rate-limits its merchant API.\n */\nconst TRANSIENT_CLIENT_STATUSES = new Set([408, 425, 429]);\n\n/**\n * Read one HTTP answer the way the engine acts on it. Shared by every adapter\n * whose API speaks plain status codes, so the rule is one rule rather than one\n * per marketplace: 2xx accepted; a 4xx other than 408/425/429 refused and\n * final; everything else (408, 425, 429, 5xx, anything unexpected) retried.\n */\nexport function outboundResultOf(response: MarketplaceHttpResponse): OutboundResult {\n if (response.status >= 200 && response.status < 300) return { kind: \"accepted\" };\n const message = response.body.slice(0, 300);\n const refusal = response.status >= 400 && response.status < 500 && !TRANSIENT_CLIENT_STATUSES.has(response.status);\n if (refusal) return { kind: \"refused\", status: response.status, message };\n return { kind: \"retry\", reason: `status ${response.status}: ${message}` };\n}\n\n/** A call that never got an answer is a retry, with the reason a log can read. */\nexport async function answered(call: () => Promise<OutboundResult>): Promise<OutboundResult> {\n try {\n return await call();\n } catch (error) {\n return { kind: \"retry\", reason: error instanceof Error ? error.message : String(error) };\n }\n}\n"],"mappings":";;;;;AAyMA,IAAM,4BAA4B,oBAAI,IAAI,CAAC,KAAK,KAAK,GAAG,CAAC;AAQlD,SAAS,iBAAiB,UAAmD;AAClF,MAAI,SAAS,UAAU,OAAO,SAAS,SAAS,IAAK,QAAO,EAAE,MAAM,WAAW;AAC/E,QAAM,UAAU,SAAS,KAAK,MAAM,GAAG,GAAG;AAC1C,QAAM,UAAU,SAAS,UAAU,OAAO,SAAS,SAAS,OAAO,CAAC,0BAA0B,IAAI,SAAS,MAAM;AACjH,MAAI,QAAS,QAAO,EAAE,MAAM,WAAW,QAAQ,SAAS,QAAQ,QAAQ;AACxE,SAAO,EAAE,MAAM,SAAS,QAAQ,UAAU,SAAS,MAAM,KAAK,OAAO,GAAG;AAC1E;AANgB;AAShB,eAAsB,SAAS,MAA8D;AAC3F,MAAI;AACF,WAAO,MAAM,KAAK;AAAA,EACpB,SAAS,OAAO;AACd,WAAO,EAAE,MAAM,SAAS,QAAQ,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,EAAE;AAAA,EACzF;AACF;AANsB;","names":[]}
|
|
File without changes
|
|
File without changes
|