From 17bd81cf46d1faa241410667a3ec386d51ce85b0 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Thu, 23 Nov 2023 08:36:44 -1000 Subject: [PATCH] Add Link header to notifications endpoint --- server/api/api/v1/notifications/index.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/server/api/api/v1/notifications/index.ts b/server/api/api/v1/notifications/index.ts index f1480d14..f5c1ffab 100644 --- a/server/api/api/v1/notifications/index.ts +++ b/server/api/api/v1/notifications/index.ts @@ -49,7 +49,7 @@ export default async (req: Request): Promise => { return errorResponse("Can't use both types and exclude_types", 400); } - const notifications = await client.notification.findMany({ + const objects = await client.notification.findMany({ where: { notifiedId: user.id, id: { @@ -77,7 +77,24 @@ export default async (req: Request): Promise => { take: limit, }); + // Constuct HTTP Link header (next and prev) + const linkHeader = []; + if (objects.length > 0) { + const urlWithoutQuery = req.url.split("?")[0]; + linkHeader.push( + `<${urlWithoutQuery}?max_id=${objects[0].id}&limit=${limit}>; rel="next"` + ); + linkHeader.push( + `<${urlWithoutQuery}?since_id=${objects.at(-1) + ?.id}&limit=${limit}>; rel="prev"` + ); + } + return jsonResponse( - await Promise.all(notifications.map(n => notificationToAPI(n))) + await Promise.all(objects.map(n => notificationToAPI(n))), + 200, + { + Link: linkHeader.join(", "), + } ); };