diff --git a/vtex/actions/cart/addItems.ts b/vtex/actions/cart/addItems.ts index c8b883f7c..8f5f2c86d 100644 --- a/vtex/actions/cart/addItems.ts +++ b/vtex/actions/cart/addItems.ts @@ -16,6 +16,7 @@ export interface Item { export interface Props { orderItems: Item[]; allowedOutdatedData?: Array<"paymentData">; + sc?: string; } /** @@ -29,28 +30,33 @@ const action = async ( ctx: AppContext, ): Promise => { const { vcsDeprecated } = ctx; - const { - orderItems, - allowedOutdatedData = ["paymentData"], - } = props; + const { orderItems, allowedOutdatedData = ["paymentData"], sc } = props; const { orderFormId } = parseCookie(req.headers); const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); try { - const response = await vcsDeprecated - ["POST /api/checkout/pub/orderForm/:orderFormId/items"]({ + const response = await vcsDeprecated[ + "POST /api/checkout/pub/orderForm/:orderFormId/items" + ]( + { orderFormId, allowedOutdatedData, - sc: segment?.payload.channel, - }, { + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel), + }, + { body: { orderItems }, headers: { "content-type": "application/json", accept: "application/json", cookie, }, - }); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/getInstallment.ts b/vtex/actions/cart/getInstallment.ts index 17beb3838..bb9d49fd3 100644 --- a/vtex/actions/cart/getInstallment.ts +++ b/vtex/actions/cart/getInstallment.ts @@ -6,6 +6,7 @@ import { getSegmentFromBag } from "../../utils/segment.ts"; export interface Props { paymentSystem: number; + sc?: string; } /** @@ -19,16 +20,25 @@ const action = async ( ctx: AppContext, ): Promise => { const { vcsDeprecated } = ctx; - const { paymentSystem } = props; + const { paymentSystem, sc } = props; const { orderFormId } = parseCookie(req.headers); const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["GET /api/checkout/pub/orderForm/:orderFormId/installments"]( - { orderFormId, paymentSystem, sc: segment?.payload.channel }, - { headers: { accept: "application/json", cookie } }, - ); + const response = await vcsDeprecated[ + "GET /api/checkout/pub/orderForm/:orderFormId/installments" + ]( + { + orderFormId, + paymentSystem, + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel) + }, + { headers: { accept: "application/json", cookie } }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/removeItemAttachment.ts b/vtex/actions/cart/removeItemAttachment.ts index 16be3a213..726107c69 100644 --- a/vtex/actions/cart/removeItemAttachment.ts +++ b/vtex/actions/cart/removeItemAttachment.ts @@ -12,6 +12,7 @@ export interface Props { content: Record; expectedOrderFormSections?: string[]; noSplitItem?: boolean; + sc?: string; } export const DEFAULT_EXPECTED_SECTIONS = [ @@ -48,23 +49,34 @@ const action = async ( content, noSplitItem = true, expectedOrderFormSections = DEFAULT_EXPECTED_SECTIONS, + sc, } = props; const { orderFormId } = parseCookie(req.headers); const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["DELETE /api/checkout/pub/orderForm/:orderFormId/items/:index/attachments/:attachment"]( - { orderFormId, attachment, index, sc: segment?.payload.channel }, - { - body: { content, noSplitItem, expectedOrderFormSections }, - headers: { - accept: "application/json", - "content-type": "application/json", - cookie, - }, + const response = await vcsDeprecated[ + "DELETE /api/checkout/pub/orderForm/:orderFormId/items/:index/attachments/:attachment" + ]( + { + orderFormId, + attachment, + index, + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel) + }, + { + body: { content, noSplitItem, expectedOrderFormSections }, + headers: { + accept: "application/json", + "content-type": "application/json", + cookie, }, - ); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/removeItems.ts b/vtex/actions/cart/removeItems.ts index 5ad34095c..ebaea6220 100644 --- a/vtex/actions/cart/removeItems.ts +++ b/vtex/actions/cart/removeItems.ts @@ -4,32 +4,45 @@ import { parseCookie } from "../../utils/orderForm.ts"; import type { OrderForm } from "../../utils/types.ts"; import { getSegmentFromBag } from "../../utils/segment.ts"; +export interface Props { + sc?: string; +} + /** * @docs https://developers.vtex.com/docs/api-reference/checkout-api#post-/api/checkout/pub/orderForm/-orderFormId-/items/removeAll * @title Remove Items from Cart * @description Remove all items from the cart */ const action = async ( - _props: unknown, + props: Props, req: Request, ctx: AppContext, ): Promise => { const { vcsDeprecated } = ctx; + const { sc } = props; const { orderFormId } = parseCookie(req.headers); const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["POST /api/checkout/pub/orderForm/:orderFormId/items/removeAll"]( - { orderFormId, sc: segment?.payload.channel }, - { - headers: { - "content-type": "application/json", - accept: "application/json", - cookie, - }, + const response = await vcsDeprecated[ + "POST /api/checkout/pub/orderForm/:orderFormId/items/removeAll" + ]( + { + orderFormId, + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel) + }, + { + headers: { + "content-type": "application/json", + accept: "application/json", + cookie, }, - ); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/simulation.ts b/vtex/actions/cart/simulation.ts index 4bb75b72c..d9f158de0 100644 --- a/vtex/actions/cart/simulation.ts +++ b/vtex/actions/cart/simulation.ts @@ -13,6 +13,7 @@ export interface Props { postalCode: string; country: string; RnbBehavior?: 0 | 1; + sc?: string; } /** @@ -27,7 +28,7 @@ const action = async ( ): Promise => { const cookie = req.headers.get("cookie") ?? ""; const { vcsDeprecated } = ctx; - const { items, postalCode, country, RnbBehavior = 1 } = props; + const { items, postalCode, country, RnbBehavior = 1, sc } = props; const segment = getSegmentFromBag(ctx); const response = await vcsDeprecated[ @@ -35,7 +36,11 @@ const action = async ( ]( { RnbBehavior, - sc: segment?.payload.channel, + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel), }, { body: { items, country, postalCode }, diff --git a/vtex/actions/cart/updateAttachment.ts b/vtex/actions/cart/updateAttachment.ts index 37cf4c40b..e56d89298 100644 --- a/vtex/actions/cart/updateAttachment.ts +++ b/vtex/actions/cart/updateAttachment.ts @@ -10,6 +10,7 @@ export interface Props { expectedOrderFormSections?: string[]; // deno-lint-ignore no-explicit-any body: any; + sc?: string; } /** @@ -26,6 +27,7 @@ const action = async ( attachment, body, expectedOrderFormSections = DEFAULT_EXPECTED_SECTIONS, + sc, } = props; const { orderFormId } = parseCookie(req.headers); @@ -36,19 +38,27 @@ const action = async ( const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["POST /api/checkout/pub/orderForm/:orderFormId/attachments/:attachment"]({ + const response = await vcsDeprecated[ + "POST /api/checkout/pub/orderForm/:orderFormId/attachments/:attachment" + ]( + { orderFormId, attachment, - sc: segment?.payload.channel, - }, { + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel), + }, + { body: { expectedOrderFormSections, ...body }, headers: { accept: "application/json", "content-type": "application/json", cookie, }, - }); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/updateCoupons.ts b/vtex/actions/cart/updateCoupons.ts index 852c42065..417b04c04 100644 --- a/vtex/actions/cart/updateCoupons.ts +++ b/vtex/actions/cart/updateCoupons.ts @@ -6,6 +6,7 @@ import { getSegmentFromBag } from "../../utils/segment.ts"; export interface Props { text: string; + sc?: string; } /** @@ -19,23 +20,31 @@ const action = async ( ctx: AppContext, ): Promise => { const { vcsDeprecated } = ctx; - const { text } = props; + const { text, sc } = props; const cookie = req.headers.get("cookie") ?? ""; const { orderFormId } = parseCookie(req.headers); const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["POST /api/checkout/pub/orderForm/:orderFormId/coupons"]({ + const response = await vcsDeprecated[ + "POST /api/checkout/pub/orderForm/:orderFormId/coupons" + ]( + { orderFormId, - sc: segment?.payload.channel, - }, { + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel), + }, + { body: { text }, headers: { accept: "application/json", "content-type": "application/json", cookie, }, - }); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/updateItemAttachment.ts b/vtex/actions/cart/updateItemAttachment.ts index 3b20412fe..6ab1c1dc0 100644 --- a/vtex/actions/cart/updateItemAttachment.ts +++ b/vtex/actions/cart/updateItemAttachment.ts @@ -12,6 +12,7 @@ export interface Props { content: Record; expectedOrderFormSections?: string[]; noSplitItem?: boolean; + sc?: string; } export const DEFAULT_EXPECTED_SECTIONS = [ @@ -48,28 +49,34 @@ const action = async ( content, noSplitItem = true, expectedOrderFormSections = DEFAULT_EXPECTED_SECTIONS, + sc, } = props; const { orderFormId } = parseCookie(req.headers); const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["POST /api/checkout/pub/orderForm/:orderFormId/items/:index/attachments/:attachment"]( - { - orderFormId, - attachment, - index, - sc: segment?.payload.channel, + const response = await vcsDeprecated[ + "POST /api/checkout/pub/orderForm/:orderFormId/items/:index/attachments/:attachment" + ]( + { + orderFormId, + attachment, + index, + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel), + }, + { + body: { content, noSplitItem, expectedOrderFormSections }, + headers: { + accept: "application/json", + "content-type": "application/json", + cookie, }, - { - body: { content, noSplitItem, expectedOrderFormSections }, - headers: { - accept: "application/json", - "content-type": "application/json", - cookie, - }, - }, - ); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/updateItemPrice.ts b/vtex/actions/cart/updateItemPrice.ts index 034bcbc84..e16cda9b8 100644 --- a/vtex/actions/cart/updateItemPrice.ts +++ b/vtex/actions/cart/updateItemPrice.ts @@ -7,6 +7,7 @@ import { getSegmentFromBag } from "../../utils/segment.ts"; export interface Props { itemIndex: number; price: number; + sc?: string; } /** @@ -20,27 +21,32 @@ const action = async ( ctx: AppContext, ): Promise => { const { vcsDeprecated } = ctx; - const { - itemIndex, - price, - } = props; + const { itemIndex, price, sc } = props; const { orderFormId } = parseCookie(req.headers); const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["PUT /api/checkout/pub/orderForm/:orderFormId/items/:index/price"]({ + const response = await vcsDeprecated[ + "PUT /api/checkout/pub/orderForm/:orderFormId/items/:index/price" + ]( + { orderFormId, index: itemIndex, - sc: segment?.payload.channel, - }, { + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel), + }, + { body: { price }, headers: { accept: "application/json", "content-type": "application/json", cookie, }, - }); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/updateItems.ts b/vtex/actions/cart/updateItems.ts index bb0ea3e8c..4bcbd25b7 100644 --- a/vtex/actions/cart/updateItems.ts +++ b/vtex/actions/cart/updateItems.ts @@ -13,6 +13,7 @@ export interface Props { orderItems: Item[]; allowedOutdatedData?: Array<"paymentData">; noSplitItem?: boolean; + sc?: string; } /** @@ -30,24 +31,33 @@ const action = async ( orderItems, allowedOutdatedData = ["paymentData"], noSplitItem, + sc, } = props; const { orderFormId } = parseCookie(req.headers); const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["POST /api/checkout/pub/orderForm/:orderFormId/items/update"]({ + const response = await vcsDeprecated[ + "POST /api/checkout/pub/orderForm/:orderFormId/items/update" + ]( + { orderFormId, allowedOutdatedData, - sc: segment?.payload.channel, - }, { + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel), + }, + { body: { orderItems, noSplitItem: Boolean(noSplitItem) }, headers: { "content-type": "application/json", accept: "application/json", cookie, }, - }); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/updateProfile.ts b/vtex/actions/cart/updateProfile.ts index cb621dc91..c3c551119 100644 --- a/vtex/actions/cart/updateProfile.ts +++ b/vtex/actions/cart/updateProfile.ts @@ -6,6 +6,7 @@ import { getSegmentFromBag } from "../../utils/segment.ts"; export interface Props { ignoreProfileData: boolean; + sc?: string; } /** @@ -19,23 +20,31 @@ const action = async ( ctx: AppContext, ): Promise => { const { vcsDeprecated } = ctx; - const { ignoreProfileData } = props; + const { ignoreProfileData, sc } = props; const { orderFormId } = parseCookie(req.headers); const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["PATCH /api/checkout/pub/orderForm/:orderFormId/profile"]({ + const response = await vcsDeprecated[ + "PATCH /api/checkout/pub/orderForm/:orderFormId/profile" + ]( + { orderFormId, - sc: segment?.payload.channel, - }, { + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel), + }, + { body: { ignoreProfileData }, headers: { "content-type": "application/json", accept: "application/json", cookie, }, - }); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/actions/cart/updateUser.ts b/vtex/actions/cart/updateUser.ts index dca9184e8..713c95d64 100644 --- a/vtex/actions/cart/updateUser.ts +++ b/vtex/actions/cart/updateUser.ts @@ -4,31 +4,44 @@ import { parseCookie } from "../../utils/orderForm.ts"; import type { OrderForm } from "../../utils/types.ts"; import { getSegmentFromBag } from "../../utils/segment.ts"; +export interface Props { + sc?: string; +} + /** * @docs https://developers.vtex.com/docs/api-reference/checkout-api#get-/checkout/changeToAnonymousUser/-orderFormId- * @title Update User * @description Update the user */ const action = async ( - _props: unknown, + props: Props, req: Request, ctx: AppContext, ): Promise => { const { vcsDeprecated } = ctx; + const { sc } = props; const { orderFormId } = parseCookie(req.headers); const cookie = req.headers.get("cookie") ?? ""; const segment = getSegmentFromBag(ctx); - const response = await vcsDeprecated - ["GET /api/checkout/changeToAnonymousUser/:orderFormId"]({ + const response = await vcsDeprecated[ + "GET /api/checkout/changeToAnonymousUser/:orderFormId" + ]( + { orderFormId, - sc: segment?.payload.channel, - }, { + sc: + sc ? sc : + (ctx.allowMixedSegments + ? segment?.payload.channel + : ctx.salesChannel), + }, + { headers: { accept: "application/json", cookie, }, - }); + }, + ); proxySetCookie(response.headers, ctx.response.headers, req.url); diff --git a/vtex/mod.ts b/vtex/mod.ts index 3d89168fd..bc8a30522 100644 --- a/vtex/mod.ts +++ b/vtex/mod.ts @@ -105,6 +105,12 @@ export interface Props { */ extraTerms?: string[]; }; + + /** + * @title Allow Mixed Segments + * @description Allow mixed segments in cart operations + */ + allowMixedSegments?: boolean; } export const color = 0xf71963; /** @@ -114,10 +120,14 @@ export const color = 0xf71963; * @category Ecommmerce * @logo https://assets.decocache.com/mcp/0d6e795b-cefd-4853-9a51-93b346c52c3f/VTEX.svg */ -export default function VTEX( - { appKey, appToken, account, publicUrl: _publicUrl, salesChannel, ...props }: - Props, -) { +export default function VTEX({ + appKey, + appToken, + account, + publicUrl: _publicUrl, + salesChannel, + ...props +}: Props) { const publicUrl = _publicUrl?.startsWith("https://") ? _publicUrl : `https://${_publicUrl}`; @@ -125,12 +135,12 @@ export default function VTEX( appKey && headers.set( "X-VTEX-API-AppKey", - typeof appKey === "string" ? appKey : appKey?.get?.() ?? "", + typeof appKey === "string" ? appKey : (appKey?.get?.() ?? ""), ); appToken && headers.set( "X-VTEX-API-AppToken", - typeof appToken === "string" ? appToken : appToken?.get?.() ?? "", + typeof appToken === "string" ? appToken : (appToken?.get?.() ?? ""), ); const sp = createHttpClient({ base: `https://sp.vtex.com`, @@ -181,8 +191,8 @@ export default function VTEX( }); const cachedSearchTerms = [ - ...(props.cachedSearchTerms?.terms?.searches ?? []).map((search) => - search.term + ...(props.cachedSearchTerms?.terms?.searches ?? []).map( + (search) => search.term, ), ...(props.cachedSearchTerms?.extraTerms ?? []), ]; @@ -202,9 +212,7 @@ export default function VTEX( sub, cachedSearchTerms, }; - const app: A, - ]> = { + const app: A]> = { state, manifest, middleware,