mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
30 lines
836 B
TypeScript
30 lines
836 B
TypeScript
import type { OperationContext } from '@commerce/api/operations'
|
|
import type { GetAllProductsOperation } from '../../types/product'
|
|
import type { CommercejsConfig, Provider } from '../index'
|
|
|
|
import { normalizeProduct } from '../../utils/normalize-product'
|
|
|
|
export default function getAllProductsOperation({
|
|
commerce,
|
|
}: OperationContext<Provider>) {
|
|
async function getAllProducts<T extends GetAllProductsOperation>({
|
|
config,
|
|
}: {
|
|
config?: Partial<CommercejsConfig>
|
|
} = {}): Promise<T['data']> {
|
|
const { sdkFetch } = commerce.getConfig(config)
|
|
const { data } = await sdkFetch('products', 'list', {
|
|
sortBy: 'sort_order',
|
|
})
|
|
|
|
const productsFormatted =
|
|
data?.map((product) => normalizeProduct(product)) || []
|
|
|
|
return {
|
|
products: productsFormatted,
|
|
}
|
|
}
|
|
|
|
return getAllProducts
|
|
}
|