forked from crowetic/commerce
Moved provider to its own file
This commit is contained in:
parent
2a5cbadd3a
commit
479b30ba5f
@ -1,91 +1,13 @@
|
|||||||
import { ReactNode } from 'react'
|
import { ReactNode } from 'react'
|
||||||
import * as React from 'react'
|
|
||||||
import { Fetcher } from '@commerce/utils/types'
|
|
||||||
import {
|
import {
|
||||||
CommerceConfig,
|
CommerceConfig,
|
||||||
CommerceProvider as CoreCommerceProvider,
|
CommerceProvider as CoreCommerceProvider,
|
||||||
useCommerce as useCoreCommerce,
|
useCommerce as useCoreCommerce,
|
||||||
} from '@commerce'
|
} from '@commerce'
|
||||||
import { FetcherError } from '@commerce/utils/errors'
|
import { bigcommerceProvider, BigcommerceProvider } from './provider'
|
||||||
import type { HookHandler } from '@commerce/utils/types'
|
|
||||||
import type { FetchCartInput } from '@commerce/cart/use-cart'
|
|
||||||
import { normalizeCart } from './lib/normalize'
|
|
||||||
import { Cart } from './types'
|
|
||||||
|
|
||||||
async function getText(res: Response) {
|
export { bigcommerceProvider }
|
||||||
try {
|
export type { BigcommerceProvider }
|
||||||
return (await res.text()) || res.statusText
|
|
||||||
} catch (error) {
|
|
||||||
return res.statusText
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getError(res: Response) {
|
|
||||||
if (res.headers.get('Content-Type')?.includes('application/json')) {
|
|
||||||
const data = await res.json()
|
|
||||||
return new FetcherError({ errors: data.errors, status: res.status })
|
|
||||||
}
|
|
||||||
return new FetcherError({ message: await getText(res), status: res.status })
|
|
||||||
}
|
|
||||||
|
|
||||||
const fetcher: Fetcher = async ({
|
|
||||||
url,
|
|
||||||
method = 'GET',
|
|
||||||
variables,
|
|
||||||
body: bodyObj,
|
|
||||||
}) => {
|
|
||||||
const hasBody = Boolean(variables || bodyObj)
|
|
||||||
const body = hasBody
|
|
||||||
? JSON.stringify(variables ? { variables } : bodyObj)
|
|
||||||
: undefined
|
|
||||||
const headers = hasBody ? { 'Content-Type': 'application/json' } : undefined
|
|
||||||
const res = await fetch(url!, { method, body, headers })
|
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
const { data } = await res.json()
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
throw await getError(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
const useCart: HookHandler<
|
|
||||||
Cart | null,
|
|
||||||
[],
|
|
||||||
FetchCartInput,
|
|
||||||
any,
|
|
||||||
any,
|
|
||||||
{ isEmpty?: boolean }
|
|
||||||
> = {
|
|
||||||
fetchOptions: {
|
|
||||||
url: '/api/bigcommerce/cart',
|
|
||||||
method: 'GET',
|
|
||||||
},
|
|
||||||
swrOptions: {
|
|
||||||
revalidateOnFocus: false,
|
|
||||||
},
|
|
||||||
normalizer: normalizeCart,
|
|
||||||
onResponse(response) {
|
|
||||||
return Object.create(response, {
|
|
||||||
isEmpty: {
|
|
||||||
get() {
|
|
||||||
return (response.data?.lineItems.length ?? 0) <= 0
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
export const bigcommerceProvider = {
|
|
||||||
locale: 'en-us',
|
|
||||||
cartCookie: 'bc_cartId',
|
|
||||||
fetcher,
|
|
||||||
cartNormalizer: normalizeCart,
|
|
||||||
cart: { useCart },
|
|
||||||
}
|
|
||||||
|
|
||||||
export type BigcommerceProvider = typeof bigcommerceProvider
|
|
||||||
|
|
||||||
export const bigcommerceConfig: CommerceConfig = {
|
export const bigcommerceConfig: CommerceConfig = {
|
||||||
locale: 'en-us',
|
locale: 'en-us',
|
||||||
|
108
framework/bigcommerce/provider.ts
Normal file
108
framework/bigcommerce/provider.ts
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
import { FetcherError } from '@commerce/utils/errors'
|
||||||
|
import type { Fetcher, HookHandler } from '@commerce/utils/types'
|
||||||
|
import type { FetchCartInput } from '@commerce/cart/use-cart'
|
||||||
|
import { normalizeCart } from './lib/normalize'
|
||||||
|
import type { Cart } from './types'
|
||||||
|
|
||||||
|
async function getText(res: Response) {
|
||||||
|
try {
|
||||||
|
return (await res.text()) || res.statusText
|
||||||
|
} catch (error) {
|
||||||
|
return res.statusText
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getError(res: Response) {
|
||||||
|
if (res.headers.get('Content-Type')?.includes('application/json')) {
|
||||||
|
const data = await res.json()
|
||||||
|
return new FetcherError({ errors: data.errors, status: res.status })
|
||||||
|
}
|
||||||
|
return new FetcherError({ message: await getText(res), status: res.status })
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetcher: Fetcher = async ({
|
||||||
|
url,
|
||||||
|
method = 'GET',
|
||||||
|
variables,
|
||||||
|
body: bodyObj,
|
||||||
|
}) => {
|
||||||
|
const hasBody = Boolean(variables || bodyObj)
|
||||||
|
const body = hasBody
|
||||||
|
? JSON.stringify(variables ? { variables } : bodyObj)
|
||||||
|
: undefined
|
||||||
|
const headers = hasBody ? { 'Content-Type': 'application/json' } : undefined
|
||||||
|
const res = await fetch(url!, { method, body, headers })
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
const { data } = await res.json()
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
throw await getError(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
const useCart: HookHandler<
|
||||||
|
Cart | null,
|
||||||
|
[],
|
||||||
|
FetchCartInput,
|
||||||
|
any,
|
||||||
|
any,
|
||||||
|
{ isEmpty?: boolean }
|
||||||
|
> = {
|
||||||
|
fetchOptions: {
|
||||||
|
url: '/api/bigcommerce/cart',
|
||||||
|
method: 'GET',
|
||||||
|
},
|
||||||
|
swrOptions: {
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
},
|
||||||
|
normalizer: normalizeCart,
|
||||||
|
onResponse(response) {
|
||||||
|
return Object.create(response, {
|
||||||
|
isEmpty: {
|
||||||
|
get() {
|
||||||
|
return (response.data?.lineItems.length ?? 0) <= 0
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const useWishlist: HookHandler<
|
||||||
|
Cart | null,
|
||||||
|
[],
|
||||||
|
FetchCartInput,
|
||||||
|
any,
|
||||||
|
any,
|
||||||
|
{ isEmpty?: boolean }
|
||||||
|
> = {
|
||||||
|
fetchOptions: {
|
||||||
|
url: '/api/bigcommerce/wishlist',
|
||||||
|
method: 'GET',
|
||||||
|
},
|
||||||
|
swrOptions: {
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
},
|
||||||
|
onResponse(response) {
|
||||||
|
return Object.create(response, {
|
||||||
|
isEmpty: {
|
||||||
|
get() {
|
||||||
|
return (response.data?.lineItems.length ?? 0) <= 0
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const bigcommerceProvider = {
|
||||||
|
locale: 'en-us',
|
||||||
|
cartCookie: 'bc_cartId',
|
||||||
|
fetcher,
|
||||||
|
cartNormalizer: normalizeCart,
|
||||||
|
cart: { useCart },
|
||||||
|
wishlist: { useWishlist },
|
||||||
|
}
|
||||||
|
|
||||||
|
export type BigcommerceProvider = typeof bigcommerceProvider
|
@ -18,6 +18,9 @@ export type Provider = CommerceConfig & {
|
|||||||
cart?: {
|
cart?: {
|
||||||
useCart?: HookHandler<Cart | null, [...any], FetchCartInput>
|
useCart?: HookHandler<Cart | null, [...any], FetchCartInput>
|
||||||
}
|
}
|
||||||
|
wishlist?: {
|
||||||
|
useWishlist?: HookHandler<Cart | null, [...any], FetchCartInput>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CommerceProps<P extends Provider> = {
|
export type CommerceProps<P extends Provider> = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user