diff --git a/framework/ordercloud/api/endpoints/login/index.ts b/framework/ordercloud/api/endpoints/login/index.ts new file mode 100644 index 000000000..491bf0ac9 --- /dev/null +++ b/framework/ordercloud/api/endpoints/login/index.ts @@ -0,0 +1 @@ +export default function noopApi(...args: any[]): void {} diff --git a/framework/ordercloud/api/index.ts b/framework/ordercloud/api/index.ts index ad81811d2..6f0d08cf5 100644 --- a/framework/ordercloud/api/index.ts +++ b/framework/ordercloud/api/index.ts @@ -3,11 +3,14 @@ import { getCommerceApi as commerceApi } from '@commerce/api' 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 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' + +import { API_URL, API_VERSION, CART_COOKIE, CUSTOMER_COOKIE } from '../constants' export interface OrdercloudConfig extends CommerceAPIConfig { restFetch: ( @@ -15,12 +18,14 @@ export interface OrdercloudConfig extends CommerceAPIConfig { resource: string, body?: Record, fetchOptions?: Record - ) => Promise + ) => Promise, + apiVersion: string; } const config: OrdercloudConfig = { commerceUrl: API_URL, apiToken: '', + apiVersion: API_VERSION, cartCookie: CART_COOKIE, customerCookie: CUSTOMER_COOKIE, cartCookieMaxAge: 2592000, @@ -29,6 +34,8 @@ const config: OrdercloudConfig = { } const operations = { + getAllPages, + getPage, getSiteInfo, getAllProductPaths, getAllProducts, diff --git a/framework/ordercloud/api/operations/get-all-pages.ts b/framework/ordercloud/api/operations/get-all-pages.ts new file mode 100644 index 000000000..1727532e2 --- /dev/null +++ b/framework/ordercloud/api/operations/get-all-pages.ts @@ -0,0 +1,22 @@ +import type { OrdercloudConfig } from '../' + +import { GetAllPagesOperation } from '@commerce/types/page' + +export type Page = { url: string } +export type GetAllPagesResult = { pages: Page[] } + +export default function getAllPagesOperation() { + async 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-page.ts b/framework/ordercloud/api/operations/get-page.ts new file mode 100644 index 000000000..6b0a86a4d --- /dev/null +++ b/framework/ordercloud/api/operations/get-page.ts @@ -0,0 +1,15 @@ +import { GetPageOperation } from "@commerce/types/page" + +export type Page = any +export type GetPageResult = { page?: Page } + +export type PageVariables = { + id: number +} + +export default function getPageOperation() { + async function getPage(): Promise { + return Promise.resolve({}) + } + return getPage +} diff --git a/framework/ordercloud/api/operations/index.ts b/framework/ordercloud/api/operations/index.ts index d0f4ad4d8..84b04a978 100644 --- a/framework/ordercloud/api/operations/index.ts +++ b/framework/ordercloud/api/operations/index.ts @@ -1,3 +1,5 @@ +export { default as getAllPages } from './get-all-pages' +export { default as getPage } from './get-page' export { default as getSiteInfo } from './get-site-info' export { default as getProduct } from './get-product' export { default as getAllProducts } from './get-all-products' diff --git a/framework/ordercloud/api/utils/fetch-rest.ts b/framework/ordercloud/api/utils/fetch-rest.ts index 66717e8d1..3002b16ef 100644 --- a/framework/ordercloud/api/utils/fetch-rest.ts +++ b/framework/ordercloud/api/utils/fetch-rest.ts @@ -40,13 +40,14 @@ export async function fetchData( path: string method: string baseUrl: string + apiVersion: string fetchOptions?: Record body?: Record }, retries = 0 ): Promise { // Destructure opts - const { path, body, fetchOptions, baseUrl, method = 'GET' } = opts + const { path, body, fetchOptions, baseUrl, apiVersion, method = 'GET' } = opts // Decode token const decoded = jwt.decode(global.token as string) as jwt.JwtPayload | null @@ -64,7 +65,7 @@ export async function fetchData( } // Do the request with the correct headers - const dataResponse = await fetch(`${baseUrl}${path}`, { + const dataResponse = await fetch(`${baseUrl}/${apiVersion}${path}`, { ...fetchOptions, method, headers: { @@ -78,9 +79,6 @@ export async function fetchData( // If something failed getting the data response if (!dataResponse.ok) { - // Log error - console.log(await dataResponse.textConverted()) - // If token is expired if (dataResponse.status === 401) { // Get a new one @@ -131,13 +129,14 @@ const serverFetcher: ( fetchOptions?: Record ) => { // Get provider config - const { commerceUrl } = getConfig() + const { commerceUrl, apiVersion } = getConfig() // Return the data and specify the expected type return fetchData({ fetchOptions, method, baseUrl: commerceUrl, + apiVersion, path, body, }) diff --git a/framework/ordercloud/constants.ts b/framework/ordercloud/constants.ts index 7c46d6c6f..0c7ad21b3 100644 --- a/framework/ordercloud/constants.ts +++ b/framework/ordercloud/constants.ts @@ -1,4 +1,5 @@ export const CART_COOKIE = 'ordercloud.cart' export const CUSTOMER_COOKIE = 'ordercloud.customer' -export const API_URL = 'https://sandboxapi.ordercloud.io/v1' +export const API_URL = 'https://sandboxapi.ordercloud.io' +export const API_VERSION = 'v1' export const LOCALE = 'en-us' diff --git a/framework/ordercloud/provider.ts b/framework/ordercloud/provider.ts index 718081324..8a3ded527 100644 --- a/framework/ordercloud/provider.ts +++ b/framework/ordercloud/provider.ts @@ -2,15 +2,17 @@ import { handler as useCart } from './cart/use-cart' import { handler as useAddItem } from './cart/use-add-item' import { handler as useUpdateItem } from './cart/use-update-item' import { handler as useRemoveItem } from './cart/use-remove-item' + import { handler as useCustomer } from './customer/use-customer' import { handler as useSearch } from './product/use-search' + import { handler as useLogin } from './auth/use-login' import { handler as useLogout } from './auth/use-logout' import { handler as useSignup } from './auth/use-signup' -import { default as fetcher } from './fetcher' -import { CART_COOKIE, LOCALE } from './constants' -export type Provider = typeof ordercloudProvider +import { CART_COOKIE, LOCALE } from './constants' +import { default as fetcher } from './fetcher' + export const ordercloudProvider = { locale: LOCALE, cartCookie: CART_COOKIE, @@ -20,3 +22,5 @@ export const ordercloudProvider = { products: { useSearch }, auth: { useLogin, useLogout, useSignup }, } + +export type Provider = typeof ordercloudProvider