mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 13:41:22 +00:00
34 lines
766 B
TypeScript
34 lines
766 B
TypeScript
import { GetProductBySlugQuery, Product as ShopifyProduct } from '../schema'
|
|
import { getConfig, ShopifyConfig } from '../api'
|
|
import { normalizeProduct, getProductQuery } from '../utils'
|
|
import { Product } from '@commerce/types'
|
|
|
|
type Variables = {
|
|
slug: string
|
|
}
|
|
|
|
const getProduct = async (options: {
|
|
variables: Variables
|
|
config: ShopifyConfig
|
|
preview?: boolean
|
|
}): Promise<{
|
|
product?: Product
|
|
}> => {
|
|
let { config, variables } = options ?? {}
|
|
config = getConfig(config)
|
|
|
|
const {
|
|
data: { productByHandle },
|
|
} = await config.fetch<GetProductBySlugQuery>(getProductQuery, {
|
|
variables,
|
|
})
|
|
|
|
return {
|
|
...(productByHandle && {
|
|
product: normalizeProduct(productByHandle as ShopifyProduct),
|
|
}),
|
|
}
|
|
}
|
|
|
|
export default getProduct
|