Updated defaults in all hooks

This commit is contained in:
Luis Alvarez 2020-10-21 10:35:49 -05:00
parent d98897a7e3
commit 2804627aef
11 changed files with 31 additions and 20 deletions

View File

@ -26,8 +26,8 @@ export const fetcher: HookFetcher<Cart, AddItemBody> = (
}
return fetch({
url: options?.url ?? defaultOpts.url,
method: options?.method ?? defaultOpts.method,
...defaultOpts,
...options,
body: { item },
})
}

View File

@ -5,6 +5,7 @@ import type { Cart } from '../api/cart'
const defaultOpts = {
url: '/api/bigcommerce/cart',
method: 'GET',
}
export type { Cart }
@ -14,7 +15,7 @@ export const fetcher: HookFetcher<Cart | null, CartInput> = (
{ cartId },
fetch
) => {
return cartId ? fetch({ ...options }) : null
return cartId ? fetch({ ...defaultOpts, ...options }) : null
}
export function extendHook(

View File

@ -19,8 +19,8 @@ export const fetcher: HookFetcher<Cart | null, RemoveItemBody> = (
fetch
) => {
return fetch({
url: options?.url ?? defaultOpts.url,
method: options?.method ?? defaultOpts.method,
...defaultOpts,
...options,
body: { itemId },
})
}

View File

@ -28,8 +28,8 @@ export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = (
}
return fetch({
url: options?.url ?? defaultOpts.url,
method: options?.method ?? defaultOpts.method,
...defaultOpts,
...options,
body: { itemId, item },
})
}

View File

@ -5,6 +5,7 @@ import type { Customer, CustomerData } from './api/customers'
const defaultOpts = {
url: '/api/bigcommerce/customer',
method: 'GET',
}
export type { Customer }
@ -14,7 +15,7 @@ export const fetcher: HookFetcher<CustomerData | null, CustomerInput> = (
{ cartId },
fetch
) => {
return cartId ? fetch({ ...options }) : null
return cartId ? fetch({ ...defaultOpts, ...options }) : null
}
export function extendHook(

View File

@ -22,8 +22,8 @@ export const fetcher: HookFetcher<null, LoginBody> = (
}
return fetch({
url: options?.url ?? defaultOpts.url,
method: options?.method ?? defaultOpts.method,
...defaultOpts,
...options,
body: { email, password },
})
}

View File

@ -22,8 +22,8 @@ export const fetcher: HookFetcher<null, SignupBody> = (
}
return fetch({
url: options?.url ?? defaultOpts.url,
method: options?.method ?? defaultOpts.method,
...defaultOpts,
...options,
body: { firstName, lastName, email, password },
})
}

View File

@ -17,8 +17,8 @@ export const fetcher: HookFetcher<Wishlist, AddItemBody> = (
fetch
) => {
return fetch({
url: options?.url ?? defaultOpts.url,
method: options?.method ?? defaultOpts.method,
...defaultOpts,
...options,
body: { wishlistId, item },
})
}

View File

@ -19,8 +19,8 @@ export const fetcher: HookFetcher<Wishlist | null, RemoveItemBody> = (
fetch
) => {
return fetch({
url: options?.url ?? defaultOpts.url,
method: options?.method ?? defaultOpts.method,
...defaultOpts,
...options,
body: { wishlistId, itemId },
})
}

View File

@ -4,6 +4,7 @@ import type { Wishlist } from '../api/wishlist'
const defaultOpts = {
url: '/api/bigcommerce/wishlists',
method: 'GET',
}
export type { Wishlist }
@ -18,7 +19,8 @@ export const fetcher: HookFetcher<Wishlist | null, WishlistInput> = (
fetch
) => {
return fetch({
url: options?.url,
...defaultOpts,
...options,
body: { wishlistId },
})
}

View File

@ -9,9 +9,14 @@ export default function useData<T, Input = any>(
swrOptions?: ConfigInterface<T>
) {
const { fetcherRef } = useCommerce()
const fetcher = (url?: string, query?: string, ...args: any[]) => {
const fetcher = (
url?: string,
query?: string,
method?: string,
...args: any[]
) => {
return fetcherFn(
{ url, query },
{ url, query, method },
// Transform the input array into an object
args.reduce((obj, val, i) => {
obj[input[i][0]!] = val
@ -24,7 +29,9 @@ export default function useData<T, Input = any>(
const response = useSWR(
() => {
const opts = typeof options === 'function' ? options() : options
return opts ? [opts.url, opts.query, ...input.map((e) => e[1])] : null
return opts
? [opts.url, opts.query, opts.method, ...input.map((e) => e[1])]
: null
},
fetcher,
swrOptions