@12-apps/marketplace 1.4.0 → 1.6.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 (41) hide show
  1. package/ADOPTING.md +1 -0
  2. package/README.md +52 -2
  3. package/dist/99food/index.d.ts +1 -1
  4. package/dist/99food/index.js +1 -1
  5. package/dist/{adapter-B8QtQ2RA.d.ts → adapter-CCWMqt2g.d.ts} +150 -1
  6. package/dist/{chunk-DPCZVN2C.js → chunk-4CJ7B5OR.js} +2 -2
  7. package/dist/{chunk-EBAIZQPB.js → chunk-NFZT5TWR.js} +19 -5
  8. package/dist/chunk-NFZT5TWR.js.map +1 -0
  9. package/dist/{chunk-SZRZTLVU.js → chunk-RGMSD7S7.js} +1 -1
  10. package/dist/chunk-RGMSD7S7.js.map +1 -0
  11. package/dist/chunk-WN2EK4KX.js +15 -0
  12. package/dist/chunk-WN2EK4KX.js.map +1 -0
  13. package/dist/{engine-o5cm54vC.d.ts → engine-w2TgRrlc.d.ts} +26 -2
  14. package/dist/hono/index.d.ts +2 -2
  15. package/dist/hono/index.js +2 -2
  16. package/dist/ifood/index.d.ts +32 -2
  17. package/dist/ifood/index.js +286 -9
  18. package/dist/ifood/index.js.map +1 -1
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +44 -2
  21. package/dist/index.js.map +1 -1
  22. package/dist/manifest/server.d.ts +2 -2
  23. package/dist/manifest/server.js +2 -2
  24. package/dist/rappi/index.d.ts +1 -1
  25. package/dist/rappi/index.js +1 -1
  26. package/dist/server/index.d.ts +2 -2
  27. package/dist/server/index.js +2 -2
  28. package/package.json +1 -1
  29. package/src/core/adapter.ts +7 -0
  30. package/src/core/catalogue-read-port.ts +151 -0
  31. package/src/core/catalogue-read.ts +51 -0
  32. package/src/core/engine.ts +9 -0
  33. package/src/core/ingest.ts +32 -4
  34. package/src/core/ports.ts +11 -0
  35. package/src/ifood/catalogue-read.ts +187 -0
  36. package/src/ifood/catalogue-snapshot.ts +246 -0
  37. package/src/ifood/index.ts +5 -0
  38. package/src/index.ts +15 -0
  39. package/dist/chunk-EBAIZQPB.js.map +0 -1
  40. package/dist/chunk-SZRZTLVU.js.map +0 -1
  41. /package/dist/{chunk-DPCZVN2C.js.map → chunk-4CJ7B5OR.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
+ }
@@ -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,