mirror of
https://github.com/vercel/commerce.git
synced 2025-06-20 06:01:21 +00:00
* TEC-252: Base integration with commercetools using fetchers for REST and GraphQL endpoints * TEC-252: WIP commenting some components that are failing while we don't have all the hooks defined * add sdk integration * TEC-256: Implementing product search * TEC-256: removing unnecessary env variables * TEC-256: review comments * TEC-256: other remaining review fixes Co-authored-by: nicossosa93 <nicolas.sosa@devgurus.io>
39 lines
971 B
TypeScript
39 lines
971 B
TypeScript
import { Product } from '@commerce/types/product'
|
|
import { OperationContext } from '@commerce/api/operations'
|
|
import { Provider, CommercetoolsConfig } from '@framework/api'
|
|
import { normalizeProduct } from '@framework/lib/normalize'
|
|
|
|
export default function getProductOperation({
|
|
commerce,
|
|
}: OperationContext<Provider>) {
|
|
async function getProduct({
|
|
variables,
|
|
config: cfg,
|
|
}: {
|
|
variables: {
|
|
slug?: string
|
|
id?: string
|
|
locale?: string
|
|
}
|
|
config?: Partial<CommercetoolsConfig>
|
|
preview?: boolean
|
|
}): Promise<Product | {} | any> {
|
|
const config = commerce.getConfig(cfg)
|
|
|
|
// TODO: TEC-264: Handle the locale properly
|
|
const queryArg = {
|
|
where: `slug(en="${variables.slug}")`,
|
|
}
|
|
const projection = await config.fetchProducts(queryArg)
|
|
const product = projection.body.results[0]
|
|
|
|
if (product) {
|
|
return { product: normalizeProduct(product) }
|
|
}
|
|
|
|
return {}
|
|
}
|
|
|
|
return getProduct
|
|
}
|