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