mirror of
https://github.com/vercel/commerce.git
synced 2025-07-05 20:51:21 +00:00
Rename fetch
This commit is contained in:
parent
9e14314d22
commit
017380515c
@ -9,7 +9,7 @@ import { formatCart } from '../../utils/cart'
|
|||||||
const addItem: CartEndpoint['handlers']['addItem'] = async ({
|
const addItem: CartEndpoint['handlers']['addItem'] = async ({
|
||||||
res,
|
res,
|
||||||
body: { cartId, item },
|
body: { cartId, item },
|
||||||
config: { storeRestFetch, cartCookie },
|
config: { restFetch, cartCookie },
|
||||||
}) => {
|
}) => {
|
||||||
// Return an error if no item is present
|
// Return an error if no item is present
|
||||||
if (!item) {
|
if (!item) {
|
||||||
@ -24,7 +24,7 @@ const addItem: CartEndpoint['handlers']['addItem'] = async ({
|
|||||||
|
|
||||||
// Create an order if it doesn't exist
|
// Create an order if it doesn't exist
|
||||||
if (!cartId) {
|
if (!cartId) {
|
||||||
cartId = await storeRestFetch('POST', `/orders/Outgoing`).then(
|
cartId = await restFetch('POST', `/orders/Outgoing`).then(
|
||||||
(response: { ID: string }) => response.ID
|
(response: { ID: string }) => response.ID
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -46,14 +46,14 @@ const addItem: CartEndpoint['handlers']['addItem'] = async ({
|
|||||||
|
|
||||||
// If a variant is present, fetch its specs
|
// If a variant is present, fetch its specs
|
||||||
if (item.variantId) {
|
if (item.variantId) {
|
||||||
specs = await storeRestFetch(
|
specs = await restFetch(
|
||||||
'GET',
|
'GET',
|
||||||
`/me/products/${item.productId}/variants/${item.variantId}`
|
`/me/products/${item.productId}/variants/${item.variantId}`
|
||||||
).then((res: RawVariant) => res.Specs)
|
).then((res: RawVariant) => res.Specs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the item to the order
|
// Add the item to the order
|
||||||
await storeRestFetch('POST', `/orders/Outgoing/${cartId}/lineitems`, {
|
await restFetch('POST', `/orders/Outgoing/${cartId}/lineitems`, {
|
||||||
ProductID: item.productId,
|
ProductID: item.productId,
|
||||||
Quantity: item.quantity,
|
Quantity: item.quantity,
|
||||||
Specs: specs,
|
Specs: specs,
|
||||||
@ -61,8 +61,8 @@ const addItem: CartEndpoint['handlers']['addItem'] = async ({
|
|||||||
|
|
||||||
// Get cart
|
// Get cart
|
||||||
const [cart, lineItems] = await Promise.all([
|
const [cart, lineItems] = await Promise.all([
|
||||||
storeRestFetch('GET', `/orders/Outgoing/${cartId}`),
|
restFetch('GET', `/orders/Outgoing/${cartId}`),
|
||||||
storeRestFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
|
restFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
|
||||||
(response: { Items: OrdercloudLineItem[] }) => response.Items
|
(response: { Items: OrdercloudLineItem[] }) => response.Items
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
@ -9,7 +9,7 @@ import { formatCart } from '../../utils/cart'
|
|||||||
const getCart: CartEndpoint['handlers']['getCart'] = async ({
|
const getCart: CartEndpoint['handlers']['getCart'] = async ({
|
||||||
res,
|
res,
|
||||||
body: { cartId },
|
body: { cartId },
|
||||||
config: { storeRestFetch, cartCookie },
|
config: { restFetch, cartCookie },
|
||||||
}) => {
|
}) => {
|
||||||
if (!cartId) {
|
if (!cartId) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@ -20,10 +20,10 @@ const getCart: CartEndpoint['handlers']['getCart'] = async ({
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Get cart
|
// Get cart
|
||||||
const cart = await storeRestFetch('GET', `/orders/Outgoing/${cartId}`)
|
const cart = await restFetch('GET', `/orders/Outgoing/${cartId}`)
|
||||||
|
|
||||||
// Get line items
|
// Get line items
|
||||||
const lineItems = await storeRestFetch(
|
const lineItems = await restFetch(
|
||||||
'GET',
|
'GET',
|
||||||
`/orders/Outgoing/${cartId}/lineitems`
|
`/orders/Outgoing/${cartId}/lineitems`
|
||||||
).then((response: { Items: OrdercloudLineItem[] }) => response.Items)
|
).then((response: { Items: OrdercloudLineItem[] }) => response.Items)
|
||||||
|
@ -6,7 +6,7 @@ import { OrdercloudLineItem } from '../../../types/cart'
|
|||||||
const removeItem: CartEndpoint['handlers']['removeItem'] = async ({
|
const removeItem: CartEndpoint['handlers']['removeItem'] = async ({
|
||||||
res,
|
res,
|
||||||
body: { cartId, itemId },
|
body: { cartId, itemId },
|
||||||
config: { storeRestFetch },
|
config: { restFetch },
|
||||||
}) => {
|
}) => {
|
||||||
if (!cartId || !itemId) {
|
if (!cartId || !itemId) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@ -16,15 +16,15 @@ const removeItem: CartEndpoint['handlers']['removeItem'] = async ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove the item to the order
|
// Remove the item to the order
|
||||||
await storeRestFetch(
|
await restFetch(
|
||||||
'DELETE',
|
'DELETE',
|
||||||
`/orders/Outgoing/${cartId}/lineitems/${itemId}`
|
`/orders/Outgoing/${cartId}/lineitems/${itemId}`
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get cart
|
// Get cart
|
||||||
const [cart, lineItems] = await Promise.all([
|
const [cart, lineItems] = await Promise.all([
|
||||||
storeRestFetch('GET', `/orders/Outgoing/${cartId}`),
|
restFetch('GET', `/orders/Outgoing/${cartId}`),
|
||||||
storeRestFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
|
restFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
|
||||||
(response: { Items: OrdercloudLineItem[] }) => response.Items
|
(response: { Items: OrdercloudLineItem[] }) => response.Items
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
@ -7,7 +7,7 @@ import { formatCart } from '../../utils/cart'
|
|||||||
const updateItem: CartEndpoint['handlers']['updateItem'] = async ({
|
const updateItem: CartEndpoint['handlers']['updateItem'] = async ({
|
||||||
res,
|
res,
|
||||||
body: { cartId, itemId, item },
|
body: { cartId, itemId, item },
|
||||||
config: { storeRestFetch },
|
config: { restFetch },
|
||||||
}) => {
|
}) => {
|
||||||
if (!cartId || !itemId || !item) {
|
if (!cartId || !itemId || !item) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@ -21,14 +21,14 @@ const updateItem: CartEndpoint['handlers']['updateItem'] = async ({
|
|||||||
|
|
||||||
// If a variant is present, fetch its specs
|
// If a variant is present, fetch its specs
|
||||||
if (item.variantId) {
|
if (item.variantId) {
|
||||||
specs = await storeRestFetch(
|
specs = await restFetch(
|
||||||
'GET',
|
'GET',
|
||||||
`/me/products/${item.productId}/variants/${item.variantId}`
|
`/me/products/${item.productId}/variants/${item.variantId}`
|
||||||
).then((res: RawVariant) => res.Specs)
|
).then((res: RawVariant) => res.Specs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the item to the order
|
// Add the item to the order
|
||||||
await storeRestFetch(
|
await restFetch(
|
||||||
'PATCH',
|
'PATCH',
|
||||||
`/orders/Outgoing/${cartId}/lineitems/${itemId}`,
|
`/orders/Outgoing/${cartId}/lineitems/${itemId}`,
|
||||||
{
|
{
|
||||||
@ -40,8 +40,8 @@ const updateItem: CartEndpoint['handlers']['updateItem'] = async ({
|
|||||||
|
|
||||||
// Get cart
|
// Get cart
|
||||||
const [cart, lineItems] = await Promise.all([
|
const [cart, lineItems] = await Promise.all([
|
||||||
storeRestFetch('GET', `/orders/Outgoing/${cartId}`),
|
restFetch('GET', `/orders/Outgoing/${cartId}`),
|
||||||
storeRestFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
|
restFetch('GET', `/orders/Outgoing/${cartId}/lineitems`).then(
|
||||||
(response: { Items: OrdercloudLineItem[] }) => response.Items
|
(response: { Items: OrdercloudLineItem[] }) => response.Items
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
@ -10,7 +10,7 @@ import getProduct from './operations/get-product'
|
|||||||
import { API_URL, CART_COOKIE, CUSTOMER_COOKIE } from '../constants'
|
import { API_URL, CART_COOKIE, CUSTOMER_COOKIE } from '../constants'
|
||||||
|
|
||||||
export interface OrdercloudConfig extends CommerceAPIConfig {
|
export interface OrdercloudConfig extends CommerceAPIConfig {
|
||||||
storeRestFetch: <T>(
|
restFetch: <T>(
|
||||||
method: string,
|
method: string,
|
||||||
resource: string,
|
resource: string,
|
||||||
body?: Record<string, unknown>,
|
body?: Record<string, unknown>,
|
||||||
@ -24,7 +24,7 @@ const config: OrdercloudConfig = {
|
|||||||
cartCookie: CART_COOKIE,
|
cartCookie: CART_COOKIE,
|
||||||
customerCookie: CUSTOMER_COOKIE,
|
customerCookie: CUSTOMER_COOKIE,
|
||||||
cartCookieMaxAge: 2592000,
|
cartCookieMaxAge: 2592000,
|
||||||
storeRestFetch: createRestFetcher(() => getCommerceApi().getConfig()),
|
restFetch: createRestFetcher(() => getCommerceApi().getConfig()),
|
||||||
fetch: createGraphqlFetcher(() => getCommerceApi().getConfig()),
|
fetch: createGraphqlFetcher(() => getCommerceApi().getConfig()),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,10 +17,10 @@ export default function getAllProductPathsOperation({
|
|||||||
config?: Partial<OrdercloudConfig>
|
config?: Partial<OrdercloudConfig>
|
||||||
} = {}): Promise<T['data']> {
|
} = {}): Promise<T['data']> {
|
||||||
// Get fetch from the config
|
// Get fetch from the config
|
||||||
const { storeRestFetch } = commerce.getConfig(config)
|
const { restFetch } = commerce.getConfig(config)
|
||||||
|
|
||||||
// Get all products
|
// Get all products
|
||||||
const rawProducts: RawProduct[] = await storeRestFetch<{
|
const rawProducts: RawProduct[] = await restFetch<{
|
||||||
Items: RawProduct[]
|
Items: RawProduct[]
|
||||||
}>('GET', '/me/products').then((response) => response.Items)
|
}>('GET', '/me/products').then((response) => response.Items)
|
||||||
|
|
||||||
|
@ -18,10 +18,10 @@ export default function getAllProductsOperation({
|
|||||||
preview?: boolean
|
preview?: boolean
|
||||||
} = {}): Promise<T['data']> {
|
} = {}): Promise<T['data']> {
|
||||||
// Get fetch from the config
|
// Get fetch from the config
|
||||||
const { storeRestFetch } = commerce.getConfig(config)
|
const { restFetch } = commerce.getConfig(config)
|
||||||
|
|
||||||
// Get all products
|
// Get all products
|
||||||
const rawProducts: RawProduct[] = await storeRestFetch<{
|
const rawProducts: RawProduct[] = await restFetch<{
|
||||||
Items: RawProduct[]
|
Items: RawProduct[]
|
||||||
}>('GET', '/me/products').then((response) => response.Items)
|
}>('GET', '/me/products').then((response) => response.Items)
|
||||||
|
|
||||||
|
@ -19,22 +19,22 @@ export default function getProductOperation({
|
|||||||
preview?: boolean
|
preview?: boolean
|
||||||
} = {}): Promise<T['data']> {
|
} = {}): Promise<T['data']> {
|
||||||
// Get fetch from the config
|
// Get fetch from the config
|
||||||
const { storeRestFetch } = commerce.getConfig(config)
|
const { restFetch } = commerce.getConfig(config)
|
||||||
|
|
||||||
// Get a single product
|
// Get a single product
|
||||||
const productPromise = storeRestFetch<RawProduct>(
|
const productPromise = restFetch<RawProduct>(
|
||||||
'GET',
|
'GET',
|
||||||
`/me/products/${variables?.slug}`
|
`/me/products/${variables?.slug}`
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get product specs
|
// Get product specs
|
||||||
const specsPromise = storeRestFetch<{ Items: RawSpec[] }>(
|
const specsPromise = restFetch<{ Items: RawSpec[] }>(
|
||||||
'GET',
|
'GET',
|
||||||
`/me/products/${variables?.slug}/specs`
|
`/me/products/${variables?.slug}/specs`
|
||||||
).then((res) => res.Items)
|
).then((res) => res.Items)
|
||||||
|
|
||||||
// Get product variants
|
// Get product variants
|
||||||
const variantsPromise = storeRestFetch<{ Items: RawVariant[] }>(
|
const variantsPromise = restFetch<{ Items: RawVariant[] }>(
|
||||||
'GET',
|
'GET',
|
||||||
`/me/products/${variables?.slug}/variants`
|
`/me/products/${variables?.slug}/variants`
|
||||||
).then((res) => res.Items)
|
).then((res) => res.Items)
|
||||||
|
@ -23,10 +23,10 @@ export default function getSiteInfoOperation({
|
|||||||
preview?: boolean
|
preview?: boolean
|
||||||
} = {}): Promise<T['data']> {
|
} = {}): Promise<T['data']> {
|
||||||
// Get fetch from the config
|
// Get fetch from the config
|
||||||
const { storeRestFetch } = commerce.getConfig(config)
|
const { restFetch } = commerce.getConfig(config)
|
||||||
|
|
||||||
// Get list of categories
|
// Get list of categories
|
||||||
const rawCategories: RawCategory[] = await storeRestFetch<{
|
const rawCategories: RawCategory[] = await restFetch<{
|
||||||
Items: RawCategory[]
|
Items: RawCategory[]
|
||||||
}>('GET', `/me/categories`).then((response) => response.Items)
|
}>('GET', `/me/categories`).then((response) => response.Items)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user