Catalin Pinte 6c2610584d
Update types (#831)
* Update product types

* Cart types progress, add zod & initial schema validator

* Update normalize.ts

* Update with-schema-parser.ts

* Updated types, schemas & providers

* Fix providers after schema parse errors

* Fix paths

* More provider fixes

* Fix kibocommerce & commercejs

* Add customer updated types & fixes

* Add checkout & customer types

* Import core types only from commerce

* Update tsconfig.json

* Convert hooks interfaces to types

* Requested changes

* Change to relative paths

* Move Zod dependency
2022-10-05 09:02:29 +03:00

45 lines
1.1 KiB
TypeScript

import type { OperationContext } from '@vercel/commerce/api/operations'
import type { GetProductOperation } from '@vercel/commerce/types/product'
import type { CommercejsConfig, Provider } from '../index'
import { normalizeProduct } from '../../utils/normalize-product'
export default function getProductOperation({
commerce,
}: OperationContext<Provider>) {
async function getProduct<T extends GetProductOperation>({
config,
variables,
}: {
query?: string
variables?: T['variables']
config?: Partial<CommercejsConfig>
preview?: boolean
} = {}): Promise<T['data']> {
const { sdkFetch } = commerce.getConfig(config)
// Fetch a product by its permalink.
const product = await sdkFetch(
'products',
'retrieve',
variables?.slug || '',
{
type: 'permalink',
}
)
const { data: variants } = await sdkFetch(
'products',
'getVariants',
product.id
)
const productFormatted = normalizeProduct(product, variants)
return {
product: productFormatted,
}
}
return getProduct
}