mirror of
https://github.com/vercel/commerce.git
synced 2025-03-27 07:45:53 +00:00
47 lines
976 B
TypeScript
47 lines
976 B
TypeScript
import type { OperationContext } from '@commerce/api/operations'
|
|
import { normalizeProduct } from '../../utils'
|
|
import type { Provider, SaleorConfig } from '..'
|
|
|
|
import * as Query from '../../utils/queries'
|
|
|
|
type Variables = {
|
|
slug: string
|
|
}
|
|
|
|
type ReturnType = {
|
|
product: any
|
|
}
|
|
|
|
export default function getProductOperation({ commerce }: OperationContext<Provider>) {
|
|
async function getProduct({
|
|
query = Query.ProductOneBySlug,
|
|
variables,
|
|
config: cfg,
|
|
}: {
|
|
query?: string
|
|
variables: Variables
|
|
config?: Partial<SaleorConfig>
|
|
preview?: boolean
|
|
}): Promise<ReturnType> {
|
|
const { fetch, locale } = commerce.getConfig(cfg)
|
|
|
|
const { data } = await fetch(
|
|
query,
|
|
{ variables },
|
|
{
|
|
...(locale && {
|
|
headers: {
|
|
'Accept-Language': locale,
|
|
},
|
|
}),
|
|
}
|
|
)
|
|
|
|
return {
|
|
product: data?.product ? normalizeProduct(data.product) : null,
|
|
}
|
|
}
|
|
|
|
return getProduct
|
|
}
|