Add Link header to notifications endpoint

This commit is contained in:
Jesse Wierzbinski 2023-11-23 08:36:44 -10:00
parent 82162fccf4
commit 17bd81cf46
No known key found for this signature in database

View file

@ -49,7 +49,7 @@ export default async (req: Request): Promise<Response> => {
return errorResponse("Can't use both types and exclude_types", 400); return errorResponse("Can't use both types and exclude_types", 400);
} }
const notifications = await client.notification.findMany({ const objects = await client.notification.findMany({
where: { where: {
notifiedId: user.id, notifiedId: user.id,
id: { id: {
@ -77,7 +77,24 @@ export default async (req: Request): Promise<Response> => {
take: limit, 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( return jsonResponse(
await Promise.all(notifications.map(n => notificationToAPI(n))) await Promise.all(objects.map(n => notificationToAPI(n))),
200,
{
Link: linkHeader.join(", "),
}
); );
}; };