Adding types, fixes and other updates

This commit is contained in:
Luis Alvarez
2021-03-26 12:50:25 -06:00
parent e6df8dfb40
commit b2fbaab5d5
11 changed files with 400 additions and 38 deletions

View File

@@ -1,8 +1,9 @@
import { GetAPISchema } from '@commerce/api'
import type { GetAPISchema } from '@commerce/api'
import type { AddItemOperation } from '@commerce/types'
import getCart from './get-cart'
import addItem from './add-item'
import updateItem from './handlers/update-item'
import removeItem from './handlers/remove-item'
import updateItem from './update-item'
import removeItem from './remove-item'
import type {
GetCartHandlerBody,
AddCartItemHandlerBody,
@@ -18,14 +19,10 @@ export type CartAPI = GetAPISchema<
endpoint: {
options: {}
operations: {
getCart: {
data: Cart | null
body: GetCartHandlerBody
options: { yay: string }
}
addItem: { data: Cart; body: AddCartItemHandlerBody; options: {} }
updateItem: { data: Cart; body: UpdateCartItemHandlerBody; options: {} }
removeItem: { data: Cart; body: RemoveCartItemHandlerBody; options: {} }
getCart: { data: Cart | null; body: GetCartHandlerBody }
addItem: { data: Cart; body: AddItemOperation['body'] }
updateItem: { data: Cart; body: UpdateCartItemHandlerBody }
removeItem: { data: Cart; body: RemoveCartItemHandlerBody }
}
}
}
@@ -33,4 +30,4 @@ export type CartAPI = GetAPISchema<
export type CartEndpoint = CartAPI['endpoint']
export const operations = { getCart, addItem }
export const operations = { getCart, addItem, updateItem, removeItem }

View File

@@ -0,0 +1,34 @@
import { normalizeCart } from '@framework/lib/normalize'
import getCartCookie from '../utils/get-cart-cookie'
import type { CartEndpoint } from '.'
const removeItem: CartEndpoint['operations']['removeItem'] = async ({
res,
body: { cartId, itemId },
config,
}) => {
if (!cartId || !itemId) {
return res.status(400).json({
data: null,
errors: [{ message: 'Invalid request' }],
})
}
const result = await config.storeApiFetch<{ data: any } | null>(
`/v3/carts/${cartId}/items/${itemId}?include=line_items.physical_items.options`,
{ method: 'DELETE' }
)
const data = result?.data ?? null
res.setHeader(
'Set-Cookie',
data
? // Update the cart cookie
getCartCookie(config.cartCookie, cartId, config.cartCookieMaxAge)
: // Remove the cart cookie if the cart was removed (empty items)
getCartCookie(config.cartCookie)
)
res.status(200).json({ data: normalizeCart(data) })
}
export default removeItem

View File

@@ -0,0 +1,36 @@
import { normalizeCart } from '@framework/lib/normalize'
import { parseCartItem } from '../utils/parse-item'
import getCartCookie from '../utils/get-cart-cookie'
import type { CartEndpoint } from '.'
const updateItem: CartEndpoint['operations']['updateItem'] = async ({
res,
body: { cartId, itemId, item },
config,
}) => {
if (!cartId || !itemId || !item) {
return res.status(400).json({
data: null,
errors: [{ message: 'Invalid request' }],
})
}
const { data } = await config.storeApiFetch(
`/v3/carts/${cartId}/items/${itemId}?include=line_items.physical_items.options`,
{
method: 'PUT',
body: JSON.stringify({
line_item: parseCartItem(item),
}),
}
)
// Update the cart cookie
res.setHeader(
'Set-Cookie',
getCartCookie(config.cartCookie, cartId, config.cartCookieMaxAge)
)
res.status(200).json({ data: normalizeCart(data) })
}
export default updateItem

View File

@@ -1,3 +1,4 @@
import type { NextApiHandler } from 'next'
import type { RequestInit } from '@vercel/fetch'
import {
CommerceAPI as CoreCommerceAPI,
@@ -6,6 +7,8 @@ import {
import fetchGraphqlApi from './utils/fetch-graphql-api'
import fetchStoreApi from './utils/fetch-store-api'
import type { CartAPI } from './cart'
export interface BigcommerceConfig extends CommerceAPIConfig {
// Indicates if the returned metadata with translations should be applied to the
// data or returned as it is
@@ -104,11 +107,20 @@ export const provider = {
export type Provider = typeof provider
export class CommerceAPI<
P extends Provider = Provider
> extends CoreCommerceAPI<P> {
constructor(readonly provider: P = provider) {
super(provider)
export type APIs = CartAPI
export class CommerceAPI extends CoreCommerceAPI<Provider> {
constructor(customProvider: Provider = provider) {
super(customProvider)
}
endpoint<E extends APIs>(
context: E['endpoint'] & {
config?: Provider['config']
options?: E['schema']['endpoint']['options']
}
): NextApiHandler {
return this.endpoint(context)
}
}

View File

@@ -25,7 +25,6 @@ export type BigcommerceCart = {
export type Cart = Core.Cart & {
lineItems: LineItem[]
core: string
}
export type LineItem = Core.LineItem