mirror of
https://github.com/vercel/commerce.git
synced 2025-03-27 07:45:53 +00:00
* Improved Categories * Improved Categories * Improved Categories * Improved Categories * Improved Categories * Improved Categories
118 lines
2.7 KiB
TypeScript
118 lines
2.7 KiB
TypeScript
import type { GetSiteInfoQuery, GetSiteInfoQueryVariables } from '../schema'
|
|
import type { RecursivePartial, RecursiveRequired } from '../api/utils/types'
|
|
import filterEdges from '../api/utils/filter-edges'
|
|
import { BigcommerceConfig, getConfig } from '../api'
|
|
import { categoryTreeItemFragment } from '../api/fragments/category-tree'
|
|
import { Category } from '@commerce/types'
|
|
import getSlug from '@lib/get-slug'
|
|
|
|
// Get 3 levels of categories
|
|
export const getSiteInfoQuery = /* GraphQL */ `
|
|
query getSiteInfo {
|
|
site {
|
|
categoryTree {
|
|
...categoryTreeItem
|
|
children {
|
|
...categoryTreeItem
|
|
children {
|
|
...categoryTreeItem
|
|
}
|
|
}
|
|
}
|
|
brands {
|
|
pageInfo {
|
|
startCursor
|
|
endCursor
|
|
}
|
|
edges {
|
|
cursor
|
|
node {
|
|
entityId
|
|
name
|
|
defaultImage {
|
|
urlOriginal
|
|
altText
|
|
}
|
|
pageTitle
|
|
metaDesc
|
|
metaKeywords
|
|
searchKeywords
|
|
path
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
${categoryTreeItemFragment}
|
|
`
|
|
|
|
export type CategoriesTree = NonNullable<
|
|
GetSiteInfoQuery['site']['categoryTree']
|
|
>
|
|
|
|
export type BrandEdge = NonNullable<
|
|
NonNullable<GetSiteInfoQuery['site']['brands']['edges']>[0]
|
|
>
|
|
|
|
export type Brands = BrandEdge[]
|
|
|
|
export type GetSiteInfoResult<
|
|
T extends { categories: any[]; brands: any[] } = {
|
|
categories: Category[]
|
|
brands: Brands
|
|
}
|
|
> = T
|
|
|
|
async function getSiteInfo(opts?: {
|
|
variables?: GetSiteInfoQueryVariables
|
|
config?: BigcommerceConfig
|
|
preview?: boolean
|
|
}): Promise<GetSiteInfoResult>
|
|
|
|
async function getSiteInfo<
|
|
T extends { categories: Category[]; brands: any[] },
|
|
V = any
|
|
>(opts: {
|
|
query: string
|
|
variables?: V
|
|
config?: BigcommerceConfig
|
|
preview?: boolean
|
|
}): Promise<GetSiteInfoResult<T>>
|
|
|
|
async function getSiteInfo({
|
|
query = getSiteInfoQuery,
|
|
variables,
|
|
config,
|
|
}: {
|
|
query?: string
|
|
variables?: GetSiteInfoQueryVariables
|
|
config?: BigcommerceConfig
|
|
preview?: boolean
|
|
} = {}): Promise<GetSiteInfoResult> {
|
|
config = getConfig(config)
|
|
// 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<RecursivePartial<GetSiteInfoQuery>>(
|
|
query,
|
|
{ variables }
|
|
)
|
|
|
|
let categories = data!.site!.categoryTree?.map(
|
|
({ entityId, name, path }: any) => ({
|
|
id: `${entityId}`,
|
|
name,
|
|
slug: getSlug(path),
|
|
path,
|
|
})
|
|
)
|
|
|
|
const brands = data.site?.brands?.edges
|
|
|
|
return {
|
|
categories: categories ?? [],
|
|
brands: filterEdges(brands as RecursiveRequired<typeof brands>),
|
|
}
|
|
}
|
|
|
|
export default getSiteInfo
|