Suggestions: add offset based pagination

This commit is contained in:
Alex Gleason
2024-06-02 22:46:07 -05:00
parent 3874a7fe8d
commit da0139ff4e
2 changed files with 60 additions and 30 deletions

View File

@@ -202,11 +202,16 @@ function buildListLinkHeader(url: string, params: { offset: number; limit: numbe
return `<${next}>; rel="next", <${prev}>; rel="prev"`;
}
interface PaginatedListParams {
offset: number;
limit: number;
}
/** paginate a list of tags. */
function paginatedList(
c: AppContext,
params: { offset: number; limit: number },
entities: (Entity | undefined)[],
params: PaginatedListParams,
entities: unknown[],
headers: HeaderRecord = {},
) {
const link = buildListLinkHeader(c.req.url, params);
@@ -217,7 +222,7 @@ function paginatedList(
}
// Filter out undefined entities.
const results = entities.filter((entity): entity is Entity => Boolean(entity));
const results = entities.filter(Boolean);
return c.json(results, 200, headers);
}
@@ -255,6 +260,7 @@ export {
localRequest,
paginated,
paginatedList,
type PaginatedListParams,
type PaginationParams,
paginationSchema,
parseBody,