forked from crowetic/commerce
Moved use-login and use-logout
This commit is contained in:
parent
aa227e3de2
commit
7cd64d2c2a
@ -1,54 +1,40 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import type { HookFetcher } from '@commerce/utils/types'
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
import useCommerceLogin from '@commerce/use-login'
|
import useLogin, { UseLogin } from '@commerce/use-login'
|
||||||
import type { LoginBody } from '../api/customers/login'
|
import type { LoginBody } from '../api/customers/login'
|
||||||
import useCustomer from '../customer/use-customer'
|
import useCustomer from '../customer/use-customer'
|
||||||
|
|
||||||
const defaultOpts = {
|
export default useLogin as UseLogin<typeof handler>
|
||||||
url: '/api/bigcommerce/customers/login',
|
|
||||||
method: 'POST',
|
|
||||||
}
|
|
||||||
|
|
||||||
export type LoginInput = LoginBody
|
export const handler: MutationHook<null, {}, LoginBody> = {
|
||||||
|
fetchOptions: {
|
||||||
|
url: '/api/bigcommerce/customers/login',
|
||||||
|
method: 'POST',
|
||||||
|
},
|
||||||
|
async fetcher({ input: { email, password }, options, fetch }) {
|
||||||
|
if (!(email && password)) {
|
||||||
|
throw new CommerceError({
|
||||||
|
message:
|
||||||
|
'A first name, last name, email and password are required to login',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export const fetcher: HookFetcher<null, LoginBody> = (
|
return fetch({
|
||||||
options,
|
...options,
|
||||||
{ email, password },
|
body: { email, password },
|
||||||
fetch
|
|
||||||
) => {
|
|
||||||
if (!(email && password)) {
|
|
||||||
throw new CommerceError({
|
|
||||||
message:
|
|
||||||
'A first name, last name, email and password are required to login',
|
|
||||||
})
|
})
|
||||||
}
|
},
|
||||||
|
useHook: ({ fetch }) => () => {
|
||||||
return fetch({
|
|
||||||
...defaultOpts,
|
|
||||||
...options,
|
|
||||||
body: { email, password },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
|
||||||
const useLogin = () => {
|
|
||||||
const { revalidate } = useCustomer()
|
const { revalidate } = useCustomer()
|
||||||
const fn = useCommerceLogin<null, LoginInput>(defaultOpts, customFetcher)
|
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
async function login(input: LoginInput) {
|
async function login(input) {
|
||||||
const data = await fn(input)
|
const data = await fetch({ input })
|
||||||
await revalidate()
|
await revalidate()
|
||||||
return data
|
return data
|
||||||
},
|
},
|
||||||
[fn]
|
[fetch, revalidate]
|
||||||
)
|
)
|
||||||
}
|
},
|
||||||
|
|
||||||
useLogin.extend = extendHook
|
|
||||||
|
|
||||||
return useLogin
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default extendHook(fetcher)
|
|
||||||
|
@ -1,38 +1,25 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import type { HookFetcher } from '@commerce/utils/types'
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
import useCommerceLogout from '@commerce/use-logout'
|
import useLogout, { UseLogout } from '@commerce/use-logout'
|
||||||
import useCustomer from '../customer/use-customer'
|
import useCustomer from '../customer/use-customer'
|
||||||
|
|
||||||
const defaultOpts = {
|
export default useLogout as UseLogout<typeof handler>
|
||||||
url: '/api/bigcommerce/customers/logout',
|
|
||||||
method: 'GET',
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fetcher: HookFetcher<null> = (options, _, fetch) => {
|
export const handler: MutationHook<null> = {
|
||||||
return fetch({
|
fetchOptions: {
|
||||||
...defaultOpts,
|
url: '/api/bigcommerce/customers/logout',
|
||||||
...options,
|
method: 'GET',
|
||||||
})
|
},
|
||||||
}
|
useHook: ({ fetch }) => () => {
|
||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
|
||||||
const useLogout = () => {
|
|
||||||
const { mutate } = useCustomer()
|
const { mutate } = useCustomer()
|
||||||
const fn = useCommerceLogout<null>(defaultOpts, customFetcher)
|
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
async function login() {
|
async function logout() {
|
||||||
const data = await fn(null)
|
const data = await fetch()
|
||||||
await mutate(null, false)
|
await mutate(null, false)
|
||||||
return data
|
return data
|
||||||
},
|
},
|
||||||
[fn]
|
[fetch, mutate]
|
||||||
)
|
)
|
||||||
}
|
},
|
||||||
|
|
||||||
useLogout.extend = extendHook
|
|
||||||
|
|
||||||
return useLogout
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default extendHook(fetcher)
|
|
||||||
|
@ -9,6 +9,9 @@ import { handler as useWishlistRemoveItem } from './wishlist/use-remove-item'
|
|||||||
|
|
||||||
import { handler as useCustomer } from './customer/use-customer'
|
import { handler as useCustomer } from './customer/use-customer'
|
||||||
import { handler as useSearch } from './product/use-search'
|
import { handler as useSearch } from './product/use-search'
|
||||||
|
|
||||||
|
import { handler as useLogin } from './auth/use-login'
|
||||||
|
|
||||||
import fetcher from './fetcher'
|
import fetcher from './fetcher'
|
||||||
|
|
||||||
export const bigcommerceProvider = {
|
export const bigcommerceProvider = {
|
||||||
@ -23,6 +26,7 @@ export const bigcommerceProvider = {
|
|||||||
},
|
},
|
||||||
customer: { useCustomer },
|
customer: { useCustomer },
|
||||||
products: { useSearch },
|
products: { useSearch },
|
||||||
|
auth: { useLogin },
|
||||||
}
|
}
|
||||||
|
|
||||||
export type BigcommerceProvider = typeof bigcommerceProvider
|
export type BigcommerceProvider = typeof bigcommerceProvider
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
import { useHook, useMutationHook } from '../utils/use-hook'
|
import { useHook, useMutationHook } from '../utils/use-hook'
|
||||||
import { mutationFetcher } from '../utils/default-fetcher'
|
import { mutationFetcher } from '../utils/default-fetcher'
|
||||||
import type { MutationHook, HookFetcherFn } from '../utils/types'
|
import type { HookFetcherFn, MutationHook } from '../utils/types'
|
||||||
import type { Cart, CartItemBody, AddCartItemBody } from '../types'
|
import type { Cart, CartItemBody, AddCartItemBody } from '../types'
|
||||||
import type { Provider } from '..'
|
import type { Provider } from '..'
|
||||||
|
|
||||||
|
export type UseAddItem<
|
||||||
|
H extends MutationHook<any, any, any> = MutationHook<Cart, {}, CartItemBody>
|
||||||
|
> = ReturnType<H['useHook']>
|
||||||
|
|
||||||
export const fetcher: HookFetcherFn<
|
export const fetcher: HookFetcherFn<
|
||||||
Cart,
|
Cart,
|
||||||
AddCartItemBody<CartItemBody>
|
AddCartItemBody<CartItemBody>
|
||||||
> = mutationFetcher
|
> = mutationFetcher
|
||||||
|
|
||||||
export type UseAddItem<
|
|
||||||
H extends MutationHook<any, any, any> = MutationHook<Cart, {}, CartItemBody>
|
|
||||||
> = ReturnType<H['useHook']>
|
|
||||||
|
|
||||||
const fn = (provider: Provider) => provider.cart?.useAddItem!
|
const fn = (provider: Provider) => provider.cart?.useAddItem!
|
||||||
|
|
||||||
const useAddItem: UseAddItem = (...args) => {
|
const useAddItem: UseAddItem = (...args) => {
|
||||||
|
@ -25,12 +25,17 @@ export type Provider = CommerceConfig & {
|
|||||||
useAddItem?: MutationHook<any, any, any>
|
useAddItem?: MutationHook<any, any, any>
|
||||||
useRemoveItem?: MutationHook<any, any, any>
|
useRemoveItem?: MutationHook<any, any, any>
|
||||||
}
|
}
|
||||||
customer: {
|
customer?: {
|
||||||
useCustomer?: SWRHook<Customer | null, any, any>
|
useCustomer?: SWRHook<Customer | null, any, any>
|
||||||
}
|
}
|
||||||
products: {
|
products?: {
|
||||||
useSearch?: SWRHook<SearchProductsData, any, any>
|
useSearch?: SWRHook<SearchProductsData, any, any>
|
||||||
}
|
}
|
||||||
|
auth?: {
|
||||||
|
useSignup?: MutationHook<any, any, any>
|
||||||
|
useLogin?: MutationHook<any, any, any>
|
||||||
|
useLogout?: MutationHook<any, any, any>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CommerceProps<P extends Provider> = {
|
export type CommerceProps<P extends Provider> = {
|
||||||
|
@ -1,5 +1,19 @@
|
|||||||
import useAction from './utils/use-action'
|
import { useHook, useMutationHook } from './utils/use-hook'
|
||||||
|
import { mutationFetcher } from './utils/default-fetcher'
|
||||||
|
import type { MutationHook, HookFetcherFn } from './utils/types'
|
||||||
|
import type { Provider } from '.'
|
||||||
|
|
||||||
const useLogin = useAction
|
export type UseLogin<
|
||||||
|
H extends MutationHook<any, any, any> = MutationHook<null, {}, {}>
|
||||||
|
> = ReturnType<H['useHook']>
|
||||||
|
|
||||||
|
export const fetcher: HookFetcherFn<null, {}> = mutationFetcher
|
||||||
|
|
||||||
|
const fn = (provider: Provider) => provider.auth?.useLogin!
|
||||||
|
|
||||||
|
const useLogin: UseLogin = (...args) => {
|
||||||
|
const hook = useHook(fn)
|
||||||
|
return useMutationHook({ fetcher, ...hook })(...args)
|
||||||
|
}
|
||||||
|
|
||||||
export default useLogin
|
export default useLogin
|
||||||
|
@ -1,5 +1,19 @@
|
|||||||
import useAction from './utils/use-action'
|
import { useHook, useMutationHook } from './utils/use-hook'
|
||||||
|
import { mutationFetcher } from './utils/default-fetcher'
|
||||||
|
import type { HookFetcherFn, MutationHook } from './utils/types'
|
||||||
|
import type { Provider } from '.'
|
||||||
|
|
||||||
const useLogout = useAction
|
export type UseLogout<
|
||||||
|
H extends MutationHook<any, any, any> = MutationHook<null>
|
||||||
|
> = ReturnType<H['useHook']>
|
||||||
|
|
||||||
|
export const fetcher: HookFetcherFn<null> = mutationFetcher
|
||||||
|
|
||||||
|
const fn = (provider: Provider) => provider.auth?.useLogout!
|
||||||
|
|
||||||
|
const useLogout: UseLogout = (...args) => {
|
||||||
|
const hook = useHook(fn)
|
||||||
|
return useMutationHook({ fetcher, ...hook })(...args)
|
||||||
|
}
|
||||||
|
|
||||||
export default useLogout
|
export default useLogout
|
||||||
|
@ -36,11 +36,11 @@ export type HookFetcher<Data, Input = null, Result = any> = (
|
|||||||
fetch: <T = Result, Body = any>(options: FetcherOptions<Body>) => Promise<T>
|
fetch: <T = Result, Body = any>(options: FetcherOptions<Body>) => Promise<T>
|
||||||
) => Data | Promise<Data>
|
) => Data | Promise<Data>
|
||||||
|
|
||||||
export type HookFetcherFn<Data, Input = never, Result = any, Body = any> = (
|
export type HookFetcherFn<Data, Input = undefined, Result = any, Body = any> = (
|
||||||
context: HookFetcherContext<Input, Result, Body>
|
context: HookFetcherContext<Input, Result, Body>
|
||||||
) => Data | Promise<Data>
|
) => Data | Promise<Data>
|
||||||
|
|
||||||
export type HookFetcherContext<Input = never, Result = any, Body = any> = {
|
export type HookFetcherContext<Input = undefined, Result = any, Body = any> = {
|
||||||
options: HookFetcherOptions
|
options: HookFetcherOptions
|
||||||
input: Input
|
input: Input
|
||||||
fetch: <T = Result, B = Body>(options: FetcherOptions<B>) => Promise<T>
|
fetch: <T = Result, B = Body>(options: FetcherOptions<B>) => Promise<T>
|
||||||
@ -58,7 +58,7 @@ export type HookSWRInput = [string, HookInputValue][]
|
|||||||
export type HookFetchInput = { [k: string]: HookInputValue }
|
export type HookFetchInput = { [k: string]: HookInputValue }
|
||||||
|
|
||||||
export type HookFunction<
|
export type HookFunction<
|
||||||
Input extends { [k: string]: unknown } | {},
|
Input extends { [k: string]: unknown } | null,
|
||||||
T
|
T
|
||||||
> = keyof Input extends never
|
> = keyof Input extends never
|
||||||
? () => T
|
? () => T
|
||||||
@ -115,9 +115,13 @@ export type MutationHook<
|
|||||||
|
|
||||||
export type MutationHookContext<
|
export type MutationHookContext<
|
||||||
Data,
|
Data,
|
||||||
FetchInput extends { [k: string]: unknown } = {}
|
FetchInput extends { [k: string]: unknown } | null = {}
|
||||||
> = {
|
> = {
|
||||||
fetch: (context: { input: FetchInput }) => Data | Promise<Data>
|
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 SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<
|
||||||
|
@ -37,7 +37,7 @@ export function useMutationHook<H extends MutationHook<any, any, any>>(
|
|||||||
|
|
||||||
return hook.useHook({
|
return hook.useHook({
|
||||||
fetch: useCallback(
|
fetch: useCallback(
|
||||||
({ input }) => {
|
({ input } = {}) => {
|
||||||
return hook.fetcher({
|
return hook.fetcher({
|
||||||
input,
|
input,
|
||||||
options: hook.fetchOptions,
|
options: hook.fetchOptions,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user