From cc424e58b3bb32e557fb8b2cbbd185e4629d22bf Mon Sep 17 00:00:00 2001 From: Dom Sip Date: Mon, 11 Apr 2022 17:30:43 +0100 Subject: [PATCH] get all paths for SSG --- .../api/operations/get-all-product-paths.ts | 44 ++++++++++++++++--- .../src/api/operations/get-all-products.ts | 2 +- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/packages/sfcc/src/api/operations/get-all-product-paths.ts b/packages/sfcc/src/api/operations/get-all-product-paths.ts index b01d40ca8..88d5f5135 100644 --- a/packages/sfcc/src/api/operations/get-all-product-paths.ts +++ b/packages/sfcc/src/api/operations/get-all-product-paths.ts @@ -1,14 +1,46 @@ -// import data from '../../data.json' +import { Product } from '@vercel/commerce/types/product' +import { OperationContext } from '@vercel/commerce/api/operations' +import { normalizeSearchProducts } from '../utils/normalise-product'; +import { SFCCConfig } from '..' export type GetAllProductPathsResult = { products: Array<{ path: string }> } -export default function getAllProductPathsOperation() { - function getAllProductPaths(): Promise { - return Promise.resolve({ - products: [], // TODO: SSG for products - }) +export default function getAllProductPathsOperation({ commerce }: OperationContext) { + async function getAllProductPaths({ + query, + config, + variables, + }: { + query?: string + config?: SFCCConfig + variables?: any + } = {}): Promise { + + // TODO: support locale + const { sdk, locale } = commerce.getConfig(config) as SFCCConfig + const searchClient = await sdk.getSearchClient() + + // use SDK search API for initial products same as getAllProductsOperation + const searchResults = await searchClient.productSearch({ + parameters: { q: "dress", limit: variables?.first }, + }); + + let products = [] as Product[]; + + if (searchResults.total) { + products = normalizeSearchProducts(searchResults.hits) + } else { + // TODO: handle this better? + console.log("No results for search"); + } + + return { + products: products?.map(({ slug }: Product) => ({ + path: `/${slug}`, + })), + } } return getAllProductPaths diff --git a/packages/sfcc/src/api/operations/get-all-products.ts b/packages/sfcc/src/api/operations/get-all-products.ts index 4fc86d55f..e909a9c22 100644 --- a/packages/sfcc/src/api/operations/get-all-products.ts +++ b/packages/sfcc/src/api/operations/get-all-products.ts @@ -1,7 +1,7 @@ import { Product } from '@vercel/commerce/types/product' import { GetAllProductsOperation } from '@vercel/commerce/types/product' import type { OperationContext } from '@vercel/commerce/api/operations' -import type { SFCCConfig, Provider } from '../index' +import type { SFCCConfig } from '../index' import { normalizeSearchProducts } from '../utils/normalise-product'; export default function getAllProductsOperation({