commerce/framework/saleor/utils/get-categories.ts
2021-06-09 17:02:12 +02:00

30 lines
649 B
TypeScript

import { SaleorConfig } from '../api'
import { CollectionCountableEdge } from '../schema'
import { getSiteCollectionsQuery } from './queries/'
export type Category = {
entityId: string
name: string
path: string
}
const getCategories = async (config: SaleorConfig): Promise<Category[]> => {
const { data } = await config.fetch(getSiteCollectionsQuery, {
variables: {
first: 100,
},
})
return (
data.collections?.edges?.map(
({ node: { id: entityId, name, slug } }: CollectionCountableEdge) => ({
entityId,
name,
path: `/${slug}`,
})
) ?? []
)
}
export default getCategories