mirror of
https://github.com/vercel/commerce.git
synced 2025-08-16 05:41:24 +00:00
.vscode
assets
components
config
framework
bigcommerce
commerce
local
reactioncommerce
saleor
shopify
api
endpoints
operations
get-all-pages.ts
get-all-product-paths.ts
get-all-products.ts
get-page.ts
get-product.ts
get-site-info.ts
index.ts
login.ts
utils
index.ts
auth
cart
customer
product
types
utils
wishlist
.env.template
README.md
codegen.json
commerce.config.json
const.ts
fetcher.ts
index.tsx
next.config.js
provider.ts
schema.d.ts
schema.graphql
swell
vendure
lib
pages
public
.editorconfig
.env.template
.gitignore
.prettierignore
.prettierrc
README.md
codegen.bigcommerce.json
codegen.json
commerce.config.json
global.d.ts
license.md
next-env.d.ts
next.config.js
package.json
postcss.config.js
swell-js.d.ts
tailwind.config.js
tsconfig.json
yarn.lock
72 lines
1.7 KiB
TypeScript
72 lines
1.7 KiB
TypeScript
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<Provider>) {
|
|
async function getAllPages<T extends GetAllPagesOperation>(opts?: {
|
|
config?: Partial<ShopifyConfig>
|
|
preview?: boolean
|
|
}): Promise<T['data']>
|
|
|
|
async function getAllPages<T extends GetAllPagesOperation>(
|
|
opts: {
|
|
config?: Partial<ShopifyConfig>
|
|
preview?: boolean
|
|
} & OperationOptions
|
|
): Promise<T['data']>
|
|
|
|
async function getAllPages<T extends GetAllPagesOperation>({
|
|
query = getAllPagesQuery,
|
|
config,
|
|
variables,
|
|
}: {
|
|
url?: string
|
|
config?: Partial<ShopifyConfig>
|
|
variables?: GetAllPagesQueryVariables
|
|
preview?: boolean
|
|
query?: string
|
|
} = {}): Promise<T['data']> {
|
|
const {
|
|
fetch,
|
|
locale,
|
|
locales = ['en-US', 'es'],
|
|
} = commerce.getConfig(config)
|
|
|
|
const { data } = await fetch<GetAllPagesQuery, GetAllPagesQueryVariables>(
|
|
query,
|
|
{
|
|
variables,
|
|
},
|
|
{
|
|
...(locale && {
|
|
headers: {
|
|
'Accept-Language': locale,
|
|
},
|
|
}),
|
|
}
|
|
)
|
|
|
|
return {
|
|
pages: locales.reduce<Page[]>(
|
|
(arr, locale) =>
|
|
arr.concat(normalizePages(data.pages.edges as PageEdge[], locale)),
|
|
[]
|
|
),
|
|
}
|
|
}
|
|
|
|
return getAllPages
|
|
}
|