import { GraphQLFetcherResult } from '@commerce/api' import { getConfig, SwellConfig } from '../api' import { ProductEdge } from '../schema' import { getAllProductsQuery } from '../utils/queries' 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 => { let { config, variables = { first: 250 } } = options ?? {} config = getConfig(config) const { data }: GraphQLFetcherResult = await config.fetch( getAllProductsQuery, { variables } ) const products = data.products?.edges?.map(({ node: p }: ProductEdge) => normalizeProduct(p) ) ?? [] return { products, } } export default getAllProducts