"use strict" ;
const assert = require ( "assert" ) ;
const assureResponse = require ( "../../shared/assure-response" ) ;
module . exports = function lcscFindCategories ( state ) {
let { session } = state ;
return async function ( { createItem } ) {
let response = await session . get ( "https://wwwapi.lcsc.com/v1/home/category" ) ;
assureResponse ( response ) ;
assert ( response . body . length > 0 ) ;
assert ( response . statusCode === 200 ) ;
function processCategoryEntries ( categories ) {
for ( let category of categories ) {
let productCount = category . productNum ;
let pageCount = Math . ceil ( productCount / 500 ) ;
// NOTE: We do *not* use the page count indicated by the API, but instead calculate it ourself from the product count. This is because the API-specified page count will cap out at the equivalent of 10k items, even when more pages than that are actually available.
for ( let i = 1 ; i <= pageCount ; i ++ ) {
createItem ( {
id : ` lcsc:category: ${ category . catalogId } :page- ${ i } ` ,
tags : [ "lcsc:category" ] ,
data : {
... category ,
pageNumber : i
}
} ) ;
}
if ( category . childCatelogs != null ) {
processCategoryEntries ( category . childCatelogs ) ;
}
}
}
processCategoryEntries ( response . body ) ;
} ;
} ;