Skip to content

Commit

Permalink
chore: Adapt fetchAPI calls to consider the vtexinternal environment
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfp13 committed Oct 16, 2024
1 parent a58f6d7 commit 714d28a
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 108 deletions.
228 changes: 137 additions & 91 deletions packages/api/src/platforms/vtex/clients/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,67 @@ 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, '')

return {
catalog: {
salesChannel: (sc: string): Promise<SalesChannel> =>
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<Brand[]> =>
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<CategoryTree[]> =>
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<PortalPagetype> =>
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: ({
Expand All @@ -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',
},
})
},
},
},
Expand All @@ -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: (
{
Expand All @@ -142,9 +168,9 @@ export const VtexCommerce = (
): Promise<OrderForm> => {
const deliveryWindow = setDeliveryWindow
? {
startDateUtc: deliveryMode?.deliveryWindow?.startDate,
endDateUtc: deliveryMode?.deliveryWindow?.endDate,
}
startDateUtc: deliveryMode?.deliveryWindow?.startDate,
endDateUtc: deliveryMode?.deliveryWindow?.endDate,
}
: null

const mappedBody = {
Expand All @@ -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,
Expand All @@ -193,29 +224,28 @@ 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({
'content-type': 'application/json',
'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,
Expand All @@ -240,18 +270,23 @@ 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,
noSplitItem: !shouldSplitItem,
}),
method: 'PATCH',
},
{ storeCookies }
)
requestOptions: {
account,
environment,
storeCookies,
vtexApi: 'checkout',
},
})
},
setCustomData: ({
id,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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<Session> => {
Expand All @@ -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<MasterDataResponse> => {
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' },
})
},
}
}
Loading

0 comments on commit 714d28a

Please sign in to comment.