import type { GetAllProductPathsQuery } from 'lib/bigcommerce/schema' import type { RecursivePartial, RecursiveRequired } from '../utils/types' import { BigcommerceConfig, getConfig } from '..' export const getAllProductPathsQuery = /* GraphQL */ ` query getAllProductPaths { site { products { edges { node { path } } } } } ` export interface GetAllProductPathsResult { products: T extends GetAllProductPathsQuery ? NonNullable : unknown } async function getAllProductPaths(opts?: { config?: BigcommerceConfig }): Promise> async function getAllProductPaths(opts: { query: string config?: BigcommerceConfig }): Promise> async function getAllProductPaths({ query = getAllProductPathsQuery, config = getConfig(), }: { query?: string config?: BigcommerceConfig } = {}): Promise> { // RecursivePartial forces the method to check for every prop in the data, which is // required in case there's a custom `query` const data = await config.fetch>( query ) const products = data.site?.products?.edges return { products: (products as RecursiveRequired) ?? [], } } export default getAllProductPaths