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 { 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 { ValidationError } from '@commerce/utils/errors'
|
||||||
import useRemoveItem, {
|
import useRemoveItem, {
|
||||||
RemoveItemInput as RemoveItemInputBase,
|
RemoveItemInput as RemoveItemInputBase,
|
||||||
@ -40,7 +43,9 @@ export const handler = {
|
|||||||
})
|
})
|
||||||
return normalizeCart(data)
|
return normalizeCart(data)
|
||||||
},
|
},
|
||||||
useHook: ({ fetch }: HookContext<Cart | null, RemoveCartItemBody>) => <
|
useHook: ({
|
||||||
|
fetch,
|
||||||
|
}: MutationHookContext<Cart | null, RemoveCartItemBody>) => <
|
||||||
T extends LineItem | undefined = undefined
|
T extends LineItem | undefined = undefined
|
||||||
>(
|
>(
|
||||||
ctx: { item?: T } = {}
|
ctx: { item?: T } = {}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import debounce from 'lodash.debounce'
|
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 { ValidationError } from '@commerce/utils/errors'
|
||||||
import useUpdateItem, {
|
import useUpdateItem, {
|
||||||
UpdateItemInput as UpdateItemInputBase,
|
UpdateItemInput as UpdateItemInputBase,
|
||||||
@ -54,7 +57,9 @@ export const handler = {
|
|||||||
|
|
||||||
return normalizeCart(data)
|
return normalizeCart(data)
|
||||||
},
|
},
|
||||||
useHook: ({ fetch }: HookContext<Cart | null, UpdateCartItemBody>) => <
|
useHook: ({
|
||||||
|
fetch,
|
||||||
|
}: MutationHookContext<Cart | null, UpdateCartItemBody>) => <
|
||||||
T extends LineItem | undefined = undefined
|
T extends LineItem | undefined = undefined
|
||||||
>(
|
>(
|
||||||
ctx: {
|
ctx: {
|
||||||
|
@ -6,7 +6,7 @@ import {
|
|||||||
useMemo,
|
useMemo,
|
||||||
useRef,
|
useRef,
|
||||||
} from 'react'
|
} 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 { FetchCartInput } from './cart/use-cart'
|
||||||
import type { Cart, Wishlist, Customer, SearchProductsData } from './types'
|
import type { Cart, Wishlist, Customer, SearchProductsData } from './types'
|
||||||
|
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import { LineItem } from '@framework/types'
|
|
||||||
import type { ConfigInterface } from 'swr'
|
import type { ConfigInterface } from 'swr'
|
||||||
import type { CommerceError } from './errors'
|
import type { CommerceError } from './errors'
|
||||||
import type { ResponseState } from './use-data'
|
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
|
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 HookInputValue = string | number | boolean | undefined
|
||||||
|
|
||||||
export type HookSwrInput = [string, HookInputValue][]
|
export type HookSWRInput = [string, HookInputValue][]
|
||||||
|
|
||||||
export type HookFetchInput = { [k: 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<
|
export type HookFunction<
|
||||||
Input extends { [k: string]: unknown } | {},
|
Input extends { [k: string]: unknown } | {},
|
||||||
T
|
T
|
||||||
@ -110,7 +91,7 @@ export type SWRHookContext<
|
|||||||
FetchInput extends { [k: string]: unknown } = {}
|
FetchInput extends { [k: string]: unknown } = {}
|
||||||
> = {
|
> = {
|
||||||
useData(context?: {
|
useData(context?: {
|
||||||
input?: HookFetchInput | HookSwrInput
|
input?: HookFetchInput | HookSWRInput
|
||||||
swrOptions?: SwrOptions<Data, FetchInput>
|
swrOptions?: SwrOptions<Data, FetchInput>
|
||||||
}): ResponseState<Data>
|
}): ResponseState<Data>
|
||||||
}
|
}
|
||||||
@ -126,13 +107,13 @@ export type MutationHook<
|
|||||||
FetchInput extends { [k: string]: unknown } = ActionInput
|
FetchInput extends { [k: string]: unknown } = ActionInput
|
||||||
> = {
|
> = {
|
||||||
useHook(
|
useHook(
|
||||||
context: HookContext<Data, FetchInput>
|
context: MutationHookContext<Data, FetchInput>
|
||||||
): HookFunction<Input, HookFunction<ActionInput, Data | Promise<Data>>>
|
): HookFunction<Input, HookFunction<ActionInput, Data | Promise<Data>>>
|
||||||
fetchOptions: HookFetcherOptions
|
fetchOptions: HookFetcherOptions
|
||||||
fetcher?: HookFetcherFn<Data, FetchInput>
|
fetcher?: HookFetcherFn<Data, FetchInput>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type HookContext<
|
export type MutationHookContext<
|
||||||
Data,
|
Data,
|
||||||
FetchInput extends { [k: string]: unknown } = {}
|
FetchInput extends { [k: string]: unknown } = {}
|
||||||
> = {
|
> = {
|
||||||
@ -144,22 +125,3 @@ export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
|||||||
CommerceError,
|
CommerceError,
|
||||||
HookFetcher<Data, Input, Result>
|
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 useSWR, { responseInterface } from 'swr'
|
||||||
import type {
|
import type {
|
||||||
HookSwrInput,
|
HookSWRInput,
|
||||||
HookFetchInput,
|
HookFetchInput,
|
||||||
Fetcher,
|
Fetcher,
|
||||||
SwrOptions,
|
SwrOptions,
|
||||||
@ -23,7 +23,7 @@ export type UseData = <
|
|||||||
fetchOptions: HookFetcherOptions
|
fetchOptions: HookFetcherOptions
|
||||||
fetcher: HookFetcherFn<Data, FetchInput>
|
fetcher: HookFetcherFn<Data, FetchInput>
|
||||||
},
|
},
|
||||||
input: HookFetchInput | HookSwrInput,
|
input: HookFetchInput | HookSWRInput,
|
||||||
fetcherFn: Fetcher,
|
fetcherFn: Fetcher,
|
||||||
swrOptions?: SwrOptions<Data, FetchInput>
|
swrOptions?: SwrOptions<Data, FetchInput>
|
||||||
) => ResponseState<Data>
|
) => ResponseState<Data>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user