import { Product } from '@commerce/types/product' import { GetProductOperation } from '@commerce/types/product' import type { OperationContext } from '@commerce/api/operations' import { MedusaProduct } from '@framework/types' import { normalizeProduct } from '@framework/utils/normalizers/normalize-products' import { MedusaConfig } from '..' export default function getProductOperation({ commerce, }: OperationContext) { async function getProduct({ variables, config: cfg, }: { query?: string variables?: T['variables'] config?: Partial preview?: boolean } = {}): Promise { const config = commerce.getConfig(cfg) const response = await config.fetch('products', 'list', {}) if (response.products) { const products: MedusaProduct[] = response.products const product = products ? products.find(({ handle }) => handle === variables!.slug) : null /** * Commerce only provides us with the slug/path for finding * the specified product. We do not have a endpoint for retrieving * products using handles. Perhaps we should ask Vercel if we can * change this so the variables also expose the product_id, which * we can use for retrieving products. */ if (product) { return { product: normalizeProduct(product), } } } } return getProduct }