commerce/framework/swell/api/operations/get-all-collections.ts
2021-06-08 17:09:16 -07:00

29 lines
650 B
TypeScript

import { CollectionEdge } from '../../schema'
import { getConfig, SwellConfig } from '..'
const getAllCollections = async (options?: {
variables?: any
config: SwellConfig
preview?: boolean
}) => {
let { config, variables = { limit: 25 } } = options ?? {}
config = getConfig(config)
const response = await config.fetch('categories', 'list', { variables })
const edges = response.results ?? []
const categories = edges.map(
({ node: { id: entityId, title: name, handle } }: CollectionEdge) => ({
entityId,
name,
path: `/${handle}`,
})
)
return {
categories,
}
}
export default getAllCollections