diff --git a/framework/bigcommerce/auth/use-login.tsx b/framework/bigcommerce/auth/use-login.tsx index bf2360af6..3ebacc9b7 100644 --- a/framework/bigcommerce/auth/use-login.tsx +++ b/framework/bigcommerce/auth/use-login.tsx @@ -9,7 +9,7 @@ export default useLogin as UseLogin export const handler: MutationHook = { fetchOptions: { - url: '/api/bigcommerce/customers/login', + url: '/api/login', method: 'POST', }, async fetcher({ input: { email, password }, options, fetch }) { diff --git a/framework/bigcommerce/auth/use-logout.tsx b/framework/bigcommerce/auth/use-logout.tsx index f704563e9..e75563e04 100644 --- a/framework/bigcommerce/auth/use-logout.tsx +++ b/framework/bigcommerce/auth/use-logout.tsx @@ -8,7 +8,7 @@ export default useLogout as UseLogout export const handler: MutationHook = { fetchOptions: { - url: '/api/bigcommerce/customers/logout', + url: '/api/logout', method: 'GET', }, useHook: ({ fetch }) => () => { diff --git a/framework/bigcommerce/auth/use-signup.tsx b/framework/bigcommerce/auth/use-signup.tsx index bcd6886a3..da06fd3eb 100644 --- a/framework/bigcommerce/auth/use-signup.tsx +++ b/framework/bigcommerce/auth/use-signup.tsx @@ -9,7 +9,7 @@ export default useSignup as UseSignup export const handler: MutationHook = { fetchOptions: { - url: '/api/bigcommerce/customers/signup', + url: '/api/signup', method: 'POST', }, async fetcher({ diff --git a/framework/bigcommerce/cart/use-add-item.tsx b/framework/bigcommerce/cart/use-add-item.tsx index 627697db6..1ac6ac6f8 100644 --- a/framework/bigcommerce/cart/use-add-item.tsx +++ b/framework/bigcommerce/cart/use-add-item.tsx @@ -9,7 +9,7 @@ export default useAddItem as UseAddItem export const handler: MutationHook = { fetchOptions: { - url: '/api/bigcommerce/cart', + url: '/api/cart', method: 'POST', }, async fetcher({ input: item, options, fetch }) { diff --git a/framework/bigcommerce/cart/use-cart.tsx b/framework/bigcommerce/cart/use-cart.tsx index 9e8cfc9a1..4ba1724d9 100644 --- a/framework/bigcommerce/cart/use-cart.tsx +++ b/framework/bigcommerce/cart/use-cart.tsx @@ -7,7 +7,7 @@ export default useCart as UseCart export const handler: SWRHook = { fetchOptions: { - url: '/api/bigcommerce/cart', + url: '/api/cart', method: 'GET', }, useHook: ({ useData }) => (input) => { diff --git a/framework/bigcommerce/cart/use-remove-item.tsx b/framework/bigcommerce/cart/use-remove-item.tsx index 605fbe74e..1376f29ce 100644 --- a/framework/bigcommerce/cart/use-remove-item.tsx +++ b/framework/bigcommerce/cart/use-remove-item.tsx @@ -20,7 +20,7 @@ export default useRemoveItem as UseRemoveItem export const handler = { fetchOptions: { - url: '/api/bigcommerce/cart', + url: '/api/cart', method: 'DELETE', }, async fetcher({ diff --git a/framework/bigcommerce/cart/use-update-item.tsx b/framework/bigcommerce/cart/use-update-item.tsx index 0c97d3762..0f9f5754d 100644 --- a/framework/bigcommerce/cart/use-update-item.tsx +++ b/framework/bigcommerce/cart/use-update-item.tsx @@ -18,7 +18,7 @@ export default useUpdateItem as UseUpdateItem export const handler = { fetchOptions: { - url: '/api/bigcommerce/cart', + url: '/api/cart', method: 'PUT', }, async fetcher({ diff --git a/framework/bigcommerce/product/use-search.tsx b/framework/bigcommerce/product/use-search.tsx index de4e1ac48..4aa4f4246 100644 --- a/framework/bigcommerce/product/use-search.tsx +++ b/framework/bigcommerce/product/use-search.tsx @@ -23,9 +23,9 @@ export const handler: SWRHook = { if (search) url.searchParams.set('search', search) if (Number.isInteger(categoryId)) - url.searchParams.set('category', String(categoryId)) + url.searchParams.set('categoryId', String(categoryId)) if (Number.isInteger(brandId)) - url.searchParams.set('brand', String(brandId)) + url.searchParams.set('brandId', String(brandId)) if (sort) url.searchParams.set('sort', sort) return fetch({ diff --git a/framework/bigcommerce/wishlist/use-wishlist.tsx b/framework/bigcommerce/wishlist/use-wishlist.tsx index 134e9cc24..b8fc946e3 100644 --- a/framework/bigcommerce/wishlist/use-wishlist.tsx +++ b/framework/bigcommerce/wishlist/use-wishlist.tsx @@ -8,7 +8,7 @@ export default useWishlist as UseWishlist export const handler: SWRHook = { fetchOptions: { - url: '/api/bigcommerce/wishlist', + url: '/api/wishlist', method: 'GET', }, async fetcher({ input: { customerId, includeProducts }, options, fetch }) { diff --git a/framework/commerce/new-provider.md b/framework/commerce/new-provider.md index 4051c0f01..511704af6 100644 --- a/framework/commerce/new-provider.md +++ b/framework/commerce/new-provider.md @@ -149,7 +149,7 @@ export const handler: SWRHook< { isEmpty?: boolean } > = { fetchOptions: { - url: '/api/bigcommerce/cart', + url: '/api/cart', method: 'GET', }, async fetcher({ input: { cartId }, options, fetch }) { @@ -197,7 +197,7 @@ export default useAddItem as UseAddItem export const handler: MutationHook = { fetchOptions: { - url: '/api/bigcommerce/cart', + url: '/api/cart', method: 'POST', }, async fetcher({ input: item, options, fetch }) { diff --git a/framework/commerce/types/cart.ts b/framework/commerce/types/cart.ts index 81fd8a2e6..7826f9b2d 100644 --- a/framework/commerce/types/cart.ts +++ b/framework/commerce/types/cart.ts @@ -111,14 +111,14 @@ export type CartHooks = { export type GetCartHook = { data: T['cart'] | null input: {} - fetchInput: { cartId?: string } + fetcherInput: { cartId?: string } swrState: { isEmpty: boolean } } export type AddItemHook = { data: T['cart'] - input: T['itemBody'] - fetchInput: T['itemBody'] + input?: T['itemBody'] + fetcherInput: T['itemBody'] body: { item: T['itemBody'] } actionInput: T['itemBody'] } @@ -126,15 +126,15 @@ export type AddItemHook = { export type UpdateItemHook = { data: T['cart'] | null input: { item?: T['item']; wait?: number } - fetchInput: { itemId: string; item: T['itemBody'] } - body: { itemId: string; item?: T['itemBody'] } + fetcherInput: { itemId: string; item: T['itemBody'] } + body: { itemId: string; item: T['itemBody'] } actionInput: T['itemBody'] & { id: string } } export type RemoveItemHook = { data: T['cart'] | null input: { item?: T['item'] } - fetchInput: { itemId: string } + fetcherInput: { itemId: string } body: { itemId: string } actionInput: { id: string } } diff --git a/framework/commerce/types/customer.ts b/framework/commerce/types/customer.ts index 4df0fade3..ba90acdf4 100644 --- a/framework/commerce/types/customer.ts +++ b/framework/commerce/types/customer.ts @@ -8,8 +8,6 @@ export type CustomerTypes = { export type CustomerHook = { data: T['customer'] | null fetchData: { customer: T['customer'] } | null - // actionInput: T['body'] - // fetchInput: T['body'] } export type CustomerSchema = { diff --git a/framework/commerce/types/login.ts b/framework/commerce/types/login.ts index 031574a33..b6ef228e0 100644 --- a/framework/commerce/types/login.ts +++ b/framework/commerce/types/login.ts @@ -10,7 +10,7 @@ export type LoginTypes = { export type LoginHook = { data: null actionInput: LoginBody - fetchInput: LoginBody + fetcherInput: LoginBody body: T['body'] } diff --git a/framework/commerce/types/product.ts b/framework/commerce/types/product.ts index 2339645e5..4db8269e3 100644 --- a/framework/commerce/types/product.ts +++ b/framework/commerce/types/product.ts @@ -65,20 +65,14 @@ export type SearchProductsHook = { } body: T['searchBody'] input: T['searchBody'] - fetchInput: T['searchBody'] + fetcherInput: T['searchBody'] } export type ProductsSchema = { endpoint: { options: {} handlers: { - getProducts: { - data: { - products: T['product'][] - found: boolean - } - body: SearchProductsHook['body'] - } + getProducts: SearchProductsHook } } } diff --git a/framework/commerce/types/signup.ts b/framework/commerce/types/signup.ts index df5f9aca7..4e23da6c0 100644 --- a/framework/commerce/types/signup.ts +++ b/framework/commerce/types/signup.ts @@ -13,7 +13,7 @@ export type SignupHook = { data: null body: T['body'] actionInput: T['body'] - fetchInput: T['body'] + fetcherInput: T['body'] } export type SignupSchema = { diff --git a/framework/commerce/types/wishlist.ts b/framework/commerce/types/wishlist.ts index 7a547e9fc..e71cdc7e4 100644 --- a/framework/commerce/types/wishlist.ts +++ b/framework/commerce/types/wishlist.ts @@ -15,21 +15,21 @@ export type GetWishlistHook = { data: T['wishlist'] | null body: { includeProducts?: boolean } input: { includeProducts?: boolean } - fetchInput: { customerId: string; includeProducts?: boolean } + fetcherInput: { customerId: string; includeProducts?: boolean } swrState: { isEmpty: boolean } } export type AddItemHook = { data: T['wishlist'] body: { item: T['itemBody'] } - fetchInput: { item: T['itemBody'] } + fetcherInput: { item: T['itemBody'] } actionInput: T['itemBody'] } export type RemoveItemHook = { data: T['wishlist'] | null body: { itemId: string } - fetchInput: { itemId: string } + fetcherInput: { itemId: string } actionInput: { id: string } input: { wishlist?: { includeProducts?: boolean } } } diff --git a/framework/commerce/utils/types.ts b/framework/commerce/utils/types.ts index e842df58c..751cea4a5 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -42,13 +42,13 @@ export type HookFetcherFn = ( export type HookFetcherContext = { options: HookFetcherOptions - input: H['fetchInput'] - fetch: { - ( - options: FetcherOptions - ): Promise - (options: FetcherOptions): Promise - } + input: H['fetcherInput'] + fetch: < + T = H['fetchData'] extends {} | null ? H['fetchData'] : any, + B = H['body'] + >( + options: FetcherOptions + ) => Promise } export type HookFetcherOptions = { method?: string } & ( @@ -77,7 +77,7 @@ export type HookSchemaBase = { // Input expected by the hook input?: {} // Input expected before doing a fetch operation (aka fetch handler) - fetchInput?: {} + fetcherInput?: {} // Body object expected by the fetch operation body?: {} // Data returned by the fetch operation @@ -101,7 +101,7 @@ export type SWRHook = { useHook( context: SWRHookContext ): HookFunction< - H['input'] & { swrOptions?: SwrOptions }, + H['input'] & { swrOptions?: SwrOptions }, ResponseState & H['swrState'] > fetchOptions: HookFetcherOptions @@ -111,7 +111,7 @@ export type SWRHook = { export type SWRHookContext = { useData(context?: { input?: HookFetchInput | HookSWRInput - swrOptions?: SwrOptions + swrOptions?: SwrOptions }): ResponseState } @@ -130,11 +130,13 @@ export type MutationHook = { } export type MutationHookContext = { - fetch: keyof H['fetchInput'] extends never + fetch: keyof H['fetcherInput'] extends never ? () => H['data'] | Promise - : Partial extends H['fetchInput'] - ? (context?: { input?: H['fetchInput'] }) => H['data'] | Promise - : (context: { input: H['fetchInput'] }) => H['data'] | Promise + : Partial extends H['fetcherInput'] + ? (context?: { + input?: H['fetcherInput'] + }) => H['data'] | Promise + : (context: { input: H['fetcherInput'] }) => H['data'] | Promise } export type SwrOptions = ConfigInterface< diff --git a/framework/commerce/utils/use-data.tsx b/framework/commerce/utils/use-data.tsx index 3bd4e4faf..4fc208bab 100644 --- a/framework/commerce/utils/use-data.tsx +++ b/framework/commerce/utils/use-data.tsx @@ -22,7 +22,7 @@ export type UseData = ( }, input: HookFetchInput | HookSWRInput, fetcherFn: Fetcher, - swrOptions?: SwrOptions + swrOptions?: SwrOptions ) => ResponseState const useData: UseData = (options, input, fetcherFn, swrOptions) => { diff --git a/next.config.js b/next.config.js index c5db9e3b4..128849678 100644 --- a/next.config.js +++ b/next.config.js @@ -24,7 +24,7 @@ module.exports = withCommerceConfig({ // you can allow a logout! isBC && { source: '/logout', - destination: '/api/bigcommerce/customers/logout?redirect_to=/', + destination: '/api/logout?redirect_to=/', }, // Rewrites for /search { diff --git a/pages/search.tsx b/pages/search.tsx index ef7c2ba8f..57b8de80f 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -6,6 +6,7 @@ import { useRouter } from 'next/router' import { Layout } from '@components/common' import { ProductCard } from '@components/product' +import type { Product } from '@commerce/types/product' import { Container, Grid, Skeleton } from '@components/ui' import useSearch from '@framework/product/use-search' @@ -19,8 +20,6 @@ import { useSearchMeta, } from '@lib/search' -import type { Product } from '@commerce/types/product' - // TODO(bc) Remove this. This should come from the API import getSlug from '@lib/get-slug' @@ -35,9 +34,8 @@ const SORT = Object.entries({ export async function getStaticProps({ preview, locale, - locales, }: GetStaticPropsContext) { - const config = { locale, locales } + const config = { locale } const { pages } = await commerce.getAllPages({ config, preview }) const { categories, brands } = await commerce.getSiteInfo({ config, preview }) return {