mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Merge branch 'master' of github.com:vercel/commerce into outgrow-reaction-commerce-provider
Signed-off-by: Loan Laux <loan@outgrow.io>
This commit is contained in:
47
framework/saleor/api/operations/get-all-pages.ts
Normal file
47
framework/saleor/api/operations/get-all-pages.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { OperationContext } from '@commerce/api/operations'
|
||||
|
||||
import { QueryPagesArgs, PageCountableEdge } from '../../schema'
|
||||
import type { SaleorConfig, Provider } from '..'
|
||||
import * as Query from '../../utils/queries'
|
||||
|
||||
export type Page = any
|
||||
|
||||
export type GetAllPagesResult<T extends { pages: any[] } = { pages: Page[] }> = T
|
||||
|
||||
export default function getAllPagesOperation({ commerce }: OperationContext<Provider>) {
|
||||
async function getAllPages({
|
||||
query = Query.PageMany,
|
||||
config,
|
||||
variables,
|
||||
}: {
|
||||
url?: string
|
||||
config?: Partial<SaleorConfig>
|
||||
variables?: QueryPagesArgs
|
||||
preview?: boolean
|
||||
query?: string
|
||||
} = {}): Promise<GetAllPagesResult> {
|
||||
const { fetch, locale, locales = ['en-US'] } = commerce.getConfig(config)
|
||||
|
||||
const { data } = await fetch(
|
||||
query,
|
||||
{ variables },
|
||||
{
|
||||
...(locale && {
|
||||
headers: {
|
||||
'Accept-Language': locale,
|
||||
},
|
||||
}),
|
||||
}
|
||||
)
|
||||
|
||||
const pages = data.pages?.edges?.map(({ node: { title: name, slug, ...node } }: PageCountableEdge) => ({
|
||||
...node,
|
||||
url: `/${locale}/${slug}`,
|
||||
name,
|
||||
}))
|
||||
|
||||
return { pages }
|
||||
}
|
||||
|
||||
return getAllPages
|
||||
}
|
Reference in New Issue
Block a user