forked from crowetic/commerce
40 lines
755 B
TypeScript
40 lines
755 B
TypeScript
import { SwellProduct } from '../types'
|
|
import { getConfig, SwellConfig } from '../api'
|
|
|
|
type ProductPath = {
|
|
path: string
|
|
}
|
|
|
|
export type ProductPathNode = {
|
|
node: ProductPath
|
|
}
|
|
|
|
type ReturnType = {
|
|
products: ProductPathNode[]
|
|
}
|
|
|
|
const getAllProductPaths = async (options?: {
|
|
variables?: any
|
|
config?: SwellConfig
|
|
preview?: boolean
|
|
}): Promise<ReturnType> => {
|
|
let { config, variables = [{ limit: 100 }] } = options ?? {}
|
|
config = getConfig(config)
|
|
|
|
const { results } = await config.fetch('products', 'list', [
|
|
{
|
|
limit: variables.first,
|
|
},
|
|
])
|
|
|
|
return {
|
|
products: results?.map(({ slug: handle }: SwellProduct) => ({
|
|
node: {
|
|
path: `/${handle}`,
|
|
},
|
|
})),
|
|
}
|
|
}
|
|
|
|
export default getAllProductPaths
|