import type { OperationContext, OperationOptions, } from '@commerce/api/operations' import type { GetSiteInfoOperation } from '../../types/site' import type { GetSiteInfoQuery } from '../../schema' import type { RecursivePartial, RecursiveRequired } from '../utils/types' import filterEdges from '../utils/filter-edges' import type { BigcommerceConfig, Provider } from '..' import { categoryTreeItemFragment } from '../fragments/category-tree' // 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 default function getSiteInfoOperation({ commerce, }: OperationContext) { async function getSiteInfo(opts?: { config?: Partial preview?: boolean }): Promise async function getSiteInfo( opts: { config?: Partial preview?: boolean } & OperationOptions ): Promise async function getSiteInfo({ query = getSiteInfoQuery, config, }: { query?: string config?: Partial preview?: boolean } = {}): Promise { const cfg = commerce.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 cfg.fetch>(query) const categories = data.site?.categoryTree const brands = data.site?.brands?.edges return { categories: (categories as RecursiveRequired) ?? [], brands: filterEdges(brands as RecursiveRequired), } } return getSiteInfo }