mirror of
https://github.com/vercel/commerce.git
synced 2025-07-05 12:41:21 +00:00
Divide fetcher with rest and graphql
This commit is contained in:
parent
d4adfd079f
commit
5d76337f9e
@ -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
|
||||
),
|
||||
])
|
||||
|
@ -9,7 +9,7 @@ import { formatCart } from '../../utils/cart'
|
||||
const getCart: CartEndpoint['handlers']['getCart'] = async ({
|
||||
res,
|
||||
body: { cartId },
|
||||
config: { fetch, cartCookie },
|
||||
config: { storeRestFetch, cartCookie },
|
||||
}) => {
|
||||
if (!cartId) {
|
||||
return res.status(400).json({
|
||||
@ -20,10 +20,10 @@ const getCart: CartEndpoint['handlers']['getCart'] = async ({
|
||||
|
||||
try {
|
||||
// Get cart
|
||||
const cart = await fetch('GET', `/orders/Outgoing/${cartId}`)
|
||||
const cart = await storeRestFetch('GET', `/orders/Outgoing/${cartId}`)
|
||||
|
||||
// Get line items
|
||||
const lineItems = await fetch(
|
||||
const lineItems = await storeRestFetch(
|
||||
'GET',
|
||||
`/orders/Outgoing/${cartId}/lineitems`
|
||||
).then((response: { Items: OrdercloudLineItem[] }) => response.Items)
|
||||
|
@ -6,7 +6,7 @@ import { OrdercloudLineItem } from '../../../types/cart'
|
||||
const removeItem: CartEndpoint['handlers']['removeItem'] = async ({
|
||||
res,
|
||||
body: { cartId, itemId },
|
||||
config: { fetch },
|
||||
config: { storeRestFetch },
|
||||
}) => {
|
||||
if (!cartId || !itemId) {
|
||||
return res.status(400).json({
|
||||
@ -16,12 +16,15 @@ const removeItem: CartEndpoint['handlers']['removeItem'] = async ({
|
||||
}
|
||||
|
||||
// Remove the item to the order
|
||||
await fetch('DELETE', `/orders/Outgoing/${cartId}/lineitems/${itemId}`)
|
||||
await storeRestFetch(
|
||||
'DELETE',
|
||||
`/orders/Outgoing/${cartId}/lineitems/${itemId}`
|
||||
)
|
||||
|
||||
// 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
|
||||
),
|
||||
])
|
||||
|
@ -7,7 +7,7 @@ import { formatCart } from '../../utils/cart'
|
||||
const updateItem: CartEndpoint['handlers']['updateItem'] = async ({
|
||||
res,
|
||||
body: { cartId, itemId, item },
|
||||
config: { fetch },
|
||||
config: { storeRestFetch },
|
||||
}) => {
|
||||
if (!cartId || !itemId || !item) {
|
||||
return res.status(400).json({
|
||||
@ -21,23 +21,27 @@ const updateItem: CartEndpoint['handlers']['updateItem'] = 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('PATCH', `/orders/Outgoing/${cartId}/lineitems/${itemId}`, {
|
||||
ProductID: item.productId,
|
||||
Quantity: item.quantity,
|
||||
Specs: specs,
|
||||
})
|
||||
await storeRestFetch(
|
||||
'PATCH',
|
||||
`/orders/Outgoing/${cartId}/lineitems/${itemId}`,
|
||||
{
|
||||
ProductID: item.productId,
|
||||
Quantity: item.quantity,
|
||||
Specs: specs,
|
||||
}
|
||||
)
|
||||
|
||||
// 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
|
||||
),
|
||||
])
|
||||
|
@ -1,18 +1,17 @@
|
||||
import type { CommerceAPI, CommerceAPIConfig } from '@commerce/api'
|
||||
import { getCommerceApi as commerceApi } from '@commerce/api'
|
||||
import createFetcher from './utils/fetch'
|
||||
import createRestFetcher from './utils/fetch-rest'
|
||||
import createGraphqlFetcher from './utils/fetch-graphql'
|
||||
|
||||
import getAllPages from './operations/get-all-pages'
|
||||
import getPage from './operations/get-page'
|
||||
import getSiteInfo from './operations/get-site-info'
|
||||
import getCustomerWishlist from './operations/get-customer-wishlist'
|
||||
import getAllProductPaths from './operations/get-all-product-paths'
|
||||
import getAllProducts from './operations/get-all-products'
|
||||
import getProduct from './operations/get-product'
|
||||
import { API_URL, CART_COOKIE, CUSTOMER_COOKIE } from '../constants'
|
||||
|
||||
export interface OrdercloudConfig extends Omit<CommerceAPIConfig, 'fetch'> {
|
||||
fetch: <T>(
|
||||
export interface OrdercloudConfig extends CommerceAPIConfig {
|
||||
fetch: any
|
||||
storeRestFetch: <T>(
|
||||
method: string,
|
||||
resource: string,
|
||||
body?: Record<string, unknown>,
|
||||
@ -26,14 +25,12 @@ const config: OrdercloudConfig = {
|
||||
cartCookie: CART_COOKIE,
|
||||
customerCookie: CUSTOMER_COOKIE,
|
||||
cartCookieMaxAge: 2592000,
|
||||
fetch: createFetcher(() => getCommerceApi().getConfig()),
|
||||
storeRestFetch: createRestFetcher(() => getCommerceApi().getConfig()),
|
||||
fetch: createGraphqlFetcher(() => getCommerceApi().getConfig()),
|
||||
}
|
||||
|
||||
const operations = {
|
||||
getAllPages,
|
||||
getPage,
|
||||
getSiteInfo,
|
||||
getCustomerWishlist,
|
||||
getAllProductPaths,
|
||||
getAllProducts,
|
||||
getProduct,
|
||||
|
@ -1,19 +0,0 @@
|
||||
export type Page = { url: string }
|
||||
export type GetAllPagesResult = { pages: Page[] }
|
||||
import type { OrdercloudConfig } from '../index'
|
||||
|
||||
export default function getAllPagesOperation() {
|
||||
function getAllPages({
|
||||
config,
|
||||
preview,
|
||||
}: {
|
||||
url?: string
|
||||
config?: Partial<OrdercloudConfig>
|
||||
preview?: boolean
|
||||
}): Promise<GetAllPagesResult> {
|
||||
return Promise.resolve({
|
||||
pages: [],
|
||||
})
|
||||
}
|
||||
return getAllPages
|
||||
}
|
@ -2,7 +2,7 @@ import type { OperationContext } from '@commerce/api/operations'
|
||||
import type { GetAllProductPathsOperation } from '@commerce/types/product'
|
||||
|
||||
import type { RawProduct } from '../../types/product'
|
||||
import type { OrdercloudConfig, Provider } from '../index'
|
||||
import type { OrdercloudConfig, Provider } from '../'
|
||||
|
||||
export type GetAllProductPathsResult = {
|
||||
products: Array<{ path: string }>
|
||||
@ -17,13 +17,12 @@ export default function getAllProductPathsOperation({
|
||||
config?: Partial<OrdercloudConfig>
|
||||
} = {}): Promise<T['data']> {
|
||||
// Get fetch from the config
|
||||
const { fetch } = commerce.getConfig(config)
|
||||
const { storeRestFetch } = commerce.getConfig(config)
|
||||
|
||||
// Get all products
|
||||
const rawProducts: RawProduct[] = await fetch<{ Items: RawProduct[] }>(
|
||||
'GET',
|
||||
'/me/products'
|
||||
).then((response) => response.Items)
|
||||
const rawProducts: RawProduct[] = await storeRestFetch<{
|
||||
Items: RawProduct[]
|
||||
}>('GET', '/me/products').then((response) => response.Items)
|
||||
|
||||
return {
|
||||
// Match a path for every product retrieved
|
||||
|
@ -18,13 +18,12 @@ export default function getAllProductsOperation({
|
||||
preview?: boolean
|
||||
} = {}): Promise<T['data']> {
|
||||
// Get fetch from the config
|
||||
const { fetch } = commerce.getConfig(config)
|
||||
const { storeRestFetch } = commerce.getConfig(config)
|
||||
|
||||
// Get all products
|
||||
const rawProducts: RawProduct[] = await fetch<{ Items: RawProduct[] }>(
|
||||
'GET',
|
||||
'/me/products'
|
||||
).then((response) => response.Items)
|
||||
const rawProducts: RawProduct[] = await storeRestFetch<{
|
||||
Items: RawProduct[]
|
||||
}>('GET', '/me/products').then((response) => response.Items)
|
||||
|
||||
return {
|
||||
// Normalize products to commerce schema
|
||||
|
@ -1,6 +0,0 @@
|
||||
export default function getCustomerWishlistOperation() {
|
||||
function getCustomerWishlist(): any {
|
||||
return { wishlist: {} }
|
||||
}
|
||||
return getCustomerWishlist
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
export type Page = any
|
||||
export type GetPageResult = { page?: Page }
|
||||
|
||||
export type PageVariables = {
|
||||
id: number
|
||||
}
|
||||
|
||||
export default function getPageOperation() {
|
||||
function getPage(): Promise<GetPageResult> {
|
||||
return Promise.resolve({})
|
||||
}
|
||||
return getPage
|
||||
}
|
@ -19,22 +19,22 @@ export default function getProductOperation({
|
||||
preview?: boolean
|
||||
} = {}): Promise<T['data']> {
|
||||
// Get fetch from the config
|
||||
const { fetch } = commerce.getConfig(config)
|
||||
const { storeRestFetch } = commerce.getConfig(config)
|
||||
|
||||
// Get a single product
|
||||
const productPromise = fetch<RawProduct>(
|
||||
const productPromise = storeRestFetch<RawProduct>(
|
||||
'GET',
|
||||
`/me/products/${variables?.slug}`
|
||||
)
|
||||
|
||||
// Get product specs
|
||||
const specsPromise = fetch<{ Items: RawSpec[] }>(
|
||||
const specsPromise = storeRestFetch<{ Items: RawSpec[] }>(
|
||||
'GET',
|
||||
`/me/products/${variables?.slug}/specs`
|
||||
).then((res) => res.Items)
|
||||
|
||||
// Get product variants
|
||||
const variantsPromise = fetch<{ Items: RawVariant[] }>(
|
||||
const variantsPromise = storeRestFetch<{ Items: RawVariant[] }>(
|
||||
'GET',
|
||||
`/me/products/${variables?.slug}/variants`
|
||||
).then((res) => res.Items)
|
||||
|
@ -23,13 +23,12 @@ export default function getSiteInfoOperation({
|
||||
preview?: boolean
|
||||
} = {}): Promise<T['data']> {
|
||||
// Get fetch from the config
|
||||
const { fetch } = commerce.getConfig(config)
|
||||
const { storeRestFetch } = commerce.getConfig(config)
|
||||
|
||||
// Get list of categories
|
||||
const rawCategories: RawCategory[] = await fetch<{ Items: RawCategory[] }>(
|
||||
'GET',
|
||||
`/me/categories`
|
||||
).then((response) => response.Items)
|
||||
const rawCategories: RawCategory[] = await storeRestFetch<{
|
||||
Items: RawCategory[]
|
||||
}>('GET', `/me/categories`).then((response) => response.Items)
|
||||
|
||||
return {
|
||||
// Normalize categories
|
||||
|
@ -1,6 +1,4 @@
|
||||
export { default as getPage } from './get-page'
|
||||
export { default as getSiteInfo } from './get-site-info'
|
||||
export { default as getAllPages } from './get-all-pages'
|
||||
export { default as getProduct } from './get-product'
|
||||
export { default as getAllProducts } from './get-all-products'
|
||||
export { default as getAllProductPaths } from './get-all-product-paths'
|
||||
|
14
framework/ordercloud/api/utils/fetch-graphql.ts
Normal file
14
framework/ordercloud/api/utils/fetch-graphql.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import type { GraphQLFetcher } from '@commerce/api'
|
||||
import type { OrdercloudConfig } from '../'
|
||||
|
||||
import { FetcherError } from '@commerce/utils/errors'
|
||||
|
||||
const fetchGraphqlApi: (getConfig: () => OrdercloudConfig) => GraphQLFetcher =
|
||||
() => async () => {
|
||||
throw new FetcherError({
|
||||
errors: [{ message: 'GraphQL fetch is not implemented' }],
|
||||
status: 500,
|
||||
})
|
||||
}
|
||||
|
||||
export default fetchGraphqlApi
|
@ -64,7 +64,7 @@ export async function fetchData<T>(
|
||||
}
|
||||
|
||||
// Do the request with the correct headers
|
||||
const dataResponse = await fetch(`${baseUrl}/v1${path}`, {
|
||||
const dataResponse = await fetch(`${baseUrl}${path}`, {
|
||||
...fetchOptions,
|
||||
method,
|
||||
headers: {
|
@ -1,4 +1,4 @@
|
||||
export const CART_COOKIE = 'ordercloud.cart'
|
||||
export const CUSTOMER_COOKIE = 'ordercloud.customer'
|
||||
export const API_URL = 'https://sandboxapi.ordercloud.io'
|
||||
export const API_URL = 'https://sandboxapi.ordercloud.io/v1'
|
||||
export const LOCALE = 'en-us'
|
||||
|
Loading…
x
Reference in New Issue
Block a user