From 714d28ac10da428ffe9d877c1997262688125e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Feij=C3=B3?= Date: Wed, 16 Oct 2024 12:22:25 -0300 Subject: [PATCH] chore: Adapt `fetchAPI` calls to consider the `vtexinternal` environment --- .../platforms/vtex/clients/commerce/index.ts | 228 +++++++++++------- .../platforms/vtex/clients/search/index.ts | 51 ++-- 2 files changed, 171 insertions(+), 108 deletions(-) diff --git a/packages/api/src/platforms/vtex/clients/commerce/index.ts b/packages/api/src/platforms/vtex/clients/commerce/index.ts index 50e243da5a..e5e789bd6d 100644 --- a/packages/api/src/platforms/vtex/clients/commerce/index.ts +++ b/packages/api/src/platforms/vtex/clients/commerce/index.ts @@ -34,17 +34,16 @@ export const VtexCommerce = ( { account, environment, incrementAddress, subDomainPrefix }: Options, ctx: Context ) => { - const base = `https://${account}.${environment}.com.br` const storeCookies = getStoreCookie(ctx) const withCookie = getWithCookie(ctx) const host = new Headers(ctx.headers).get('x-forwarded-host') ?? ctx.headers?.host ?? '' - const selectedPrefix = subDomainPrefix ? - subDomainPrefix - .map((prefix) => prefix + '.') - .find((prefix) => host.includes(prefix)) || '' + const selectedPrefix = subDomainPrefix + ? subDomainPrefix + .map((prefix) => prefix + '.') + .find((prefix) => host.includes(prefix)) || '' : '' const forwardedHost = host.replace(selectedPrefix, '') @@ -52,32 +51,50 @@ export const VtexCommerce = ( return { catalog: { salesChannel: (sc: string): Promise => - fetchAPI( - `${base}/api/catalog_system/pub/saleschannel/${sc}`, - undefined, - { storeCookies } - ), + fetchAPI({ + requestPath: `/api/catalog_system/pub/saleschannel/${sc}`, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'catalog', + }, + }), brand: { list: (): Promise => - fetchAPI(`${base}/api/catalog_system/pub/brand/list`, undefined, { - storeCookies, + fetchAPI({ + requestPath: `/api/catalog_system/pub/brand/list`, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'catalog', + }, }), }, category: { tree: (depth = 3): Promise => - fetchAPI( - `${base}/api/catalog_system/pub/category/tree/${depth}`, - undefined, - { storeCookies } - ), + fetchAPI({ + requestPath: `/api/catalog_system/pub/category/tree/${depth}`, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'catalog', + }, + }), }, portal: { pagetype: (slug: string): Promise => - fetchAPI( - `${base}/api/catalog_system/pub/portal/pagetype/${slug}`, - undefined, - { storeCookies } - ), + fetchAPI({ + requestPath: `/api/catalog_system/pub/portal/pagetype/${slug}`, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'catalog', + }, + }), }, products: { crossselling: ({ @@ -94,11 +111,15 @@ export const VtexCommerce = ( groupByProduct: groupByProduct.toString(), }) - return fetchAPI( - `${base}/api/catalog_system/pub/products/crossselling/${type}/${productId}?${params}`, - undefined, - { storeCookies } - ) + return fetchAPI({ + requestPath: `/api/catalog_system/pub/products/crossselling/${type}/${productId}?${params}`, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'catalog', + }, + }) }, }, }, @@ -116,15 +137,20 @@ export const VtexCommerce = ( 'X-FORWARDED-HOST': forwardedHost, }) - return fetchAPI( - `${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`, - { + return fetchAPI({ + requestPath: `/api/checkout/pub/orderForms/simulation?${params.toString()}`, + requestInit: { ...BASE_INIT, headers, body: JSON.stringify(args), }, - { storeCookies } - ) + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'checkout', + }, + }) }, shippingData: ( { @@ -142,9 +168,9 @@ export const VtexCommerce = ( ): Promise => { const deliveryWindow = setDeliveryWindow ? { - startDateUtc: deliveryMode?.deliveryWindow?.startDate, - endDateUtc: deliveryMode?.deliveryWindow?.endDate, - } + startDateUtc: deliveryMode?.deliveryWindow?.startDate, + endDateUtc: deliveryMode?.deliveryWindow?.endDate, + } : null const mappedBody = { @@ -163,15 +189,20 @@ export const VtexCommerce = ( 'X-FORWARDED-HOST': forwardedHost, }) - return fetchAPI( - `${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, - { + return fetchAPI({ + requestPath: `/api/checkout/pub/orderForm/${id}/attachments/shippingData`, + requestInit: { ...BASE_INIT, headers, body: JSON.stringify(mappedBody), }, - { storeCookies } - ) + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'checkout', + }, + }) }, orderForm: ({ id, @@ -193,14 +224,16 @@ export const VtexCommerce = ( 'X-FORWARDED-HOST': forwardedHost, }) - return fetchAPI( - `${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, - { - ...BASE_INIT, - headers, + return fetchAPI({ + requestPath: `/api/checkout/pub/orderForm/${id}?${params.toString()}`, + requestInit: { ...BASE_INIT, headers }, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'checkout', }, - { storeCookies } - ) + }) }, clearOrderFormMessages: ({ id }: { id: string }) => { const headers: HeadersInit = withCookie({ @@ -208,14 +241,11 @@ export const VtexCommerce = ( 'X-FORWARDED-HOST': forwardedHost, }) - return fetchAPI( - `${base}/api/checkout/pub/orderForm/${id}/messages/clear`, - { - ...BASE_INIT, - headers, - body: '{}', - } - ) + return fetchAPI({ + requestPath: `/api/checkout/pub/orderForm/${id}/messages/clear`, + requestInit: { ...BASE_INIT, headers, body: '{}' }, + requestOptions: { account, environment, vtexApi: 'checkout' }, + }) }, updateOrderFormItems: ({ id, @@ -240,9 +270,9 @@ export const VtexCommerce = ( 'X-FORWARDED-HOST': forwardedHost, }) - return fetchAPI( - `${base}/api/checkout/pub/orderForm/${id}/items?${params}`, - { + return fetchAPI({ + requestPath: `/api/checkout/pub/orderForm/${id}/items?${params}`, + requestInit: { headers, body: JSON.stringify({ orderItems, @@ -250,8 +280,13 @@ export const VtexCommerce = ( }), method: 'PATCH', }, - { storeCookies } - ) + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'checkout', + }, + }) }, setCustomData: ({ id, @@ -269,14 +304,15 @@ export const VtexCommerce = ( 'X-FORWARDED-HOST': forwardedHost, }) - return fetchAPI( - `${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`, - { + return fetchAPI({ + requestPath: `/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`, + requestInit: { headers, body: JSON.stringify({ value }), method: 'PUT', - } - ) + }, + requestOptions: { account, environment, vtexApi: 'checkout' }, + }) }, region: async ({ postalCode, @@ -292,23 +328,25 @@ export const VtexCommerce = ( postalCode ? params.append('postalCode', postalCode) : params.append( - 'geoCoordinates', - `${geoCoordinates?.longitude};${geoCoordinates?.latitude}` - ) + 'geoCoordinates', + `${geoCoordinates?.longitude};${geoCoordinates?.latitude}` + ) - const url = `${base}/api/checkout/pub/regions/?${params.toString()}` const headers: HeadersInit = withCookie({ 'content-type': 'application/json', 'X-FORWARDED-HOST': forwardedHost, }) - return fetchAPI( - url, - { - headers, + return fetchAPI({ + requestPath: `/api/checkout/pub/regions/?${params.toString()}`, + requestInit: { headers }, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'checkout', }, - { storeCookies } - ) + }) }, address: async ({ postalCode, @@ -319,13 +357,16 @@ export const VtexCommerce = ( 'X-FORWARDED-HOST': forwardedHost, }) - return fetchAPI( - `${base}/api/checkout/pub/postal-code/${country}/${postalCode}`, - { - headers, + return fetchAPI({ + requestPath: `/api/checkout/pub/postal-code/${country}/${postalCode}`, + requestInit: { headers }, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'checkout', }, - { storeCookies } - ) + }) }, }, session: (search: string): Promise => { @@ -340,29 +381,34 @@ export const VtexCommerce = ( 'content-type': 'application/json', }) - return fetchAPI( - `${base}/api/sessions?${params.toString()}`, - { + return fetchAPI({ + requestPath: `/api/sessions?${params.toString()}`, + requestInit: { method: 'POST', headers, body: '{}', }, - { storeCookies } - ) + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'sessions', + }, + }) }, subscribeToNewsletter: (data: { name: string email: string }): Promise => { - return fetchAPI( - `${base}/api/dataentities/NL/documents/`, - { + return fetchAPI({ + requestPath: `/api/dataentities/NL/documents/`, + requestInit: { ...BASE_INIT, body: JSON.stringify({ ...data, isNewsletterOptIn: true }), method: 'PATCH', }, - { storeCookies } - ) + requestOptions: { account, environment, storeCookies, vtexApi: 'md' }, + }) }, } } diff --git a/packages/api/src/platforms/vtex/clients/search/index.ts b/packages/api/src/platforms/vtex/clients/search/index.ts index a64a383c82..9c192c8a0b 100644 --- a/packages/api/src/platforms/vtex/clients/search/index.ts +++ b/packages/api/src/platforms/vtex/clients/search/index.ts @@ -50,10 +50,15 @@ export const isFacetBoolean = ( ): facet is Facet => facet.type === 'TEXT' export const IntelligentSearch = ( - { account, environment, hideUnavailableItems, simulationBehavior, showSponsored }: Options, + { + account, + environment, + hideUnavailableItems, + simulationBehavior, + showSponsored, + }: Options, ctx: Context ) => { - const base = `https://${account}.${environment}.com.br/api/io` const storeCookies = getStoreCookie(ctx) const getPolicyFacet = (): IStoreSelectedFacet | null => { @@ -145,11 +150,15 @@ export const IntelligentSearch = ( .map(({ key, value }) => `${key}/${value}`) .join('/') - return fetchAPI( - `${base}/_v/api/intelligent-search/${type}/${pathname}?${params.toString()}`, - undefined, - { storeCookies } - ) + return fetchAPI({ + requestPath: `/api/io/_v/api/intelligent-search/${type}/${pathname}?${params.toString()}`, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'io', + }, + }) } const products = (args: Omit) => @@ -163,11 +172,15 @@ export const IntelligentSearch = ( locale: ctx.storage.locale, }) - return fetchAPI( - `${base}/_v/api/intelligent-search/search_suggestions?${params.toString()}`, - undefined, - { storeCookies } - ) + return fetchAPI({ + requestPath: `/api/io/_v/api/intelligent-search/search_suggestions?${params.toString()}`, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'io', + }, + }) } const topSearches = (): Promise => { @@ -175,11 +188,15 @@ export const IntelligentSearch = ( locale: ctx.storage.locale, }) - return fetchAPI( - `${base}/_v/api/intelligent-search/top_searches?${params.toString()}`, - undefined, - { storeCookies } - ) + return fetchAPI({ + requestPath: `/api/io/_v/api/intelligent-search/top_searches?${params.toString()}`, + requestOptions: { + account, + environment, + storeCookies, + vtexApi: 'io', + }, + }) } const facets = (args: Omit) =>