import { SwellProduct } from '../../types' import { SwellConfig, Provider } from '..' import { OperationContext, OperationOptions } from '@commerce/api/operations' import { GetAllProductPathsOperation } from '@commerce/types/product' export default function getAllProductPathsOperation({ commerce, }: OperationContext) { async function getAllProductPaths< T extends GetAllProductPathsOperation >(opts?: { variables?: T['variables'] config?: SwellConfig }): Promise async function getAllProductPaths( opts: { variables?: T['variables'] config?: SwellConfig } & OperationOptions ): Promise async function getAllProductPaths({ variables, config: cfg, }: { query?: string variables?: T['variables'] config?: SwellConfig } = {}): Promise { const config = commerce.getConfig(cfg) // RecursivePartial forces the method to check for every prop in the data, which is // required in case there's a custom `query` const { results } = await config.fetch('products', 'list', [ { limit: variables?.first, }, ]) return { products: results?.map(({ slug: handle }: SwellProduct) => ({ path: `/${handle}`, })), } } return getAllProductPaths }