commerce/lib/bigcommerce/api/operations/get-customer-wishlist.ts
2020-10-26 17:13:34 -05:00

52 lines
1.3 KiB
TypeScript

import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import { BigcommerceConfig, getConfig } from '..'
import { definitions } from '../definitions/wishlist'
export type Wishlist = definitions['wishlist_Full']
export type GetCustomerWishlistResult<
T extends { wishlist?: any } = { wishlist?: Wishlist }
> = T
export type GetCustomerWishlistVariables = {
customerId: number
}
async function getCustomerWishlist(opts: {
variables: GetCustomerWishlistVariables
config?: BigcommerceConfig
preview?: boolean
}): Promise<GetCustomerWishlistResult>
async function getCustomerWishlist<
T extends { wishlist?: any },
V = any
>(opts: {
url: string
variables: V
config?: BigcommerceConfig
preview?: boolean
}): Promise<GetCustomerWishlistResult<T>>
async function getCustomerWishlist({
config,
variables,
}: {
url?: string
variables: GetCustomerWishlistVariables
config?: BigcommerceConfig
preview?: boolean
}): Promise<GetCustomerWishlistResult> {
config = getConfig(config)
const { data } = await config.storeApiFetch<
RecursivePartial<{ data: Wishlist[] }>
>(`/v3/wishlists?customer_id=${variables.customerId}`)
const wishlists = (data as RecursiveRequired<typeof data>) ?? []
const wishlist = wishlists[0]
return { wishlist }
}
export default getCustomerWishlist