Skip to content

Commit

Permalink
add links to current, previous & next page in PageResponse (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
zefir-git authored Jul 10, 2024
2 parents a5fb88c + 44bb8ab commit efc8e08
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/response/PageResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ import JsonResponse from "./JsonResponse.js";
import Api from "../api/Api.js";

export default class PageResponse extends JsonResponse {
public constructor(resources: JsonResponse.Object[], page: number, limit: number, total: number = resources.length) {
super({resources, page, limit, total}, 200);
public constructor(req: Api.Request, resources: JsonResponse.Object[], page: number, limit: number, total: number = resources.length) {
const href: URL = new URL(req.url);
const previous: URL | null = page > 1 ? new URL(req.url) : null;
if (previous !== null) previous.searchParams.set("page", String(page - 1));
const next: URL | null = page < Math.ceil(total / limit) ? new URL(req.url) : null;
if (next !== null) next.searchParams.set("page", String(page + 1));
super({
page,
limit,
total,
href: href.pathname + href.search,
previous: previous === null ? null : previous.pathname + previous.search,
next: next === null ? null : next.pathname + next.search,
resources,
}, 200);
}

public static array<T = JsonResponse.Object>(limits: { page: number, limit: number }, array: T[]): {
Expand Down Expand Up @@ -31,6 +44,6 @@ export default class PageResponse extends JsonResponse {

const data = this.array<T>(req.limit(), resources);
const res: JsonResponse.Object[] = (mapFn ? data.resources.map(mapFn) : data.resources) as JsonResponse.Object[];
return new PageResponse(res, data.page, data.limit, data.total);
return new PageResponse(req, res, data.page, data.limit, data.total);
}
}

0 comments on commit efc8e08

Please sign in to comment.