mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 12:24:18 +00:00
.vscode
assets
components
config
framework
bigcommerce
api
definitions
endpoints
fragments
operations
get-all-pages.ts
get-all-product-paths.ts
get-all-products.ts
get-customer-wishlist.ts
get-page.ts
get-product.ts
get-site-info.ts
login.ts
utils
index.ts
auth
cart
customer
lib
product
scripts
types
wishlist
.env.template
README.md
commerce.config.json
fetcher.ts
index.tsx
next.config.js
provider.ts
schema.d.ts
schema.graphql
commerce
shopify
lib
pages
public
.editorconfig
.env.template
.gitignore
.prettierignore
.prettierrc
README.md
codegen.json
commerce.config.json
global.d.ts
license.md
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import type {
|
|
OperationContext,
|
|
OperationOptions,
|
|
} from '@commerce/api/operations'
|
|
import type { GetAllProductPathsQuery } from '../../schema'
|
|
import type { GetAllProductPathsOperation } from '../../types/product'
|
|
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
|
|
import filterEdges from '../utils/filter-edges'
|
|
import { BigcommerceConfig, Provider } from '..'
|
|
|
|
export const getAllProductPathsQuery = /* GraphQL */ `
|
|
query getAllProductPaths($first: Int = 100) {
|
|
site {
|
|
products(first: $first) {
|
|
edges {
|
|
node {
|
|
path
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
export default function getAllProductPathsOperation({
|
|
commerce,
|
|
}: OperationContext<Provider>) {
|
|
async function getAllProductPaths<
|
|
T extends GetAllProductPathsOperation
|
|
>(opts?: {
|
|
variables?: T['variables']
|
|
config?: BigcommerceConfig
|
|
}): Promise<T['data']>
|
|
|
|
async function getAllProductPaths<
|
|
T extends GetAllProductPathsOperation
|
|
>(opts: {
|
|
variables?: T['variables']
|
|
config?: BigcommerceConfig
|
|
} & OperationOptions): Promise<T['data']>
|
|
|
|
async function getAllProductPaths<T extends GetAllProductPathsOperation>({
|
|
query = getAllProductPathsQuery,
|
|
variables,
|
|
config,
|
|
}: {
|
|
query?: string
|
|
variables?: T['variables']
|
|
config?: BigcommerceConfig
|
|
} = {}): Promise<T['data']> {
|
|
config = commerce.getConfig(config)
|
|
// RecursivePartial forces the method to check for every prop in the data, which is
|
|
// required in case there's a custom `query`
|
|
const { data } = await config.fetch<
|
|
RecursivePartial<GetAllProductPathsQuery>
|
|
>(query, { variables })
|
|
const products = data.site?.products?.edges
|
|
|
|
return {
|
|
products: filterEdges(products as RecursiveRequired<typeof products>).map(
|
|
({ node }) => node
|
|
),
|
|
}
|
|
}
|
|
return getAllProductPaths
|
|
}
|