mirror of
https://github.com/vercel/commerce.git
synced 2025-03-14 22:42:33 +00:00
Moved normalizer to happen after fetch
This commit is contained in:
parent
1588fcf6bb
commit
7edd5aa182
@ -10,17 +10,20 @@ const defaultOpts = {
|
||||
method: 'GET',
|
||||
}
|
||||
|
||||
export const fetcher: HookFetcher<BigcommerceCart | null, CartInput> = (
|
||||
export const fetcher: HookFetcher<Cart | null, CartInput> = async (
|
||||
options,
|
||||
{ cartId },
|
||||
fetch
|
||||
) => {
|
||||
return cartId ? fetch({ ...defaultOpts, ...options }) : null
|
||||
const data = cartId
|
||||
? await fetch<BigcommerceCart>({ ...defaultOpts, ...options })
|
||||
: null
|
||||
return data && normalizeCart(data)
|
||||
}
|
||||
|
||||
export function extendHook(
|
||||
customFetcher: typeof fetcher,
|
||||
swrOptions?: SwrOptions<BigcommerceCart | null, CartInput>
|
||||
swrOptions?: SwrOptions<Cart | null, CartInput>
|
||||
) {
|
||||
const useCart = () => {
|
||||
const response = useCommerceCart(defaultOpts, [], customFetcher, {
|
||||
@ -28,7 +31,6 @@ export function extendHook(
|
||||
...swrOptions,
|
||||
})
|
||||
const res = useResponse(response, {
|
||||
normalizer: normalizeCart,
|
||||
descriptors: {
|
||||
isEmpty: {
|
||||
get() {
|
||||
|
@ -3,18 +3,18 @@ import type { HookInput, HookFetcher, HookFetcherOptions } from '../utils/types'
|
||||
import useData, { ResponseState, SwrOptions } from '../utils/use-data'
|
||||
import { useCommerce } from '..'
|
||||
|
||||
export type CartResponse<Result> = ResponseState<Result> & { isEmpty?: boolean }
|
||||
export type CartResponse<Data> = ResponseState<Data> & { isEmpty?: boolean }
|
||||
|
||||
export type CartInput = {
|
||||
cartId: Cart['id']
|
||||
}
|
||||
|
||||
export default function useCart<Result>(
|
||||
export default function useCart<Data>(
|
||||
options: HookFetcherOptions,
|
||||
input: HookInput,
|
||||
fetcherFn: HookFetcher<Result, CartInput>,
|
||||
swrOptions?: SwrOptions<Result, CartInput>
|
||||
): CartResponse<Result> {
|
||||
fetcherFn: HookFetcher<Data, CartInput>,
|
||||
swrOptions?: SwrOptions<Data, CartInput>
|
||||
): CartResponse<Data> {
|
||||
const { cartCookie } = useCommerce()
|
||||
const fetcher: typeof fetcherFn = (options, input, fetch) => {
|
||||
input.cartId = Cookies.get(cartCookie)
|
||||
|
@ -9,11 +9,11 @@ export type FetcherOptions = {
|
||||
body?: any
|
||||
}
|
||||
|
||||
export type HookFetcher<Result, Input = null> = (
|
||||
export type HookFetcher<Data, Input = null, Result = any> = (
|
||||
options: HookFetcherOptions | null,
|
||||
input: Input,
|
||||
fetch: <T = Result>(options: FetcherOptions) => Promise<T>
|
||||
) => Result | Promise<Result>
|
||||
) => Data | Promise<Data>
|
||||
|
||||
export type HookFetcherOptions = {
|
||||
query?: string
|
||||
|
@ -4,22 +4,22 @@ import defineProperty from './define-property'
|
||||
import { CommerceError } from './errors'
|
||||
import { useCommerce } from '..'
|
||||
|
||||
export type SwrOptions<Result, Input = null> = ConfigInterface<
|
||||
Result,
|
||||
export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
||||
Data,
|
||||
CommerceError,
|
||||
HookFetcher<Result, Input>
|
||||
HookFetcher<Data, Input, Result>
|
||||
>
|
||||
|
||||
export type ResponseState<Result> = responseInterface<Result, CommerceError> & {
|
||||
isLoading: boolean
|
||||
}
|
||||
|
||||
export type UseData = <Result = any, Input = null>(
|
||||
export type UseData = <Data = any, Input = null, Result = any>(
|
||||
options: HookFetcherOptions | (() => HookFetcherOptions | null),
|
||||
input: HookInput,
|
||||
fetcherFn: HookFetcher<Result, Input>,
|
||||
swrOptions?: SwrOptions<Result, Input>
|
||||
) => ResponseState<Result>
|
||||
fetcherFn: HookFetcher<Data, Input, Result>,
|
||||
swrOptions?: SwrOptions<Data, Input, Result>
|
||||
) => ResponseState<Data>
|
||||
|
||||
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
||||
const { fetcherRef } = useCommerce()
|
||||
|
Loading…
x
Reference in New Issue
Block a user