mirror of
https://github.com/vercel/commerce.git
synced 2025-06-18 21:21:21 +00:00
Fixes
This commit is contained in:
parent
61f1292161
commit
4afb5f569e
@ -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<BigcommerceConfig>) {
|
||||
return config.getConfig(userConfig)
|
||||
|
@ -3,7 +3,7 @@ import { CommerceAPIError } from '../utils/errors'
|
||||
import isAllowedOperation from '../utils/is-allowed-operation'
|
||||
import type { APIProvider, CartHandlers } from '..'
|
||||
|
||||
const cartApi: APIEndpoint<APIProvider, CartHandlers> = async (ctx) => {
|
||||
const cartApi: APIEndpoint<APIProvider, CartHandlers<any>> = async (ctx) => {
|
||||
const { req, res, handlers, config } = ctx
|
||||
|
||||
if (
|
||||
@ -55,3 +55,5 @@ const cartApi: APIEndpoint<APIProvider, CartHandlers> = async (ctx) => {
|
||||
res.status(500).json({ data: null, errors: [{ message }] })
|
||||
}
|
||||
}
|
||||
|
||||
export default cartApi
|
||||
|
@ -1,19 +1,19 @@
|
||||
import type { RequestInit, Response } from '@vercel/fetch'
|
||||
import type { APIEndpoint, APIHandler } from './utils/types'
|
||||
|
||||
export type CartHandlers = {
|
||||
getCart: APIHandler<any>
|
||||
addItem: APIHandler<any>
|
||||
updateItem: APIHandler<any>
|
||||
removeItem: APIHandler<any>
|
||||
export type CartHandlers<Body extends { cartId: 'string' }> = {
|
||||
getCart: APIHandler<any, CartHandlers<Body>, any, Body>
|
||||
addItem: APIHandler<any, CartHandlers<Body>, any, Body>
|
||||
updateItem: APIHandler<any, CartHandlers<Body>, any, Body>
|
||||
removeItem: APIHandler<any, CartHandlers<Body>, any, Body>
|
||||
}
|
||||
|
||||
export type CoreAPIProvider = {
|
||||
config: CommerceAPIConfig
|
||||
endpoints?: {
|
||||
cart?: {
|
||||
handler: APIEndpoint<any, any, CartHandlers, any>
|
||||
handlers: CartHandlers
|
||||
handler: APIEndpoint<any, any, CartHandlers<any>, any>
|
||||
handlers: CartHandlers<any>
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -23,6 +23,23 @@ export type APIProvider<P extends CoreAPIProvider = CoreAPIProvider> = P & {
|
||||
setConfig(newConfig: Partial<P['config']>): void
|
||||
}
|
||||
|
||||
export class CommerceAPI<P extends CoreAPIProvider = CoreAPIProvider> {
|
||||
constructor(readonly provider: P) {
|
||||
this.provider = provider
|
||||
}
|
||||
|
||||
getConfig(userConfig: Partial<P['config']> = {}) {
|
||||
return Object.entries(userConfig).reduce(
|
||||
(cfg, [key, value]) => Object.assign(cfg, { [key]: value }),
|
||||
{ ...this.provider.config }
|
||||
)
|
||||
}
|
||||
|
||||
setConfig(newConfig: Partial<P['config']>) {
|
||||
Object.assign(this.provider.config, newConfig)
|
||||
}
|
||||
}
|
||||
|
||||
export function createAPIProvider<P extends CoreAPIProvider>(
|
||||
provider: P
|
||||
): APIProvider<P> {
|
||||
|
@ -5,7 +5,7 @@ import { APIHandler } from './types'
|
||||
export default function isAllowedOperation(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse,
|
||||
allowedHandlers: { [k in HTTP_METHODS]?: APIHandler<any> }
|
||||
allowedHandlers: { [k in HTTP_METHODS]?: APIHandler<any, any> }
|
||||
) {
|
||||
const methods = Object.keys(allowedHandlers) as HTTP_METHODS[]
|
||||
const allowedMethods = methods.reduce<HTTP_METHODS[]>((arr, method) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user