mirror of
https://github.com/vercel/commerce.git
synced 2025-05-01 23:37:56 +00:00
Removed old use-data lib
This commit is contained in:
parent
1549368c88
commit
505d3fe04b
@ -6,7 +6,7 @@ import type {
|
|||||||
UseHookInput,
|
UseHookInput,
|
||||||
UseHookResponse,
|
UseHookResponse,
|
||||||
} from '../utils/types'
|
} from '../utils/types'
|
||||||
import useData from '../utils/use-data-2'
|
import useData from '../utils/use-data'
|
||||||
import { Provider, useCommerce } from '..'
|
import { Provider, useCommerce } from '..'
|
||||||
|
|
||||||
export type FetchCartInput = {
|
export type FetchCartInput = {
|
||||||
|
@ -6,7 +6,7 @@ import type {
|
|||||||
UseHookResponse,
|
UseHookResponse,
|
||||||
} from '../utils/types'
|
} from '../utils/types'
|
||||||
import defaultFetcher from '../utils/default-fetcher'
|
import defaultFetcher from '../utils/default-fetcher'
|
||||||
import useData from '../utils/use-data-2'
|
import useData from '../utils/use-data'
|
||||||
import { Provider, useCommerce } from '..'
|
import { Provider, useCommerce } from '..'
|
||||||
|
|
||||||
export type UseCustomerHandler<P extends Provider> = Prop<
|
export type UseCustomerHandler<P extends Provider> = Prop<
|
||||||
|
@ -6,7 +6,7 @@ import type {
|
|||||||
UseHookResponse,
|
UseHookResponse,
|
||||||
} from '../utils/types'
|
} from '../utils/types'
|
||||||
import defaultFetcher from '../utils/default-fetcher'
|
import defaultFetcher from '../utils/default-fetcher'
|
||||||
import useData from '../utils/use-data-2'
|
import useData from '../utils/use-data'
|
||||||
import { Provider, useCommerce } from '..'
|
import { Provider, useCommerce } from '..'
|
||||||
import { BigcommerceProvider } from '@framework'
|
import { BigcommerceProvider } from '@framework'
|
||||||
|
|
||||||
|
@ -1,84 +0,0 @@
|
|||||||
import useSWR, { responseInterface } from 'swr'
|
|
||||||
import type {
|
|
||||||
HookHandler,
|
|
||||||
HookSwrInput,
|
|
||||||
HookFetchInput,
|
|
||||||
PickRequired,
|
|
||||||
Fetcher,
|
|
||||||
SwrOptions,
|
|
||||||
} from './types'
|
|
||||||
import defineProperty from './define-property'
|
|
||||||
import { CommerceError } from './errors'
|
|
||||||
|
|
||||||
export type ResponseState<Result> = responseInterface<Result, CommerceError> & {
|
|
||||||
isLoading: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export type UseData = <
|
|
||||||
Data = any,
|
|
||||||
Input extends { [k: string]: unknown } = {},
|
|
||||||
FetchInput extends HookFetchInput = {},
|
|
||||||
Result = any,
|
|
||||||
Body = any
|
|
||||||
>(
|
|
||||||
options: PickRequired<
|
|
||||||
HookHandler<Data, Input, FetchInput, Result, Body>,
|
|
||||||
'fetcher'
|
|
||||||
>,
|
|
||||||
input: HookFetchInput | HookSwrInput,
|
|
||||||
fetcherFn: Fetcher,
|
|
||||||
swrOptions?: SwrOptions<Data, FetchInput, Result>
|
|
||||||
) => ResponseState<Data>
|
|
||||||
|
|
||||||
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
|
||||||
const hookInput = Array.isArray(input) ? input : Object.entries(input)
|
|
||||||
const fetcher = async (
|
|
||||||
url: string,
|
|
||||||
query?: string,
|
|
||||||
method?: string,
|
|
||||||
...args: any[]
|
|
||||||
) => {
|
|
||||||
try {
|
|
||||||
return await options.fetcher({
|
|
||||||
options: { url, query, method },
|
|
||||||
// Transform the input array into an object
|
|
||||||
input: args.reduce((obj, val, i) => {
|
|
||||||
obj[hookInput[i][0]!] = val
|
|
||||||
return obj
|
|
||||||
}, {}),
|
|
||||||
fetch: fetcherFn,
|
|
||||||
normalize: options.normalizer,
|
|
||||||
})
|
|
||||||
} catch (error) {
|
|
||||||
// SWR will not log errors, but any error that's not an instance
|
|
||||||
// of CommerceError is not welcomed by this hook
|
|
||||||
if (!(error instanceof CommerceError)) {
|
|
||||||
console.error(error)
|
|
||||||
}
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const response = useSWR(
|
|
||||||
() => {
|
|
||||||
const opts = options.fetchOptions
|
|
||||||
return opts
|
|
||||||
? [opts.url, opts.query, opts.method, ...hookInput.map((e) => e[1])]
|
|
||||||
: null
|
|
||||||
},
|
|
||||||
fetcher,
|
|
||||||
swrOptions
|
|
||||||
)
|
|
||||||
|
|
||||||
if (!('isLoading' in response)) {
|
|
||||||
defineProperty(response, 'isLoading', {
|
|
||||||
get() {
|
|
||||||
return response.data === undefined
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return response
|
|
||||||
}
|
|
||||||
|
|
||||||
export default useData
|
|
@ -1,44 +1,54 @@
|
|||||||
import useSWR, { ConfigInterface, responseInterface } from 'swr'
|
import useSWR, { responseInterface } from 'swr'
|
||||||
import type { HookSwrInput, HookFetcher, HookFetcherOptions } from './types'
|
import type {
|
||||||
|
HookHandler,
|
||||||
|
HookSwrInput,
|
||||||
|
HookFetchInput,
|
||||||
|
PickRequired,
|
||||||
|
Fetcher,
|
||||||
|
SwrOptions,
|
||||||
|
} from './types'
|
||||||
import defineProperty from './define-property'
|
import defineProperty from './define-property'
|
||||||
import { CommerceError } from './errors'
|
import { CommerceError } from './errors'
|
||||||
import { useCommerce } from '..'
|
|
||||||
|
|
||||||
export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
|
||||||
Data,
|
|
||||||
CommerceError,
|
|
||||||
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 = <Data = any, Input = null, Result = any>(
|
export type UseData = <
|
||||||
options: HookFetcherOptions | (() => HookFetcherOptions | null),
|
Data = any,
|
||||||
input: HookSwrInput,
|
Input extends { [k: string]: unknown } = {},
|
||||||
fetcherFn: HookFetcher<Data, Input, Result>,
|
FetchInput extends HookFetchInput = {},
|
||||||
swrOptions?: SwrOptions<Data, Input, Result>
|
Result = any,
|
||||||
|
Body = any
|
||||||
|
>(
|
||||||
|
options: PickRequired<
|
||||||
|
HookHandler<Data, Input, FetchInput, Result, Body>,
|
||||||
|
'fetcher'
|
||||||
|
>,
|
||||||
|
input: HookFetchInput | HookSwrInput,
|
||||||
|
fetcherFn: Fetcher,
|
||||||
|
swrOptions?: SwrOptions<Data, FetchInput, Result>
|
||||||
) => ResponseState<Data>
|
) => ResponseState<Data>
|
||||||
|
|
||||||
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
||||||
const { fetcherRef } = useCommerce()
|
const hookInput = Array.isArray(input) ? input : Object.entries(input)
|
||||||
const fetcher = async (
|
const fetcher = async (
|
||||||
url?: string,
|
url: string,
|
||||||
query?: string,
|
query?: string,
|
||||||
method?: string,
|
method?: string,
|
||||||
...args: any[]
|
...args: any[]
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
return await fetcherFn(
|
return await options.fetcher({
|
||||||
{ url, query, method },
|
options: { url, query, method },
|
||||||
// Transform the input array into an object
|
// Transform the input array into an object
|
||||||
args.reduce((obj, val, i) => {
|
input: args.reduce((obj, val, i) => {
|
||||||
obj[input[i][0]!] = val
|
obj[hookInput[i][0]!] = val
|
||||||
return obj
|
return obj
|
||||||
}, {}),
|
}, {}),
|
||||||
fetcherRef.current
|
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
|
||||||
// of CommerceError is not welcomed by this hook
|
// of CommerceError is not welcomed by this hook
|
||||||
@ -50,9 +60,9 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
|||||||
}
|
}
|
||||||
const response = useSWR(
|
const response = useSWR(
|
||||||
() => {
|
() => {
|
||||||
const opts = typeof options === 'function' ? options() : options
|
const opts = options.fetchOptions
|
||||||
return opts
|
return opts
|
||||||
? [opts.url, opts.query, opts.method, ...input.map((e) => e[1])]
|
? [opts.url, opts.query, opts.method, ...hookInput.map((e) => e[1])]
|
||||||
: null
|
: null
|
||||||
},
|
},
|
||||||
fetcher,
|
fetcher,
|
||||||
|
@ -6,7 +6,7 @@ import type {
|
|||||||
UseHookResponse,
|
UseHookResponse,
|
||||||
} from '../utils/types'
|
} from '../utils/types'
|
||||||
import defaultFetcher from '../utils/default-fetcher'
|
import defaultFetcher from '../utils/default-fetcher'
|
||||||
import useData from '../utils/use-data-2'
|
import useData from '../utils/use-data'
|
||||||
import { Provider, useCommerce } from '..'
|
import { Provider, useCommerce } from '..'
|
||||||
|
|
||||||
export type UseWishlistHandler<P extends Provider> = Prop<
|
export type UseWishlistHandler<P extends Provider> = Prop<
|
||||||
|
Loading…
x
Reference in New Issue
Block a user