4
0
forked from crowetic/commerce
commerce/lib/bigcommerce/api/operations/get-all-product-paths.ts
2020-10-05 01:13:20 -05:00

60 lines
1.5 KiB
TypeScript

import type { GetAllProductPathsQuery } from 'lib/bigcommerce/schema'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import { BigcommerceConfig, getConfig } from '..'
export const getAllProductPathsQuery = /* GraphQL */ `
query getAllProductPaths {
site {
products {
edges {
node {
path
}
}
}
}
}
`
export type ProductPaths = NonNullable<
GetAllProductPathsQuery['site']['products']['edges']
>
export type GetAllProductPathsResult<
T extends { products: any[] } = { products: ProductPaths }
> = T
async function getAllProductPaths(opts?: {
config?: BigcommerceConfig
}): Promise<GetAllProductPathsResult>
async function getAllProductPaths<
T extends { products: any[] },
V = any
>(opts: {
query: string
config?: BigcommerceConfig
}): Promise<GetAllProductPathsResult<T>>
async function getAllProductPaths({
query = getAllProductPathsQuery,
config,
}: {
query?: string
config?: BigcommerceConfig
} = {}): 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
)
const products = data.site?.products?.edges
return {
products: (products as RecursiveRequired<typeof products>) ?? [],
}
}
export default getAllProductPaths