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