@@ -20,6 +20,7 @@ export const common_params = z.object({
2020 limit : z . coerce . number ( ) . int ( ) . positive ( ) . default ( 500 ) ,
2121 page_cursor : z
2222 . string ( )
23+ . base64 ( )
2324 . optional ( )
2425 . nullable ( )
2526 . transform ( ( page_cursor ) => {
@@ -30,11 +31,13 @@ export const common_params = z.object({
3031 } ) ,
3132} )
3233
33- const page_cursor_schema = z . object ( {
34- created_at : z . coerce . date ( ) ,
35- device_id : z . string ( ) ,
36- query_hash : z . string ( ) ,
37- } )
34+ const page_cursor_schema = z . tuple ( [
35+ z . string ( ) ,
36+ z . object ( {
37+ created_at : z . coerce . date ( ) ,
38+ device_id : z . string ( ) ,
39+ } ) ,
40+ ] )
3841
3942export default withRouteSpec ( {
4043 auth : [ "console_session_with_workspace" , "client_session" , "api_key" ] ,
@@ -44,13 +47,24 @@ export default withRouteSpec({
4447 devices : z . array ( device ) ,
4548 pagination : z . object ( {
4649 has_next_page : z . boolean ( ) ,
47- next_page_cursor : z . string ( ) . nullable ( ) ,
50+ next_page_cursor : z . string ( ) . base64 ( ) . nullable ( ) ,
4851 next_page_url : z . string ( ) . url ( ) . nullable ( ) ,
4952 } ) ,
5053 } ) ,
5154} as const ) ( async ( req , res ) => {
5255 const { page_cursor, ...params } = req . commonParams
5356
57+ const query_hash = getPageCursorQueryHash ( params )
58+ const page_cursor_query_hash = page_cursor ?. [ 0 ]
59+ const page_cursor_pointer = page_cursor ?. [ 1 ]
60+ if ( page_cursor_query_hash != null && page_cursor_query_hash !== query_hash ) {
61+ throw new BadRequestException ( {
62+ type : "mismatched_page_parameters" ,
63+ message :
64+ "When using next_page_cursor, the request must send parameters identical to the initial request." ,
65+ } )
66+ }
67+
5468 const {
5569 device_ids,
5670 connected_account_id,
@@ -84,7 +98,7 @@ export default withRouteSpec({
8498
8599 devices = sortBy ( devices , [ "created_at" , "device_id" ] )
86100
87- const device_id = page_cursor ?. device_id
101+ const device_id = page_cursor_pointer ?. device_id
88102 const startIdx =
89103 device_id == null
90104 ? 0
@@ -95,29 +109,21 @@ export default withRouteSpec({
95109 const next_device = devices [ endIdx ]
96110 const has_next_page = next_device != null
97111
98- const query_hash = getPageCursorQueryHash ( params )
99- if (
100- page_cursor ?. query_hash != null &&
101- page_cursor . query_hash !== query_hash
102- ) {
103- throw new BadRequestException ( {
104- type : "mismatched_page_parameters" ,
105- message :
106- "When using next_page_cursor, the request send parameters identical to the initial request." ,
107- } )
112+ let next_page_cursor = null
113+ if ( has_next_page ) {
114+ const next_page_cursor_data = page_cursor_schema . parse ( [
115+ query_hash ,
116+ {
117+ device_id : next_device . device_id ,
118+ created_at : next_device . created_at ,
119+ } ,
120+ ] )
121+ next_page_cursor = Buffer . from (
122+ JSON . stringify ( next_page_cursor_data ) ,
123+ "utf8" ,
124+ ) . toString ( "base64" )
108125 }
109126
110- const next_page_cursor = has_next_page
111- ? Buffer . from (
112- JSON . stringify ( {
113- device_id : next_device . device_id ,
114- created_at : next_device . created_at ,
115- query_hash,
116- } ) ,
117- "utf8" ,
118- ) . toString ( "base64" )
119- : null
120-
121127 const next_page_url = getNextPageUrl ( next_page_cursor , { req } )
122128
123129 res . status ( 200 ) . json ( {
0 commit comments