forked from crowetic/commerce
assets
components
config
lib
bigcommerce
api
cart
catalog
customers
definitions
fragments
operations
get-all-pages.ts
get-all-product-paths.ts
get-all-products.ts
get-page.ts
get-product.ts
get-site-info.ts
login.ts
utils
wishlist
checkout.ts
index.ts
cart
products
scripts
wishlist
index.tsx
schema.d.ts
schema.graphql
use-customer.tsx
use-login.tsx
use-logout.tsx
use-price.tsx
use-signup.tsx
commerce
browser.ts
colors.ts
logger.ts
range-map.ts
to-pixels.ts
pages
public
utils
.gitignore
.prettierignore
README.md
codegen.json
global.d.ts
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
107 lines
2.5 KiB
TypeScript
107 lines
2.5 KiB
TypeScript
import type {
|
|
GetSiteInfoQuery,
|
|
GetSiteInfoQueryVariables,
|
|
} from 'lib/bigcommerce/schema'
|
|
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
|
|
import filterEdges from '../utils/filter-edges'
|
|
import { BigcommerceConfig, getConfig } 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 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: CategoriesTree
|
|
brands: Brands
|
|
}
|
|
> = T
|
|
|
|
async function getSiteInfo(opts?: {
|
|
variables?: GetSiteInfoQueryVariables
|
|
config?: BigcommerceConfig
|
|
}): Promise<GetSiteInfoResult>
|
|
|
|
async function getSiteInfo<
|
|
T extends { categories: any[]; brands: any[] },
|
|
V = any
|
|
>(opts: {
|
|
query: string
|
|
variables?: V
|
|
config?: BigcommerceConfig
|
|
}): Promise<GetSiteInfoResult<T>>
|
|
|
|
async function getSiteInfo({
|
|
query = getSiteInfoQuery,
|
|
variables,
|
|
config,
|
|
}: {
|
|
query?: string
|
|
variables?: GetSiteInfoQueryVariables
|
|
config?: BigcommerceConfig
|
|
} = {}): 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 }
|
|
)
|
|
const categories = data.site?.categoryTree
|
|
const brands = data.site?.brands?.edges
|
|
|
|
return {
|
|
categories: (categories as RecursiveRequired<typeof categories>) ?? [],
|
|
brands: filterEdges(brands as RecursiveRequired<typeof brands>),
|
|
}
|
|
}
|
|
|
|
export default getSiteInfo
|