@@ -3,8 +3,9 @@ import take from 'lodash/take';
33import { NextApiRequest , NextApiResponse } from 'next' ;
44
55import data from '~/assets/data.json' ;
6- import { type LibraryType } from '~/types' ;
6+ import { type LibraryType , QueryOrder } from '~/types' ;
77import { NUM_PER_PAGE } from '~/util/Constants' ;
8+ import { parseQueryParams } from '~/util/parseQueryParams' ;
89import { handleFilterLibraries } from '~/util/search' ;
910import * as Sorting from '~/util/sorting' ;
1011
@@ -29,7 +30,7 @@ const ReversedSortedData = Object.entries(getData()).reduce(
2930 { }
3031) ;
3132
32- const getAllowedOrderString = ( req : NextApiRequest , querySearch ?: string ) => {
33+ function getAllowedOrderString ( req : NextApiRequest , querySearch ?: string ) : QueryOrder {
3334 let sortBy = querySearch ? SortingKeys . at ( - 1 ) : SortingKeys [ 0 ] ;
3435
3536 SortingKeys . forEach ( sortName => {
@@ -38,58 +39,60 @@ const getAllowedOrderString = (req: NextApiRequest, querySearch?: string) => {
3839 }
3940 } ) ;
4041
41- return sortBy ;
42- } ;
42+ return sortBy as QueryOrder ;
43+ }
4344
4445export default function handler ( req : NextApiRequest , res : NextApiResponse ) {
4546 res . statusCode = 200 ;
4647 res . setHeader ( 'Content-Type' , 'application/json' ) ;
4748
48- const querySearch = req . query . search
49- ? req . query . search . toString ( ) . toLowerCase ( ) . trim ( )
49+ const parsedQuery = parseQueryParams ( req . query ) ;
50+
51+ const querySearch = parsedQuery . search
52+ ? parsedQuery . search . toString ( ) . toLowerCase ( ) . trim ( )
5053 : undefined ;
5154
5255 const sortBy = getAllowedOrderString ( req , querySearch ) ;
53- const sortDirection = req . query . direction ?? 'descending' ;
56+ const sortDirection = parsedQuery . direction ?? 'descending' ;
5457 const libraries = sortDirection === 'ascending' ? ReversedSortedData [ sortBy ] : SortedData [ sortBy ] ;
5558
5659 const filteredLibraries = handleFilterLibraries ( {
5760 libraries,
5861 sortBy,
59- queryTopic : req . query . topic ,
62+ queryTopic : parsedQuery . topic ,
6063 querySearch,
6164 support : {
62- ios : req . query . ios ,
63- android : req . query . android ,
64- web : req . query . web ,
65- windows : req . query . windows ,
66- macos : req . query . macos ,
67- expoGo : req . query . expoGo ,
68- fireos : req . query . fireos ,
69- tvos : req . query . tvos ,
70- visionos : req . query . visionos ,
65+ ios : parsedQuery . ios ,
66+ android : parsedQuery . android ,
67+ web : parsedQuery . web ,
68+ windows : parsedQuery . windows ,
69+ macos : parsedQuery . macos ,
70+ expoGo : parsedQuery . expoGo ,
71+ fireos : parsedQuery . fireos ,
72+ tvos : parsedQuery . tvos ,
73+ visionos : parsedQuery . visionos ,
7174 } ,
72- hasExample : req . query . hasExample ,
73- hasImage : req . query . hasImage ,
74- hasTypes : req . query . hasTypes ,
75- hasNativeCode : req . query . hasNativeCode ,
76- isMaintained : req . query . isMaintained ,
77- isPopular : req . query . isPopular ,
78- isRecommended : req . query . isRecommended ,
79- wasRecentlyUpdated : req . query . wasRecentlyUpdated ,
80- minPopularity : req . query . minPopularity ,
81- minMonthlyDownloads : req . query . minMonthlyDownloads ,
82- newArchitecture : req . query . newArchitecture ,
83- skipLibs : req . query . skipLibs ,
84- skipTools : req . query . skipTools ,
85- skipTemplates : req . query . skipTemplates ,
75+ hasExample : parsedQuery . hasExample ,
76+ hasImage : parsedQuery . hasImage ,
77+ hasTypes : parsedQuery . hasTypes ,
78+ hasNativeCode : parsedQuery . hasNativeCode ,
79+ isMaintained : parsedQuery . isMaintained ,
80+ isPopular : parsedQuery . isPopular ,
81+ isRecommended : parsedQuery . isRecommended ,
82+ wasRecentlyUpdated : parsedQuery . wasRecentlyUpdated ,
83+ minPopularity : parsedQuery . minPopularity ,
84+ minMonthlyDownloads : parsedQuery . minMonthlyDownloads ,
85+ newArchitecture : parsedQuery . newArchitecture ,
86+ skipLibs : parsedQuery . skipLibs ,
87+ skipTools : parsedQuery . skipTools ,
88+ skipTemplates : parsedQuery . skipTemplates ,
8689 } ) ;
8790
88- const offset = req . query . offset ? parseInt ( req . query . offset . toString ( ) , 10 ) : 0 ;
89- const limit = req . query . limit ? parseInt ( req . query . limit . toString ( ) , 10 ) : NUM_PER_PAGE ;
91+ const offset = parsedQuery . offset ? parseInt ( parsedQuery . offset . toString ( ) , 10 ) : 0 ;
92+ const limit = parsedQuery . limit ? parseInt ( parsedQuery . limit . toString ( ) , 10 ) : NUM_PER_PAGE ;
9093
9194 const relevanceSortedLibraries =
92- querySearch ?. length && ( ! req . query . order || req . query . order === 'relevance' )
95+ querySearch ?. length && ( ! parsedQuery . order || parsedQuery . order === 'relevance' )
9396 ? sortDirection === 'ascending'
9497 ? Sorting . relevance ( [ ...filteredLibraries ] ) . reverse ( )
9598 : Sorting . relevance ( [ ...filteredLibraries ] )
0 commit comments