mirror of
https://github.com/vercel/commerce.git
synced 2025-03-26 23:44:33 +00:00
38 lines
829 B
TypeScript
38 lines
829 B
TypeScript
import { getConfig, SwellConfig } from '../api'
|
|
import { normalizeProduct } from '../utils/normalize'
|
|
import { Product } from '@commerce/types'
|
|
|
|
type Variables = {
|
|
first?: number
|
|
field?: string
|
|
}
|
|
|
|
type ReturnType = {
|
|
products: Product[]
|
|
}
|
|
|
|
const getAllProducts = async (options: {
|
|
variables?: Variables
|
|
config?: SwellConfig
|
|
preview?: boolean
|
|
}): Promise<ReturnType> => {
|
|
let { config, variables = { first: 250 } } = options ?? {}
|
|
config = getConfig(config)
|
|
const { results } = await config.fetchSwell('products', 'list', [
|
|
{
|
|
limit: variables.first,
|
|
},
|
|
])
|
|
const products = results.map((product) => {
|
|
if (product.variants) {
|
|
product.variants = product.variants.results
|
|
}
|
|
return normalizeProduct(product) ?? []
|
|
})
|
|
return {
|
|
products,
|
|
}
|
|
}
|
|
|
|
export default getAllProducts
|