mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 15:06:59 +00:00
get-all-product-path operation
This commit is contained in:
parent
7bf6e07cec
commit
b4a81e7d9a
@ -1,12 +1,36 @@
|
|||||||
|
import { OperationContext } from '@vercel/commerce/api/operations'
|
||||||
|
import type { Product } from '@vercel/commerce/types/product'
|
||||||
|
import { SyliusConfig } from 'api'
|
||||||
|
import { SyliusProduct } from 'types/products'
|
||||||
|
import { normalizeProduct } from '../../utils/normalize/normalize-product'
|
||||||
|
|
||||||
export type GetAllProductPathsResult = {
|
export type GetAllProductPathsResult = {
|
||||||
products: Array<{ path: string }>
|
products: Array<{ path: string }>
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function getAllProductPathsOperation() {
|
export default function getAllProductPathsOperation({
|
||||||
function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
commerce,
|
||||||
return Promise.resolve({
|
}: OperationContext<any>) {
|
||||||
products: [],
|
async function getAllProductPaths({
|
||||||
})
|
query,
|
||||||
|
variables,
|
||||||
|
config: cfg,
|
||||||
|
}: {
|
||||||
|
query?: string
|
||||||
|
variables?: any
|
||||||
|
config?: Partial<SyliusConfig>
|
||||||
|
preview?: boolean
|
||||||
|
} = {}): Promise<GetAllProductPathsResult> {
|
||||||
|
const config = commerce.getConfig(cfg)
|
||||||
|
const syliusProducts = await config.fetch('GET', '/products')
|
||||||
|
const products = syliusProducts.map((syliusProduct: SyliusProduct) =>
|
||||||
|
normalizeProduct(syliusProduct)
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
products: products.map((product: Product) => ({
|
||||||
|
path: product.path,
|
||||||
|
})),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return getAllProductPaths
|
return getAllProductPaths
|
||||||
|
@ -21,7 +21,7 @@ export const normalizeProduct = (product: SyliusProduct): Product => {
|
|||||||
description: product.shortDescription,
|
description: product.shortDescription,
|
||||||
descriptionHtml: product.description,
|
descriptionHtml: product.description,
|
||||||
slug: product.slug,
|
slug: product.slug,
|
||||||
path: product.slug,
|
path: `/${product.slug}`,
|
||||||
images: product.images.map((image) => normalizeProductImage(image)),
|
images: product.images.map((image) => normalizeProductImage(image)),
|
||||||
variants: product.variants.map((variant) =>
|
variants: product.variants.map((variant) =>
|
||||||
normalizeProductVariant(variant)
|
normalizeProductVariant(variant)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user