Divide fetcher with rest and graphql

This commit is contained in:
goncy
2021-08-25 19:51:15 -03:00
parent d4adfd079f
commit 5d76337f9e
16 changed files with 69 additions and 94 deletions

View File

@@ -9,7 +9,7 @@ import { formatCart } from '../../utils/cart'
const addItem: CartEndpoint['handlers']['addItem'] = async ({
res,
body: { cartId, item },
config: { fetch, cartCookie },
config: { storeRestFetch, cartCookie },
}) => {
// Return an error if no item is present
if (!item) {
@@ -24,7 +24,7 @@ const addItem: CartEndpoint['handlers']['addItem'] = async ({
// Create an order if it doesn't exist
if (!cartId) {
cartId = await fetch('POST', `/orders/Outgoing`, {}).then(
cartId = await storeRestFetch('POST', `/orders/Outgoing`).then(
(response: { ID: string }) => response.ID
)
}
@@ -46,14 +46,14 @@ const addItem: CartEndpoint['handlers']['addItem'] = async ({
// If a variant is present, fetch its specs
if (item.variantId) {
specs = await fetch(
specs = await storeRestFetch(
'GET',
`/me/products/${item.productId}/variants/${item.variantId}`
).then((res: RawVariant) => res.Specs)
}
// Add the item to the order
await fetch('POST', `/orders/Outgoing/${cartId}/lineitems`, {
await storeRestFetch('POST', `/orders/Outgoing/${cartId}/lineitems`, {
ProductID: item.productId,
Quantity: item.quantity,
Specs: specs,
@@ -61,8 +61,8 @@ const addItem: CartEndpoint['handlers']['addItem'] = async ({
// Get cart
const [cart, lineItems] = await Promise.all([
fetch('GET', `/orders/Outgoing/${cartId}`),
fetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
storeRestFetch('GET', `/orders/Outgoing/${cartId}`),
storeRestFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
(response: { Items: OrdercloudLineItem[] }) => response.Items
),
])