import type { OperationContext, OperationOptions, } from '@commerce/api/operations' import { normalizePage } from '../../utils' import type { ShopifyConfig, Provider } from '..' import { GetPageQuery, GetPageQueryVariables, Page as ShopifyPage, } from '../../schema' import { GetPageOperation } from '../../types/page' import getPageQuery from '../../utils/queries/get-page-query' export default function getPageOperation({ commerce, }: OperationContext) { async function getPage(opts: { variables: T['variables'] config?: Partial preview?: boolean }): Promise async function getPage( opts: { variables: T['variables'] config?: Partial preview?: boolean } & OperationOptions ): Promise async function getPage({ query = getPageQuery, variables, config, }: { query?: string variables: T['variables'] config?: Partial preview?: boolean }): Promise { const { fetch, locale } = commerce.getConfig(config) const { data: { node: page }, } = await fetch( query, { variables, }, { ...(locale && { headers: { 'Accept-Language': locale, }, }), } ) return page ? { page: normalizePage(page as ShopifyPage, locale) } : {} } return getPage }