@@ -11,14 +11,22 @@ const currencyMap = {
1111} ;
1212
1313export async function getLiteApiResponse ( request , url , env , serverContext ) {
14- let q , lang ;
14+ let q , lang , uid , tags , sort , sort_direction ;
1515 if ( request . method . toUpperCase ( ) === 'GET' ) {
1616 q = url . searchParams . get ( 'q' ) ;
1717 lang = url . searchParams . get ( 'lang' ) ?? 'en' ;
18+ uid = url . searchParams . get ( 'uid' ) ;
19+ tags = url . searchParams . get ( 'tags' ) ?. split ( ',' ) ?? [ ] ;
20+ sort = url . searchParams . get ( 'sort' ) ;
21+ sort_direction = url . searchParams . get ( 'sort_direction' ) ;
1822 } else if ( request . method . toUpperCase ( ) === 'POST' ) {
1923 const body = await request . json ( ) ;
2024 q = body . q ;
2125 lang = body . lang ?? 'en' ;
26+ uid = body . uid ;
27+ tags = body . tags ?. split ( ',' ) ?? [ ] ;
28+ sort = body . sort ;
29+ sort_direction = body . sort_direction ;
2230 } else {
2331 return new Response ( null , {
2432 status : 405 ,
@@ -35,7 +43,7 @@ export async function getLiteApiResponse(request, url, env, serverContext) {
3543 let key ;
3644 if ( env . SKIP_CACHE !== 'true' && ! request . headers . has ( 'cache-check-complete' ) ) {
3745 const requestStart = new Date ( ) ;
38- key = await cacheMachine . createKey ( env , url . pathname , { q, lang, gameMode } ) ;
46+ key = await cacheMachine . createKey ( env , url . pathname , { q, lang, gameMode, uid , tags , sort , sort_direction } ) ;
3947 const cachedResponse = await cacheMachine . get ( env , { key} ) ;
4048 if ( cachedResponse ) {
4149 // Construct a new response with the cached data
@@ -105,25 +113,46 @@ export async function getLiteApiResponse(request, url, env, serverContext) {
105113 if ( endpoint . endsWith ( '/download' ) ) {
106114 responseOptions . headers [ 'Content-Disposition' ] = 'attachment; filename="items.json"' ;
107115 }
116+ if ( tags ) {
117+ items = await data . worker . item . getItemsByTypes ( context , info , tags , items ) ;
118+ }
108119 }
109120 if ( ! items && endpoint . startsWith ( 'item' ) ) {
110- if ( ! q ) {
111- throw new Error ( 'No q parameter provided' ) ;
121+ if ( ! q && ! uid ) {
122+ throw new Error ( 'The item request requires either a q or uid parameter' ) ;
123+ }
124+ if ( q ) {
125+ items = await data . worker . item . getItemsByName ( context , info , q ) ;
126+ } else if ( uid ) {
127+ items = [ await data . worker . item . getItem ( context , info , uid ) ] ;
112128 }
113- items = await data . worker . item . getItemsByName ( context , info , q ) ;
114129 }
115130 items = items . map ( toLiteApiItem ) ;
116131 ttl = data . getRequestTtl ( context . requestId ) ;
117132 } catch ( error ) {
118- throw ( error ) ;
133+ return new Response ( error . message , { status : 400 } ) ;
119134 } finally {
120135 data . clearRequestData ( context . requestId ) ;
121136 }
137+ if ( sort && items ?. length ) {
138+ items . sort ( ( a , b ) => {
139+ let aValue = sort_direction === 'desc' ? b [ sort ] : a [ sort ] ;
140+ let bValue = sort_direction === 'desc' ? a [ sort ] : b [ sort ] ;
141+ if ( sort === 'updated' ) {
142+ aValue = new Date ( aValue ) ;
143+ bValue = new Date ( bValue ) ;
144+ }
145+ if ( typeof aValue === 'string' ) {
146+ return aValue . localeCompare ( bValue , lang ) ;
147+ }
148+ return aValue - bValue ;
149+ } ) ;
150+ }
122151 const responseBody = JSON . stringify ( items ?? [ ] , null , 4 ) ;
123152
124153 // Update the cache with the results of the query
125154 if ( env . SKIP_CACHE !== 'true' && ttl > 0 ) {
126- const putCachePromise = cacheMachine . put ( env , responseBody , { key, query : url . pathname , variables : { q, lang, gameMode } , ttl : String ( ttl ) } ) ;
155+ const putCachePromise = cacheMachine . put ( env , responseBody , { key, query : url . pathname , variables : { q, lang, gameMode, uid , tags , sort , sort_direction } , ttl : String ( ttl ) } ) ;
127156 // using waitUntil doens't hold up returning a response but keeps the worker alive as long as needed
128157 if ( request . ctx ?. waitUntil ) {
129158 request . ctx . waitUntil ( putCachePromise ) ;
0 commit comments