mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Updated types for hooks now using the API
This commit is contained in:
parent
d3fe16e1d7
commit
8ad6a34175
@ -1,17 +1,12 @@
|
||||
import { useMemo } from 'react'
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import useCart, { UseCart, FetchCartInput } from '@commerce/cart/use-cart'
|
||||
import useCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import type { Cart } from '../types'
|
||||
import type { GetCartHook } from '../types/cart'
|
||||
|
||||
export default useCart as UseCart<typeof handler>
|
||||
|
||||
export const handler: SWRHook<
|
||||
Cart | null,
|
||||
{},
|
||||
FetchCartInput,
|
||||
{ isEmpty?: boolean }
|
||||
> = {
|
||||
export const handler: SWRHook<GetCartHook> = {
|
||||
fetchOptions: {
|
||||
url: '/api/bigcommerce/cart',
|
||||
method: 'GET',
|
||||
|
@ -1,17 +1,14 @@
|
||||
import { useHook, useMutationHook } from '../utils/use-hook'
|
||||
import { mutationFetcher } from '../utils/default-fetcher'
|
||||
import type { HookFetcherFn, MutationHook } from '../utils/types'
|
||||
import type { Cart, CartItemBody, AddCartItemBody } from '../types'
|
||||
import type { AddItemHook } from '../types/cart'
|
||||
import type { Provider } from '..'
|
||||
|
||||
export type UseAddItem<
|
||||
H extends MutationHook<any, any, any> = MutationHook<Cart, {}, CartItemBody>
|
||||
H extends MutationHook<AddItemHook> = MutationHook<AddItemHook>
|
||||
> = ReturnType<H['useHook']>
|
||||
|
||||
export const fetcher: HookFetcherFn<
|
||||
Cart,
|
||||
AddCartItemBody<CartItemBody>
|
||||
> = mutationFetcher
|
||||
export const fetcher: HookFetcherFn<AddItemHook> = mutationFetcher
|
||||
|
||||
const fn = (provider: Provider) => provider.cart?.useAddItem!
|
||||
|
||||
|
@ -1,23 +1,14 @@
|
||||
import Cookies from 'js-cookie'
|
||||
import { useHook, useSWRHook } from '../utils/use-hook'
|
||||
import type { HookFetcherFn, SWRHook } from '../utils/types'
|
||||
import type { Cart } from '../types/cart'
|
||||
import type { SWRHook, HookFetcherFn } from '../utils/types'
|
||||
import type { GetCartHook } from '../types/cart'
|
||||
import { Provider, useCommerce } from '..'
|
||||
|
||||
export type FetchCartInput = {
|
||||
cartId?: Cart['id']
|
||||
}
|
||||
|
||||
export type UseCart<
|
||||
H extends SWRHook<any, any, any> = SWRHook<
|
||||
Cart | null,
|
||||
{},
|
||||
FetchCartInput,
|
||||
{ isEmpty?: boolean }
|
||||
>
|
||||
H extends SWRHook<GetCartHook> = SWRHook<GetCartHook>
|
||||
> = ReturnType<H['useHook']>
|
||||
|
||||
export const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
||||
export const fetcher: HookFetcherFn<GetCartHook> = async ({
|
||||
options,
|
||||
input: { cartId },
|
||||
fetch,
|
||||
|
@ -94,11 +94,16 @@ export type CartHooks = {
|
||||
|
||||
export type GetCartHook = {
|
||||
data: Cart | null
|
||||
input: {}
|
||||
fetchInput: { cartId?: string }
|
||||
swrState: { isEmpty: boolean }
|
||||
}
|
||||
|
||||
export type AddItemHook = {
|
||||
data: Cart
|
||||
body: { item: CartItemBody }
|
||||
input: CartItemBody
|
||||
fetchInput: { item: CartItemBody }
|
||||
}
|
||||
|
||||
export type UpdateItemHook = {
|
||||
|
@ -36,9 +36,13 @@ export type HookFetcher<Data, Input = null, Result = any> = (
|
||||
fetch: <T = Result, Body = any>(options: FetcherOptions<Body>) => Promise<T>
|
||||
) => Data | Promise<Data>
|
||||
|
||||
export type HookFetcherFn<Data, Input = undefined, Result = any, Body = any> = (
|
||||
context: HookFetcherContext<Input, Result, Body>
|
||||
) => Data | Promise<Data>
|
||||
export type HookFetcherFn<
|
||||
H extends HookSchemaBase,
|
||||
Result = any,
|
||||
Body = any
|
||||
> = (
|
||||
context: HookFetcherContext<H['fetchInput'], Result, Body>
|
||||
) => H['data'] | Promise<H['data']>
|
||||
|
||||
export type HookFetcherContext<Input = undefined, Result = any, Body = any> = {
|
||||
options: HookFetcherOptions
|
||||
@ -58,7 +62,7 @@ export type HookSWRInput = [string, HookInputValue][]
|
||||
export type HookFetchInput = { [k: string]: HookInputValue }
|
||||
|
||||
export type HookFunction<
|
||||
Input extends { [k: string]: unknown } | null,
|
||||
Input extends { [k: string]: unknown } | undefined,
|
||||
T
|
||||
> = keyof Input extends never
|
||||
? () => T
|
||||
@ -66,62 +70,68 @@ export type HookFunction<
|
||||
? (input?: Input) => T
|
||||
: (input: Input) => T
|
||||
|
||||
export type SWRHook<
|
||||
export type HookSchemaBase = {
|
||||
// Data obj returned by the hook and fetch operation
|
||||
Data,
|
||||
data: any
|
||||
// Input expected by the hook
|
||||
Input extends { [k: string]: unknown } = {},
|
||||
input: {}
|
||||
// Input expected before doing a fetch operation
|
||||
FetchInput extends HookFetchInput = {},
|
||||
fetchInput?: {}
|
||||
}
|
||||
|
||||
export type SWRHookSchemaBase = HookSchemaBase & {
|
||||
// Custom state added to the response object of SWR
|
||||
State = {}
|
||||
> = {
|
||||
swrState: {}
|
||||
}
|
||||
|
||||
export type MutationSchemaBase = HookSchemaBase & {
|
||||
// Input expected by the action returned by the hook
|
||||
actionInput?: {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a SWR hook handler based on the schema of a hook
|
||||
*/
|
||||
export type SWRHook<H extends SWRHookSchemaBase> = {
|
||||
useHook(
|
||||
context: SWRHookContext<Data, FetchInput>
|
||||
context: SWRHookContext<H>
|
||||
): HookFunction<
|
||||
Input & { swrOptions?: SwrOptions<Data, FetchInput> },
|
||||
ResponseState<Data> & State
|
||||
H['input'] & { swrOptions?: SwrOptions<H['data'], H['fetchInput']> },
|
||||
ResponseState<H['data']> & H['swrState']
|
||||
>
|
||||
fetchOptions: HookFetcherOptions
|
||||
fetcher?: HookFetcherFn<Data, FetchInput>
|
||||
fetcher?: HookFetcherFn<H>
|
||||
}
|
||||
|
||||
export type SWRHookContext<
|
||||
Data,
|
||||
FetchInput extends { [k: string]: unknown } = {}
|
||||
> = {
|
||||
type X = {} & undefined
|
||||
|
||||
export type SWRHookContext<H extends SWRHookSchemaBase> = {
|
||||
useData(context?: {
|
||||
input?: HookFetchInput | HookSWRInput
|
||||
swrOptions?: SwrOptions<Data, FetchInput>
|
||||
}): ResponseState<Data>
|
||||
swrOptions?: SwrOptions<H['data'], H['fetchInput']>
|
||||
}): ResponseState<H['data']>
|
||||
}
|
||||
|
||||
export type MutationHook<
|
||||
// Data obj returned by the hook and fetch operation
|
||||
Data,
|
||||
// Input expected by the hook
|
||||
Input extends { [k: string]: unknown } = {},
|
||||
// Input expected by the action returned by the hook
|
||||
ActionInput extends { [k: string]: unknown } = {},
|
||||
// Input expected before doing a fetch operation
|
||||
FetchInput extends { [k: string]: unknown } = ActionInput
|
||||
> = {
|
||||
/**
|
||||
* Generates a mutation hook handler based on the schema of a hook
|
||||
*/
|
||||
export type MutationHook<H extends MutationSchemaBase> = {
|
||||
useHook(
|
||||
context: MutationHookContext<Data, FetchInput>
|
||||
): HookFunction<Input, HookFunction<ActionInput, Data | Promise<Data>>>
|
||||
context: MutationHookContext<H>
|
||||
): HookFunction<
|
||||
H['input'],
|
||||
HookFunction<H['actionInput'], H['data'] | Promise<H['data']>>
|
||||
>
|
||||
fetchOptions: HookFetcherOptions
|
||||
fetcher?: HookFetcherFn<Data, FetchInput>
|
||||
fetcher?: HookFetcherFn<H['data'], H['fetchInput']>
|
||||
}
|
||||
|
||||
export type MutationHookContext<
|
||||
Data,
|
||||
FetchInput extends { [k: string]: unknown } | null = {}
|
||||
> = {
|
||||
fetch: keyof FetchInput extends never
|
||||
? () => Data | Promise<Data>
|
||||
: Partial<FetchInput> extends FetchInput
|
||||
? (context?: { input?: FetchInput }) => Data | Promise<Data>
|
||||
: (context: { input: FetchInput }) => Data | Promise<Data>
|
||||
export type MutationHookContext<H extends MutationSchemaBase> = {
|
||||
fetch: keyof H['fetchInput'] extends never
|
||||
? () => H['data'] | Promise<H['data']>
|
||||
: Partial<H['fetchInput']> extends H['fetchInput']
|
||||
? (context?: { input?: H['fetchInput'] }) => H['data'] | Promise<H['data']>
|
||||
: (context: { input: H['fetchInput'] }) => H['data'] | Promise<H['data']>
|
||||
}
|
||||
|
||||
export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
||||
|
Loading…
x
Reference in New Issue
Block a user