mirror of
https://github.com/vercel/commerce.git
synced 2025-03-27 15:55:54 +00:00
* Add ordercloud provider * Fix provider errors * Make submit checkout optional * Make submit checkout optional * Remove nullables when creating endpoint type * Update readme * Log checkout error * Log error * Save token to cookie * Update fetch rest * Use token at checkout Co-authored-by: Luis Alvarez <luis@vercel.com>
72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import type { CommerceAPI, CommerceAPIConfig } from '@commerce/api'
|
|
import { getCommerceApi as commerceApi } from '@commerce/api'
|
|
import { createBuyerFetcher, createMiddlewareFetcher } 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,
|
|
API_VERSION,
|
|
CART_COOKIE,
|
|
CUSTOMER_COOKIE,
|
|
TOKEN_COOKIE,
|
|
} from '../constants'
|
|
|
|
export interface OrdercloudConfig extends CommerceAPIConfig {
|
|
restBuyerFetch: <T>(
|
|
method: string,
|
|
resource: string,
|
|
body?: Record<string, unknown>,
|
|
fetchOptions?: Record<string, any>
|
|
) => Promise<T>
|
|
restMiddlewareFetch: <T>(
|
|
method: string,
|
|
resource: string,
|
|
body?: Record<string, unknown>,
|
|
fetchOptions?: Record<string, any>
|
|
) => Promise<T>
|
|
apiVersion: string
|
|
tokenCookie: string
|
|
}
|
|
|
|
const config: OrdercloudConfig = {
|
|
commerceUrl: API_URL,
|
|
apiToken: '',
|
|
apiVersion: API_VERSION,
|
|
cartCookie: CART_COOKIE,
|
|
customerCookie: CUSTOMER_COOKIE,
|
|
tokenCookie: TOKEN_COOKIE,
|
|
cartCookieMaxAge: 2592000,
|
|
restBuyerFetch: createBuyerFetcher(() => getCommerceApi().getConfig()),
|
|
restMiddlewareFetch: createMiddlewareFetcher(() =>
|
|
getCommerceApi().getConfig()
|
|
),
|
|
fetch: createGraphqlFetcher(() => getCommerceApi().getConfig()),
|
|
}
|
|
|
|
const operations = {
|
|
getAllPages,
|
|
getPage,
|
|
getSiteInfo,
|
|
getAllProductPaths,
|
|
getAllProducts,
|
|
getProduct,
|
|
}
|
|
|
|
export const provider = { config, operations }
|
|
|
|
export type Provider = typeof provider
|
|
export type OrdercloudAPI<P extends Provider = Provider> = CommerceAPI<P | any>
|
|
|
|
export function getCommerceApi<P extends Provider>(
|
|
customProvider: P = provider as any
|
|
): OrdercloudAPI<P> {
|
|
return commerceApi(customProvider as any)
|
|
}
|