commerce/framework/commercetools/api/operations/get-all-product-paths.ts
matias-delavega-dg-dmi 716b540966
TEC-252 and TEC-256: Implementing fetchers, products list and search (#5)
* TEC-252: Base integration with commercetools using fetchers for REST and GraphQL endpoints

* TEC-252: WIP commenting some components that are failing while we don't have all the hooks defined

* add sdk integration

* TEC-256: Implementing product search

* TEC-256: removing unnecessary env variables

* TEC-256: review comments

* TEC-256: other remaining review fixes

Co-authored-by: nicossosa93 <nicolas.sosa@devgurus.io>
2021-06-30 10:05:26 -03:00

51 lines
1.5 KiB
TypeScript

import { OperationContext, OperationOptions } from '@commerce/api/operations'
import { GetAllProductPathsOperation } from '@commerce/types/product'
import { CommercetoolsConfig, Provider } from '@framework/api'
export type GetAllProductPathsResult = {
products: Array<{ node: { path: string } }>
}
export default function getAllProductPathsOperation({
commerce,
}: OperationContext<Provider>) {
async function getAllProductPaths<
T extends GetAllProductPathsOperation
>(opts?: {
variables?: T['variables']
config?: CommercetoolsConfig
}): Promise<T['data']>
async function getAllProductPaths<T extends GetAllProductPathsOperation>(
opts: {
variables?: T['variables']
config?: CommercetoolsConfig
} & OperationOptions
): Promise<T['data']>
async function getAllProductPaths<T extends GetAllProductPathsOperation>({
query,
variables,
config: cfg,
}: {
query?: string
variables?: T['variables']
config?: CommercetoolsConfig
} = {}): Promise<T['data']> {
const config = commerce.getConfig(cfg)
// RecursivePartial forces the method to check for every prop in the data, which is
// required in case there's a custom `query`
const data: any = await config.fetchProducts(query)
const paths = data.body.results.map((prod: any) => ({
// TODO: TEC-264: Handle the locale properly
path: `/${prod.slug.en}`,
}))
return {
products: paths,
}
}
return getAllProductPaths
}