From 746028f25b26309583434ea24451fd4f16840f9b Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Tue, 25 May 2021 21:53:04 -0500 Subject: [PATCH 1/4] Updated urls to the API --- framework/bigcommerce/auth/use-login.tsx | 2 +- framework/bigcommerce/auth/use-logout.tsx | 2 +- framework/bigcommerce/auth/use-signup.tsx | 2 +- framework/bigcommerce/cart/use-add-item.tsx | 2 +- framework/bigcommerce/cart/use-cart.tsx | 2 +- framework/bigcommerce/cart/use-remove-item.tsx | 2 +- framework/bigcommerce/cart/use-update-item.tsx | 4 ++-- framework/bigcommerce/wishlist/use-wishlist.tsx | 2 +- framework/commerce/new-provider.md | 4 ++-- framework/commerce/utils/types.ts | 9 +++------ next.config.js | 4 ++-- 11 files changed, 16 insertions(+), 19 deletions(-) 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 de6ee226c..205ee82c1 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 b32f2d2ec..768c3ffd7 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 44f8cadd5..8d0659ecb 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 6d014cc24..cf1f98226 100644 --- a/framework/bigcommerce/cart/use-update-item.tsx +++ b/framework/bigcommerce/cart/use-update-item.tsx @@ -6,7 +6,7 @@ import type { } from '@commerce/utils/types' import { ValidationError } from '@commerce/utils/errors' import useUpdateItem, { UseUpdateItem } from '@commerce/cart/use-update-item' -import type { LineItem, UpdateItemHook } from '../types' +import type { LineItem, UpdateItemHook } from '../types/cart' import { handler as removeItemHandler } from './use-remove-item' import useCart from './use-cart' @@ -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/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/utils/types.ts b/framework/commerce/utils/types.ts index e842df58c..f95d1148a 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -43,12 +43,9 @@ export type HookFetcherFn = ( export type HookFetcherContext = { options: HookFetcherOptions input: H['fetchInput'] - fetch: { - ( - options: FetcherOptions - ): Promise - (options: FetcherOptions): Promise - } + fetch: ( + options: FetcherOptions + ) => Promise } export type HookFetcherOptions = { method?: string } & ( diff --git a/next.config.js b/next.config.js index c8694f1e0..128849678 100644 --- a/next.config.js +++ b/next.config.js @@ -18,13 +18,13 @@ module.exports = withCommerceConfig({ return [ (isBC || isShopify) && { source: '/checkout', - destination: '/api/bigcommerce/checkout', + destination: '/api/checkout', }, // The logout is also an action so this route is not required, but it's also another way // you can allow a logout! isBC && { source: '/logout', - destination: '/api/bigcommerce/customers/logout?redirect_to=/', + destination: '/api/logout?redirect_to=/', }, // Rewrites for /search { From 3b74f8d854c61e648498c81ac2d2bf600d8d5152 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Tue, 25 May 2021 21:54:13 -0500 Subject: [PATCH 2/4] Renamed fetchInput to fetcherInput --- framework/commerce/types/cart.ts | 8 ++++---- framework/commerce/types/customer.ts | 2 -- framework/commerce/types/login.ts | 2 +- framework/commerce/types/product.ts | 2 +- framework/commerce/types/signup.ts | 2 +- framework/commerce/types/wishlist.ts | 6 +++--- framework/commerce/utils/types.ts | 18 ++++++++++-------- framework/commerce/utils/use-data.tsx | 2 +- 8 files changed, 21 insertions(+), 21 deletions(-) diff --git a/framework/commerce/types/cart.ts b/framework/commerce/types/cart.ts index 3ab6eb867..4cd9d13da 100644 --- a/framework/commerce/types/cart.ts +++ b/framework/commerce/types/cart.ts @@ -101,14 +101,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'] + fetcherInput: T['itemBody'] body: { item: T['itemBody'] } actionInput: T['itemBody'] } @@ -116,7 +116,7 @@ export type AddItemHook = { export type UpdateItemHook = { data: T['cart'] | null input: { item?: T['item']; wait?: number } - fetchInput: { itemId: string; item: T['itemBody'] } + fetcherInput: { itemId: string; item: T['itemBody'] } body: { itemId: string; item: T['itemBody'] } actionInput: T['itemBody'] & { id: string } } @@ -124,7 +124,7 @@ export type UpdateItemHook = { 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 2ac45389e..782f2a31a 100644 --- a/framework/commerce/types/product.ts +++ b/framework/commerce/types/product.ts @@ -58,7 +58,7 @@ export type SearchProductsHook = { data: T['product'][] body: T['searchBody'] input: T['searchBody'] - fetchInput: T['searchBody'] + fetcherInput: T['searchBody'] } export type ProductsSchema = { 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 f95d1148a..fe1507d08 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -42,7 +42,7 @@ export type HookFetcherFn = ( export type HookFetcherContext = { options: HookFetcherOptions - input: H['fetchInput'] + input: H['fetcherInput'] fetch: ( options: FetcherOptions ) => Promise @@ -74,7 +74,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 @@ -98,7 +98,7 @@ export type SWRHook = { useHook( context: SWRHookContext ): HookFunction< - H['input'] & { swrOptions?: SwrOptions }, + H['input'] & { swrOptions?: SwrOptions }, ResponseState & H['swrState'] > fetchOptions: HookFetcherOptions @@ -108,7 +108,7 @@ export type SWRHook = { export type SWRHookContext = { useData(context?: { input?: HookFetchInput | HookSWRInput - swrOptions?: SwrOptions + swrOptions?: SwrOptions }): ResponseState } @@ -127,11 +127,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) => { From 874cf9d5b3777789436f83c579cb0b6415fcbcd6 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Fri, 28 May 2021 11:50:33 -0500 Subject: [PATCH 3/4] Updated fetch type --- framework/commerce/types/cart.ts | 2 +- framework/commerce/utils/types.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/framework/commerce/types/cart.ts b/framework/commerce/types/cart.ts index 4cd9d13da..e27374d98 100644 --- a/framework/commerce/types/cart.ts +++ b/framework/commerce/types/cart.ts @@ -107,7 +107,7 @@ export type GetCartHook = { export type AddItemHook = { data: T['cart'] - input: T['itemBody'] + input?: T['itemBody'] fetcherInput: T['itemBody'] body: { item: T['itemBody'] } actionInput: T['itemBody'] diff --git a/framework/commerce/utils/types.ts b/framework/commerce/utils/types.ts index fe1507d08..751cea4a5 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -43,7 +43,10 @@ export type HookFetcherFn = ( export type HookFetcherContext = { options: HookFetcherOptions input: H['fetcherInput'] - fetch: ( + fetch: < + T = H['fetchData'] extends {} | null ? H['fetchData'] : any, + B = H['body'] + >( options: FetcherOptions ) => Promise } From 7f2a0d903aa76f0317e376b36fbb999a75c691a2 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Fri, 28 May 2021 12:56:50 -0500 Subject: [PATCH 4/4] Fixes in search hook --- framework/bigcommerce/product/use-search.tsx | 4 ++-- framework/commerce/types/product.ts | 13 +++++-------- pages/search.tsx | 1 - 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/framework/bigcommerce/product/use-search.tsx b/framework/bigcommerce/product/use-search.tsx index c9ac8f9a0..3c81d1171 100644 --- a/framework/bigcommerce/product/use-search.tsx +++ b/framework/bigcommerce/product/use-search.tsx @@ -22,9 +22,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/commerce/types/product.ts b/framework/commerce/types/product.ts index 782f2a31a..27e98598a 100644 --- a/framework/commerce/types/product.ts +++ b/framework/commerce/types/product.ts @@ -55,7 +55,10 @@ export type ProductTypes = { } export type SearchProductsHook = { - data: T['product'][] + data: { + products: T['product'][] + found: boolean + } body: T['searchBody'] input: T['searchBody'] fetcherInput: T['searchBody'] @@ -65,13 +68,7 @@ export type ProductsSchema = { endpoint: { options: {} handlers: { - getProducts: { - data: { - products: T['product'][] - found: boolean - } - body: SearchProductsHook['body'] - } + getProducts: SearchProductsHook } } } diff --git a/pages/search.tsx b/pages/search.tsx index 53452b74a..400217a26 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -18,7 +18,6 @@ import { getDesignerPath, useSearchMeta, } from '@lib/search' -import { Product } from '@commerce/types' // TODO(bc) Remove this. This should come from the API import getSlug from '@lib/get-slug'