Skip to content

Commit

Permalink
fix(core-api): add missing offset handling to /api/peers (#3075)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Oct 17, 2019
1 parent 98c88c4 commit 3bcc8fa
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/core-api/src/handlers/peers/controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { P2P } from "@arkecosystem/core-interfaces";
import Boom from "@hapi/boom";
import Hapi from "@hapi/hapi";
import { get } from "dottie";
import semver from "semver";
import { Controller } from "../shared/controller";

Expand All @@ -15,7 +16,17 @@ export class PeersController extends Controller {

const count: number = result.length;

result = result.slice(0, +request.query.limit || 100);
const limit: number = +request.query.limit || 100;

let offset: number = +get(request.query, "offset", 0);

if (offset <= 0 && +request.query.page > 1) {
offset = (+request.query.page - 1) * limit;
}

if (Number.isNaN(offset)) {
offset = 0;
}

const orderBy: string = request.query.orderBy as string;
if (orderBy) {
Expand All @@ -29,6 +40,8 @@ export class PeersController extends Controller {
}
}

result = result.slice(offset, offset + limit);

return super.toPagination({ rows: result, count }, "peer");
}

Expand Down

0 comments on commit 3bcc8fa

Please sign in to comment.