@@ -323,18 +323,17 @@ export async function getCategoriesList(
323
323
324
324
/**
325
325
* Fetches a category by handle, using the Medusa API or the Medusa Product Module, depending on the feature flag.
326
- * @param handle (string) - The handle of the category to retrieve
326
+ * @param categoryHandle (string) - The handle of the category to retrieve
327
327
* @returns collections (array) - An array of categories (should only be one)
328
328
* @returns response (object) - An object containing the products and the number of products in the category
329
329
* @returns nextPage (number) - The offset of the next page of products
330
330
*/
331
- export async function getCategoryByHandle ( handle : string ) : Promise < {
331
+ export async function getCategoryByHandle ( categoryHandle : string [ ] ) : Promise < {
332
332
product_categories : ProductCategoryWithChildren [ ]
333
- parent : ProductCategoryWithChildren
334
333
} > {
335
334
if ( PRODUCT_MODULE_ENABLED ) {
336
335
DEBUG && console . log ( "PRODUCT_MODULE_ENABLED" )
337
- const data = await fetch ( `${ API_BASE_URL } /api/categories/${ handle } ` )
336
+ const data = await fetch ( `${ API_BASE_URL } /api/categories/${ categoryHandle } ` )
338
337
. then ( ( res ) => res . json ( ) )
339
338
. catch ( ( err ) => {
340
339
throw err
@@ -344,19 +343,29 @@ export async function getCategoryByHandle(handle: string): Promise<{
344
343
}
345
344
346
345
DEBUG && console . log ( "PRODUCT_MODULE_DISABLED" )
347
- const data = await medusaRequest ( "GET" , "/product-categories" , {
348
- query : {
349
- handle,
350
- } ,
346
+
347
+ const handles = categoryHandle . map ( ( handle : string , index : number ) => {
348
+ return categoryHandle . slice ( 0 , index + 1 ) . join ( "/" )
351
349
} )
352
- . then ( ( res ) => res . body )
353
- . catch ( ( err ) => {
354
- throw err
350
+
351
+ const product_categories = [ ] as ProductCategoryWithChildren [ ]
352
+
353
+ for ( const handle of handles ) {
354
+ await medusaRequest ( "GET" , "/product-categories" , {
355
+ query : {
356
+ handle,
357
+ } ,
355
358
} )
359
+ . then ( ( { body } ) => {
360
+ product_categories . push ( body . product_categories [ 0 ] )
361
+ } )
362
+ . catch ( ( err ) => {
363
+ throw err
364
+ } )
365
+ }
356
366
357
367
return {
358
- product_categories : data . product_categories ,
359
- parent : data . product_categories [ 0 ] . parent_category ,
368
+ product_categories,
360
369
}
361
370
}
362
371
@@ -399,7 +408,7 @@ export async function getProductsByCategoryHandle({
399
408
}
400
409
401
410
DEBUG && console . log ( "PRODUCT_MODULE_DISABLED" )
402
- const { id } = await getCategoryByHandle ( handle ) . then (
411
+ const { id } = await getCategoryByHandle ( [ handle ] ) . then (
403
412
( res ) => res . product_categories [ 0 ]
404
413
)
405
414
0 commit comments