4
0
forked from crowetic/commerce
commerce/lib/commerce/utils/use-data.tsx
2020-10-15 12:12:28 -03:00

34 lines
902 B
TypeScript

import useSWR, { ConfigInterface } from 'swr'
import type { HookInput, HookFetcher, HookFetcherOptions } from './types'
import { useCommerce } from '..'
export default function useData<T, Input = any>(
options: HookFetcherOptions | (() => HookFetcherOptions | null),
input: HookInput,
fetcherFn: HookFetcher<T, Input>,
swrOptions?: ConfigInterface<T>
) {
const { fetcherRef } = useCommerce()
const fetcher = (url?: string, query?: string, ...args: any[]) => {
return fetcherFn(
{ url, query },
args.reduce((obj, val, i) => {
obj[input[i][0]!] = val
return obj
}, {}),
fetcherRef.current
)
}
const response = useSWR(
() => {
const opts = typeof options === 'function' ? options() : options
return opts ? [opts.url, opts.query, ...input.map((e) => e[1])] : null
},
fetcher,
swrOptions
)
return response
}