Rename fetch

This commit is contained in:
goncy 2021-08-26 14:59:21 -03:00
parent 9e14314d22
commit 017380515c
9 changed files with 30 additions and 30 deletions

View File

@ -9,7 +9,7 @@ import { formatCart } from '../../utils/cart'
const addItem: CartEndpoint['handlers']['addItem'] = async ({
res,
body: { cartId, item },
config: { storeRestFetch, cartCookie },
config: { restFetch, 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 storeRestFetch('POST', `/orders/Outgoing`).then(
cartId = await restFetch('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 storeRestFetch(
specs = await restFetch(
'GET',
`/me/products/${item.productId}/variants/${item.variantId}`
).then((res: RawVariant) => res.Specs)
}
// Add the item to the order
await storeRestFetch('POST', `/orders/Outgoing/${cartId}/lineitems`, {
await restFetch('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([
storeRestFetch('GET', `/orders/Outgoing/${cartId}`),
storeRestFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
restFetch('GET', `/orders/Outgoing/${cartId}`),
restFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
(response: { Items: OrdercloudLineItem[] }) => response.Items
),
])

View File

@ -9,7 +9,7 @@ import { formatCart } from '../../utils/cart'
const getCart: CartEndpoint['handlers']['getCart'] = async ({
res,
body: { cartId },
config: { storeRestFetch, cartCookie },
config: { restFetch, cartCookie },
}) => {
if (!cartId) {
return res.status(400).json({
@ -20,10 +20,10 @@ const getCart: CartEndpoint['handlers']['getCart'] = async ({
try {
// Get cart
const cart = await storeRestFetch('GET', `/orders/Outgoing/${cartId}`)
const cart = await restFetch('GET', `/orders/Outgoing/${cartId}`)
// Get line items
const lineItems = await storeRestFetch(
const lineItems = await restFetch(
'GET',
`/orders/Outgoing/${cartId}/lineitems`
).then((response: { Items: OrdercloudLineItem[] }) => response.Items)

View File

@ -6,7 +6,7 @@ import { OrdercloudLineItem } from '../../../types/cart'
const removeItem: CartEndpoint['handlers']['removeItem'] = async ({
res,
body: { cartId, itemId },
config: { storeRestFetch },
config: { restFetch },
}) => {
if (!cartId || !itemId) {
return res.status(400).json({
@ -16,15 +16,15 @@ const removeItem: CartEndpoint['handlers']['removeItem'] = async ({
}
// Remove the item to the order
await storeRestFetch(
await restFetch(
'DELETE',
`/orders/Outgoing/${cartId}/lineitems/${itemId}`
)
// Get cart
const [cart, lineItems] = await Promise.all([
storeRestFetch('GET', `/orders/Outgoing/${cartId}`),
storeRestFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
restFetch('GET', `/orders/Outgoing/${cartId}`),
restFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
(response: { Items: OrdercloudLineItem[] }) => response.Items
),
])

View File

@ -7,7 +7,7 @@ import { formatCart } from '../../utils/cart'
const updateItem: CartEndpoint['handlers']['updateItem'] = async ({
res,
body: { cartId, itemId, item },
config: { storeRestFetch },
config: { restFetch },
}) => {
if (!cartId || !itemId || !item) {
return res.status(400).json({
@ -21,14 +21,14 @@ const updateItem: CartEndpoint['handlers']['updateItem'] = async ({
// If a variant is present, fetch its specs
if (item.variantId) {
specs = await storeRestFetch(
specs = await restFetch(
'GET',
`/me/products/${item.productId}/variants/${item.variantId}`
).then((res: RawVariant) => res.Specs)
}
// Add the item to the order
await storeRestFetch(
await restFetch(
'PATCH',
`/orders/Outgoing/${cartId}/lineitems/${itemId}`,
{
@ -40,8 +40,8 @@ const updateItem: CartEndpoint['handlers']['updateItem'] = async ({
// Get cart
const [cart, lineItems] = await Promise.all([
storeRestFetch('GET', `/orders/Outgoing/${cartId}`),
storeRestFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
restFetch('GET', `/orders/Outgoing/${cartId}`),
restFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
(response: { Items: OrdercloudLineItem[] }) => response.Items
),
])

View File

@ -10,7 +10,7 @@ import getProduct from './operations/get-product'
import { API_URL, CART_COOKIE, CUSTOMER_COOKIE } from '../constants'
export interface OrdercloudConfig extends CommerceAPIConfig {
storeRestFetch: <T>(
restFetch: <T>(
method: string,
resource: string,
body?: Record<string, unknown>,
@ -24,7 +24,7 @@ const config: OrdercloudConfig = {
cartCookie: CART_COOKIE,
customerCookie: CUSTOMER_COOKIE,
cartCookieMaxAge: 2592000,
storeRestFetch: createRestFetcher(() => getCommerceApi().getConfig()),
restFetch: createRestFetcher(() => getCommerceApi().getConfig()),
fetch: createGraphqlFetcher(() => getCommerceApi().getConfig()),
}

View File

@ -17,10 +17,10 @@ export default function getAllProductPathsOperation({
config?: Partial<OrdercloudConfig>
} = {}): Promise<T['data']> {
// Get fetch from the config
const { storeRestFetch } = commerce.getConfig(config)
const { restFetch } = commerce.getConfig(config)
// Get all products
const rawProducts: RawProduct[] = await storeRestFetch<{
const rawProducts: RawProduct[] = await restFetch<{
Items: RawProduct[]
}>('GET', '/me/products').then((response) => response.Items)

View File

@ -18,10 +18,10 @@ export default function getAllProductsOperation({
preview?: boolean
} = {}): Promise<T['data']> {
// Get fetch from the config
const { storeRestFetch } = commerce.getConfig(config)
const { restFetch } = commerce.getConfig(config)
// Get all products
const rawProducts: RawProduct[] = await storeRestFetch<{
const rawProducts: RawProduct[] = await restFetch<{
Items: RawProduct[]
}>('GET', '/me/products').then((response) => response.Items)

View File

@ -19,22 +19,22 @@ export default function getProductOperation({
preview?: boolean
} = {}): Promise<T['data']> {
// Get fetch from the config
const { storeRestFetch } = commerce.getConfig(config)
const { restFetch } = commerce.getConfig(config)
// Get a single product
const productPromise = storeRestFetch<RawProduct>(
const productPromise = restFetch<RawProduct>(
'GET',
`/me/products/${variables?.slug}`
)
// Get product specs
const specsPromise = storeRestFetch<{ Items: RawSpec[] }>(
const specsPromise = restFetch<{ Items: RawSpec[] }>(
'GET',
`/me/products/${variables?.slug}/specs`
).then((res) => res.Items)
// Get product variants
const variantsPromise = storeRestFetch<{ Items: RawVariant[] }>(
const variantsPromise = restFetch<{ Items: RawVariant[] }>(
'GET',
`/me/products/${variables?.slug}/variants`
).then((res) => res.Items)

View File

@ -23,10 +23,10 @@ export default function getSiteInfoOperation({
preview?: boolean
} = {}): Promise<T['data']> {
// Get fetch from the config
const { storeRestFetch } = commerce.getConfig(config)
const { restFetch } = commerce.getConfig(config)
// Get list of categories
const rawCategories: RawCategory[] = await storeRestFetch<{
const rawCategories: RawCategory[] = await restFetch<{
Items: RawCategory[]
}>('GET', `/me/categories`).then((response) => response.Items)