Skip to content

Commit e159aa0

Browse files
authored
Update next_page_cursor to be tuple (#237)
* Update next_page_cursor to be tuple * Use parse * Remove PageCursorSchema * Update url-search-params-serializer
1 parent 142bf9b commit e159aa0

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@
7070
"@seamapi/fake-devicedb": "^1.6.0",
7171
"@seamapi/logger": "^1.9.2",
7272
"@seamapi/types": "^1.200.0",
73-
"@seamapi/url-search-params-serializer": "^1.1.0",
73+
"@seamapi/url-search-params-serializer": "^1.2.0",
7474
"@tsconfig/next": "^1.0.5",
75+
"@types/jsonwebtoken": "^9.0.6",
7576
"@types/lodash": "^4.14.202",
7677
"@types/luxon": "^3.4.2",
7778
"@types/node": "^20.8.10",
7879
"@types/react": "^18.0.35",
7980
"@types/react-dom": "^18.0.11",
80-
"@types/jsonwebtoken": "^9.0.6",
8181
"ava": "^5.0.1",
8282
"bs58": "^5.0.0",
8383
"c8": "^8.0.0",

src/pages/api/devices/list.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3942
export 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

Comments
 (0)