Merge branch 'nodejs-provider' of https://github.com/vercel/commerce into shopify-updates

This commit is contained in:
cond0r 2021-05-29 09:25:45 +03:00
commit 55fa2fdd81
20 changed files with 45 additions and 53 deletions

View File

@ -9,7 +9,7 @@ export default useLogin as UseLogin<typeof handler>
export const handler: MutationHook<LoginHook> = {
fetchOptions: {
url: '/api/bigcommerce/customers/login',
url: '/api/login',
method: 'POST',
},
async fetcher({ input: { email, password }, options, fetch }) {

View File

@ -8,7 +8,7 @@ export default useLogout as UseLogout<typeof handler>
export const handler: MutationHook<LogoutHook> = {
fetchOptions: {
url: '/api/bigcommerce/customers/logout',
url: '/api/logout',
method: 'GET',
},
useHook: ({ fetch }) => () => {

View File

@ -9,7 +9,7 @@ export default useSignup as UseSignup<typeof handler>
export const handler: MutationHook<SignupHook> = {
fetchOptions: {
url: '/api/bigcommerce/customers/signup',
url: '/api/signup',
method: 'POST',
},
async fetcher({

View File

@ -9,7 +9,7 @@ export default useAddItem as UseAddItem<typeof handler>
export const handler: MutationHook<AddItemHook> = {
fetchOptions: {
url: '/api/bigcommerce/cart',
url: '/api/cart',
method: 'POST',
},
async fetcher({ input: item, options, fetch }) {

View File

@ -7,7 +7,7 @@ export default useCart as UseCart<typeof handler>
export const handler: SWRHook<GetCartHook> = {
fetchOptions: {
url: '/api/bigcommerce/cart',
url: '/api/cart',
method: 'GET',
},
useHook: ({ useData }) => (input) => {

View File

@ -20,7 +20,7 @@ export default useRemoveItem as UseRemoveItem<typeof handler>
export const handler = {
fetchOptions: {
url: '/api/bigcommerce/cart',
url: '/api/cart',
method: 'DELETE',
},
async fetcher({

View File

@ -18,7 +18,7 @@ export default useUpdateItem as UseUpdateItem<typeof handler>
export const handler = {
fetchOptions: {
url: '/api/bigcommerce/cart',
url: '/api/cart',
method: 'PUT',
},
async fetcher({

View File

@ -23,9 +23,9 @@ export const handler: SWRHook<SearchProductsHook> = {
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({

View File

@ -8,7 +8,7 @@ export default useWishlist as UseWishlist<typeof handler>
export const handler: SWRHook<GetWishlistHook> = {
fetchOptions: {
url: '/api/bigcommerce/wishlist',
url: '/api/wishlist',
method: 'GET',
},
async fetcher({ input: { customerId, includeProducts }, options, fetch }) {

View File

@ -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<typeof handler>
export const handler: MutationHook<Cart, {}, CartItemBody> = {
fetchOptions: {
url: '/api/bigcommerce/cart',
url: '/api/cart',
method: 'POST',
},
async fetcher({ input: item, options, fetch }) {

View File

@ -111,14 +111,14 @@ export type CartHooks<T extends CartTypes = CartTypes> = {
export type GetCartHook<T extends CartTypes = CartTypes> = {
data: T['cart'] | null
input: {}
fetchInput: { cartId?: string }
fetcherInput: { cartId?: string }
swrState: { isEmpty: boolean }
}
export type AddItemHook<T extends CartTypes = CartTypes> = {
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<T extends CartTypes = CartTypes> = {
export type UpdateItemHook<T extends CartTypes = CartTypes> = {
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<T extends CartTypes = CartTypes> = {
data: T['cart'] | null
input: { item?: T['item'] }
fetchInput: { itemId: string }
fetcherInput: { itemId: string }
body: { itemId: string }
actionInput: { id: string }
}

View File

@ -8,8 +8,6 @@ export type CustomerTypes = {
export type CustomerHook<T extends CustomerTypes = CustomerTypes> = {
data: T['customer'] | null
fetchData: { customer: T['customer'] } | null
// actionInput: T['body']
// fetchInput: T['body']
}
export type CustomerSchema<T extends CustomerTypes = CustomerTypes> = {

View File

@ -10,7 +10,7 @@ export type LoginTypes = {
export type LoginHook<T extends LoginTypes = LoginTypes> = {
data: null
actionInput: LoginBody
fetchInput: LoginBody
fetcherInput: LoginBody
body: T['body']
}

View File

@ -65,20 +65,14 @@ export type SearchProductsHook<T extends ProductTypes = ProductTypes> = {
}
body: T['searchBody']
input: T['searchBody']
fetchInput: T['searchBody']
fetcherInput: T['searchBody']
}
export type ProductsSchema<T extends ProductTypes = ProductTypes> = {
endpoint: {
options: {}
handlers: {
getProducts: {
data: {
products: T['product'][]
found: boolean
}
body: SearchProductsHook<T>['body']
}
getProducts: SearchProductsHook<T>
}
}
}

View File

@ -13,7 +13,7 @@ export type SignupHook<T extends SignupTypes = SignupTypes> = {
data: null
body: T['body']
actionInput: T['body']
fetchInput: T['body']
fetcherInput: T['body']
}
export type SignupSchema<T extends SignupTypes = SignupTypes> = {

View File

@ -15,21 +15,21 @@ export type GetWishlistHook<T extends WishlistTypes = WishlistTypes> = {
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<T extends WishlistTypes = WishlistTypes> = {
data: T['wishlist']
body: { item: T['itemBody'] }
fetchInput: { item: T['itemBody'] }
fetcherInput: { item: T['itemBody'] }
actionInput: T['itemBody']
}
export type RemoveItemHook<T extends WishlistTypes = WishlistTypes> = {
data: T['wishlist'] | null
body: { itemId: string }
fetchInput: { itemId: string }
fetcherInput: { itemId: string }
actionInput: { id: string }
input: { wishlist?: { includeProducts?: boolean } }
}

View File

@ -42,13 +42,13 @@ export type HookFetcherFn<H extends HookSchemaBase> = (
export type HookFetcherContext<H extends HookSchemaBase> = {
options: HookFetcherOptions
input: H['fetchInput']
fetch: {
(
options: FetcherOptions<H['body'] extends {} ? H['body'] : never>
): Promise<H['fetchData'] extends {} | null ? H['fetchData'] : any>
<T = any, B = any>(options: FetcherOptions<B>): Promise<T>
}
input: H['fetcherInput']
fetch: <
T = H['fetchData'] extends {} | null ? H['fetchData'] : any,
B = H['body']
>(
options: FetcherOptions<B>
) => Promise<T>
}
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<H extends SWRHookSchemaBase> = {
useHook(
context: SWRHookContext<H>
): HookFunction<
H['input'] & { swrOptions?: SwrOptions<H['data'], H['fetchInput']> },
H['input'] & { swrOptions?: SwrOptions<H['data'], H['fetcherInput']> },
ResponseState<H['data']> & H['swrState']
>
fetchOptions: HookFetcherOptions
@ -111,7 +111,7 @@ export type SWRHook<H extends SWRHookSchemaBase> = {
export type SWRHookContext<H extends SWRHookSchemaBase> = {
useData(context?: {
input?: HookFetchInput | HookSWRInput
swrOptions?: SwrOptions<H['data'], H['fetchInput']>
swrOptions?: SwrOptions<H['data'], H['fetcherInput']>
}): ResponseState<H['data']>
}
@ -130,11 +130,13 @@ export type MutationHook<H extends MutationSchemaBase> = {
}
export type MutationHookContext<H extends MutationSchemaBase> = {
fetch: keyof H['fetchInput'] extends never
fetch: keyof H['fetcherInput'] extends never
? () => H['data'] | Promise<H['data']>
: Partial<H['fetchInput']> extends H['fetchInput']
? (context?: { input?: H['fetchInput'] }) => H['data'] | Promise<H['data']>
: (context: { input: H['fetchInput'] }) => H['data'] | Promise<H['data']>
: Partial<H['fetcherInput']> extends H['fetcherInput']
? (context?: {
input?: H['fetcherInput']
}) => H['data'] | Promise<H['data']>
: (context: { input: H['fetcherInput'] }) => H['data'] | Promise<H['data']>
}
export type SwrOptions<Data, Input = null, Result = any> = ConfigInterface<

View File

@ -22,7 +22,7 @@ export type UseData = <H extends SWRHookSchemaBase>(
},
input: HookFetchInput | HookSWRInput,
fetcherFn: Fetcher,
swrOptions?: SwrOptions<H['data'], H['fetchInput']>
swrOptions?: SwrOptions<H['data'], H['fetcherInput']>
) => ResponseState<H['data']>
const useData: UseData = (options, input, fetcherFn, swrOptions) => {

View File

@ -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
{

View File

@ -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 {