mirror of
https://github.com/vercel/commerce.git
synced 2025-07-04 12:11:22 +00:00
Create needed files
This commit is contained in:
parent
017380515c
commit
f428d5d5d4
1
framework/ordercloud/api/endpoints/login/index.ts
Normal file
1
framework/ordercloud/api/endpoints/login/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@ -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: <T>(
|
||||
@ -15,12 +18,14 @@ export interface OrdercloudConfig extends CommerceAPIConfig {
|
||||
resource: string,
|
||||
body?: Record<string, unknown>,
|
||||
fetchOptions?: Record<string, any>
|
||||
) => Promise<T>
|
||||
) => Promise<T>,
|
||||
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,
|
||||
|
22
framework/ordercloud/api/operations/get-all-pages.ts
Normal file
22
framework/ordercloud/api/operations/get-all-pages.ts
Normal file
@ -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<T extends GetAllPagesOperation>({
|
||||
config,
|
||||
preview,
|
||||
}: {
|
||||
url?: string
|
||||
config?: Partial<OrdercloudConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<T['data']> {
|
||||
return Promise.resolve({
|
||||
pages: [],
|
||||
})
|
||||
}
|
||||
return getAllPages
|
||||
}
|
15
framework/ordercloud/api/operations/get-page.ts
Normal file
15
framework/ordercloud/api/operations/get-page.ts
Normal file
@ -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<T extends GetPageOperation>(): Promise<T['data']> {
|
||||
return Promise.resolve({})
|
||||
}
|
||||
return getPage
|
||||
}
|
@ -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'
|
||||
|
@ -40,13 +40,14 @@ export async function fetchData<T>(
|
||||
path: string
|
||||
method: string
|
||||
baseUrl: string
|
||||
apiVersion: string
|
||||
fetchOptions?: Record<string, any>
|
||||
body?: Record<string, unknown>
|
||||
},
|
||||
retries = 0
|
||||
): Promise<T> {
|
||||
// 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<T>(
|
||||
}
|
||||
|
||||
// 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<T>(
|
||||
|
||||
// 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<string, any>
|
||||
) => {
|
||||
// Get provider config
|
||||
const { commerceUrl } = getConfig()
|
||||
const { commerceUrl, apiVersion } = getConfig()
|
||||
|
||||
// Return the data and specify the expected type
|
||||
return fetchData<T>({
|
||||
fetchOptions,
|
||||
method,
|
||||
baseUrl: commerceUrl,
|
||||
apiVersion,
|
||||
path,
|
||||
body,
|
||||
})
|
||||
|
@ -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'
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user