mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
- Checking related products with param - Changing the normalizer - Separate calls for product & products in normalizer
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import type { ElasticpathConfig } from '../index'
|
|
import { Product } from '@commerce/types/product'
|
|
import { GetProductOperation } from '@commerce/types/product'
|
|
import type { OperationContext } from '@commerce/api/operations'
|
|
import {normalizeProduct} from '../../utils/normalize'
|
|
import epClient from '../../utils/ep-client'
|
|
|
|
|
|
export default function getProductOperation({
|
|
commerce,
|
|
}: OperationContext<any>) {
|
|
async function getProduct<T extends GetProductOperation>({
|
|
query = '',
|
|
variables,
|
|
config,
|
|
}: {
|
|
query?: string
|
|
variables?: T['variables']
|
|
config?: Partial<ElasticpathConfig>
|
|
preview?: boolean
|
|
} = {}): Promise<Product | {} | any> {
|
|
let variablesS = ''
|
|
if (typeof variables?.slug == "undefined") {
|
|
variablesS = ''
|
|
} else {
|
|
variablesS = variables.slug;
|
|
}
|
|
let {data:[product]} = await epClient.PCM.Filter({
|
|
eq: {
|
|
slug: variablesS
|
|
}
|
|
}).All();
|
|
let productData = await normalizeProduct(product, true)
|
|
return {
|
|
// product: data.products.find(({ slug }) => slug === variables!.slug),
|
|
product: productData
|
|
}
|
|
}
|
|
|
|
return getProduct
|
|
}
|