Create needed files

This commit is contained in:
goncy 2021-08-27 14:10:55 -03:00
parent 017380515c
commit f428d5d5d4
8 changed files with 63 additions and 12 deletions

View File

@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}

View File

@ -3,11 +3,14 @@ import { getCommerceApi as commerceApi } from '@commerce/api'
import createRestFetcher from './utils/fetch-rest' import createRestFetcher from './utils/fetch-rest'
import createGraphqlFetcher from './utils/fetch-graphql' 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 getSiteInfo from './operations/get-site-info'
import getAllProductPaths from './operations/get-all-product-paths' import getAllProductPaths from './operations/get-all-product-paths'
import getAllProducts from './operations/get-all-products' import getAllProducts from './operations/get-all-products'
import getProduct from './operations/get-product' 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 { export interface OrdercloudConfig extends CommerceAPIConfig {
restFetch: <T>( restFetch: <T>(
@ -15,12 +18,14 @@ export interface OrdercloudConfig extends CommerceAPIConfig {
resource: string, resource: string,
body?: Record<string, unknown>, body?: Record<string, unknown>,
fetchOptions?: Record<string, any> fetchOptions?: Record<string, any>
) => Promise<T> ) => Promise<T>,
apiVersion: string;
} }
const config: OrdercloudConfig = { const config: OrdercloudConfig = {
commerceUrl: API_URL, commerceUrl: API_URL,
apiToken: '', apiToken: '',
apiVersion: API_VERSION,
cartCookie: CART_COOKIE, cartCookie: CART_COOKIE,
customerCookie: CUSTOMER_COOKIE, customerCookie: CUSTOMER_COOKIE,
cartCookieMaxAge: 2592000, cartCookieMaxAge: 2592000,
@ -29,6 +34,8 @@ const config: OrdercloudConfig = {
} }
const operations = { const operations = {
getAllPages,
getPage,
getSiteInfo, getSiteInfo,
getAllProductPaths, getAllProductPaths,
getAllProducts, getAllProducts,

View 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
}

View 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
}

View File

@ -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 getSiteInfo } from './get-site-info'
export { default as getProduct } from './get-product' export { default as getProduct } from './get-product'
export { default as getAllProducts } from './get-all-products' export { default as getAllProducts } from './get-all-products'

View File

@ -40,13 +40,14 @@ export async function fetchData<T>(
path: string path: string
method: string method: string
baseUrl: string baseUrl: string
apiVersion: string
fetchOptions?: Record<string, any> fetchOptions?: Record<string, any>
body?: Record<string, unknown> body?: Record<string, unknown>
}, },
retries = 0 retries = 0
): Promise<T> { ): Promise<T> {
// Destructure opts // Destructure opts
const { path, body, fetchOptions, baseUrl, method = 'GET' } = opts const { path, body, fetchOptions, baseUrl, apiVersion, method = 'GET' } = opts
// Decode token // Decode token
const decoded = jwt.decode(global.token as string) as jwt.JwtPayload | null 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 // Do the request with the correct headers
const dataResponse = await fetch(`${baseUrl}${path}`, { const dataResponse = await fetch(`${baseUrl}/${apiVersion}${path}`, {
...fetchOptions, ...fetchOptions,
method, method,
headers: { headers: {
@ -78,9 +79,6 @@ export async function fetchData<T>(
// If something failed getting the data response // If something failed getting the data response
if (!dataResponse.ok) { if (!dataResponse.ok) {
// Log error
console.log(await dataResponse.textConverted())
// If token is expired // If token is expired
if (dataResponse.status === 401) { if (dataResponse.status === 401) {
// Get a new one // Get a new one
@ -131,13 +129,14 @@ const serverFetcher: (
fetchOptions?: Record<string, any> fetchOptions?: Record<string, any>
) => { ) => {
// Get provider config // Get provider config
const { commerceUrl } = getConfig() const { commerceUrl, apiVersion } = getConfig()
// Return the data and specify the expected type // Return the data and specify the expected type
return fetchData<T>({ return fetchData<T>({
fetchOptions, fetchOptions,
method, method,
baseUrl: commerceUrl, baseUrl: commerceUrl,
apiVersion,
path, path,
body, body,
}) })

View File

@ -1,4 +1,5 @@
export const CART_COOKIE = 'ordercloud.cart' export const CART_COOKIE = 'ordercloud.cart'
export const CUSTOMER_COOKIE = 'ordercloud.customer' 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' export const LOCALE = 'en-us'

View File

@ -2,15 +2,17 @@ import { handler as useCart } from './cart/use-cart'
import { handler as useAddItem } from './cart/use-add-item' import { handler as useAddItem } from './cart/use-add-item'
import { handler as useUpdateItem } from './cart/use-update-item' import { handler as useUpdateItem } from './cart/use-update-item'
import { handler as useRemoveItem } from './cart/use-remove-item' import { handler as useRemoveItem } from './cart/use-remove-item'
import { handler as useCustomer } from './customer/use-customer' import { handler as useCustomer } from './customer/use-customer'
import { handler as useSearch } from './product/use-search' import { handler as useSearch } from './product/use-search'
import { handler as useLogin } from './auth/use-login' import { handler as useLogin } from './auth/use-login'
import { handler as useLogout } from './auth/use-logout' import { handler as useLogout } from './auth/use-logout'
import { handler as useSignup } from './auth/use-signup' 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 = { export const ordercloudProvider = {
locale: LOCALE, locale: LOCALE,
cartCookie: CART_COOKIE, cartCookie: CART_COOKIE,
@ -20,3 +22,5 @@ export const ordercloudProvider = {
products: { useSearch }, products: { useSearch },
auth: { useLogin, useLogout, useSignup }, auth: { useLogin, useLogout, useSignup },
} }
export type Provider = typeof ordercloudProvider