Added optional mutations to hook schema

This commit is contained in:
Luis Alvarez
2021-09-14 14:51:33 -05:00
parent 30744c5a6c
commit ac1a8be6c3
2 changed files with 19 additions and 12 deletions

View File

@@ -1,10 +1,12 @@
import type { UseSubmitCheckout } from '../checkout/use-submit-checkout'
// Index // Index
export type CheckoutTypes = { export type CheckoutTypes = {
card?: any; card?: any
address?: any; address?: any
checkout?: any; checkout?: any
hasPayment?: boolean; hasPayment?: boolean
hasShipping?: boolean; hasShipping?: boolean
} }
export type SubmitCheckoutHook<T extends CheckoutTypes = CheckoutTypes> = { export type SubmitCheckoutHook<T extends CheckoutTypes = CheckoutTypes> = {
@@ -20,6 +22,7 @@ export type GetCheckoutHook<T extends CheckoutTypes = CheckoutTypes> = {
input: {} input: {}
fetcherInput: { cartId?: string } fetcherInput: { cartId?: string }
swrState: { isEmpty: boolean } swrState: { isEmpty: boolean }
mutations: { submit: UseSubmitCheckout }
} }
export type CheckoutHooks<T extends CheckoutTypes = CheckoutTypes> = { export type CheckoutHooks<T extends CheckoutTypes = CheckoutTypes> = {
@@ -27,13 +30,15 @@ export type CheckoutHooks<T extends CheckoutTypes = CheckoutTypes> = {
getCheckout: GetCheckoutHook<T> getCheckout: GetCheckoutHook<T>
} }
export type GetCheckoutHandler<T extends CheckoutTypes = CheckoutTypes> = GetCheckoutHook<T> & { export type GetCheckoutHandler<T extends CheckoutTypes = CheckoutTypes> =
body: { cartId: string } GetCheckoutHook<T> & {
} body: { cartId: string }
}
export type SubmitCheckoutHandler<T extends CheckoutTypes = CheckoutTypes> = SubmitCheckoutHook<T> & { export type SubmitCheckoutHandler<T extends CheckoutTypes = CheckoutTypes> =
body: { cartId: string } SubmitCheckoutHook<T> & {
} body: { cartId: string }
}
export type CheckoutHandlers<T extends CheckoutTypes = CheckoutTypes> = { export type CheckoutHandlers<T extends CheckoutTypes = CheckoutTypes> = {
getCheckout: GetCheckoutHandler<T> getCheckout: GetCheckoutHandler<T>

View File

@@ -87,6 +87,8 @@ export type HookSchemaBase = {
export type SWRHookSchemaBase = HookSchemaBase & { export type SWRHookSchemaBase = HookSchemaBase & {
// Custom state added to the response object of SWR // Custom state added to the response object of SWR
swrState?: {} swrState?: {}
// Instances of MutationSchemaBase that the hook returns for better DX
mutations?: Record<string, ReturnType<MutationHook<any>['useHook']>>
} }
export type MutationSchemaBase = HookSchemaBase & { export type MutationSchemaBase = HookSchemaBase & {
@@ -102,7 +104,7 @@ export type SWRHook<H extends SWRHookSchemaBase> = {
context: SWRHookContext<H> context: SWRHookContext<H>
): HookFunction< ): HookFunction<
H['input'] & { swrOptions?: SwrOptions<H['data'], H['fetcherInput']> }, H['input'] & { swrOptions?: SwrOptions<H['data'], H['fetcherInput']> },
ResponseState<H['data']> & H['swrState'] ResponseState<H['data']> & H['swrState'] & H['mutations']
> >
fetchOptions: HookFetcherOptions fetchOptions: HookFetcherOptions
fetcher?: HookFetcherFn<H> fetcher?: HookFetcherFn<H>