Skip to content

Commit

Permalink
Remove filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lariciamota committed Nov 29, 2023
1 parent 4bac95f commit a44cae7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
73 changes: 33 additions & 40 deletions packages/api/src/platforms/vtex/clients/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type { SalesChannel } from './types/SalesChannel'
import { MasterDataResponse } from './types/Newsletter'
import type { Address, AddressInput } from './types/Address'
import { DeliveryMode, SelectedAddress } from './types/ShippingData'
import getCookieByName from '../../../../utils/get-cookie-by-name'

type ValueOf<T> = T extends Record<string, infer K> ? K : never

Expand All @@ -30,18 +29,6 @@ const BASE_INIT = {
},
}

const setCheckoutOrderFormOwnershipCookie = (
headers: Headers,
ctx: Context
) => {
if (headers) {
ctx.storage.cookies = `CheckoutOrderFormOwnership=${getCookieByName(
'CheckoutOrderFormOwnership',
headers.get('set-cookie') ?? ''
)}`
}
}

export const VtexCommerce = (
{ account, environment, incrementAddress }: Options,
ctx: Context
Expand All @@ -51,18 +38,24 @@ export const VtexCommerce = (
return {
catalog: {
salesChannel: (sc: string): Promise<SalesChannel> =>
fetchAPI(`${base}/api/catalog_system/pub/saleschannel/${sc}`),
fetchAPI(`${base}/api/catalog_system/pub/saleschannel/${sc}`, ctx),
brand: {
list: (): Promise<Brand[]> =>
fetchAPI(`${base}/api/catalog_system/pub/brand/list`),
fetchAPI(`${base}/api/catalog_system/pub/brand/list`, ctx),
},
category: {
tree: (depth = 3): Promise<CategoryTree[]> =>
fetchAPI(`${base}/api/catalog_system/pub/category/tree/${depth}`),
fetchAPI(
`${base}/api/catalog_system/pub/category/tree/${depth}`,
ctx
),
},
portal: {
pagetype: (slug: string): Promise<PortalPagetype> =>
fetchAPI(`${base}/api/catalog_system/pub/portal/pagetype/${slug}`),
fetchAPI(
`${base}/api/catalog_system/pub/portal/pagetype/${slug}`,
ctx
),
},
products: {
crossselling: ({
Expand All @@ -80,7 +73,8 @@ export const VtexCommerce = (
})

return fetchAPI(
`${base}/api/catalog_system/pub/products/crossselling/${type}/${productId}?${params}`
`${base}/api/catalog_system/pub/products/crossselling/${type}/${productId}?${params}`,
ctx
)
},
},
Expand All @@ -96,6 +90,7 @@ export const VtexCommerce = (

return fetchAPI(
`${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`,
ctx,
{
...BASE_INIT,
body: JSON.stringify(args),
Expand Down Expand Up @@ -136,15 +131,15 @@ export const VtexCommerce = (
}
return fetchAPI(
`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`,
ctx,
{
...BASE_INIT,
body: JSON.stringify(mappedBody),
headers: {
'content-type': 'application/json',
cookie: ctx.headers.cookie,
},
},
(headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
}
)
},

Expand Down Expand Up @@ -174,14 +169,15 @@ export const VtexCommerce = (

return fetchAPI(
`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`,
requestInit,
(headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
ctx,
requestInit
)
},

clearOrderFormMessages: ({ id }: { id: string }) => {
return fetchAPI(
`${base}/api/checkout/pub/orderForm/${id}/messages/clear`,
ctx,
{
...BASE_INIT,
body: '{}',
Expand Down Expand Up @@ -231,15 +227,15 @@ export const VtexCommerce = (

return fetchAPI(
`${base}/api/checkout/pub/orderForm/${id}/items?${params}`,
ctx,
{
...requestInit,
body: JSON.stringify({
orderItems,
noSplitItem: !shouldSplitItem,
}),
method: 'PATCH',
},
(headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
}
)
},
setCustomData: ({
Expand All @@ -255,12 +251,12 @@ export const VtexCommerce = (
}): Promise<OrderForm> => {
return fetchAPI(
`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`,
ctx,
{
...BASE_INIT,
body: JSON.stringify({ value }),
method: 'PUT',
},
(headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
}
)
},
region: async ({
Expand All @@ -282,14 +278,15 @@ export const VtexCommerce = (
)

const url = `${base}/api/checkout/pub/regions/?${params.toString()}`
return fetchAPI(url)
return fetchAPI(url, ctx)
},
address: async ({
postalCode,
country,
}: AddressInput): Promise<Address> => {
return fetchAPI(
`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`
`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`,
ctx
)
},
},
Expand All @@ -300,24 +297,20 @@ export const VtexCommerce = (
'items',
'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol'
)
return fetchAPI(
`${base}/api/sessions?${params.toString()}`,
{
method: 'POST',
headers: {
'content-type': 'application/json',
cookie: ctx.headers.cookie,
},
body: '{}',
return fetchAPI(`${base}/api/sessions?${params.toString()}`, ctx, {
method: 'POST',
headers: {
'content-type': 'application/json',
cookie: ctx.headers.cookie,
},
(headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
)
body: '{}',
})
},
subscribeToNewsletter: (data: {
name: string
email: string
}): Promise<MasterDataResponse> => {
return fetchAPI(`${base}/api/dataentities/NL/documents/`, {
return fetchAPI(`${base}/api/dataentities/NL/documents/`, ctx, {
...BASE_INIT,
body: JSON.stringify({ ...data, isNewsletterOptIn: true }),
method: 'PATCH',
Expand Down
7 changes: 3 additions & 4 deletions packages/api/src/platforms/vtex/clients/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import fetch from 'isomorphic-unfetch'
import packageJson from '../../../../package.json'
import type { Context } from '../index'

const USER_AGENT = `${packageJson.name}@${packageJson.version}`

export const fetchAPI = async (
info: RequestInfo,
ctx: Context,
init?: RequestInit,
getHeaders?: (headers: Headers) => void
) => {
const response = await fetch(info, {
...init,
Expand All @@ -17,9 +18,7 @@ export const fetchAPI = async (
})

if (response.ok) {
if (getHeaders) {
getHeaders(response.headers)
}
ctx.storage.cookies = response.headers?.get('set-cookie') ?? ''

return response.status !== 204 ? response.json() : undefined
}
Expand Down
6 changes: 0 additions & 6 deletions packages/api/src/utils/get-cookie-by-name.ts

This file was deleted.

0 comments on commit a44cae7

Please sign in to comment.