diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index 4f00613c3..840645459 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -1,8 +1,7 @@ import type { RequestInit } from '@vercel/fetch' import { - CommerceAPI, + CommerceAPI as CoreCommerceAPI, CommerceAPIConfig, - createAPIProvider, } from '@commerce/api' import fetchGraphqlApi from './utils/fetch-graphql-api' import fetchStoreApi from './utils/fetch-store-api' @@ -99,13 +98,17 @@ const config2: BigcommerceConfig = { storeApiFetch: fetchStoreApi, } -const provider = { +export const provider = { config: config2, - // endpoints } -export const commerce2 = createAPIProvider(provider) -export const commerce = new CommerceAPI(provider) +export type Provider = typeof provider + +export class CommerceAPI

extends CoreCommerceAPI

{ + constructor(readonly provider: P = provider) { + super(provider) + } +} export function getConfig(userConfig?: Partial) { return config.getConfig(userConfig) diff --git a/framework/commerce/api/index.ts b/framework/commerce/api/index.ts index 4d99d7821..85079bb34 100644 --- a/framework/commerce/api/index.ts +++ b/framework/commerce/api/index.ts @@ -1,6 +1,9 @@ +import type { NextApiHandler } from 'next' import type { RequestInit, Response } from '@vercel/fetch' import type { APIEndpoint, APIHandler } from './utils/types' +export type CartEndpoint = APIEndpoint, any> + export type CartHandlers = { getCart: APIHandler, any, Body> addItem: APIHandler, any, Body> @@ -8,14 +11,18 @@ export type CartHandlers = { removeItem: APIHandler, any, Body> } +export type Endpoints = CartEndpoint + +export type EndpointHandlers = E extends APIEndpoint + ? T + : never + +export type EndpointOptions = E extends APIEndpoint + ? T + : never + export type CoreAPIProvider = { config: CommerceAPIConfig - endpoints?: { - cart?: { - handler: APIEndpoint, any> - handlers: CartHandlers - } - } } export type APIProvider

= P & { @@ -23,7 +30,10 @@ export type APIProvider

= P & { setConfig(newConfig: Partial): void } -export class CommerceAPI

{ +export class CommerceAPI< + P extends CoreAPIProvider = CoreAPIProvider, + E extends Endpoints = Endpoints +> { constructor(readonly provider: P) { this.provider = provider } @@ -38,6 +48,27 @@ export class CommerceAPI

{ setConfig(newConfig: Partial) { Object.assign(this.provider.config, newConfig) } + + endpoint(context: { + handler: E + config?: P['config'] + operations: EndpointHandlers + options?: EndpointOptions + }): NextApiHandler { + const provider = this + const cfg = this.getConfig(context.config) + + return function apiHandler(req, res) { + return context.handler({ + req, + res, + provider, + config: cfg, + handlers: context.operations, + options: context.options, + }) + } + } } export function createAPIProvider

( diff --git a/lib/api/commerce.ts b/lib/api/commerce.ts new file mode 100644 index 000000000..35b659a79 --- /dev/null +++ b/lib/api/commerce.ts @@ -0,0 +1,3 @@ +import { CommerceAPI } from '@framework/api' + +export default new CommerceAPI() diff --git a/pages/api/bigcommerce/cart.ts b/pages/api/bigcommerce/cart.ts index 68ffc3b15..ff2e1c981 100644 --- a/pages/api/bigcommerce/cart.ts +++ b/pages/api/bigcommerce/cart.ts @@ -1,3 +1,7 @@ import cartApi from '@framework/api/cart' +import cart from '@commerce/api/endpoints/cart' +import commerce from '@lib/api/commerce' + +const x = commerce.endpoint({ handler: cart, operations: {} as any }) export default cartApi()