mirror of
https://github.com/vercel/commerce.git
synced 2025-05-02 07:47:50 +00:00
Moved types
This commit is contained in:
parent
17b336017c
commit
74c55cc50e
@ -5,10 +5,10 @@ import {
|
|||||||
CommerceConfig,
|
CommerceConfig,
|
||||||
CommerceProvider as CoreCommerceProvider,
|
CommerceProvider as CoreCommerceProvider,
|
||||||
useCommerce as useCoreCommerce,
|
useCommerce as useCoreCommerce,
|
||||||
HookHandler,
|
|
||||||
} from '@commerce'
|
} from '@commerce'
|
||||||
import { FetcherError } from '@commerce/utils/errors'
|
import { FetcherError } from '@commerce/utils/errors'
|
||||||
import type { CartInput } from '@commerce/cart/use-cart'
|
import type { HookHandler } from '@commerce/utils/types'
|
||||||
|
import type { FetchCartInput } from '@commerce/cart/use-cart'
|
||||||
import { normalizeCart } from './lib/normalize'
|
import { normalizeCart } from './lib/normalize'
|
||||||
import { Cart } from './types'
|
import { Cart } from './types'
|
||||||
|
|
||||||
@ -51,7 +51,8 @@ const fetcher: Fetcher<any> = async ({
|
|||||||
|
|
||||||
const useCart: HookHandler<
|
const useCart: HookHandler<
|
||||||
Cart | null,
|
Cart | null,
|
||||||
CartInput,
|
[],
|
||||||
|
FetchCartInput,
|
||||||
any,
|
any,
|
||||||
any,
|
any,
|
||||||
{ isEmpty?: boolean }
|
{ isEmpty?: boolean }
|
||||||
@ -63,9 +64,6 @@ const useCart: HookHandler<
|
|||||||
swrOptions: {
|
swrOptions: {
|
||||||
revalidateOnFocus: false,
|
revalidateOnFocus: false,
|
||||||
},
|
},
|
||||||
// fetcher(context) {
|
|
||||||
// return undefined as any
|
|
||||||
// },
|
|
||||||
normalizer: normalizeCart,
|
normalizer: normalizeCart,
|
||||||
onResponse(response) {
|
onResponse(response) {
|
||||||
return Object.create(response, {
|
return Object.create(response, {
|
||||||
|
@ -6,7 +6,7 @@ import useData from '../utils/use-data-2'
|
|||||||
import { Provider, useCommerce } from '..'
|
import { Provider, useCommerce } from '..'
|
||||||
|
|
||||||
// Input expected by the `useCart` hook
|
// Input expected by the `useCart` hook
|
||||||
export type CartInput = {
|
export type FetchCartInput = {
|
||||||
cartId?: Cart['id']
|
cartId?: Cart['id']
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,9 +14,15 @@ export type CartResponse<P extends Provider> = ReturnType<
|
|||||||
NonNullable<NonNullable<NonNullable<P['cart']>['useCart']>['onResponse']>
|
NonNullable<NonNullable<NonNullable<P['cart']>['useCart']>['onResponse']>
|
||||||
>
|
>
|
||||||
|
|
||||||
export type UseCart<P extends Provider> = () => CartResponse<P>
|
export type UseCart<P extends Provider> = (
|
||||||
|
...input: UseCartInput<P>
|
||||||
|
) => CartResponse<P>
|
||||||
|
|
||||||
export const fetcher: HookFetcherFn<Cart | null, CartInput> = async ({
|
export type UseCartInput<P extends Provider> = NonNullable<
|
||||||
|
NonNullable<NonNullable<NonNullable<P['cart']>['useCart']>>['input']
|
||||||
|
>
|
||||||
|
|
||||||
|
export const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
||||||
options,
|
options,
|
||||||
input: { cartId },
|
input: { cartId },
|
||||||
fetch,
|
fetch,
|
||||||
@ -26,7 +32,7 @@ export const fetcher: HookFetcherFn<Cart | null, CartInput> = async ({
|
|||||||
return data && normalize ? normalize(data) : data
|
return data && normalize ? normalize(data) : data
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function useCart<P extends Provider>() {
|
export default function useCart<P extends Provider>(...input: UseCartInput<P>) {
|
||||||
const { providerRef, cartCookie } = useCommerce<P>()
|
const { providerRef, cartCookie } = useCommerce<P>()
|
||||||
|
|
||||||
const provider = providerRef.current
|
const provider = providerRef.current
|
||||||
@ -36,7 +42,7 @@ export default function useCart<P extends Provider>() {
|
|||||||
context.input.cartId = Cookies.get(cartCookie)
|
context.input.cartId = Cookies.get(cartCookie)
|
||||||
return fetcherFn(context)
|
return fetcherFn(context)
|
||||||
}
|
}
|
||||||
const response = useData(opts!, [], wrapper, opts?.swrOptions)
|
const response = useData(opts!, input, wrapper, opts?.swrOptions)
|
||||||
const memoizedResponse = useMemo(
|
const memoizedResponse = useMemo(
|
||||||
() => (opts?.onResponse ? opts.onResponse(response) : response),
|
() => (opts?.onResponse ? opts.onResponse(response) : response),
|
||||||
[response]
|
[response]
|
||||||
|
@ -7,27 +7,16 @@ import {
|
|||||||
useRef,
|
useRef,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import * as React from 'react'
|
import * as React from 'react'
|
||||||
import { Fetcher, HookFetcherFn, HookFetcherOptions } from './utils/types'
|
import { Fetcher, HookHandler } from './utils/types'
|
||||||
import { Cart } from './types'
|
import { Cart } from './types'
|
||||||
import type { ResponseState, SwrOptions } from './utils/use-data'
|
import type { FetchCartInput } from './cart/use-cart'
|
||||||
import type { CartInput } from './cart/use-cart'
|
|
||||||
|
|
||||||
const Commerce = createContext<CommerceContextValue<any> | {}>({})
|
const Commerce = createContext<CommerceContextValue<any> | {}>({})
|
||||||
|
|
||||||
export type Provider = CommerceConfig & {
|
export type Provider = CommerceConfig & {
|
||||||
cart?: {
|
cart?: {
|
||||||
useCart?: HookHandler<Cart | null, CartInput>
|
useCart?: HookHandler<Cart | null, [...any], FetchCartInput>
|
||||||
}
|
}
|
||||||
cartNormalizer(data: any): Cart
|
|
||||||
}
|
|
||||||
|
|
||||||
export type HookHandler<Data, Input, Result = any, Body = any, State = {}> = {
|
|
||||||
swrOptions?: SwrOptions<Data, Input, Result>
|
|
||||||
onResponse?(response: ResponseState<Data>): ResponseState<Data> & State
|
|
||||||
onMutation?: any
|
|
||||||
fetchOptions?: HookFetcherOptions
|
|
||||||
fetcher?: HookFetcherFn<Data, Input, Result, Body>
|
|
||||||
normalizer?(data: Result): Data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CommerceProps<P extends Provider> = {
|
export type CommerceProps<P extends Provider> = {
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
import type { ResponseState, SwrOptions } from './use-data'
|
||||||
|
|
||||||
|
export type Override<T, K> = Omit<T, keyof K> & K
|
||||||
|
|
||||||
// Core fetcher added by CommerceProvider
|
// Core fetcher added by CommerceProvider
|
||||||
export type Fetcher<T> = (options: FetcherOptions) => T | Promise<T>
|
export type Fetcher<T> = (options: FetcherOptions) => T | Promise<T>
|
||||||
|
|
||||||
@ -15,7 +19,12 @@ export type HookFetcher<Data, Input = null, Result = any> = (
|
|||||||
fetch: <T = Result, Body = any>(options: FetcherOptions<Body>) => Promise<T>
|
fetch: <T = Result, Body = any>(options: FetcherOptions<Body>) => Promise<T>
|
||||||
) => Data | Promise<Data>
|
) => Data | Promise<Data>
|
||||||
|
|
||||||
export type HookFetcherFn<Data, Input, Result = any, Body = any> = (context: {
|
export type HookFetcherFn<
|
||||||
|
Data,
|
||||||
|
Input = unknown,
|
||||||
|
Result = any,
|
||||||
|
Body = any
|
||||||
|
> = (context: {
|
||||||
options: HookFetcherOptions | null
|
options: HookFetcherOptions | null
|
||||||
input: Input
|
input: Input
|
||||||
fetch: <T = Result, B = Body>(options: FetcherOptions<B>) => Promise<T>
|
fetch: <T = Result, B = Body>(options: FetcherOptions<B>) => Promise<T>
|
||||||
@ -30,4 +39,25 @@ export type HookFetcherOptions = {
|
|||||||
|
|
||||||
export type HookInput = [string, string | number | boolean | undefined][]
|
export type HookInput = [string, string | number | boolean | undefined][]
|
||||||
|
|
||||||
export type Override<T, K> = Omit<T, keyof K> & K
|
export type HookHandler<
|
||||||
|
// Data obj returned by the hook and fetch operation
|
||||||
|
Data,
|
||||||
|
// Input expected by the hook
|
||||||
|
Input = [...any],
|
||||||
|
// Input expected before doing a fetch operation
|
||||||
|
FetchInput = unknown,
|
||||||
|
// 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 = {}
|
||||||
|
> = {
|
||||||
|
input?: Input
|
||||||
|
swrOptions?: SwrOptions<Data, FetchInput, Result>
|
||||||
|
onResponse?(response: ResponseState<Data>): ResponseState<Data> & State
|
||||||
|
onMutation?: any
|
||||||
|
fetchOptions?: HookFetcherOptions
|
||||||
|
fetcher?: HookFetcherFn<Data, FetchInput, Result, Body>
|
||||||
|
normalizer?(data: Result): Data
|
||||||
|
}
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
import useSWR, { ConfigInterface, responseInterface } from 'swr'
|
import useSWR, { ConfigInterface, responseInterface } from 'swr'
|
||||||
import type { HookInput, HookFetcher, HookFetcherFn } from './types'
|
import type {
|
||||||
|
HookHandler,
|
||||||
|
HookInput,
|
||||||
|
HookFetcher,
|
||||||
|
HookFetcherFn,
|
||||||
|
} from './types'
|
||||||
import defineProperty from './define-property'
|
import defineProperty from './define-property'
|
||||||
import { CommerceError } from './errors'
|
import { CommerceError } from './errors'
|
||||||
import { HookHandler, useCommerce } from '..'
|
import { useCommerce } from '..'
|
||||||
|
|
||||||
export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
||||||
Data,
|
Data,
|
||||||
@ -14,11 +19,17 @@ export type ResponseState<Result> = responseInterface<Result, CommerceError> & {
|
|||||||
isLoading: boolean
|
isLoading: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UseData = <Data = any, Input = null, Result = any>(
|
export type UseData = <
|
||||||
options: HookHandler<Data, Input, Result>,
|
Data = any,
|
||||||
|
Input = [...any],
|
||||||
|
FetchInput = unknown,
|
||||||
|
Result = any,
|
||||||
|
Body = any
|
||||||
|
>(
|
||||||
|
options: HookHandler<Data, Input, FetchInput, Result, Body>,
|
||||||
input: HookInput,
|
input: HookInput,
|
||||||
fetcherFn: HookFetcherFn<Data, Input, Result>,
|
fetcherFn: HookFetcherFn<Data, FetchInput, Result, Body>,
|
||||||
swrOptions?: SwrOptions<Data, Input, Result>
|
swrOptions?: SwrOptions<Data, FetchInput, Result>
|
||||||
) => ResponseState<Data>
|
) => ResponseState<Data>
|
||||||
|
|
||||||
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user