mirror of
https://github.com/vercel/commerce.git
synced 2025-07-03 19:51:22 +00:00
36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
import type { OperationContext } from '@commerce/api/operations'
|
|
import type { GetAllProductPathsOperation } from '@commerce/types/product'
|
|
|
|
import type { RawProduct } from '../../types/product'
|
|
import type { OrdercloudConfig, Provider } from '../index'
|
|
|
|
export type GetAllProductPathsResult = {
|
|
products: Array<{ path: string }>
|
|
}
|
|
|
|
export default function getAllProductPathsOperation({
|
|
commerce,
|
|
}: OperationContext<Provider>) {
|
|
async function getAllProductPaths<T extends GetAllProductPathsOperation>({
|
|
config,
|
|
}: {
|
|
config?: Partial<OrdercloudConfig>
|
|
} = {}): Promise<T['data']> {
|
|
// Get fetch from the config
|
|
const { fetch } = commerce.getConfig(config)
|
|
|
|
// Get all products
|
|
const rawProducts: RawProduct[] = await fetch<{ Items: RawProduct[] }>(
|
|
'GET',
|
|
'/products'
|
|
).then((response) => response.Items)
|
|
|
|
return {
|
|
// Match a path for every product retrieved
|
|
products: rawProducts.map((product) => ({ path: `/${product.ID}` })),
|
|
}
|
|
}
|
|
|
|
return getAllProductPaths
|
|
}
|