diff --git a/framework/ordercloud/api/endpoints/cart/add-item.ts b/framework/ordercloud/api/endpoints/cart/add-item.ts index a9b36afb2..8925a5c09 100644 --- a/framework/ordercloud/api/endpoints/cart/add-item.ts +++ b/framework/ordercloud/api/endpoints/cart/add-item.ts @@ -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 ), ]) diff --git a/framework/ordercloud/api/endpoints/cart/get-cart.ts b/framework/ordercloud/api/endpoints/cart/get-cart.ts index 3ed8da349..d7bf698fa 100644 --- a/framework/ordercloud/api/endpoints/cart/get-cart.ts +++ b/framework/ordercloud/api/endpoints/cart/get-cart.ts @@ -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) diff --git a/framework/ordercloud/api/endpoints/cart/remove-item.ts b/framework/ordercloud/api/endpoints/cart/remove-item.ts index 664e398dd..f31e63809 100644 --- a/framework/ordercloud/api/endpoints/cart/remove-item.ts +++ b/framework/ordercloud/api/endpoints/cart/remove-item.ts @@ -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 ), ]) diff --git a/framework/ordercloud/api/endpoints/cart/update-item.ts b/framework/ordercloud/api/endpoints/cart/update-item.ts index 08c38b13b..74356bf7b 100644 --- a/framework/ordercloud/api/endpoints/cart/update-item.ts +++ b/framework/ordercloud/api/endpoints/cart/update-item.ts @@ -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 ), ]) diff --git a/framework/ordercloud/api/index.ts b/framework/ordercloud/api/index.ts index 91f727c10..25deb9c1e 100644 --- a/framework/ordercloud/api/index.ts +++ b/framework/ordercloud/api/index.ts @@ -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 { - fetch: ( +export interface OrdercloudConfig extends CommerceAPIConfig { + fetch: any + storeRestFetch: ( method: string, resource: string, body?: Record, @@ -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, diff --git a/framework/ordercloud/api/operations/get-all-pages.ts b/framework/ordercloud/api/operations/get-all-pages.ts deleted file mode 100644 index 642047dba..000000000 --- a/framework/ordercloud/api/operations/get-all-pages.ts +++ /dev/null @@ -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 - preview?: boolean - }): Promise { - return Promise.resolve({ - pages: [], - }) - } - return getAllPages -} diff --git a/framework/ordercloud/api/operations/get-all-product-paths.ts b/framework/ordercloud/api/operations/get-all-product-paths.ts index 8b7f8dcd0..b97d1ce52 100644 --- a/framework/ordercloud/api/operations/get-all-product-paths.ts +++ b/framework/ordercloud/api/operations/get-all-product-paths.ts @@ -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 } = {}): Promise { // 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 diff --git a/framework/ordercloud/api/operations/get-all-products.ts b/framework/ordercloud/api/operations/get-all-products.ts index f0614bd68..5930ee83c 100644 --- a/framework/ordercloud/api/operations/get-all-products.ts +++ b/framework/ordercloud/api/operations/get-all-products.ts @@ -18,13 +18,12 @@ export default function getAllProductsOperation({ preview?: boolean } = {}): Promise { // 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 diff --git a/framework/ordercloud/api/operations/get-customer-wishlist.ts b/framework/ordercloud/api/operations/get-customer-wishlist.ts deleted file mode 100644 index 8c34b9e87..000000000 --- a/framework/ordercloud/api/operations/get-customer-wishlist.ts +++ /dev/null @@ -1,6 +0,0 @@ -export default function getCustomerWishlistOperation() { - function getCustomerWishlist(): any { - return { wishlist: {} } - } - return getCustomerWishlist -} diff --git a/framework/ordercloud/api/operations/get-page.ts b/framework/ordercloud/api/operations/get-page.ts deleted file mode 100644 index b0cfdf58f..000000000 --- a/framework/ordercloud/api/operations/get-page.ts +++ /dev/null @@ -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 { - return Promise.resolve({}) - } - return getPage -} diff --git a/framework/ordercloud/api/operations/get-product.ts b/framework/ordercloud/api/operations/get-product.ts index 636404770..7cb265171 100644 --- a/framework/ordercloud/api/operations/get-product.ts +++ b/framework/ordercloud/api/operations/get-product.ts @@ -19,22 +19,22 @@ export default function getProductOperation({ preview?: boolean } = {}): Promise { // Get fetch from the config - const { fetch } = commerce.getConfig(config) + const { storeRestFetch } = commerce.getConfig(config) // Get a single product - const productPromise = fetch( + const productPromise = storeRestFetch( '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) diff --git a/framework/ordercloud/api/operations/get-site-info.ts b/framework/ordercloud/api/operations/get-site-info.ts index 5a135048f..ed8c0acfc 100644 --- a/framework/ordercloud/api/operations/get-site-info.ts +++ b/framework/ordercloud/api/operations/get-site-info.ts @@ -23,13 +23,12 @@ export default function getSiteInfoOperation({ preview?: boolean } = {}): Promise { // 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 diff --git a/framework/ordercloud/api/operations/index.ts b/framework/ordercloud/api/operations/index.ts index 086fdf83a..d0f4ad4d8 100644 --- a/framework/ordercloud/api/operations/index.ts +++ b/framework/ordercloud/api/operations/index.ts @@ -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' diff --git a/framework/ordercloud/api/utils/fetch-graphql.ts b/framework/ordercloud/api/utils/fetch-graphql.ts new file mode 100644 index 000000000..af72a337c --- /dev/null +++ b/framework/ordercloud/api/utils/fetch-graphql.ts @@ -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 diff --git a/framework/ordercloud/api/utils/fetch.ts b/framework/ordercloud/api/utils/fetch-rest.ts similarity index 98% rename from framework/ordercloud/api/utils/fetch.ts rename to framework/ordercloud/api/utils/fetch-rest.ts index e6abf15a3..66717e8d1 100644 --- a/framework/ordercloud/api/utils/fetch.ts +++ b/framework/ordercloud/api/utils/fetch-rest.ts @@ -64,7 +64,7 @@ export async function fetchData( } // Do the request with the correct headers - const dataResponse = await fetch(`${baseUrl}/v1${path}`, { + const dataResponse = await fetch(`${baseUrl}${path}`, { ...fetchOptions, method, headers: { diff --git a/framework/ordercloud/constants.ts b/framework/ordercloud/constants.ts index b6cdf73fd..7c46d6c6f 100644 --- a/framework/ordercloud/constants.ts +++ b/framework/ordercloud/constants.ts @@ -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'