2021-04-29 19:22:22 +03:00

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