forked from crowetic/commerce
Removed generics for result and body
This commit is contained in:
parent
505d3fe04b
commit
b907c31ef2
@ -50,15 +50,16 @@ const useCart: HookHandler<
|
||||
Cart | null,
|
||||
{},
|
||||
FetchCartInput,
|
||||
any,
|
||||
any,
|
||||
{ isEmpty?: boolean }
|
||||
> = {
|
||||
fetchOptions: {
|
||||
url: '/api/bigcommerce/cart',
|
||||
method: 'GET',
|
||||
},
|
||||
normalizer: normalizeCart,
|
||||
async fetcher({ input: { cartId }, options, fetch }) {
|
||||
const data = cartId ? await fetch(options) : null
|
||||
return data && normalizeCart(data)
|
||||
},
|
||||
useHook({ input, useData }) {
|
||||
const response = useData({
|
||||
swrOptions: { revalidateOnFocus: false, ...input.swrOptions },
|
||||
@ -83,8 +84,6 @@ const useWishlist: HookHandler<
|
||||
Wishlist | null,
|
||||
{ includeProducts?: boolean },
|
||||
{ customerId?: number; includeProducts: boolean },
|
||||
any,
|
||||
any,
|
||||
{ isEmpty?: boolean }
|
||||
> = {
|
||||
fetchOptions: {
|
||||
@ -132,18 +131,15 @@ const useWishlist: HookHandler<
|
||||
},
|
||||
}
|
||||
|
||||
const useCustomerHandler: HookHandler<
|
||||
Customer | null,
|
||||
{},
|
||||
{},
|
||||
CustomerData | null,
|
||||
any
|
||||
> = {
|
||||
const useCustomerHandler: HookHandler<Customer | null> = {
|
||||
fetchOptions: {
|
||||
url: '/api/bigcommerce/customers',
|
||||
method: 'GET',
|
||||
},
|
||||
normalizer: (data) => data.customer,
|
||||
async fetcher({ options, fetch }) {
|
||||
const data = await fetch<CustomerData | null>(options)
|
||||
return data?.customer ?? null
|
||||
},
|
||||
useHook({ input, useData }) {
|
||||
return useData({
|
||||
swrOptions: {
|
||||
@ -164,9 +160,7 @@ export type SearchProductsInput = {
|
||||
const useSearch: HookHandler<
|
||||
SearchProductsData,
|
||||
SearchProductsInput,
|
||||
SearchProductsInput,
|
||||
any,
|
||||
any
|
||||
SearchProductsInput
|
||||
> = {
|
||||
fetchOptions: {
|
||||
url: '/api/bigcommerce/catalog/products',
|
||||
|
@ -34,10 +34,8 @@ export const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
||||
options,
|
||||
input: { cartId },
|
||||
fetch,
|
||||
normalize,
|
||||
}) => {
|
||||
const data = cartId ? await fetch({ ...options }) : null
|
||||
return data && normalize ? normalize(data) : data
|
||||
return cartId ? await fetch({ ...options }) : null
|
||||
}
|
||||
|
||||
export default function useCart<P extends Provider>(
|
||||
|
@ -1,12 +1,6 @@
|
||||
import { HookFetcherFn } from './types'
|
||||
import type { HookFetcherFn } from './types'
|
||||
|
||||
const defaultFetcher: HookFetcherFn<any> = async ({
|
||||
options,
|
||||
fetch,
|
||||
normalize,
|
||||
}) => {
|
||||
const data = await fetch({ ...options })
|
||||
return data && normalize ? normalize(data) : data
|
||||
}
|
||||
const defaultFetcher: HookFetcherFn<any> = ({ options, fetch }) =>
|
||||
fetch(options)
|
||||
|
||||
export default defaultFetcher
|
||||
|
@ -40,7 +40,6 @@ export type HookFetcherFn<
|
||||
options: HookFetcherOptions
|
||||
input: Input
|
||||
fetch: <T = Result, B = Body>(options: FetcherOptions<B>) => Promise<T>
|
||||
normalize?(data: Result): Data
|
||||
}) => Data | Promise<Data>
|
||||
|
||||
export type HookFetcherOptions = { method?: string } & (
|
||||
@ -63,23 +62,18 @@ export type HookHandler<
|
||||
Input extends { [k: string]: unknown } = {},
|
||||
// Input expected before doing a fetch operation
|
||||
FetchInput extends HookFetchInput = {},
|
||||
// Data returned by the API after a fetch operation
|
||||
Result = any,
|
||||
// Body expected by the API endpoint
|
||||
Body = any,
|
||||
// Custom state added to the response object of SWR
|
||||
State = {}
|
||||
> = {
|
||||
useHook?(context: {
|
||||
input: Input & { swrOptions?: SwrOptions<Data, FetchInput, Result> }
|
||||
input: Input & { swrOptions?: SwrOptions<Data, FetchInput> }
|
||||
useData(context?: {
|
||||
input?: HookFetchInput | HookSwrInput
|
||||
swrOptions?: SwrOptions<Data, FetchInput, Result>
|
||||
swrOptions?: SwrOptions<Data, FetchInput>
|
||||
}): ResponseState<Data>
|
||||
}): ResponseState<Data> & State
|
||||
fetchOptions: HookFetcherOptions
|
||||
fetcher?: HookFetcherFn<Data, FetchInput, Result, Body>
|
||||
normalizer?(data: NonNullable<Result>): Data
|
||||
fetcher?: HookFetcherFn<Data, FetchInput>
|
||||
}
|
||||
|
||||
export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
||||
|
@ -17,17 +17,12 @@ export type ResponseState<Result> = responseInterface<Result, CommerceError> & {
|
||||
export type UseData = <
|
||||
Data = any,
|
||||
Input extends { [k: string]: unknown } = {},
|
||||
FetchInput extends HookFetchInput = {},
|
||||
Result = any,
|
||||
Body = any
|
||||
FetchInput extends HookFetchInput = {}
|
||||
>(
|
||||
options: PickRequired<
|
||||
HookHandler<Data, Input, FetchInput, Result, Body>,
|
||||
'fetcher'
|
||||
>,
|
||||
options: PickRequired<HookHandler<Data, Input, FetchInput>, 'fetcher'>,
|
||||
input: HookFetchInput | HookSwrInput,
|
||||
fetcherFn: Fetcher,
|
||||
swrOptions?: SwrOptions<Data, FetchInput, Result>
|
||||
swrOptions?: SwrOptions<Data, FetchInput>
|
||||
) => ResponseState<Data>
|
||||
|
||||
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
||||
@ -47,7 +42,6 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
||||
return obj
|
||||
}, {}),
|
||||
fetch: fetcherFn,
|
||||
normalize: options.normalizer,
|
||||
})
|
||||
} catch (error) {
|
||||
// SWR will not log errors, but any error that's not an instance
|
||||
|
Loading…
x
Reference in New Issue
Block a user