import queryString from 'query-string' ; import { get , getRaw , post } from 'YesterTech/api/utils' ; import { Product , ProductId } from 'YesterTech/types' ; type ProductQuery = { categories ? : string | undefined | null ; brands ? : string | undefined | null ; conditions ? : string | undefined | null ; } ; export async function getProducts ( search = { } , page = 1 ) { const realSearch = { ... search , _limit : 10 , _page : page , page : undefined , category : search . categories ? search . categories . split ( ',' ) : undefined , brand : search . brands ? search . brands . split ( ',' ) : undefined , condition : search . conditions ? search . conditions . split ( ',' ) : undefined , } ; const query = queryString . stringify ( realSearch || { } ) ; const res = await getRaw ( `/products?${ query }` ) ; const products = await res . json ( ) ; return { products , totalResults : parseInt ( res . headers . get ( 'x-total-count' ) ! , 10 ) , } ; } export async function getProduct ( productId ) { return await get ( `/products/${ productId }` ) ; } export async function addProduct ( data ) { return await post ( `/products` , data ) ; } export async function getMetaData ( ) { const products = await get ( '/products' ) ; const categories = products . reduce < string [ ] > ( ( c , p ) => c . concat ( [ p . category || '' ] ) , [ ] ) ; const brands = products . reduce < string [ ] > ( ( b , p_1 ) => b . concat ( [ p_1 . brand || '' ] ) , [ ] ) ; return { categories : [ ... new Set ( categories ) ] , brands : [ ... new Set ( brands ) ] , } ; }