Updated useCustomer and core types

This commit is contained in:
Luis Alvarez 2021-05-25 10:59:22 -05:00
parent 58d782b780
commit e7980e17f5
4 changed files with 26 additions and 14 deletions

View File

@ -1,16 +1,16 @@
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer' import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
import type { Customer, CustomerData } from '../api/customers' import type { CustomerHook } from '../types/customer'
export default useCustomer as UseCustomer<typeof handler> export default useCustomer as UseCustomer<typeof handler>
export const handler: SWRHook<Customer | null> = { export const handler: SWRHook<CustomerHook> = {
fetchOptions: { fetchOptions: {
url: '/api/bigcommerce/customers', url: '/api/customer',
method: 'GET', method: 'GET',
}, },
async fetcher({ options, fetch }) { async fetcher({ options, fetch }) {
const data = await fetch<CustomerData | null>(options) const data = await fetch(options)
return data?.customer ?? null return data?.customer ?? null
}, },
useHook: ({ useData }) => (input) => { useHook: ({ useData }) => (input) => {

View File

@ -1,14 +1,14 @@
import { useHook, useSWRHook } from '../utils/use-hook' import { useHook, useSWRHook } from '../utils/use-hook'
import { SWRFetcher } from '../utils/default-fetcher' import { SWRFetcher } from '../utils/default-fetcher'
import type { CustomerHook } from '../types/customer'
import type { HookFetcherFn, SWRHook } from '../utils/types' import type { HookFetcherFn, SWRHook } from '../utils/types'
import type { Customer } from '../types' import type { Provider } from '..'
import { Provider } from '..'
export type UseCustomer< export type UseCustomer<
H extends SWRHook<any, any, any> = SWRHook<Customer | null> H extends SWRHook<CustomerHook<any>> = SWRHook<CustomerHook>
> = ReturnType<H['useHook']> > = ReturnType<H['useHook']>
export const fetcher: HookFetcherFn<Customer | null, any> = SWRFetcher export const fetcher: HookFetcherFn<CustomerHook> = SWRFetcher
const fn = (provider: Provider) => provider.customer?.useCustomer! const fn = (provider: Provider) => provider.customer?.useCustomer!

View File

@ -5,6 +5,13 @@ export type CustomerTypes = {
customer: Customer customer: Customer
} }
export type CustomerHook<T extends CustomerTypes = CustomerTypes> = {
data: T['customer'] | null
fetchData: { customer: T['customer'] } | null
// actionInput: T['body']
// fetchInput: T['body']
}
export type CustomerSchema<T extends CustomerTypes = CustomerTypes> = { export type CustomerSchema<T extends CustomerTypes = CustomerTypes> = {
endpoint: { endpoint: {
options: {} options: {}

View File

@ -43,7 +43,12 @@ export type HookFetcherFn<H extends HookSchemaBase> = (
export type HookFetcherContext<H extends HookSchemaBase> = { export type HookFetcherContext<H extends HookSchemaBase> = {
options: HookFetcherOptions options: HookFetcherOptions
input: H['fetchInput'] input: H['fetchInput']
fetch: <T = any, B = H['body']>(options: FetcherOptions<B>) => Promise<T> fetch: {
(
options: FetcherOptions<H['body'] extends {} ? H['body'] : never>
): Promise<H['fetchData'] extends {} | null ? H['fetchData'] : any>
<T = any, B = any>(options: FetcherOptions<B>): Promise<T>
}
} }
export type HookFetcherOptions = { method?: string } & ( export type HookFetcherOptions = { method?: string } & (
@ -67,19 +72,21 @@ export type HookFunction<
: (input: Input) => T : (input: Input) => T
export type HookSchemaBase = { export type HookSchemaBase = {
// Data obj returned by the hook and fetch operation // Data obj returned by the hook
data: any data: any
// Input expected by the hook // Input expected by the hook
input?: {} input?: {}
// Input expected before doing a fetch operation (aka fetch handler) // Input expected before doing a fetch operation (aka fetch handler)
fetchInput?: {} fetchInput?: {}
// Data expected by the fetch operation // Body object expected by the fetch operation
body?: {} body?: {}
// Data returned by the fetch operation
fetchData?: any
} }
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?: {}
} }
export type MutationSchemaBase = HookSchemaBase & { export type MutationSchemaBase = HookSchemaBase & {
@ -101,8 +108,6 @@ export type SWRHook<H extends SWRHookSchemaBase> = {
fetcher?: HookFetcherFn<H> fetcher?: HookFetcherFn<H>
} }
type X = {} & undefined
export type SWRHookContext<H extends SWRHookSchemaBase> = { export type SWRHookContext<H extends SWRHookSchemaBase> = {
useData(context?: { useData(context?: {
input?: HookFetchInput | HookSWRInput input?: HookFetchInput | HookSWRInput