4
0
forked from crowetic/commerce

Removed generics for result and body

This commit is contained in:
Luis Alvarez 2021-02-12 13:13:08 -05:00
parent 505d3fe04b
commit b907c31ef2
5 changed files with 20 additions and 46 deletions

View File

@ -50,15 +50,16 @@ const useCart: HookHandler<
Cart | null, Cart | null,
{}, {},
FetchCartInput, FetchCartInput,
any,
any,
{ isEmpty?: boolean } { isEmpty?: boolean }
> = { > = {
fetchOptions: { fetchOptions: {
url: '/api/bigcommerce/cart', url: '/api/bigcommerce/cart',
method: 'GET', method: 'GET',
}, },
normalizer: normalizeCart, async fetcher({ input: { cartId }, options, fetch }) {
const data = cartId ? await fetch(options) : null
return data && normalizeCart(data)
},
useHook({ input, useData }) { useHook({ input, useData }) {
const response = useData({ const response = useData({
swrOptions: { revalidateOnFocus: false, ...input.swrOptions }, swrOptions: { revalidateOnFocus: false, ...input.swrOptions },
@ -83,8 +84,6 @@ const useWishlist: HookHandler<
Wishlist | null, Wishlist | null,
{ includeProducts?: boolean }, { includeProducts?: boolean },
{ customerId?: number; includeProducts: boolean }, { customerId?: number; includeProducts: boolean },
any,
any,
{ isEmpty?: boolean } { isEmpty?: boolean }
> = { > = {
fetchOptions: { fetchOptions: {
@ -132,18 +131,15 @@ const useWishlist: HookHandler<
}, },
} }
const useCustomerHandler: HookHandler< const useCustomerHandler: HookHandler<Customer | null> = {
Customer | null,
{},
{},
CustomerData | null,
any
> = {
fetchOptions: { fetchOptions: {
url: '/api/bigcommerce/customers', url: '/api/bigcommerce/customers',
method: 'GET', method: 'GET',
}, },
normalizer: (data) => data.customer, async fetcher({ options, fetch }) {
const data = await fetch<CustomerData | null>(options)
return data?.customer ?? null
},
useHook({ input, useData }) { useHook({ input, useData }) {
return useData({ return useData({
swrOptions: { swrOptions: {
@ -164,9 +160,7 @@ export type SearchProductsInput = {
const useSearch: HookHandler< const useSearch: HookHandler<
SearchProductsData, SearchProductsData,
SearchProductsInput, SearchProductsInput,
SearchProductsInput, SearchProductsInput
any,
any
> = { > = {
fetchOptions: { fetchOptions: {
url: '/api/bigcommerce/catalog/products', url: '/api/bigcommerce/catalog/products',

View File

@ -34,10 +34,8 @@ export const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
options, options,
input: { cartId }, input: { cartId },
fetch, fetch,
normalize,
}) => { }) => {
const data = cartId ? await fetch({ ...options }) : null return cartId ? await fetch({ ...options }) : null
return data && normalize ? normalize(data) : data
} }
export default function useCart<P extends Provider>( export default function useCart<P extends Provider>(

View File

@ -1,12 +1,6 @@
import { HookFetcherFn } from './types' import type { HookFetcherFn } from './types'
const defaultFetcher: HookFetcherFn<any> = async ({ const defaultFetcher: HookFetcherFn<any> = ({ options, fetch }) =>
options, fetch(options)
fetch,
normalize,
}) => {
const data = await fetch({ ...options })
return data && normalize ? normalize(data) : data
}
export default defaultFetcher export default defaultFetcher

View File

@ -40,7 +40,6 @@ export type HookFetcherFn<
options: HookFetcherOptions options: HookFetcherOptions
input: Input input: Input
fetch: <T = Result, B = Body>(options: FetcherOptions<B>) => Promise<T> fetch: <T = Result, B = Body>(options: FetcherOptions<B>) => Promise<T>
normalize?(data: Result): Data
}) => Data | Promise<Data> }) => Data | Promise<Data>
export type HookFetcherOptions = { method?: string } & ( export type HookFetcherOptions = { method?: string } & (
@ -63,23 +62,18 @@ export type HookHandler<
Input extends { [k: string]: unknown } = {}, Input extends { [k: string]: unknown } = {},
// Input expected before doing a fetch operation // Input expected before doing a fetch operation
FetchInput extends HookFetchInput = {}, 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 // Custom state added to the response object of SWR
State = {} State = {}
> = { > = {
useHook?(context: { useHook?(context: {
input: Input & { swrOptions?: SwrOptions<Data, FetchInput, Result> } input: Input & { swrOptions?: SwrOptions<Data, FetchInput> }
useData(context?: { useData(context?: {
input?: HookFetchInput | HookSwrInput input?: HookFetchInput | HookSwrInput
swrOptions?: SwrOptions<Data, FetchInput, Result> swrOptions?: SwrOptions<Data, FetchInput>
}): ResponseState<Data> }): ResponseState<Data>
}): ResponseState<Data> & State }): ResponseState<Data> & State
fetchOptions: HookFetcherOptions fetchOptions: HookFetcherOptions
fetcher?: HookFetcherFn<Data, FetchInput, Result, Body> fetcher?: HookFetcherFn<Data, FetchInput>
normalizer?(data: NonNullable<Result>): Data
} }
export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface< export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<

View File

@ -17,17 +17,12 @@ export type ResponseState<Result> = responseInterface<Result, CommerceError> & {
export type UseData = < export type UseData = <
Data = any, Data = any,
Input extends { [k: string]: unknown } = {}, Input extends { [k: string]: unknown } = {},
FetchInput extends HookFetchInput = {}, FetchInput extends HookFetchInput = {}
Result = any,
Body = any
>( >(
options: PickRequired< options: PickRequired<HookHandler<Data, Input, FetchInput>, 'fetcher'>,
HookHandler<Data, Input, FetchInput, Result, Body>,
'fetcher'
>,
input: HookFetchInput | HookSwrInput, input: HookFetchInput | HookSwrInput,
fetcherFn: Fetcher, fetcherFn: Fetcher,
swrOptions?: SwrOptions<Data, FetchInput, Result> swrOptions?: SwrOptions<Data, FetchInput>
) => ResponseState<Data> ) => ResponseState<Data>
const useData: UseData = (options, input, fetcherFn, swrOptions) => { const useData: UseData = (options, input, fetcherFn, swrOptions) => {
@ -47,7 +42,6 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => {
return obj return obj
}, {}), }, {}),
fetch: fetcherFn, fetch: fetcherFn,
normalize: options.normalizer,
}) })
} catch (error) { } catch (error) {
// SWR will not log errors, but any error that's not an instance // SWR will not log errors, but any error that's not an instance