Commit 3 of 4:
a86e2d6b9b
feat(api): pagination metadata on list endpoints (#978)
previous
next
whole branch
Review threads live on the whole diff, not on one commit, so
none are shown here - a thread's line means something in the
branch's final form, and painting it into an intermediate
step would put it on code it is not about.
app/Actions/Activity/ActivityIndexAction.ts modified +4 -3 Changes to app/Actions/Activity/ActivityIndexAction.ts ⋯ 29 lines Show the 29 lines above this hunk @@ -30,13 +30,13 @@ export default new Action({ 30 30 31 31 async handle ( request ) { 32 32 const userId = request . get < number > ( 'user_id' ) 33 - const limit = Math . min ( Math . max ( request . get < number > ( 'limit' ) || 100 , 1 ) , 500 ) 33 + const page = readPageParams ( request , { defaultLimit : 100 , maxLimit : 500 } ) 34 34 35 35 try { 36 36 const query = userId 37 37 ? Activity . where ( 'user_id' , '=' , userId ) 38 38 : Activity . query ( ) 39 - const rows = ( await query . orderBy ( 'completed_at' , 'desc' ) . limit ( limit ) . get ( ) ) ? ? [ ] 39 + const rows = ( await query . orderBy ( 'completed_at' , 'desc' ) . get ( ) ) ? ? [ ] 40 40 41 41 42 42 const userIds = [ ... new Set ( rows . map ( ( a : any ) => a . user_id ) . filter ( Boolean ) ) ] ⋯ 28 lines Show the 28 lines above this hunk @@ -71,7 +71,8 @@ export default new Action({ 71 71 } 72 72 } ) 73 73 74 - return response . json ( { activities , meta : { total : activities . length } } ) 74 + const { items , meta } = paginate ( activities , page ) 75 + return response . json ( { activities : items , meta } ) 75 76 } 76 77 catch ( error ) { 77 78 console . error ( '[activities] index failed:' , error )
app/Actions/Challenge/ChallengeIndexAction.ts modified +2 -1 Changes to app/Actions/Challenge/ChallengeIndexAction.ts ⋯ 32 lines Show the 32 lines above this hunk @@ -33,7 +33,8 @@ export default new Action({ 33 33 } ) ) 34 34 . sort ( ( a : any , b : any ) => Date . parse ( b . createdAt ? ? '' ) - Date . parse ( a . createdAt ? ? '' ) ) 35 35 36 - return response . json ( { success : true , challenges , meta : { total : challenges . length } } ) 36 + const paged = paginate ( challenges , readPageParams ( request ) ) 37 + return response . json ( { success : true , challenges : paged . items , meta : paged . meta } ) 37 38 } 38 39 catch ( error ) { 39 40 console . error ( '[challenges] index failed:' , error )
app/Actions/Club/ClubIndexAction.ts modified +2 -1 Changes to app/Actions/Club/ClubIndexAction.ts ⋯ 72 lines Show the 72 lines above this hunk @@ -73,7 +73,8 @@ export default new Action({ 73 73 } ) 74 74 . sort ( ( a : any , b : any ) => b . memberCount - a . memberCount || a . id - b . id ) 75 75 76 - return response . json ( { success : true , clubs : result , meta : { total : result . length } } ) 76 + const paged = paginate ( result , readPageParams ( request ) ) 77 + return response . json ( { success : true , clubs : paged . items , meta : paged . meta } ) 77 78 } 78 79 catch ( error ) { 79 80 console . error ( '[clubs] index failed:' , error )
app/Actions/Social/UserSearchAction.ts modified +2 -1 Changes to app/Actions/Social/UserSearchAction.ts ⋯ 45 lines Show the 45 lines above this hunk @@ -46,7 +46,8 @@ export default new Action({ 46 46 47 47 . sort ( ( a : any , b : any ) => b . activityCount - a . activityCount || b . followerCount - a . followerCount || a . id - b . id ) 48 48 49 - return response . json ( { success : true , athletes , meta : { total : athletes . length , query : q } } ) 49 + const paged = paginate ( athletes , readPageParams ( request , { defaultLimit : 20 , maxLimit : 50 } ) ) 50 + return response . json ( { success : true , athletes : paged . items , meta : { ... paged . meta , query : q } } ) 50 51 } 51 52 catch ( error ) { 52 53 console . error ( '[users] search failed:' , error )
app/Actions/Trail/SavedTrailIndexAction.ts modified +2 -1 Changes to app/Actions/Trail/SavedTrailIndexAction.ts ⋯ 47 lines Show the 47 lines above this hunk @@ -48,7 +48,8 @@ export default new Action({ 48 48 } 49 49 } ) 50 50 51 - return response . json ( { success : true , savedTrails , meta : { total : savedTrails . length } } ) 51 + const paged = paginate ( savedTrails , readPageParams ( request ) ) 52 + return response . json ( { success : true , savedTrails : paged . items , meta : paged . meta } ) 52 53 } 53 54 catch ( error ) { 54 55 console . error ( '[saved-trails] index failed:' , error )
app/Actions/Trail/TrailIndexAction.ts modified +7 -4 Changes to app/Actions/Trail/TrailIndexAction.ts ⋯ 3 lines Show the 3 lines above this hunk @@ -4,20 +4,23 @@ export default new Action({ 4 4 method : 'GET' , 5 5 6 6 async handle ( request ) { 7 - const limit = Math . min ( Math . max ( request . get < number > ( 'limit' ) || 500 , 1 ) , 500 ) 7 + const page = readPageParams ( request , { defaultLimit : 500 , maxLimit : 500 } ) 8 8 9 9 try { 10 - const rows = await Trail . limit ( limit ) . get ( ) 10 + const rows = await Trail . all ( ) 11 11 const trails = ( rows ? ? [ ] ) . map ( ( row : Record < string , unknown > ) => ( { 12 12 ... row , 13 13 lat : row . latitude ? ? row . lat , 14 14 lng : row . longitude ? ? row . lng , 15 15 } ) ) 16 - return response . json ( trails ) 16 + 17 + 18 + const { items , meta } = paginate ( trails , page ) 19 + return response . json ( { success : true , trails : items , meta } ) 17 20 } 18 21 catch ( error ) { 19 22 console . error ( '[trails] index failed:' , error ) 20 - return response . json ( [ ] , 200 ) 23 + return response . json ( { success : false , trails : [ ] , meta : { offset : 0 , limit : 0 , total : 0 , hasMore : false } , error : 'Failed to fetch trails' } , 500 ) 21 24 } 22 25 } , 23 26 } )
app/Actions/Trail/TrailReviewIndexAction.ts modified +2 -1 Changes to app/Actions/Trail/TrailReviewIndexAction.ts ⋯ 35 lines Show the 35 lines above this hunk @@ -36,7 +36,8 @@ export default new Action({ 36 36 createdAt : r . created_at , 37 37 } ) ) 38 38 39 - return response . json ( { success : true , reviews , meta : { total : reviews . length } } ) 39 + const paged = paginate ( reviews , readPageParams ( request ) ) 40 + return response . json ( { success : true , reviews : paged . items , meta : paged . meta } ) 40 41 } 41 42 catch ( error ) { 42 43 console . error ( '[trail-reviews] index failed:' , error )
storage/framework/auto-imports/functions.ts modified +2 -0 Changes to storage/framework/auto-imports/functions.ts ⋯ 4 lines Show the 4 lines above this hunk @@ -5,6 +5,8 @@ export type { TerritoryRankStatsRow, TerritoryGainEvent, TerritoryRankAssignment 5 5 export { increment , count } from '../../../resources/functions/counter' 6 6 export { shapeChallenge } from '../../../resources/functions/challenges' 7 7 export type { ChallengeNames } from '../../../resources/functions/challenges' 8 + export { readPageParams , paginate } from '../../../resources/functions/pagination' 9 + export type { PageParams , PageMeta } from '../../../resources/functions/pagination' 8 10 export { longestDayStreak , hasSubSevenMile , computeAchievementProgress } from '../../../resources/functions/achievements' 9 11 export type { AchievementDefinitionRow , AchievementProgressEntry } from '../../../resources/functions/achievements' 10 12 export { extractMentionQuery , mentionCandidates , applyMention , parseMentions } from '../../../resources/functions/mentions'
storage/framework/auto-imports/globals.ts modified +2 -0 Changes to storage/framework/auto-imports/globals.ts ⋯ 13 lines Show the 13 lines above this hunk @@ -14,6 +14,8 @@ declare global { 14 14 const increment : typeof autoImports . increment 15 15 const count : typeof autoImports . count 16 16 const shapeChallenge : typeof autoImports . shapeChallenge 17 + const readPageParams : typeof autoImports . readPageParams 18 + const paginate : typeof autoImports . paginate 17 19 const longestDayStreak : typeof autoImports . longestDayStreak 18 20 const hasSubSevenMile : typeof autoImports . hasSubSevenMile 19 21 const computeAchievementProgress : typeof autoImports . computeAchievementProgress