mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 13:41:22 +00:00
get-all-products.ts
This commit is contained in:
parent
800ba45fae
commit
ad1a3a11c3
@ -1,37 +1,37 @@
|
|||||||
import { GraphQLFetcherResult } from '@commerce/api'
|
{ GraphQLFetcherResult } '@commerce/api'
|
||||||
import { getConfig, ShopifyConfig } from '../api'
|
{ getConfig, ShopifyConfig } '../api'
|
||||||
import { ProductEdge } from '../schema'
|
{ ProductEdge } '../schema'
|
||||||
import { getAllProductsQuery } from '../utils/queries'
|
{ getAllProductsQuery } '../utils/queries'
|
||||||
import { normalizeProduct } from '../utils/normalize'
|
{ normalizeProduct } '../utils/normalize'
|
||||||
import { Product } from '@commerce/types'
|
{ Product } '@commerce/types'
|
||||||
|
|
||||||
type Variables = {
|
Variables = {
|
||||||
first?: number
|
first?: number
|
||||||
field?: string
|
field?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReturnType = {
|
ReturnType = {
|
||||||
products : Product[]
|
products : Product[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const getAllProducts = async (options: {
|
getAllProducts = (options: {
|
||||||
variables?: Variables
|
variables?: Variables
|
||||||
config?: ShopifyConfig
|
config?: ShopifyConfig
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
}): Promise<ReturnType> => {
|
}): PromiseReturnType => {
|
||||||
let { config, variables = { first: 250 } } = options ?? {}
|
let { config, variables = { first: 250 } } = options ?? {}
|
||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
|
|
||||||
const { data }: GraphQLFetcherResult = await config.fetch(
|
{ data }: GraphQLFetcherResult = config.fetch(
|
||||||
getAllProductsQuery,
|
getAllProductsQuery,
|
||||||
{ variables }
|
{ variables }
|
||||||
)
|
)
|
||||||
|
|
||||||
const products = data.products?.edges?.map(({ node: p }: ProductEdge) =>
|
products = data.products?.edges?.map(({ node: p }: ProductEdge) =>
|
||||||
normalizeProduct(p)
|
normalizeProduct(p)
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
{
|
||||||
products,
|
products,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user