forked from crowetic/commerce
Updated api handler
This commit is contained in:
parent
5165f824e1
commit
6d241937d9
@ -109,4 +109,4 @@ const cartApi: BigcommerceApiHandler<Cart, CartHandlers> = async (
|
|||||||
|
|
||||||
export const handlers = { getCart, addItem, updateItem, removeItem }
|
export const handlers = { getCart, addItem, updateItem, removeItem }
|
||||||
|
|
||||||
export default createApiHandler(cartApi, handlers)
|
export default createApiHandler(cartApi, handlers, {})
|
||||||
|
@ -45,4 +45,4 @@ const cartApi: BigcommerceApiHandler<
|
|||||||
|
|
||||||
export const handlers = { getProducts }
|
export const handlers = { getProducts }
|
||||||
|
|
||||||
export default createApiHandler(cartApi, handlers)
|
export default createApiHandler(cartApi, handlers, {})
|
||||||
|
@ -73,4 +73,4 @@ const checkoutApi: BigcommerceApiHandler<any> = async (req, res, config) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default createApiHandler(checkoutApi, {})
|
export default createApiHandler(checkoutApi, {}, {})
|
||||||
|
@ -53,4 +53,4 @@ const handlers = {
|
|||||||
createCustomer,
|
createCustomer,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default createApiHandler(customersApi, handlers)
|
export default createApiHandler(customersApi, handlers, {})
|
||||||
|
@ -3,12 +3,15 @@ import { BigcommerceConfig, getConfig } from '..'
|
|||||||
|
|
||||||
export type BigcommerceApiHandler<
|
export type BigcommerceApiHandler<
|
||||||
T = any,
|
T = any,
|
||||||
H extends BigcommerceHandlers = {}
|
H extends BigcommerceHandlers = {},
|
||||||
|
Options extends {} = {}
|
||||||
> = (
|
> = (
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse<BigcommerceApiResponse<T>>,
|
res: NextApiResponse<BigcommerceApiResponse<T>>,
|
||||||
config: BigcommerceConfig,
|
config: BigcommerceConfig,
|
||||||
handlers: H
|
handlers: H,
|
||||||
|
// Custom configs that may be used by a particular handler
|
||||||
|
options: Options
|
||||||
) => void | Promise<void>
|
) => void | Promise<void>
|
||||||
|
|
||||||
export type BigcommerceHandler<T = any, Body = any> = (options: {
|
export type BigcommerceHandler<T = any, Body = any> = (options: {
|
||||||
@ -27,21 +30,29 @@ export type BigcommerceApiResponse<T> = {
|
|||||||
errors?: { message: string }[]
|
errors?: { message: string }[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function createApiHandler<H extends BigcommerceHandlers>(
|
export default function createApiHandler<
|
||||||
handler: BigcommerceApiHandler<any, H>,
|
T = any,
|
||||||
handlers: H
|
H extends BigcommerceHandlers = {},
|
||||||
|
Options extends {} = {}
|
||||||
|
>(
|
||||||
|
handler: BigcommerceApiHandler<T, H, Options>,
|
||||||
|
handlers: H,
|
||||||
|
defaultOptions: Options
|
||||||
) {
|
) {
|
||||||
return function getApiHandler({
|
return function getApiHandler({
|
||||||
config,
|
config,
|
||||||
operations,
|
operations,
|
||||||
|
options,
|
||||||
}: {
|
}: {
|
||||||
config?: BigcommerceConfig
|
config?: BigcommerceConfig
|
||||||
operations?: Partial<H>
|
operations?: Partial<H>
|
||||||
|
options?: Options extends {} ? Partial<Options> : never
|
||||||
} = {}): NextApiHandler {
|
} = {}): NextApiHandler {
|
||||||
const ops = { ...operations, ...handlers }
|
const ops = { ...operations, ...handlers }
|
||||||
|
const opts = { ...defaultOptions, ...options }
|
||||||
|
|
||||||
return function apiHandler(req, res) {
|
return function apiHandler(req, res) {
|
||||||
return handler(req, res, getConfig(config), ops)
|
return handler(req, res, getConfig(config), ops, opts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user