mirror of
https://github.com/vercel/commerce.git
synced 2025-06-18 13:11:23 +00:00
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import type {
|
|
GetAllProductPathsQuery,
|
|
GetAllProductPathsQueryVariables,
|
|
} from '../schema'
|
|
import type { RecursivePartial, RecursiveRequired } from '../api/utils/types'
|
|
import filterEdges from '../api/utils/filter-edges'
|
|
import { VendureConfig, getConfig } from '../api'
|
|
|
|
export const getAllProductPathsQuery = /* GraphQL */ `
|
|
query getAllProductPaths($first: Int = 100) {
|
|
products(options: { take: $first }) {
|
|
items {
|
|
slug
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
export type ProductPath = NonNullable<
|
|
NonNullable<GetAllProductPathsQuery['site']['products']['edges']>[0]
|
|
>
|
|
|
|
export type ProductPaths = ProductPath[]
|
|
|
|
export type { GetAllProductPathsQueryVariables }
|
|
|
|
export type GetAllProductPathsResult<
|
|
T extends { products: any[] } = { products: ProductPaths }
|
|
> = T
|
|
|
|
async function getAllProductPaths(opts?: {
|
|
variables?: GetAllProductPathsQueryVariables
|
|
config?: VendureConfig
|
|
}): Promise<GetAllProductPathsResult>
|
|
|
|
async function getAllProductPaths<
|
|
T extends { products: any[] },
|
|
V = any
|
|
>(opts: {
|
|
query: string
|
|
variables?: V
|
|
config?: VendureConfig
|
|
}): Promise<GetAllProductPathsResult<T>>
|
|
|
|
async function getAllProductPaths({
|
|
query = getAllProductPathsQuery,
|
|
variables,
|
|
config,
|
|
}: {
|
|
query?: string
|
|
variables?: GetAllProductPathsQueryVariables
|
|
config?: VendureConfig
|
|
} = {}): Promise<GetAllProductPathsResult> {
|
|
config = 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.products.items
|
|
|
|
return {
|
|
products: products.map(p => ({ node: { path: `/${p.slug}` } })),
|
|
}
|
|
}
|
|
|
|
export default getAllProductPaths
|