import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next' import { ShopifyConfig, getConfig } from '..' export type ShopifyApiHandler< T = any, H extends ShopifyHandlers = {}, Options extends {} = {} > = ( req: NextApiRequest, res: NextApiResponse>, config: ShopifyConfig, handlers: H, // Custom configs that may be used by a particular handler options: Options ) => void | Promise export type ShopifyHandler = (options: { req: NextApiRequest res: NextApiResponse> config: ShopifyConfig body: Body }) => void | Promise export type ShopifyHandlers = { [k: string]: ShopifyHandler } export type ShopifyApiResponse = { data: T | null errors?: { message: string; code?: string }[] } export default function createApiHandler< T = any, H extends ShopifyHandlers = {}, Options extends {} = {} >( handler: ShopifyApiHandler, handlers: H, defaultOptions: Options ) { return function getApiHandler({ config, operations, options, }: { config?: ShopifyConfig operations?: Partial options?: Options extends {} ? Partial : never } = {}): NextApiHandler { const ops = { ...operations, ...handlers } const opts = { ...defaultOptions, ...options } return function apiHandler(req, res) { return handler(req, res, getConfig(config), ops, opts) } } }