commerce/framework/shopify/common/get-all-pages.ts
2021-01-29 00:07:17 +08:00

56 lines
1022 B
TypeScript

import { getConfig, ShopifyConfig } from '../api'
import { Page as PageType, PageEdge } from '../utils/types'
export type Page = PageType
export const getAllPagesQuery = /* GraphQL */ `
query($first: Int!) {
pages(first: $first) {
edges {
node {
id
title
handle
body
bodySummary
url
}
}
}
}
`
type Variables = {
first?: number
}
type Options = {
variables?: Variables
config: ShopifyConfig
preview?: boolean
}
type ReturnType = {
pages: Page[]
}
const getAllPages = async (options?: Options): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options || {}
config = getConfig(config)
const { data } = await config.fetch(getAllPagesQuery, { variables })
const pages = data.pages.edges.map(({ node }: PageEdge) => {
return {
...node,
name: node.handle,
url: `${config!.locale}/${node.handle}`,
}
})
return { pages }
}
export default getAllPages