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