mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
@@ -1,48 +0,0 @@
|
||||
import type {
|
||||
GetAllProductPathsQuery,
|
||||
GetAllProductPathsQueryVariables,
|
||||
} from '../schema'
|
||||
import { getConfig, VendureConfig } from '../api'
|
||||
import { getAllProductPathsQuery } from '../lib/queries/get-all-product-paths-query'
|
||||
|
||||
export type GetAllProductPathsResult = {
|
||||
products: Array<{ node: { path: string } }>
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
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<GetAllProductPathsQuery>(query, {
|
||||
variables,
|
||||
})
|
||||
const products = data.products.items
|
||||
|
||||
return {
|
||||
products: products.map((p) => ({ node: { path: `/${p.slug}` } })),
|
||||
}
|
||||
}
|
||||
|
||||
export default getAllProductPaths
|
@@ -1,39 +0,0 @@
|
||||
import { Product } from '@commerce/types'
|
||||
import { getConfig, VendureConfig } from '../api'
|
||||
import { GetAllProductsQuery } from '../schema'
|
||||
import { normalizeSearchResult } from '../lib/normalize'
|
||||
import { getAllProductsQuery } from '../lib/queries/get-all-products-query'
|
||||
|
||||
export type ProductVariables = { first?: number }
|
||||
|
||||
async function getAllProducts(opts?: {
|
||||
variables?: ProductVariables
|
||||
config?: VendureConfig
|
||||
preview?: boolean
|
||||
}): Promise<{ products: Product[] }>
|
||||
|
||||
async function getAllProducts({
|
||||
query = getAllProductsQuery,
|
||||
variables: { ...vars } = {},
|
||||
config,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: ProductVariables
|
||||
config?: VendureConfig
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||
config = getConfig(config)
|
||||
const variables = {
|
||||
input: {
|
||||
take: vars.first,
|
||||
groupByProduct: true,
|
||||
},
|
||||
}
|
||||
const { data } = await config.fetch<GetAllProductsQuery>(query, { variables })
|
||||
|
||||
return {
|
||||
products: data.search.items.map((item) => normalizeSearchResult(item)),
|
||||
}
|
||||
}
|
||||
|
||||
export default getAllProducts
|
@@ -1,64 +0,0 @@
|
||||
import { Product } from '@commerce/types'
|
||||
import { getConfig, VendureConfig } from '../api'
|
||||
import { GetProductQuery } from '../schema'
|
||||
import { getProductQuery } from '../lib/queries/get-product-query'
|
||||
|
||||
async function getProduct({
|
||||
query = getProductQuery,
|
||||
variables,
|
||||
config,
|
||||
}: {
|
||||
query?: string
|
||||
variables: { slug: string }
|
||||
config?: VendureConfig
|
||||
preview?: boolean
|
||||
}): Promise<Product | {} | any> {
|
||||
config = getConfig(config)
|
||||
|
||||
const locale = config.locale
|
||||
const { data } = await config.fetch<GetProductQuery>(query, { variables })
|
||||
const product = data.product
|
||||
|
||||
if (product) {
|
||||
const getOptionGroupName = (id: string): string => {
|
||||
return product.optionGroups.find((og) => og.id === id)!.name
|
||||
}
|
||||
return {
|
||||
product: {
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
description: product.description,
|
||||
slug: product.slug,
|
||||
images: product.assets.map((a) => ({
|
||||
url: a.preview,
|
||||
alt: a.name,
|
||||
})),
|
||||
variants: product.variants.map((v) => ({
|
||||
id: v.id,
|
||||
options: v.options.map((o) => ({
|
||||
// This __typename property is required in order for the correct
|
||||
// variant selection to work, see `components/product/helpers.ts`
|
||||
// `getVariant()` function.
|
||||
__typename: 'MultipleChoiceOption',
|
||||
id: o.id,
|
||||
displayName: getOptionGroupName(o.groupId),
|
||||
values: [{ label: o.name }],
|
||||
})),
|
||||
})),
|
||||
price: {
|
||||
value: product.variants[0].priceWithTax / 100,
|
||||
currencyCode: product.variants[0].currencyCode,
|
||||
},
|
||||
options: product.optionGroups.map((og) => ({
|
||||
id: og.id,
|
||||
displayName: og.name,
|
||||
values: og.options.map((o) => ({ label: o.name })),
|
||||
})),
|
||||
} as Product,
|
||||
}
|
||||
}
|
||||
|
||||
return {}
|
||||
}
|
||||
|
||||
export default getProduct
|
@@ -1,4 +1,2 @@
|
||||
export { default as usePrice } from './use-price'
|
||||
export { default as useSearch } from './use-search'
|
||||
export { default as getProduct } from './get-product'
|
||||
export { default as getAllProducts } from './get-all-products'
|
||||
|
@@ -1,9 +1,10 @@
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import useSearch, { UseSearch } from '@commerce/product/use-search'
|
||||
import { Product } from '@commerce/types'
|
||||
import { Product } from '@commerce/types/product'
|
||||
import { SearchQuery, SearchQueryVariables } from '../schema'
|
||||
import { normalizeSearchResult } from '../lib/normalize'
|
||||
import { searchQuery } from '../lib/queries/search-query'
|
||||
import { normalizeSearchResult } from '../utils/normalize'
|
||||
import { searchQuery } from '../utils/queries/search-query'
|
||||
import { SearchProductsHook } from '../types/product'
|
||||
|
||||
export default useSearch as UseSearch<typeof handler>
|
||||
|
||||
@@ -19,11 +20,7 @@ export type SearchProductsData = {
|
||||
found: boolean
|
||||
}
|
||||
|
||||
export const handler: SWRHook<
|
||||
SearchProductsData,
|
||||
SearchProductsInput,
|
||||
SearchProductsInput
|
||||
> = {
|
||||
export const handler: SWRHook<SearchProductsHook> = {
|
||||
fetchOptions: {
|
||||
query: searchQuery,
|
||||
},
|
||||
@@ -33,7 +30,7 @@ export const handler: SWRHook<
|
||||
const variables: SearchQueryVariables = {
|
||||
input: {
|
||||
term: input.search,
|
||||
collectionId: input.categoryId,
|
||||
collectionId: input.categoryId?.toString(),
|
||||
groupByProduct: true,
|
||||
// TODO: what is the "sort" value?
|
||||
},
|
||||
|
Reference in New Issue
Block a user