1
0
mirror of https://github.com/vercel/commerce.git synced 2025-07-22 20:26:49 +00:00
Files
assets
components
config
lib
bigcommerce
api
cart
catalog
customers
definitions
fragments
operations
get-all-pages.ts
get-all-product-paths.ts
get-all-products.ts
get-page.ts
get-product.ts
get-site-info.ts
login.ts
utils
wishlist
checkout.ts
index.ts
cart
products
scripts
wishlist
index.tsx
schema.d.ts
schema.graphql
use-customer.tsx
use-login.tsx
use-logout.tsx
use-price.tsx
use-signup.tsx
commerce
browser.ts
colors.ts
logger.ts
range-map.ts
to-pixels.ts
pages
public
utils
.gitignore
.prettierignore
README.md
codegen.json
global.d.ts
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
commerce/lib/bigcommerce/api/operations/get-all-product-paths.ts
2020-10-22 21:38:03 -05:00

72 lines
1.8 KiB
TypeScript

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