import type { OperationContext, OperationOptions, } from '@commerce/api/operations' import type { GetAllPagesOperation, Page } from '@commerce/types/page' import { requireConfigValue } from '../../isomorphic-config' import normalizePage from '../../utils/normalizations/normalize-page' import type { IPages } from '@spree/storefront-api-v2-sdk/types/interfaces/Page' import type { SpreeSdkVariables } from '../../types' import type { SpreeApiConfig, SpreeApiProvider } from '../index' export default function getAllPagesOperation({ commerce, }: OperationContext) { async function getAllPages(options?: { config?: Partial preview?: boolean }): Promise async function getAllPages( opts: { config?: Partial preview?: boolean } & OperationOptions ): Promise async function getAllPages({ config: userConfig, preview, query, url, }: { url?: string config?: Partial preview?: boolean query?: string } = {}): Promise { console.info( 'getAllPages called. Configuration: ', 'query: ', query, 'userConfig: ', userConfig, 'preview: ', preview, 'url: ', url ) const config = commerce.getConfig(userConfig) const { fetch: apiFetch } = config const variables: SpreeSdkVariables = { methodPath: 'pages.list', arguments: [ { per_page: 500, filter: { locale_eq: config.locale || (requireConfigValue('defaultLocale') as string), }, }, ], } const { data: spreeSuccessResponse } = await apiFetch< IPages, SpreeSdkVariables >('__UNUSED__', { variables, }) const normalizedPages: Page[] = spreeSuccessResponse.data.map( (spreePage) => normalizePage(spreeSuccessResponse, spreePage, config.locales || []) ) return { pages: normalizedPages } } return getAllPages }