@@ -36,25 +36,93 @@ const getFakePackageListing = (
3636// here that's done for community listing, but getting all the filters
3737// to work properly might not be worth the effort.
3838export const getFakePackageListings = async (
39- type : PackageListingType
40- // ordering = "last-updated",
41- // page = 1,
42- // q = "",
43- // includedCategories = [],
44- // excludedCategories = [],
45- // section = "",
46- // nsfw = false,
47- // deprecated = false
48- ) => ( {
49- count : 200 ,
50- hasMore : true ,
51- results : range ( 20 ) . map ( ( ) =>
52- getFakePackageListing (
53- type . communityId ,
54- type . kind === "namespace" ? type . namespaceId : faker . word . sample ( )
55- )
56- ) ,
57- } ) ;
39+ type : PackageListingType ,
40+ ordering = "last-updated" ,
41+ page = 1 ,
42+ q = "" ,
43+ includedCategories : string [ ] = [ ] ,
44+ excludedCategories : string [ ] = [ ] ,
45+ section = "" ,
46+ nsfw = false ,
47+ deprecated = false
48+ ) => {
49+ const pageSize = 20 ;
50+ const count = 200 ;
51+ const normalizedPage = Number . isFinite ( page )
52+ ? Math . max ( 1 , Math . trunc ( page ) )
53+ : 1 ;
54+ const currentPage = normalizedPage ;
55+ const startIndex = ( currentPage - 1 ) * pageSize ;
56+ const endIndex = Math . min ( startIndex + pageSize , count ) ;
57+ const pageLength = Math . max ( endIndex - startIndex , 0 ) ;
58+
59+ const collectionPath = ( ( ) => {
60+ switch ( type . kind ) {
61+ case "community" :
62+ return `api/cyberstorm/listing/${ type . communityId . toLowerCase ( ) } /` ;
63+ case "namespace" :
64+ return `api/cyberstorm/listing/${ type . communityId . toLowerCase ( ) } /${ type . namespaceId . toLowerCase ( ) } /` ;
65+ case "package-dependants" :
66+ return `api/cyberstorm/listing/${ type . communityId . toLowerCase ( ) } /${ type . namespaceId . toLowerCase ( ) } /${ type . packageName . toLowerCase ( ) } /dependants/` ;
67+ default :
68+ return "api/cyberstorm/listing/" ;
69+ }
70+ } ) ( ) ;
71+
72+ const buildQueryString = ( targetPage : number ) => {
73+ const params = new URLSearchParams ( ) ;
74+ params . set ( "page" , String ( targetPage ) ) ;
75+ if ( ordering ) {
76+ params . set ( "ordering" , ordering ) ;
77+ }
78+ if ( q ) {
79+ params . set ( "q" , q ) ;
80+ }
81+ includedCategories ?. forEach ( ( value ) => {
82+ params . append ( "included_categories" , value ) ;
83+ } ) ;
84+ excludedCategories ?. forEach ( ( value ) => {
85+ params . append ( "excluded_categories" , value ) ;
86+ } ) ;
87+ if ( section ) {
88+ params . set ( "section" , section ) ;
89+ }
90+ if ( nsfw ) {
91+ params . set ( "nsfw" , "true" ) ;
92+ }
93+ if ( deprecated ) {
94+ params . set ( "deprecated" , "true" ) ;
95+ }
96+ return params . toString ( ) ;
97+ } ;
98+
99+ const buildPageUrl = ( targetPage : number ) =>
100+ `https://thunderstore.io/${ collectionPath } ?${ buildQueryString ( targetPage ) } ` ;
101+
102+ const hasNext = endIndex < count ;
103+ const next = hasNext ? buildPageUrl ( currentPage + 1 ) : null ;
104+ const previous = currentPage > 1 ? buildPageUrl ( currentPage - 1 ) : null ;
105+
106+ const results = range ( pageLength ) . map ( ( index ) => {
107+ const namespaceId =
108+ type . kind === "namespace" || type . kind === "package-dependants"
109+ ? type . namespaceId
110+ : `${ type . communityId } -namespace-${ currentPage } -${ index } ` ;
111+ const packageName =
112+ type . kind === "package-dependants"
113+ ? `${ type . packageName . toLowerCase ( ) } -dependant-${ currentPage } -${ index } `
114+ : `${ namespaceId } -package-${ currentPage } -${ index } ` ;
115+
116+ return getFakePackageListing ( type . communityId , namespaceId , packageName ) ;
117+ } ) ;
118+
119+ return {
120+ count,
121+ next,
122+ previous,
123+ results,
124+ } ;
125+ } ;
58126
59127const getFakeDependencies = async (
60128 community : string ,
@@ -274,11 +342,14 @@ export const getFakePackageVersionDependencies = async (
274342 page ?: number
275343) => {
276344 setSeed ( `${ namespace } -${ name } -${ version } ` ) ;
277- page = page ?? 1 ;
345+ const normalizedPage =
346+ typeof page === "number" && Number . isFinite ( page )
347+ ? Math . max ( 1 , Math . trunc ( page ) )
348+ : 1 ;
278349
279350 // Split the fake data into pages of 10 items each.
280351
281- const start = ( page - 1 ) * 10 ;
352+ const start = ( normalizedPage - 1 ) * 10 ;
282353 const end = start + 10 ;
283354 const items = fakePackageVersionDependencies . slice ( start , end ) ;
284355
@@ -287,13 +358,13 @@ export const getFakePackageVersionDependencies = async (
287358 next :
288359 end < fakePackageVersionDependencies . length
289360 ? `https://thunderstore.io/api/cyberstorm/package/${ namespace } /${ name } /v/${ version } /dependencies/?page=${
290- page + 1
361+ normalizedPage + 1
291362 } `
292363 : null ,
293364 previous :
294- page > 1
365+ normalizedPage > 1
295366 ? `https://thunderstore.io/api/cyberstorm/package/${ namespace } /${ name } /v/${ version } /dependencies/?page=${
296- page - 1
367+ normalizedPage - 1
297368 } `
298369 : null ,
299370 results : items ,
0 commit comments