diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index 96ac92477..4f00613c3 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -1,5 +1,9 @@ import type { RequestInit } from '@vercel/fetch' -import { CommerceAPIConfig, createAPIProvider } from '@commerce/api' +import { + CommerceAPI, + CommerceAPIConfig, + createAPIProvider, +} from '@commerce/api' import fetchGraphqlApi from './utils/fetch-graphql-api' import fetchStoreApi from './utils/fetch-store-api' @@ -95,7 +99,13 @@ const config2: BigcommerceConfig = { storeApiFetch: fetchStoreApi, } -export const commerce = createAPIProvider({ config: config2 }) +const provider = { + config: config2, + // endpoints +} + +export const commerce2 = createAPIProvider(provider) +export const commerce = new CommerceAPI(provider) export function getConfig(userConfig?: Partial) { return config.getConfig(userConfig) diff --git a/framework/bigcommerce/api/provider.ts b/framework/bigcommerce/api/provider.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/framework/commerce/api/endpoints/cart.ts b/framework/commerce/api/endpoints/cart.ts index 03261e1ec..32c73f264 100644 --- a/framework/commerce/api/endpoints/cart.ts +++ b/framework/commerce/api/endpoints/cart.ts @@ -3,7 +3,7 @@ import { CommerceAPIError } from '../utils/errors' import isAllowedOperation from '../utils/is-allowed-operation' import type { APIProvider, CartHandlers } from '..' -const cartApi: APIEndpoint = async (ctx) => { +const cartApi: APIEndpoint> = async (ctx) => { const { req, res, handlers, config } = ctx if ( @@ -55,3 +55,5 @@ const cartApi: APIEndpoint = async (ctx) => { res.status(500).json({ data: null, errors: [{ message }] }) } } + +export default cartApi diff --git a/framework/commerce/api/index.ts b/framework/commerce/api/index.ts index 61c9c89d2..4d99d7821 100644 --- a/framework/commerce/api/index.ts +++ b/framework/commerce/api/index.ts @@ -1,19 +1,19 @@ import type { RequestInit, Response } from '@vercel/fetch' import type { APIEndpoint, APIHandler } from './utils/types' -export type CartHandlers = { - getCart: APIHandler - addItem: APIHandler - updateItem: APIHandler - removeItem: APIHandler +export type CartHandlers = { + getCart: APIHandler, any, Body> + addItem: APIHandler, any, Body> + updateItem: APIHandler, any, Body> + removeItem: APIHandler, any, Body> } export type CoreAPIProvider = { config: CommerceAPIConfig endpoints?: { cart?: { - handler: APIEndpoint - handlers: CartHandlers + handler: APIEndpoint, any> + handlers: CartHandlers } } } @@ -23,6 +23,23 @@ export type APIProvider

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

{ + constructor(readonly provider: P) { + this.provider = provider + } + + getConfig(userConfig: Partial = {}) { + return Object.entries(userConfig).reduce( + (cfg, [key, value]) => Object.assign(cfg, { [key]: value }), + { ...this.provider.config } + ) + } + + setConfig(newConfig: Partial) { + Object.assign(this.provider.config, newConfig) + } +} + export function createAPIProvider

( provider: P ): APIProvider

{ diff --git a/framework/commerce/api/utils/is-allowed-operation.ts b/framework/commerce/api/utils/is-allowed-operation.ts index dd650c8f1..6c8f89d00 100644 --- a/framework/commerce/api/utils/is-allowed-operation.ts +++ b/framework/commerce/api/utils/is-allowed-operation.ts @@ -5,7 +5,7 @@ import { APIHandler } from './types' export default function isAllowedOperation( req: NextApiRequest, res: NextApiResponse, - allowedHandlers: { [k in HTTP_METHODS]?: APIHandler } + allowedHandlers: { [k in HTTP_METHODS]?: APIHandler } ) { const methods = Object.keys(allowedHandlers) as HTTP_METHODS[] const allowedMethods = methods.reduce((arr, method) => {