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