import type { OperationContext, OperationOptions, } from '@commerce/api/operations' import { GetAllPagesQuery, GetAllPagesQueryVariables, PageEdge, } from '../../schema' import { normalizePages } from '../../utils' import type { ShopifyConfig, Provider } from '..' import type { GetAllPagesOperation, Page } from '../../types/page' import getAllPagesQuery from '../../utils/queries/get-all-pages-query' export default function getAllPagesOperation({ commerce, }: OperationContext) { async function getAllPages(opts?: { config?: Partial preview?: boolean }): Promise async function getAllPages( opts: { config?: Partial preview?: boolean } & OperationOptions ): Promise async function getAllPages({ query = getAllPagesQuery, config, variables, }: { url?: string config?: Partial variables?: GetAllPagesQueryVariables preview?: boolean query?: string } = {}): Promise { const { fetch, locale, locales = ['en-US'] } = commerce.getConfig(config) const { data } = await fetch( query, { variables, }, { ...(locale && { headers: { 'Accept-Language': locale, }, }), } ) return { pages: locales.reduce( (arr, locale) => arr.concat(normalizePages(data.pages.edges as PageEdge[], locale)), [] ), } } return getAllPages }