@12-apps/marketplace 1.11.0 → 1.13.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/ADOPTING.md +134 -1
- package/README.md +49 -3
- package/dist/99food/index.d.ts +2 -2
- package/dist/99food/index.js +2 -2
- package/dist/{adapter-CD0SfdVS.d.ts → adapter-CQ4QiZPi.d.ts} +327 -174
- package/dist/{chunk-TVZU4HNT.js → chunk-4J2KJ3ZW.js} +1 -1
- package/dist/{chunk-TVZU4HNT.js.map → chunk-4J2KJ3ZW.js.map} +1 -1
- package/dist/{chunk-YWMEIMO3.js → chunk-CEWTV2TC.js} +176 -40
- package/dist/chunk-CEWTV2TC.js.map +1 -0
- package/dist/{chunk-DCWOQ5PN.js → chunk-NFJQQWG4.js} +2 -2
- package/dist/{chunk-OS4TSIFU.js → chunk-W57TITU4.js} +2 -2
- package/dist/{client-11sncog4.d.ts → client-CCSnylou.d.ts} +1 -1
- package/dist/{engine-C_3RK8wC.d.ts → engine-1KlJ_8jK.d.ts} +34 -5
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +2 -2
- package/dist/ifood/index.d.ts +20 -2
- package/dist/ifood/index.js +349 -125
- package/dist/ifood/index.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +162 -65
- package/dist/index.js.map +1 -1
- package/dist/keeta/index.d.ts +2 -18
- package/dist/keeta/index.js +12 -3
- package/dist/keeta/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 +8 -1
- package/src/core/connection-stamp.ts +61 -0
- package/src/core/courier-port.ts +100 -0
- package/src/core/courier.ts +153 -0
- package/src/core/engine.ts +4 -1
- package/src/core/fetch-order.ts +54 -12
- package/src/core/integration.ts +19 -4
- package/src/core/lifecycle.ts +27 -4
- package/src/core/outbound-queue.ts +24 -11
- package/src/core/outbound.ts +8 -6
- package/src/core/pickup-code.ts +136 -0
- package/src/core/pipeline.ts +15 -6
- package/src/core/ports.ts +23 -1
- package/src/core/status-store.ts +2 -3
- package/src/core/types.ts +56 -4
- package/src/ifood/courier.ts +210 -0
- package/src/ifood/fetch-order.ts +103 -0
- package/src/ifood/index.ts +9 -1
- package/src/ifood/normalize.ts +30 -3
- package/src/index.ts +16 -0
- package/src/keeta/normalize.ts +21 -6
- package/dist/chunk-YWMEIMO3.js.map +0 -1
- /package/dist/{chunk-DCWOQ5PN.js.map → chunk-NFJQQWG4.js.map} +0 -0
- /package/dist/{chunk-OS4TSIFU.js.map → chunk-W57TITU4.js.map} +0 -0
package/dist/ifood/index.js
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
answered,
|
|
18
18
|
outboundResultOf,
|
|
19
19
|
tokenGrantResultOf
|
|
20
|
-
} from "../chunk-
|
|
20
|
+
} from "../chunk-4J2KJ3ZW.js";
|
|
21
21
|
import {
|
|
22
22
|
__name
|
|
23
23
|
} from "../chunk-7QVYU63E.js";
|
|
@@ -47,10 +47,10 @@ var TOKEN_MARGIN_MS = 6e4;
|
|
|
47
47
|
var ifoodTokenKey = /* @__PURE__ */ __name((ctx) => ctx.grant ? ctx.grant.key : `IFOOD:${ctx.environment}:${ctx.credentials.clientId}`, "ifoodTokenKey");
|
|
48
48
|
function tokenOf(body) {
|
|
49
49
|
try {
|
|
50
|
-
const
|
|
51
|
-
if (typeof
|
|
52
|
-
const expires = typeof
|
|
53
|
-
return { token:
|
|
50
|
+
const parsed2 = JSON.parse(body);
|
|
51
|
+
if (typeof parsed2.accessToken !== "string" || parsed2.accessToken === "") return null;
|
|
52
|
+
const expires = typeof parsed2.expiresIn === "number" && parsed2.expiresIn > 0 ? parsed2.expiresIn : 3600;
|
|
53
|
+
return { token: parsed2.accessToken, expiresInSeconds: expires };
|
|
54
54
|
} catch {
|
|
55
55
|
return null;
|
|
56
56
|
}
|
|
@@ -158,10 +158,10 @@ function parse(body) {
|
|
|
158
158
|
}
|
|
159
159
|
__name(parse, "parse");
|
|
160
160
|
function categoryIdIn(body, name) {
|
|
161
|
-
const
|
|
162
|
-
if (!Array.isArray(
|
|
161
|
+
const parsed2 = parse(body);
|
|
162
|
+
if (!Array.isArray(parsed2)) return null;
|
|
163
163
|
const wanted = name.trim().toLowerCase();
|
|
164
|
-
const found =
|
|
164
|
+
const found = parsed2.find(
|
|
165
165
|
(entry) => typeof entry === "object" && entry !== null && String(entry.name ?? "").trim().toLowerCase() === wanted
|
|
166
166
|
);
|
|
167
167
|
return typeof found?.id === "string" ? found.id : null;
|
|
@@ -533,11 +533,11 @@ async function read(ctx) {
|
|
|
533
533
|
const listed = await get(ctx, "categories");
|
|
534
534
|
if ("kind" in listed) return listed;
|
|
535
535
|
if (!ok2(listed)) return failureOf2(listed);
|
|
536
|
-
const
|
|
537
|
-
if (!Array.isArray(
|
|
536
|
+
const parsed2 = parse2(listed.body);
|
|
537
|
+
if (!Array.isArray(parsed2)) return unexpected("category list", listed.body);
|
|
538
538
|
const categories = [];
|
|
539
539
|
const notices = [];
|
|
540
|
-
const { entries, others } = entriesOf(
|
|
540
|
+
const { entries, others } = entriesOf(parsed2);
|
|
541
541
|
for (const other of others) notices.push(unreadableCategory(null, other));
|
|
542
542
|
for (const raw of entries) {
|
|
543
543
|
const category = await categoryOf(ctx, raw, notices);
|
|
@@ -558,119 +558,149 @@ var IFOOD_CATALOGUE_READER = {
|
|
|
558
558
|
}, "read")
|
|
559
559
|
};
|
|
560
560
|
|
|
561
|
-
// src/ifood/
|
|
562
|
-
var
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
561
|
+
// src/ifood/courier.ts
|
|
562
|
+
var COURIER_STEP_OF = {
|
|
563
|
+
ADR: "ASSIGNED",
|
|
564
|
+
ASSIGN_DRIVER: "ASSIGNED",
|
|
565
|
+
GTO: "GOING_TO_ORIGIN",
|
|
566
|
+
GOING_TO_ORIGIN: "GOING_TO_ORIGIN",
|
|
567
|
+
AAO: "ARRIVED_AT_ORIGIN",
|
|
568
|
+
ARRIVED_AT_ORIGIN: "ARRIVED_AT_ORIGIN",
|
|
569
|
+
CLT: "COLLECTED",
|
|
570
|
+
COLLECTED: "COLLECTED",
|
|
571
|
+
DELIVERY_RETURNING_TO_ORIGIN: "RETURNING_TO_ORIGIN",
|
|
572
|
+
DELIVERY_RETURNED_TO_ORIGIN: "RETURNED_TO_ORIGIN"
|
|
573
|
+
};
|
|
574
|
+
var VEHICLE_OF = {
|
|
575
|
+
MOTORCYCLE: "MOTORCYCLE",
|
|
576
|
+
MOTORBIKE: "MOTORCYCLE",
|
|
577
|
+
MOTO: "MOTORCYCLE",
|
|
578
|
+
BICYCLE: "BICYCLE",
|
|
579
|
+
BIKE: "BICYCLE",
|
|
580
|
+
CAR: "CAR",
|
|
581
|
+
CARRO: "CAR"
|
|
582
|
+
};
|
|
583
|
+
var IFOOD_COURIER_NAME_MAX = 120;
|
|
584
|
+
var IFOOD_COURIER_PHOTO_URL_MAX = 2048;
|
|
585
|
+
function httpsUrl(value) {
|
|
586
|
+
if (!value || value.length > IFOOD_COURIER_PHOTO_URL_MAX) return null;
|
|
571
587
|
try {
|
|
572
|
-
return
|
|
588
|
+
return new URL(value).protocol === "https:" ? value : null;
|
|
573
589
|
} catch {
|
|
574
590
|
return null;
|
|
575
591
|
}
|
|
576
592
|
}
|
|
577
|
-
__name(
|
|
578
|
-
|
|
593
|
+
__name(httpsUrl, "httpsUrl");
|
|
594
|
+
function metadataOf(payload) {
|
|
595
|
+
const raw = pick(payload, "metadata");
|
|
596
|
+
if (typeof raw !== "string") return raw;
|
|
579
597
|
try {
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
const value = read2(json(response.body));
|
|
584
|
-
return value === null ? { kind: "refused", status: response.status, message: "iFood's answer is not the documented shape" } : { kind: "ok", value };
|
|
585
|
-
} catch (error) {
|
|
586
|
-
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
598
|
+
return JSON.parse(raw);
|
|
599
|
+
} catch {
|
|
600
|
+
return null;
|
|
587
601
|
}
|
|
588
602
|
}
|
|
589
|
-
__name(
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
const
|
|
593
|
-
const
|
|
594
|
-
const
|
|
595
|
-
|
|
603
|
+
__name(metadataOf, "metadataOf");
|
|
604
|
+
function readCourier(payload) {
|
|
605
|
+
const metadata = metadataOf(payload);
|
|
606
|
+
const name = firstString(metadata, [["workerName"]])?.slice(0, IFOOD_COURIER_NAME_MAX) ?? null;
|
|
607
|
+
const photoUrl = httpsUrl(firstString(metadata, [["workerPhotoUrl"]]));
|
|
608
|
+
const stated = firstString(metadata, [["workerVehicleType"]])?.toUpperCase() ?? null;
|
|
609
|
+
const vehicle = stated === null ? null : VEHICLE_OF[stated] ?? "OTHER";
|
|
610
|
+
return name === null && photoUrl === null && vehicle === null ? null : { name, photoUrl, vehicle };
|
|
611
|
+
}
|
|
612
|
+
__name(readCourier, "readCourier");
|
|
613
|
+
function readIfoodCourierEvent(payload, eventType) {
|
|
614
|
+
const step = COURIER_STEP_OF[eventType.toUpperCase()];
|
|
615
|
+
if (!step) return null;
|
|
596
616
|
return {
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
expiresInSeconds: positive(pick(body, "expiresIn"), USER_CODE_SECONDS)
|
|
617
|
+
step,
|
|
618
|
+
at: firstDate(payload, [["createdAt"]]),
|
|
619
|
+
courier: step === "ASSIGNED" ? readCourier(payload) : null,
|
|
620
|
+
returning: step === "RETURNING_TO_ORIGIN" ? "RETURNING" : step === "RETURNED_TO_ORIGIN" ? "RETURNED" : null
|
|
602
621
|
};
|
|
603
622
|
}
|
|
604
|
-
__name(
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
const
|
|
608
|
-
if (!
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
623
|
+
__name(readIfoodCourierEvent, "readIfoodCourierEvent");
|
|
624
|
+
var orderUrl2 = /* @__PURE__ */ __name((ref, action) => `${IFOOD_API}/order/v1.0/orders/${encodeURIComponent(ref.externalOrderId)}/${action}`, "orderUrl");
|
|
625
|
+
async function call2(ctx, request) {
|
|
626
|
+
const auth = await bearer(ctx);
|
|
627
|
+
if (!("token" in auth)) return auth.kind === "accepted" ? { kind: "retry", reason: "no token" } : auth;
|
|
628
|
+
const response = await ctx.http({
|
|
629
|
+
method: request.method,
|
|
630
|
+
url: request.url,
|
|
631
|
+
headers: {
|
|
632
|
+
Authorization: `Bearer ${auth.token}`,
|
|
633
|
+
...request.body === void 0 ? {} : { "Content-Type": "application/json" }
|
|
634
|
+
},
|
|
635
|
+
...request.body === void 0 ? {} : { body: JSON.stringify(request.body) }
|
|
617
636
|
});
|
|
637
|
+
if (response.status !== 401) return response;
|
|
638
|
+
ctx.tokens.delete(ifoodTokenKey(ctx));
|
|
639
|
+
return { kind: "retry", reason: "iFood refused the token" };
|
|
618
640
|
}
|
|
619
|
-
__name(
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
}), "tokenRequest");
|
|
626
|
-
async function listMerchants(ctx, accessToken) {
|
|
627
|
-
const all = [];
|
|
628
|
-
for (let page = 1; page <= MAX_MERCHANT_PAGES; page += 1) {
|
|
629
|
-
const answer = await call2(
|
|
630
|
-
ctx,
|
|
631
|
-
{
|
|
632
|
-
method: "GET",
|
|
633
|
-
url: `${MERCHANTS_URL}?page=${page}&size=${MERCHANT_PAGE_SIZE}`,
|
|
634
|
-
headers: { Authorization: `Bearer ${accessToken}` }
|
|
635
|
-
},
|
|
636
|
-
merchantsOf
|
|
637
|
-
);
|
|
638
|
-
if (answer.kind !== "ok") return answer;
|
|
639
|
-
all.push(...answer.value);
|
|
640
|
-
if (answer.value.length < MERCHANT_PAGE_SIZE) break;
|
|
641
|
+
__name(call2, "call");
|
|
642
|
+
function parsed(body) {
|
|
643
|
+
try {
|
|
644
|
+
return JSON.parse(body);
|
|
645
|
+
} catch {
|
|
646
|
+
return null;
|
|
641
647
|
}
|
|
642
|
-
return { kind: "ok", value: all };
|
|
643
648
|
}
|
|
644
|
-
__name(
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
ctx,
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
649
|
+
__name(parsed, "parsed");
|
|
650
|
+
async function validatePickupCode(ctx, order, input) {
|
|
651
|
+
try {
|
|
652
|
+
const response = await call2(ctx, { method: "POST", url: orderUrl2(order, "validatePickupCode"), body: { code: input.code } });
|
|
653
|
+
if ("kind" in response) return response;
|
|
654
|
+
const read2 = outboundResultOf(response);
|
|
655
|
+
if (read2.kind !== "accepted") return read2;
|
|
656
|
+
const valid = pick(parsed(response.body), "valid");
|
|
657
|
+
return typeof valid === "boolean" ? { kind: "answered", valid } : { kind: "refused", status: response.status, message: "iFood answered without a verdict", own: true };
|
|
658
|
+
} catch (error) {
|
|
659
|
+
return retryOf(error);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
__name(validatePickupCode, "validatePickupCode");
|
|
663
|
+
function retryOf(error) {
|
|
664
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
665
|
+
}
|
|
666
|
+
__name(retryOf, "retryOf");
|
|
667
|
+
function numberAt(source, key) {
|
|
668
|
+
const value = pick(source, key);
|
|
669
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
670
|
+
}
|
|
671
|
+
__name(numberAt, "numberAt");
|
|
672
|
+
function plusSeconds(base, seconds) {
|
|
673
|
+
return seconds === null ? null : new Date(base.getTime() + seconds * 1e3);
|
|
674
|
+
}
|
|
675
|
+
__name(plusSeconds, "plusSeconds");
|
|
676
|
+
function readTracking(body, now) {
|
|
677
|
+
const trackedAt = firstDate(body, [["trackDate"]]);
|
|
678
|
+
const base = trackedAt ?? now;
|
|
679
|
+
const pickupSeconds = numberAt(body, "pickupEtaStart") ?? numberAt(body, "pickupEta");
|
|
680
|
+
const deliverySeconds = numberAt(body, "deliveryEtaEnd") ?? numberAt(body, "deliveryEta");
|
|
681
|
+
return {
|
|
682
|
+
pickupEtaAt: plusSeconds(base, pickupSeconds),
|
|
683
|
+
deliveryEtaAt: firstDate(body, [["expectedDelivery"]]) ?? plusSeconds(base, deliverySeconds),
|
|
684
|
+
latitude: numberAt(body, "latitude"),
|
|
685
|
+
longitude: numberAt(body, "longitude"),
|
|
686
|
+
trackedAt
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
__name(readTracking, "readTracking");
|
|
690
|
+
async function tracking(ctx, order) {
|
|
691
|
+
try {
|
|
692
|
+
const response = await call2(ctx, { method: "GET", url: orderUrl2(order, "tracking") });
|
|
693
|
+
if ("kind" in response) return response;
|
|
694
|
+
if (response.status === 404) return { kind: "notYet" };
|
|
695
|
+
const read2 = outboundResultOf(response);
|
|
696
|
+
if (read2.kind !== "accepted") return read2;
|
|
697
|
+
return { kind: "tracking", tracking: readTracking(parsed(response.body), ctx.now) };
|
|
698
|
+
} catch (error) {
|
|
699
|
+
return retryOf(error);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
__name(tracking, "tracking");
|
|
703
|
+
var IFOOD_COURIER = { validatePickupCode, tracking };
|
|
674
704
|
|
|
675
705
|
// src/ifood/normalize.ts
|
|
676
706
|
var ORDER_PLACED = /* @__PURE__ */ new Set(["PLC", "PLACED", "ORDER_PLACED"]);
|
|
@@ -680,7 +710,10 @@ var LIFECYCLE_OF = {
|
|
|
680
710
|
DSP: "DISPATCHED",
|
|
681
711
|
DISPATCHED: "DISPATCHED",
|
|
682
712
|
CON: "CONCLUDED",
|
|
683
|
-
CONCLUDED: "CONCLUDED"
|
|
713
|
+
CONCLUDED: "CONCLUDED",
|
|
714
|
+
// iFood's courier has the bag (FUT-5665): for a fleet bag, the same hand-over as DSP.
|
|
715
|
+
CLT: "COLLECTED",
|
|
716
|
+
COLLECTED: "COLLECTED"
|
|
684
717
|
};
|
|
685
718
|
function verifyIfoodSignature(rawBody, signature, webhookSecret) {
|
|
686
719
|
return verifyHmacSha256Hex(rawBody, signature, webhookSecret);
|
|
@@ -768,7 +801,7 @@ function optional(key, value) {
|
|
|
768
801
|
return value === void 0 ? {} : { [key]: value };
|
|
769
802
|
}
|
|
770
803
|
__name(optional, "optional");
|
|
771
|
-
function
|
|
804
|
+
function readIfoodOrder(payload, externalOrderId, missingStreetNumber) {
|
|
772
805
|
const order = pick(payload, "order") ?? payload;
|
|
773
806
|
const rawItems = pick(order, "items");
|
|
774
807
|
const lines = (Array.isArray(rawItems) ? rawItems : []).map(readLine).filter((line) => line !== null);
|
|
@@ -796,10 +829,18 @@ function readOrder(payload, externalOrderId, missingStreetNumber) {
|
|
|
796
829
|
// never 0; `discounts` is absent when `benefits` is.
|
|
797
830
|
deliveryFeeCents: firstCents(order, [["total", "deliveryFee"]]),
|
|
798
831
|
subtotalCents: firstCents(order, [["total", "subTotal"]]),
|
|
799
|
-
...optional("discounts", readDiscounts(order))
|
|
832
|
+
...optional("discounts", readDiscounts(order)),
|
|
833
|
+
// The code iFood's courier states at the counter (FUT-5665; confirmed field). Null when iFood asks none.
|
|
834
|
+
pickupCode: firstString(order, [["delivery", "pickupCode"]])
|
|
800
835
|
};
|
|
801
836
|
}
|
|
802
|
-
__name(
|
|
837
|
+
__name(readIfoodOrder, "readIfoodOrder");
|
|
838
|
+
function placedOrder(payload, placed, externalOrderId, missingStreetNumber) {
|
|
839
|
+
if (!placed) return { order: null };
|
|
840
|
+
const order = readIfoodOrder(payload, externalOrderId, missingStreetNumber);
|
|
841
|
+
return order ? { order } : { order: null, orderToFetch: true };
|
|
842
|
+
}
|
|
843
|
+
__name(placedOrder, "placedOrder");
|
|
803
844
|
function normalizeIfoodEvent(payload, options) {
|
|
804
845
|
const externalOrderId = firstString(payload, [["orderId"], ["order", "id"], ["id"]]);
|
|
805
846
|
if (!externalOrderId) return null;
|
|
@@ -817,17 +858,192 @@ function normalizeIfoodEvent(payload, options) {
|
|
|
817
858
|
eventType,
|
|
818
859
|
externalMerchantId: firstString(payload, [["merchantId"], ["merchant", "id"]]),
|
|
819
860
|
externalOrderId,
|
|
820
|
-
|
|
861
|
+
...placedOrder(payload, placed, externalOrderId, options.missingStreetNumber),
|
|
821
862
|
cancelled,
|
|
822
863
|
cancellationOutcome: cancelled ? "CONFIRMED" : refusedRequest ? "FAILED" : null,
|
|
823
|
-
...LIFECYCLE_OF[eventType.toUpperCase()] ? { lifecycle: LIFECYCLE_OF[eventType.toUpperCase()] } : {}
|
|
864
|
+
...LIFECYCLE_OF[eventType.toUpperCase()] ? { lifecycle: LIFECYCLE_OF[eventType.toUpperCase()] } : {},
|
|
865
|
+
...optional("courier", readIfoodCourierEvent(payload, eventType) ?? void 0)
|
|
824
866
|
};
|
|
825
867
|
}
|
|
826
868
|
__name(normalizeIfoodEvent, "normalizeIfoodEvent");
|
|
827
869
|
|
|
870
|
+
// src/ifood/fetch-order.ts
|
|
871
|
+
var orderResourceUrl = /* @__PURE__ */ __name((externalOrderId) => `${IFOOD_API}/order/v1.0/orders/${encodeURIComponent(externalOrderId)}`, "orderResourceUrl");
|
|
872
|
+
async function getOrder(ctx, externalOrderId) {
|
|
873
|
+
const auth = await bearer(ctx);
|
|
874
|
+
if (!("token" in auth)) {
|
|
875
|
+
return auth.kind === "accepted" ? { kind: "retry", reason: "iFood answered the token grant without a token" } : auth;
|
|
876
|
+
}
|
|
877
|
+
const response = await ctx.http({
|
|
878
|
+
method: "GET",
|
|
879
|
+
url: orderResourceUrl(externalOrderId),
|
|
880
|
+
headers: { Authorization: `Bearer ${auth.token}`, Accept: "application/json" }
|
|
881
|
+
});
|
|
882
|
+
return { response };
|
|
883
|
+
}
|
|
884
|
+
__name(getOrder, "getOrder");
|
|
885
|
+
function orderOf(body, externalOrderId, missingStreetNumber) {
|
|
886
|
+
let parsed2;
|
|
887
|
+
try {
|
|
888
|
+
parsed2 = JSON.parse(body);
|
|
889
|
+
} catch {
|
|
890
|
+
return { kind: "retry", reason: "iFood answered the order read with a body that is not JSON" };
|
|
891
|
+
}
|
|
892
|
+
const named = firstString(parsed2, [["id"]]);
|
|
893
|
+
if (named !== null && named !== externalOrderId) {
|
|
894
|
+
return { kind: "refused", status: 200, message: `iFood answered order ${named} for ${externalOrderId}` };
|
|
895
|
+
}
|
|
896
|
+
const order = readIfoodOrder(parsed2, externalOrderId, missingStreetNumber);
|
|
897
|
+
if (!order) return { kind: "refused", status: 200, message: "iFood answered an order with no items" };
|
|
898
|
+
return { kind: "order", order: { ...order, externalMerchantId: firstString(parsed2, [["merchant", "id"]]) } };
|
|
899
|
+
}
|
|
900
|
+
__name(orderOf, "orderOf");
|
|
901
|
+
function failureOf3(response) {
|
|
902
|
+
const message = response.body.slice(0, 300);
|
|
903
|
+
if (response.status === 401 || response.status === 403) {
|
|
904
|
+
return { kind: "refused", status: response.status, message, own: true };
|
|
905
|
+
}
|
|
906
|
+
if (response.status === 404) return { kind: "retry", reason: `status 404: ${message}` };
|
|
907
|
+
const result = outboundResultOf(response);
|
|
908
|
+
return result.kind === "accepted" ? { kind: "retry", reason: `status ${response.status}` } : result;
|
|
909
|
+
}
|
|
910
|
+
__name(failureOf3, "failureOf");
|
|
911
|
+
async function ifoodFetchOrder(ctx, ref, options) {
|
|
912
|
+
try {
|
|
913
|
+
let answer = await getOrder(ctx, ref.externalOrderId);
|
|
914
|
+
if ("response" in answer && answer.response.status === 401) {
|
|
915
|
+
ctx.tokens.delete(ifoodTokenKey(ctx));
|
|
916
|
+
answer = await getOrder(ctx, ref.externalOrderId);
|
|
917
|
+
}
|
|
918
|
+
if (!("response" in answer)) return answer;
|
|
919
|
+
const { response } = answer;
|
|
920
|
+
if (response.status >= 200 && response.status < 300) {
|
|
921
|
+
return orderOf(response.body, ref.externalOrderId, options.missingStreetNumber);
|
|
922
|
+
}
|
|
923
|
+
return failureOf3(response);
|
|
924
|
+
} catch (error) {
|
|
925
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
__name(ifoodFetchOrder, "ifoodFetchOrder");
|
|
929
|
+
|
|
930
|
+
// src/ifood/linking.ts
|
|
931
|
+
var USER_CODE_URL = `${IFOOD_API}/authentication/v1.0/oauth/userCode`;
|
|
932
|
+
var TOKEN_URL2 = `${IFOOD_API}/authentication/v1.0/oauth/token`;
|
|
933
|
+
var MERCHANTS_URL = `${IFOOD_API}/merchant/v1.0/merchants`;
|
|
934
|
+
var USER_CODE_SECONDS = 600;
|
|
935
|
+
var ACCESS_TOKEN_SECONDS = 21600;
|
|
936
|
+
var MERCHANT_PAGE_SIZE = 100;
|
|
937
|
+
var MAX_MERCHANT_PAGES = 20;
|
|
938
|
+
var FORM = { "Content-Type": "application/x-www-form-urlencoded" };
|
|
939
|
+
function json(body) {
|
|
940
|
+
try {
|
|
941
|
+
return body === "" ? null : JSON.parse(body);
|
|
942
|
+
} catch {
|
|
943
|
+
return null;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
__name(json, "json");
|
|
947
|
+
async function call3(ctx, request, read2) {
|
|
948
|
+
try {
|
|
949
|
+
const response = await ctx.http(request);
|
|
950
|
+
const result = outboundResultOf(response);
|
|
951
|
+
if (result.kind !== "accepted") return result;
|
|
952
|
+
const value = read2(json(response.body));
|
|
953
|
+
return value === null ? { kind: "refused", status: response.status, message: "iFood's answer is not the documented shape" } : { kind: "ok", value };
|
|
954
|
+
} catch (error) {
|
|
955
|
+
return { kind: "retry", reason: error instanceof Error ? error.message : String(error) };
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
__name(call3, "call");
|
|
959
|
+
var positive = /* @__PURE__ */ __name((value, fallback) => typeof value === "number" && Number.isFinite(value) && value > 0 ? value : fallback, "positive");
|
|
960
|
+
function codeOf(body) {
|
|
961
|
+
const userCode = asString(pick(body, "userCode"));
|
|
962
|
+
const verifier = asString(pick(body, "authorizationCodeVerifier"));
|
|
963
|
+
const verificationUrl = asString(pick(body, "verificationUrl"));
|
|
964
|
+
if (!userCode || !verifier || !verificationUrl) return null;
|
|
965
|
+
return {
|
|
966
|
+
userCode,
|
|
967
|
+
verifier,
|
|
968
|
+
verificationUrl,
|
|
969
|
+
verificationUrlComplete: asString(pick(body, "verificationUrlComplete")) ?? verificationUrl,
|
|
970
|
+
expiresInSeconds: positive(pick(body, "expiresIn"), USER_CODE_SECONDS)
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
__name(codeOf, "codeOf");
|
|
974
|
+
function grantOf(body, requireRefresh) {
|
|
975
|
+
const accessToken = asString(pick(body, "accessToken"));
|
|
976
|
+
const refreshToken = asString(pick(body, "refreshToken")) ?? asString(pick(body, "refresh_token"));
|
|
977
|
+
if (!accessToken || requireRefresh && !refreshToken) return null;
|
|
978
|
+
return { accessToken, refreshToken, expiresInSeconds: positive(pick(body, "expiresIn"), ACCESS_TOKEN_SECONDS) };
|
|
979
|
+
}
|
|
980
|
+
__name(grantOf, "grantOf");
|
|
981
|
+
function merchantsOf(body) {
|
|
982
|
+
if (!Array.isArray(body)) return null;
|
|
983
|
+
return body.flatMap((raw) => {
|
|
984
|
+
const id = asString(pick(raw, "id"));
|
|
985
|
+
return id ? [{ id, name: asString(pick(raw, "name")), corporateName: asString(pick(raw, "corporateName")) }] : [];
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
__name(merchantsOf, "merchantsOf");
|
|
989
|
+
var tokenRequest = /* @__PURE__ */ __name((fields) => ({
|
|
990
|
+
method: "POST",
|
|
991
|
+
url: TOKEN_URL2,
|
|
992
|
+
headers: FORM,
|
|
993
|
+
body: new URLSearchParams(fields).toString()
|
|
994
|
+
}), "tokenRequest");
|
|
995
|
+
async function listMerchants(ctx, accessToken) {
|
|
996
|
+
const all = [];
|
|
997
|
+
for (let page = 1; page <= MAX_MERCHANT_PAGES; page += 1) {
|
|
998
|
+
const answer = await call3(
|
|
999
|
+
ctx,
|
|
1000
|
+
{
|
|
1001
|
+
method: "GET",
|
|
1002
|
+
url: `${MERCHANTS_URL}?page=${page}&size=${MERCHANT_PAGE_SIZE}`,
|
|
1003
|
+
headers: { Authorization: `Bearer ${accessToken}` }
|
|
1004
|
+
},
|
|
1005
|
+
merchantsOf
|
|
1006
|
+
);
|
|
1007
|
+
if (answer.kind !== "ok") return answer;
|
|
1008
|
+
all.push(...answer.value);
|
|
1009
|
+
if (answer.value.length < MERCHANT_PAGE_SIZE) break;
|
|
1010
|
+
}
|
|
1011
|
+
return { kind: "ok", value: all };
|
|
1012
|
+
}
|
|
1013
|
+
__name(listMerchants, "listMerchants");
|
|
1014
|
+
var IFOOD_LINKING = {
|
|
1015
|
+
requestCode: /* @__PURE__ */ __name((ctx) => call3(
|
|
1016
|
+
ctx,
|
|
1017
|
+
{ method: "POST", url: USER_CODE_URL, headers: FORM, body: new URLSearchParams({ clientId: ctx.credentials.clientId }).toString() },
|
|
1018
|
+
codeOf
|
|
1019
|
+
), "requestCode"),
|
|
1020
|
+
exchange: /* @__PURE__ */ __name((ctx, input) => call3(
|
|
1021
|
+
ctx,
|
|
1022
|
+
tokenRequest({
|
|
1023
|
+
grantType: "authorization_code",
|
|
1024
|
+
clientId: ctx.credentials.clientId,
|
|
1025
|
+
clientSecret: ctx.credentials.clientSecret,
|
|
1026
|
+
authorizationCode: input.authorizationCode,
|
|
1027
|
+
authorizationCodeVerifier: input.verifier
|
|
1028
|
+
}),
|
|
1029
|
+
(body) => grantOf(body, true)
|
|
1030
|
+
), "exchange"),
|
|
1031
|
+
refresh: /* @__PURE__ */ __name((ctx, input) => call3(
|
|
1032
|
+
ctx,
|
|
1033
|
+
tokenRequest({
|
|
1034
|
+
grantType: "refresh_token",
|
|
1035
|
+
clientId: ctx.credentials.clientId,
|
|
1036
|
+
clientSecret: ctx.credentials.clientSecret,
|
|
1037
|
+
refreshToken: input.refreshToken
|
|
1038
|
+
}),
|
|
1039
|
+
(body) => grantOf(body, false)
|
|
1040
|
+
), "refresh"),
|
|
1041
|
+
merchants: /* @__PURE__ */ __name((ctx, input) => listMerchants(ctx, input.accessToken), "merchants")
|
|
1042
|
+
};
|
|
1043
|
+
|
|
828
1044
|
// src/ifood/pause.ts
|
|
829
1045
|
var interruptionsUrl = /* @__PURE__ */ __name((merchantId) => `${IFOOD_API}/merchant/v1.0/merchants/${encodeURIComponent(merchantId)}/interruptions`, "interruptionsUrl");
|
|
830
|
-
async function
|
|
1046
|
+
async function call4(ctx, request) {
|
|
831
1047
|
const auth = await bearer(ctx);
|
|
832
1048
|
if (!("token" in auth)) return auth.kind === "accepted" ? { kind: "retry", reason: "unexpected token answer" } : auth;
|
|
833
1049
|
const response = await ctx.http({
|
|
@@ -843,7 +1059,7 @@ async function call3(ctx, request) {
|
|
|
843
1059
|
ctx.tokens.delete(ifoodTokenKey(ctx));
|
|
844
1060
|
return { kind: "retry", reason: "iFood refused the token" };
|
|
845
1061
|
}
|
|
846
|
-
__name(
|
|
1062
|
+
__name(call4, "call");
|
|
847
1063
|
var settled = /* @__PURE__ */ __name((value) => "kind" in value, "settled");
|
|
848
1064
|
function notOk(response) {
|
|
849
1065
|
const result = outboundResultOf(response);
|
|
@@ -851,14 +1067,14 @@ function notOk(response) {
|
|
|
851
1067
|
}
|
|
852
1068
|
__name(notOk, "notOk");
|
|
853
1069
|
async function listInterruptions(ctx, merchantId) {
|
|
854
|
-
const response = await
|
|
1070
|
+
const response = await call4(ctx, { method: "GET", url: interruptionsUrl(merchantId) });
|
|
855
1071
|
if (settled(response)) return response;
|
|
856
1072
|
const failure = notOk(response);
|
|
857
1073
|
if (failure) return failure;
|
|
858
1074
|
try {
|
|
859
|
-
const
|
|
860
|
-
if (!Array.isArray(
|
|
861
|
-
return
|
|
1075
|
+
const parsed2 = JSON.parse(response.body === "" ? "[]" : response.body);
|
|
1076
|
+
if (!Array.isArray(parsed2)) return [];
|
|
1077
|
+
return parsed2.flatMap((entry) => {
|
|
862
1078
|
const value = entry ?? {};
|
|
863
1079
|
if (typeof value.id !== "string") return [];
|
|
864
1080
|
return [
|
|
@@ -878,8 +1094,8 @@ function sameEnd(end, until, timeZone) {
|
|
|
878
1094
|
const sent = ifoodLocalTime(until, timeZone);
|
|
879
1095
|
if (end === sent) return true;
|
|
880
1096
|
if (/(?:[zZ]|[+-]\d{2}:?\d{2})$/u.test(end)) {
|
|
881
|
-
const
|
|
882
|
-
return !Number.isNaN(
|
|
1097
|
+
const parsed2 = Date.parse(end);
|
|
1098
|
+
return !Number.isNaN(parsed2) && Math.floor(parsed2 / 1e3) === Math.floor(until.getTime() / 1e3);
|
|
883
1099
|
}
|
|
884
1100
|
return end.slice(0, 19) === sent.slice(0, 19);
|
|
885
1101
|
}
|
|
@@ -935,7 +1151,7 @@ function ifoodPause(ctx, request) {
|
|
|
935
1151
|
if (!Array.isArray(list2)) return list2;
|
|
936
1152
|
const landed = ours(list2, request.key, { until: request.until, reason: request.reason, timeZone: request.timeZone }, request.keep);
|
|
937
1153
|
if (landed) return { kind: "paused", externalId: landed.id, until: request.until };
|
|
938
|
-
const response = await
|
|
1154
|
+
const response = await call4(ctx, {
|
|
939
1155
|
method: "POST",
|
|
940
1156
|
url: interruptionsUrl(request.merchantId),
|
|
941
1157
|
json: {
|
|
@@ -961,7 +1177,7 @@ function ifoodResume(ctx, ref) {
|
|
|
961
1177
|
id = ours(list2, ref.key, ref.sent, ref.keep)?.id ?? null;
|
|
962
1178
|
}
|
|
963
1179
|
if (id === null) return { kind: "accepted" };
|
|
964
|
-
const response = await
|
|
1180
|
+
const response = await call4(ctx, { method: "DELETE", url: `${interruptionsUrl(ref.merchantId)}/${encodeURIComponent(id)}` });
|
|
965
1181
|
if (settled(response)) return response;
|
|
966
1182
|
if (response.status === 404) return { kind: "accepted" };
|
|
967
1183
|
return outboundResultOf(response);
|
|
@@ -1024,12 +1240,16 @@ function createIfoodAdapter(config) {
|
|
|
1024
1240
|
catalogueReader: IFOOD_CATALOGUE_READER,
|
|
1025
1241
|
confirm: /* @__PURE__ */ __name((ctx, order) => ifoodConfirm(ctx, order), "confirm"),
|
|
1026
1242
|
requestCancellation: ifoodRequestCancellation,
|
|
1243
|
+
// iFood's events carry only the order id: a PLACED with no items is fetched (FUT-5800).
|
|
1244
|
+
fetchOrder: /* @__PURE__ */ __name((ctx, order) => ifoodFetchOrder(ctx, order, normalizeOptions), "fetchOrder"),
|
|
1027
1245
|
statusSupported: ifoodStatusSupported,
|
|
1028
1246
|
informStatus: ifoodInformStatus,
|
|
1029
1247
|
// The Merchant API's interruptions: an id and an end per closure (FUT-4851).
|
|
1030
1248
|
availability: IFOOD_AVAILABILITY,
|
|
1031
1249
|
// The distributed-app authorization: a store links the software's own app (FUT-5613).
|
|
1032
1250
|
linking: IFOOD_LINKING,
|
|
1251
|
+
// iFood's fleet: the pickup-code check and the courier's tracking (FUT-5665).
|
|
1252
|
+
courier: IFOOD_COURIER,
|
|
1033
1253
|
// No per-order deep link is known, so the portal, where a live order is
|
|
1034
1254
|
// on the first screen.
|
|
1035
1255
|
appLink: /* @__PURE__ */ __name(() => IFOOD_PORTAL_URL, "appLink")
|
|
@@ -1041,11 +1261,13 @@ export {
|
|
|
1041
1261
|
IFOOD_CANCELLATION_ANSWER_WINDOW_MS,
|
|
1042
1262
|
IFOOD_CATALOGUE,
|
|
1043
1263
|
IFOOD_CATALOGUE_READER,
|
|
1264
|
+
IFOOD_COURIER,
|
|
1044
1265
|
IFOOD_IMAGE_BASE_URL,
|
|
1045
1266
|
IFOOD_LINKING,
|
|
1046
1267
|
IFOOD_PROVIDER,
|
|
1047
1268
|
IFOOD_REASON_CODES,
|
|
1048
1269
|
createIfoodAdapter,
|
|
1270
|
+
ifoodFetchOrder,
|
|
1049
1271
|
ifoodItemBody,
|
|
1050
1272
|
ifoodItemId,
|
|
1051
1273
|
ifoodLocalTime,
|
|
@@ -1053,6 +1275,8 @@ export {
|
|
|
1053
1275
|
ifoodProductId,
|
|
1054
1276
|
ifoodResume,
|
|
1055
1277
|
normalizeIfoodEvent,
|
|
1278
|
+
readIfoodCourierEvent,
|
|
1279
|
+
readIfoodOrder,
|
|
1056
1280
|
verifyIfoodSignature
|
|
1057
1281
|
};
|
|
1058
1282
|
//# sourceMappingURL=index.js.map
|