forked from crowetic/commerce
Removed no longer required types
This commit is contained in:
parent
6721ef736b
commit
8fc549bf55
@ -1,5 +1,8 @@
|
||||
import { useCallback } from 'react'
|
||||
import { HookContext, HookFetcherContext } from '@commerce/utils/types'
|
||||
import type {
|
||||
MutationHookContext,
|
||||
HookFetcherContext,
|
||||
} from '@commerce/utils/types'
|
||||
import { ValidationError } from '@commerce/utils/errors'
|
||||
import useRemoveItem, {
|
||||
RemoveItemInput as RemoveItemInputBase,
|
||||
@ -40,7 +43,9 @@ export const handler = {
|
||||
})
|
||||
return normalizeCart(data)
|
||||
},
|
||||
useHook: ({ fetch }: HookContext<Cart | null, RemoveCartItemBody>) => <
|
||||
useHook: ({
|
||||
fetch,
|
||||
}: MutationHookContext<Cart | null, RemoveCartItemBody>) => <
|
||||
T extends LineItem | undefined = undefined
|
||||
>(
|
||||
ctx: { item?: T } = {}
|
||||
|
@ -1,6 +1,9 @@
|
||||
import { useCallback } from 'react'
|
||||
import debounce from 'lodash.debounce'
|
||||
import type { HookContext, HookFetcherContext } from '@commerce/utils/types'
|
||||
import type {
|
||||
MutationHookContext,
|
||||
HookFetcherContext,
|
||||
} from '@commerce/utils/types'
|
||||
import { ValidationError } from '@commerce/utils/errors'
|
||||
import useUpdateItem, {
|
||||
UpdateItemInput as UpdateItemInputBase,
|
||||
@ -54,7 +57,9 @@ export const handler = {
|
||||
|
||||
return normalizeCart(data)
|
||||
},
|
||||
useHook: ({ fetch }: HookContext<Cart | null, UpdateCartItemBody>) => <
|
||||
useHook: ({
|
||||
fetch,
|
||||
}: MutationHookContext<Cart | null, UpdateCartItemBody>) => <
|
||||
T extends LineItem | undefined = undefined
|
||||
>(
|
||||
ctx: {
|
||||
|
@ -6,7 +6,7 @@ import {
|
||||
useMemo,
|
||||
useRef,
|
||||
} from 'react'
|
||||
import { Fetcher, HookHandler, SWRHook, MutationHook } from './utils/types'
|
||||
import { Fetcher, SWRHook, MutationHook } from './utils/types'
|
||||
import type { FetchCartInput } from './cart/use-cart'
|
||||
import type { Cart, Wishlist, Customer, SearchProductsData } from './types'
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { LineItem } from '@framework/types'
|
||||
import type { ConfigInterface } from 'swr'
|
||||
import type { CommerceError } from './errors'
|
||||
import type { ResponseState } from './use-data'
|
||||
|
||||
/**
|
||||
* Returns the properties in T with the properties in type K, overriding properties defined in T
|
||||
*/
|
||||
export type Override<T, K> = Omit<T, keyof K> & K
|
||||
|
||||
/**
|
||||
@ -51,31 +53,10 @@ export type HookFetcherOptions = { method?: string } & (
|
||||
|
||||
export type HookInputValue = string | number | boolean | undefined
|
||||
|
||||
export type HookSwrInput = [string, HookInputValue][]
|
||||
export type HookSWRInput = [string, HookInputValue][]
|
||||
|
||||
export type HookFetchInput = { [k: string]: HookInputValue }
|
||||
|
||||
export type HookHandler<
|
||||
// Data obj returned by the hook and fetch operation
|
||||
Data,
|
||||
// Input expected by the hook
|
||||
Input extends { [k: string]: unknown } = {},
|
||||
// Input expected before doing a fetch operation
|
||||
FetchInput extends HookFetchInput = {},
|
||||
// Custom state added to the response object of SWR
|
||||
State = {}
|
||||
> = {
|
||||
useHook?(context: {
|
||||
input: Input & { swrOptions?: SwrOptions<Data, FetchInput> }
|
||||
useData(context?: {
|
||||
input?: HookFetchInput | HookSwrInput
|
||||
swrOptions?: SwrOptions<Data, FetchInput>
|
||||
}): ResponseState<Data>
|
||||
}): ResponseState<Data> & State
|
||||
fetchOptions: HookFetcherOptions
|
||||
fetcher?: HookFetcherFn<Data, FetchInput>
|
||||
}
|
||||
|
||||
export type HookFunction<
|
||||
Input extends { [k: string]: unknown } | {},
|
||||
T
|
||||
@ -110,7 +91,7 @@ export type SWRHookContext<
|
||||
FetchInput extends { [k: string]: unknown } = {}
|
||||
> = {
|
||||
useData(context?: {
|
||||
input?: HookFetchInput | HookSwrInput
|
||||
input?: HookFetchInput | HookSWRInput
|
||||
swrOptions?: SwrOptions<Data, FetchInput>
|
||||
}): ResponseState<Data>
|
||||
}
|
||||
@ -126,13 +107,13 @@ export type MutationHook<
|
||||
FetchInput extends { [k: string]: unknown } = ActionInput
|
||||
> = {
|
||||
useHook(
|
||||
context: HookContext<Data, FetchInput>
|
||||
context: MutationHookContext<Data, FetchInput>
|
||||
): HookFunction<Input, HookFunction<ActionInput, Data | Promise<Data>>>
|
||||
fetchOptions: HookFetcherOptions
|
||||
fetcher?: HookFetcherFn<Data, FetchInput>
|
||||
}
|
||||
|
||||
export type HookContext<
|
||||
export type MutationHookContext<
|
||||
Data,
|
||||
FetchInput extends { [k: string]: unknown } = {}
|
||||
> = {
|
||||
@ -144,22 +125,3 @@ export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
||||
CommerceError,
|
||||
HookFetcher<Data, Input, Result>
|
||||
>
|
||||
|
||||
/**
|
||||
* Returns the property K from type T excluding nullables
|
||||
*/
|
||||
export type Prop<T, K extends keyof T> = NonNullable<T[K]>
|
||||
|
||||
export type HookHandlerType = HookHandler<any, any, any>
|
||||
|
||||
export type UseHookParameters<H extends HookHandlerType> = Parameters<
|
||||
Prop<H, 'useHook'>
|
||||
>
|
||||
|
||||
export type UseHookResponse<H extends HookHandlerType> = ReturnType<
|
||||
Prop<H, 'useHook'>
|
||||
>
|
||||
|
||||
export type UseHookInput<
|
||||
H extends HookHandlerType
|
||||
> = UseHookParameters<H>[0]['input']
|
||||
|
@ -1,6 +1,6 @@
|
||||
import useSWR, { responseInterface } from 'swr'
|
||||
import type {
|
||||
HookSwrInput,
|
||||
HookSWRInput,
|
||||
HookFetchInput,
|
||||
Fetcher,
|
||||
SwrOptions,
|
||||
@ -23,7 +23,7 @@ export type UseData = <
|
||||
fetchOptions: HookFetcherOptions
|
||||
fetcher: HookFetcherFn<Data, FetchInput>
|
||||
},
|
||||
input: HookFetchInput | HookSwrInput,
|
||||
input: HookFetchInput | HookSWRInput,
|
||||
fetcherFn: Fetcher,
|
||||
swrOptions?: SwrOptions<Data, FetchInput>
|
||||
) => ResponseState<Data>
|
||||
|
Loading…
x
Reference in New Issue
Block a user