diff --git a/framework/bigcommerce/api/endpoints/catalog/products/get-products.ts b/framework/bigcommerce/api/endpoints/catalog/products/get-products.ts index 8471767aa..707f43811 100644 --- a/framework/bigcommerce/api/endpoints/catalog/products/get-products.ts +++ b/framework/bigcommerce/api/endpoints/catalog/products/get-products.ts @@ -12,7 +12,7 @@ const LIMIT = 12 // Return current cart info const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({ res, - body: { search, category, brand, sort }, + body: { search, categoryId, brandId, sort }, config, commerce, }) => { @@ -24,11 +24,11 @@ const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({ if (search) url.searchParams.set('keyword', search) - if (category && Number.isInteger(Number(category))) - url.searchParams.set('categories:in', category) + if (categoryId && Number.isInteger(Number(categoryId))) + url.searchParams.set('categories:in', categoryId) - if (brand && Number.isInteger(Number(brand))) - url.searchParams.set('brand_id', brand) + if (brandId && Number.isInteger(Number(brandId))) + url.searchParams.set('brand_id', brandId) if (sort) { const [_sort, direction] = sort.split('-') diff --git a/framework/bigcommerce/api/utils/fetch-graphql-api.ts b/framework/bigcommerce/api/utils/fetch-graphql-api.ts index 9c2eaa2ac..7dc39f987 100644 --- a/framework/bigcommerce/api/utils/fetch-graphql-api.ts +++ b/framework/bigcommerce/api/utils/fetch-graphql-api.ts @@ -3,13 +3,13 @@ import type { GraphQLFetcher } from '@commerce/api' import { provider } from '..' import fetch from './fetch' -const { config } = provider const fetchGraphqlApi: GraphQLFetcher = async ( query: string, { variables, preview } = {}, fetchOptions ) => { // log.warn(query) + const { config } = provider const res = await fetch(config.commerceUrl + (preview ? '/preview' : ''), { ...fetchOptions, method: 'POST', diff --git a/framework/bigcommerce/api/utils/fetch-store-api.ts b/framework/bigcommerce/api/utils/fetch-store-api.ts index 68817417e..a00b3777a 100644 --- a/framework/bigcommerce/api/utils/fetch-store-api.ts +++ b/framework/bigcommerce/api/utils/fetch-store-api.ts @@ -3,12 +3,11 @@ import { provider } from '..' import { BigcommerceApiError, BigcommerceNetworkError } from './errors' import fetch from './fetch' -const { config } = provider - export default async function fetchStoreApi( endpoint: string, options?: RequestInit ): Promise { + const { config } = provider let res: Response try { diff --git a/framework/bigcommerce/auth/use-login.tsx b/framework/bigcommerce/auth/use-login.tsx index 1be96a58c..bf2360af6 100644 --- a/framework/bigcommerce/auth/use-login.tsx +++ b/framework/bigcommerce/auth/use-login.tsx @@ -2,12 +2,12 @@ import { useCallback } from 'react' import type { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useLogin, { UseLogin } from '@commerce/auth/use-login' -import type { LoginBody } from '../api/customers/login' +import type { LoginHook } from '../types/login' import useCustomer from '../customer/use-customer' export default useLogin as UseLogin -export const handler: MutationHook = { +export const handler: MutationHook = { fetchOptions: { url: '/api/bigcommerce/customers/login', method: 'POST', diff --git a/framework/bigcommerce/auth/use-logout.tsx b/framework/bigcommerce/auth/use-logout.tsx index 71015a1c1..f704563e9 100644 --- a/framework/bigcommerce/auth/use-logout.tsx +++ b/framework/bigcommerce/auth/use-logout.tsx @@ -1,11 +1,12 @@ import { useCallback } from 'react' import type { MutationHook } from '@commerce/utils/types' import useLogout, { UseLogout } from '@commerce/auth/use-logout' +import type { LogoutHook } from '../types/logout' import useCustomer from '../customer/use-customer' export default useLogout as UseLogout -export const handler: MutationHook = { +export const handler: MutationHook = { fetchOptions: { url: '/api/bigcommerce/customers/logout', method: 'GET', diff --git a/framework/bigcommerce/auth/use-signup.tsx b/framework/bigcommerce/auth/use-signup.tsx index 28f7024ef..bcd6886a3 100644 --- a/framework/bigcommerce/auth/use-signup.tsx +++ b/framework/bigcommerce/auth/use-signup.tsx @@ -2,12 +2,12 @@ import { useCallback } from 'react' import type { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useSignup, { UseSignup } from '@commerce/auth/use-signup' -import type { SignupBody } from '../api/customers/signup' +import type { SignupHook } from '../types/signup' import useCustomer from '../customer/use-customer' export default useSignup as UseSignup -export const handler: MutationHook = { +export const handler: MutationHook = { fetchOptions: { url: '/api/bigcommerce/customers/signup', method: 'POST', diff --git a/framework/bigcommerce/customer/use-customer.tsx b/framework/bigcommerce/customer/use-customer.tsx index 093007824..238b1229b 100644 --- a/framework/bigcommerce/customer/use-customer.tsx +++ b/framework/bigcommerce/customer/use-customer.tsx @@ -1,16 +1,16 @@ import { SWRHook } from '@commerce/utils/types' 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 -export const handler: SWRHook = { +export const handler: SWRHook = { fetchOptions: { - url: '/api/bigcommerce/customers', + url: '/api/customer', method: 'GET', }, async fetcher({ options, fetch }) { - const data = await fetch(options) + const data = await fetch(options) return data?.customer ?? null }, useHook: ({ useData }) => (input) => { diff --git a/framework/bigcommerce/product/use-search.tsx b/framework/bigcommerce/product/use-search.tsx index 0ee135032..c9ac8f9a0 100644 --- a/framework/bigcommerce/product/use-search.tsx +++ b/framework/bigcommerce/product/use-search.tsx @@ -1,6 +1,6 @@ import { SWRHook } from '@commerce/utils/types' import useSearch, { UseSearch } from '@commerce/product/use-search' -import type { SearchProductsData } from '../api/catalog/products' +import type { SearchProductsHook } from '../types/product' export default useSearch as UseSearch @@ -11,13 +11,9 @@ export type SearchProductsInput = { sort?: string } -export const handler: SWRHook< - SearchProductsData, - SearchProductsInput, - SearchProductsInput -> = { +export const handler: SWRHook = { fetchOptions: { - url: '/api/bigcommerce/catalog/products', + url: '/api/catalog/products', method: 'GET', }, fetcher({ input: { search, categoryId, brandId, sort }, options, fetch }) { diff --git a/framework/bigcommerce/types/common.ts b/framework/bigcommerce/types/common.ts new file mode 100644 index 000000000..b52c33a4d --- /dev/null +++ b/framework/bigcommerce/types/common.ts @@ -0,0 +1 @@ +export * from '@commerce/types/common' diff --git a/framework/bigcommerce/types/index.ts b/framework/bigcommerce/types/index.ts index a0792d798..7ab0b7f64 100644 --- a/framework/bigcommerce/types/index.ts +++ b/framework/bigcommerce/types/index.ts @@ -1,2 +1,25 @@ -export * from '@commerce/types' -export * from './cart' +import * as Cart from './cart' +import * as Checkout from './checkout' +import * as Common from './common' +import * as Customer from './customer' +import * as Login from './login' +import * as Logout from './logout' +import * as Page from './page' +import * as Product from './product' +import * as Signup from './signup' +import * as Site from './site' +import * as Wishlist from './wishlist' + +export type { + Cart, + Checkout, + Common, + Customer, + Login, + Logout, + Page, + Product, + Signup, + Site, + Wishlist, +} diff --git a/framework/bigcommerce/wishlist/use-add-item.tsx b/framework/bigcommerce/wishlist/use-add-item.tsx index 402e7da8b..1bf086731 100644 --- a/framework/bigcommerce/wishlist/use-add-item.tsx +++ b/framework/bigcommerce/wishlist/use-add-item.tsx @@ -2,15 +2,15 @@ import { useCallback } from 'react' import type { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useAddItem, { UseAddItem } from '@commerce/wishlist/use-add-item' -import type { ItemBody, AddItemBody } from '../api/wishlist' +import type { AddItemHook } from '../types/wishlist' import useCustomer from '../customer/use-customer' import useWishlist from './use-wishlist' export default useAddItem as UseAddItem -export const handler: MutationHook = { +export const handler: MutationHook = { fetchOptions: { - url: '/api/bigcommerce/wishlist', + url: '/api/wishlist', method: 'POST', }, useHook: ({ fetch }) => () => { diff --git a/framework/bigcommerce/wishlist/use-remove-item.tsx b/framework/bigcommerce/wishlist/use-remove-item.tsx index 622f321db..9d25c1439 100644 --- a/framework/bigcommerce/wishlist/use-remove-item.tsx +++ b/framework/bigcommerce/wishlist/use-remove-item.tsx @@ -2,23 +2,17 @@ import { useCallback } from 'react' import type { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useRemoveItem, { - RemoveItemInput, UseRemoveItem, } from '@commerce/wishlist/use-remove-item' -import type { RemoveItemBody, Wishlist } from '../api/wishlist' +import type { RemoveItemHook } from '../types/wishlist' import useCustomer from '../customer/use-customer' -import useWishlist, { UseWishlistInput } from './use-wishlist' +import useWishlist from './use-wishlist' export default useRemoveItem as UseRemoveItem -export const handler: MutationHook< - Wishlist | null, - { wishlist?: UseWishlistInput }, - RemoveItemInput, - RemoveItemBody -> = { +export const handler: MutationHook = { fetchOptions: { - url: '/api/bigcommerce/wishlist', + url: '/api/wishlist', method: 'DELETE', }, useHook: ({ fetch }) => ({ wishlist } = {}) => { diff --git a/framework/bigcommerce/wishlist/use-wishlist.tsx b/framework/bigcommerce/wishlist/use-wishlist.tsx index 4850d1cd9..134e9cc24 100644 --- a/framework/bigcommerce/wishlist/use-wishlist.tsx +++ b/framework/bigcommerce/wishlist/use-wishlist.tsx @@ -1,19 +1,12 @@ import { useMemo } from 'react' import { SWRHook } from '@commerce/utils/types' import useWishlist, { UseWishlist } from '@commerce/wishlist/use-wishlist' -import type { Wishlist } from '../api/wishlist' +import type { GetWishlistHook } from '../types/wishlist' import useCustomer from '../customer/use-customer' -export type UseWishlistInput = { includeProducts?: boolean } - export default useWishlist as UseWishlist -export const handler: SWRHook< - Wishlist | null, - UseWishlistInput, - { customerId?: number } & UseWishlistInput, - { isEmpty?: boolean } -> = { +export const handler: SWRHook = { fetchOptions: { url: '/api/bigcommerce/wishlist', method: 'GET', diff --git a/framework/commerce/api/operations.ts b/framework/commerce/api/operations.ts index aa30f426e..2910a2d82 100644 --- a/framework/commerce/api/operations.ts +++ b/framework/commerce/api/operations.ts @@ -14,7 +14,16 @@ const noop = () => { throw new Error('Not implemented') } -export const OPERATIONS = ['login'] as const +export const OPERATIONS = [ + 'login', + 'getAllPages', + 'getPage', + 'getSiteInfo', + 'getCustomerWishlist', + 'getAllProductPaths', + 'getAllProducts', + 'getProduct', +] as const export const defaultOperations = OPERATIONS.reduce((ops, k) => { ops[k] = noop diff --git a/framework/commerce/auth/use-login.tsx b/framework/commerce/auth/use-login.tsx index cc4cf6a73..67fb429dc 100644 --- a/framework/commerce/auth/use-login.tsx +++ b/framework/commerce/auth/use-login.tsx @@ -1,13 +1,14 @@ import { useHook, useMutationHook } from '../utils/use-hook' import { mutationFetcher } from '../utils/default-fetcher' import type { MutationHook, HookFetcherFn } from '../utils/types' +import type { LoginHook } from '../types/login' import type { Provider } from '..' export type UseLogin< - H extends MutationHook = MutationHook + H extends MutationHook> = MutationHook > = ReturnType -export const fetcher: HookFetcherFn = mutationFetcher +export const fetcher: HookFetcherFn = mutationFetcher const fn = (provider: Provider) => provider.auth?.useLogin! diff --git a/framework/commerce/auth/use-logout.tsx b/framework/commerce/auth/use-logout.tsx index d0f7e3ae0..6ca16decf 100644 --- a/framework/commerce/auth/use-logout.tsx +++ b/framework/commerce/auth/use-logout.tsx @@ -1,13 +1,14 @@ import { useHook, useMutationHook } from '../utils/use-hook' import { mutationFetcher } from '../utils/default-fetcher' import type { HookFetcherFn, MutationHook } from '../utils/types' +import type { LogoutHook } from '../types/logout' import type { Provider } from '..' export type UseLogout< - H extends MutationHook = MutationHook + H extends MutationHook> = MutationHook > = ReturnType -export const fetcher: HookFetcherFn = mutationFetcher +export const fetcher: HookFetcherFn = mutationFetcher const fn = (provider: Provider) => provider.auth?.useLogout! diff --git a/framework/commerce/auth/use-signup.tsx b/framework/commerce/auth/use-signup.tsx index 72e242209..2f846fad6 100644 --- a/framework/commerce/auth/use-signup.tsx +++ b/framework/commerce/auth/use-signup.tsx @@ -1,13 +1,14 @@ import { useHook, useMutationHook } from '../utils/use-hook' import { mutationFetcher } from '../utils/default-fetcher' import type { HookFetcherFn, MutationHook } from '../utils/types' +import type { SignupHook } from '../types/signup' import type { Provider } from '..' export type UseSignup< - H extends MutationHook = MutationHook + H extends MutationHook> = MutationHook > = ReturnType -export const fetcher: HookFetcherFn = mutationFetcher +export const fetcher: HookFetcherFn = mutationFetcher const fn = (provider: Provider) => provider.auth?.useSignup! diff --git a/framework/commerce/cart/use-add-item.tsx b/framework/commerce/cart/use-add-item.tsx index a9d1db146..f4072c763 100644 --- a/framework/commerce/cart/use-add-item.tsx +++ b/framework/commerce/cart/use-add-item.tsx @@ -8,10 +8,6 @@ export type UseAddItem< H extends MutationHook> = MutationHook > = ReturnType -export type UseAddItem2< - H extends MutationHook = MutationHook -> = ReturnType - export const fetcher: HookFetcherFn = mutationFetcher const fn = (provider: Provider) => provider.cart?.useAddItem! diff --git a/framework/commerce/customer/use-customer.tsx b/framework/commerce/customer/use-customer.tsx index 5d6416a4b..bbeeb3269 100644 --- a/framework/commerce/customer/use-customer.tsx +++ b/framework/commerce/customer/use-customer.tsx @@ -1,14 +1,14 @@ import { useHook, useSWRHook } from '../utils/use-hook' import { SWRFetcher } from '../utils/default-fetcher' +import type { CustomerHook } from '../types/customer' import type { HookFetcherFn, SWRHook } from '../utils/types' -import type { Customer } from '../types' -import { Provider } from '..' +import type { Provider } from '..' export type UseCustomer< - H extends SWRHook = SWRHook + H extends SWRHook> = SWRHook > = ReturnType -export const fetcher: HookFetcherFn = SWRFetcher +export const fetcher: HookFetcherFn = SWRFetcher const fn = (provider: Provider) => provider.customer?.useCustomer! diff --git a/framework/commerce/product/use-search.tsx b/framework/commerce/product/use-search.tsx index d2b782045..342b49e6e 100644 --- a/framework/commerce/product/use-search.tsx +++ b/framework/commerce/product/use-search.tsx @@ -1,14 +1,14 @@ import { useHook, useSWRHook } from '../utils/use-hook' import { SWRFetcher } from '../utils/default-fetcher' import type { HookFetcherFn, SWRHook } from '../utils/types' -import type { SearchProductsData } from '../types' -import { Provider } from '..' +import type { SearchProductsHook } from '../types/product' +import type { Provider } from '..' export type UseSearch< - H extends SWRHook = SWRHook + H extends SWRHook> = SWRHook > = ReturnType -export const fetcher: HookFetcherFn = SWRFetcher +export const fetcher: HookFetcherFn = SWRFetcher const fn = (provider: Provider) => provider.products?.useSearch! diff --git a/framework/commerce/types/customer.ts b/framework/commerce/types/customer.ts index 46c415f0a..4df0fade3 100644 --- a/framework/commerce/types/customer.ts +++ b/framework/commerce/types/customer.ts @@ -5,6 +5,13 @@ export type CustomerTypes = { customer: Customer } +export type CustomerHook = { + data: T['customer'] | null + fetchData: { customer: T['customer'] } | null + // actionInput: T['body'] + // fetchInput: T['body'] +} + export type CustomerSchema = { endpoint: { options: {} diff --git a/framework/commerce/types/index.ts b/framework/commerce/types/index.ts index c187b29bb..7ab0b7f64 100644 --- a/framework/commerce/types/index.ts +++ b/framework/commerce/types/index.ts @@ -1,14 +1,25 @@ -import type { Wishlist as BCWishlist } from '../../bigcommerce/api/wishlist' -import type { Customer as BCCustomer } from '../../bigcommerce/api/customers' -import type { SearchProductsData as BCSearchProductsData } from '../../bigcommerce/api/catalog/products' +import * as Cart from './cart' +import * as Checkout from './checkout' +import * as Common from './common' +import * as Customer from './customer' +import * as Login from './login' +import * as Logout from './logout' +import * as Page from './page' +import * as Product from './product' +import * as Signup from './signup' +import * as Site from './site' +import * as Wishlist from './wishlist' -export * from './common' - -// TODO: Properly define this type -export interface Wishlist extends BCWishlist {} - -// TODO: Properly define this type -export interface Customer extends BCCustomer {} - -// TODO: Properly define this type -export interface SearchProductsData extends BCSearchProductsData {} +export type { + Cart, + Checkout, + Common, + Customer, + Login, + Logout, + Page, + Product, + Signup, + Site, + Wishlist, +} diff --git a/framework/commerce/types/login.ts b/framework/commerce/types/login.ts index 726649719..031574a33 100644 --- a/framework/commerce/types/login.ts +++ b/framework/commerce/types/login.ts @@ -7,14 +7,18 @@ export type LoginTypes = { body: LoginBody } +export type LoginHook = { + data: null + actionInput: LoginBody + fetchInput: LoginBody + body: T['body'] +} + export type LoginSchema = { endpoint: { options: {} handlers: { - login: { - data: null - body: T['body'] - } + login: LoginHook } } } diff --git a/framework/commerce/types/logout.ts b/framework/commerce/types/logout.ts index 6923237a7..a7240052f 100644 --- a/framework/commerce/types/logout.ts +++ b/framework/commerce/types/logout.ts @@ -1,11 +1,17 @@ -export type LogoutSchema = { +export type LogoutTypes = { + body: { redirectTo?: string } +} + +export type LogoutHook = { + data: null + body: T['body'] +} + +export type LogoutSchema = { endpoint: { options: {} handlers: { - logout: { - data: null - body: { redirectTo?: string } - } + logout: LogoutHook } } } diff --git a/framework/commerce/types/product.ts b/framework/commerce/types/product.ts index f69827f89..2ac45389e 100644 --- a/framework/commerce/types/product.ts +++ b/framework/commerce/types/product.ts @@ -42,8 +42,23 @@ export type Product = { options: ProductOption[] } +export type SearchProductsBody = { + search?: string + categoryId?: string + brandId?: string + sort?: string +} + export type ProductTypes = { product: Product + searchBody: SearchProductsBody +} + +export type SearchProductsHook = { + data: T['product'][] + body: T['searchBody'] + input: T['searchBody'] + fetchInput: T['searchBody'] } export type ProductsSchema = { @@ -55,12 +70,7 @@ export type ProductsSchema = { products: T['product'][] found: boolean } - body: { - search?: string - category?: string - brand?: string - sort?: string - } + body: SearchProductsHook['body'] } } } diff --git a/framework/commerce/types/signup.ts b/framework/commerce/types/signup.ts index 5878bedf9..df5f9aca7 100644 --- a/framework/commerce/types/signup.ts +++ b/framework/commerce/types/signup.ts @@ -9,14 +9,18 @@ export type SignupTypes = { body: SignupBody } +export type SignupHook = { + data: null + body: T['body'] + actionInput: T['body'] + fetchInput: T['body'] +} + export type SignupSchema = { endpoint: { options: {} handlers: { - signup: { - data: null - body: T['body'] - } + signup: SignupHook } } } diff --git a/framework/commerce/types/wishlist.ts b/framework/commerce/types/wishlist.ts index 24c0a7c28..7a547e9fc 100644 --- a/framework/commerce/types/wishlist.ts +++ b/framework/commerce/types/wishlist.ts @@ -11,21 +11,42 @@ export type WishlistTypes = { itemBody: WishlistItemBody } +export type GetWishlistHook = { + data: T['wishlist'] | null + body: { includeProducts?: boolean } + input: { includeProducts?: boolean } + fetchInput: { customerId: string; includeProducts?: boolean } + swrState: { isEmpty: boolean } +} + +export type AddItemHook = { + data: T['wishlist'] + body: { item: T['itemBody'] } + fetchInput: { item: T['itemBody'] } + actionInput: T['itemBody'] +} + +export type RemoveItemHook = { + data: T['wishlist'] | null + body: { itemId: string } + fetchInput: { itemId: string } + actionInput: { id: string } + input: { wishlist?: { includeProducts?: boolean } } +} + export type WishlistSchema = { endpoint: { options: {} handlers: { - getWishlist: { + getWishlist: GetWishlistHook & { data: T['wishlist'] | null - body: { customerToken?: string; includeProducts?: boolean } + body: { customerToken?: string } } - addItem: { - data: T['wishlist'] - body: { customerToken?: string; item: T['itemBody'] } + addItem: AddItemHook & { + body: { customerToken?: string } } - removeItem: { - data: T['wishlist'] | null - body: { customerToken?: string; itemId: string } + removeItem: RemoveItemHook & { + body: { customerToken?: string } } } } diff --git a/framework/commerce/utils/types.ts b/framework/commerce/utils/types.ts index 93fd8153c..e842df58c 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -43,7 +43,12 @@ export type HookFetcherFn = ( export type HookFetcherContext = { options: HookFetcherOptions input: H['fetchInput'] - fetch: (options: FetcherOptions) => Promise + fetch: { + ( + options: FetcherOptions + ): Promise + (options: FetcherOptions): Promise + } } export type HookFetcherOptions = { method?: string } & ( @@ -67,19 +72,21 @@ export type HookFunction< : (input: Input) => T export type HookSchemaBase = { - // Data obj returned by the hook and fetch operation + // Data obj returned by the hook data: any // Input expected by the hook - input: {} + input?: {} // Input expected before doing a fetch operation (aka fetch handler) fetchInput?: {} - // Data expected by the fetch operation + // Body object expected by the fetch operation body?: {} + // Data returned by the fetch operation + fetchData?: any } export type SWRHookSchemaBase = HookSchemaBase & { // Custom state added to the response object of SWR - swrState: {} + swrState?: {} } export type MutationSchemaBase = HookSchemaBase & { @@ -101,8 +108,6 @@ export type SWRHook = { fetcher?: HookFetcherFn } -type X = {} & undefined - export type SWRHookContext = { useData(context?: { input?: HookFetchInput | HookSWRInput diff --git a/framework/commerce/utils/use-data.tsx b/framework/commerce/utils/use-data.tsx index 9224b612c..3bd4e4faf 100644 --- a/framework/commerce/utils/use-data.tsx +++ b/framework/commerce/utils/use-data.tsx @@ -2,10 +2,11 @@ import useSWR, { responseInterface } from 'swr' import type { HookSWRInput, HookFetchInput, - Fetcher, - SwrOptions, HookFetcherOptions, HookFetcherFn, + Fetcher, + SwrOptions, + SWRHookSchemaBase, } from './types' import defineProperty from './define-property' import { CommerceError } from './errors' @@ -14,15 +15,15 @@ export type ResponseState = responseInterface & { isLoading: boolean } -export type UseData = ( +export type UseData = ( options: { fetchOptions: HookFetcherOptions - fetcher: HookFetcherFn + fetcher: HookFetcherFn }, input: HookFetchInput | HookSWRInput, fetcherFn: Fetcher, - swrOptions?: SwrOptions -) => ResponseState + swrOptions?: SwrOptions +) => ResponseState const useData: UseData = (options, input, fetcherFn, swrOptions) => { const hookInput = Array.isArray(input) ? input : Object.entries(input) diff --git a/framework/commerce/utils/use-hook.ts b/framework/commerce/utils/use-hook.ts index da3431e3c..1bf0779c4 100644 --- a/framework/commerce/utils/use-hook.ts +++ b/framework/commerce/utils/use-hook.ts @@ -10,14 +10,14 @@ export function useFetcher() { export function useHook< P extends Provider, - H extends MutationHook | SWRHook + H extends MutationHook | SWRHook >(fn: (provider: P) => H) { const { providerRef } = useCommerce

() const provider = providerRef.current return fn(provider) } -export function useSWRHook>( +export function useSWRHook>( hook: PickRequired ) { const fetcher = useFetcher() @@ -30,7 +30,7 @@ export function useSWRHook>( }) } -export function useMutationHook>( +export function useMutationHook>( hook: PickRequired ) { const fetcher = useFetcher() diff --git a/framework/commerce/wishlist/use-add-item.tsx b/framework/commerce/wishlist/use-add-item.tsx index 11c8cc241..f464be1ca 100644 --- a/framework/commerce/wishlist/use-add-item.tsx +++ b/framework/commerce/wishlist/use-add-item.tsx @@ -1,10 +1,11 @@ import { useHook, useMutationHook } from '../utils/use-hook' import { mutationFetcher } from '../utils/default-fetcher' import type { MutationHook } from '../utils/types' +import type { AddItemHook } from '../types/wishlist' import type { Provider } from '..' export type UseAddItem< - H extends MutationHook = MutationHook + H extends MutationHook> = MutationHook > = ReturnType export const fetcher = mutationFetcher diff --git a/framework/commerce/wishlist/use-remove-item.tsx b/framework/commerce/wishlist/use-remove-item.tsx index c8c34a5af..4419c17af 100644 --- a/framework/commerce/wishlist/use-remove-item.tsx +++ b/framework/commerce/wishlist/use-remove-item.tsx @@ -1,28 +1,20 @@ import { useHook, useMutationHook } from '../utils/use-hook' import { mutationFetcher } from '../utils/default-fetcher' import type { HookFetcherFn, MutationHook } from '../utils/types' +import type { RemoveItemHook } from '../types/wishlist' import type { Provider } from '..' -export type RemoveItemInput = { - id: string | number -} - export type UseRemoveItem< - H extends MutationHook = MutationHook< - any | null, - { wishlist?: any }, - RemoveItemInput, - {} - > + H extends MutationHook> = MutationHook > = ReturnType -export const fetcher: HookFetcherFn = mutationFetcher +export const fetcher: HookFetcherFn = mutationFetcher const fn = (provider: Provider) => provider.wishlist?.useRemoveItem! -const useRemoveItem: UseRemoveItem = (input) => { +const useRemoveItem: UseRemoveItem = (...args) => { const hook = useHook(fn) - return useMutationHook({ fetcher, ...hook })(input) + return useMutationHook({ fetcher, ...hook })(...args) } export default useRemoveItem diff --git a/framework/commerce/wishlist/use-wishlist.tsx b/framework/commerce/wishlist/use-wishlist.tsx index 7a93b20b1..672203f79 100644 --- a/framework/commerce/wishlist/use-wishlist.tsx +++ b/framework/commerce/wishlist/use-wishlist.tsx @@ -1,25 +1,20 @@ import { useHook, useSWRHook } from '../utils/use-hook' import { SWRFetcher } from '../utils/default-fetcher' import type { HookFetcherFn, SWRHook } from '../utils/types' -import type { Wishlist } from '../types' +import type { GetWishlistHook } from '../types/wishlist' import type { Provider } from '..' export type UseWishlist< - H extends SWRHook = SWRHook< - Wishlist | null, - { includeProducts?: boolean }, - { customerId?: number; includeProducts: boolean }, - { isEmpty?: boolean } - > + H extends SWRHook> = SWRHook > = ReturnType -export const fetcher: HookFetcherFn = SWRFetcher +export const fetcher: HookFetcherFn = SWRFetcher const fn = (provider: Provider) => provider.wishlist?.useWishlist! -const useWishlist: UseWishlist = (input) => { +const useWishlist: UseWishlist = (...args) => { const hook = useHook(fn) - return useSWRHook({ fetcher, ...hook })(input) + return useSWRHook({ fetcher, ...hook })(...args) } export default useWishlist