diff --git a/__tests__/unit/core-api/plugins/pagination.test.ts b/__tests__/unit/core-api/plugins/pagination.test.ts index e897aae207..fc1e956e04 100644 --- a/__tests__/unit/core-api/plugins/pagination.test.ts +++ b/__tests__/unit/core-api/plugins/pagination.test.ts @@ -35,6 +35,13 @@ describe("Pagination", () => { handler: () => { return customResponse; }, + options: { + plugins: { + pagination: { + enabled: true, + }, + }, + }, }; injectOptions = { @@ -96,11 +103,35 @@ describe("Pagination", () => { ); }); + it("should return paginated payload with data in results if pagination have not enabled property", async () => { + customResponse = { + results: customResponse, + }; + + customRoute.options = { + plugins: { + pagination: {}, + }, + }; + + const server = await initServer(app, defaults, customRoute); + + const response = await server.inject(injectOptions); + const payload = JSON.parse(response.payload || {}); + expect(payload.data).toEqual(customResponse.results); + expect(payload.meta).toEqual( + expect.objectContaining({ + count: 3, + pageCount: 1, + }), + ); + }); + it("should not return paginated payload if disabled on route", async () => { customRoute.options = { plugins: { pagination: { - pagination: false, + enabled: false, }, }, }; diff --git a/packages/core-api/src/plugins/pagination/ext.ts b/packages/core-api/src/plugins/pagination/ext.ts index 691ec5a7f7..7c87c4459d 100644 --- a/packages/core-api/src/plugins/pagination/ext.ts +++ b/packages/core-api/src/plugins/pagination/ext.ts @@ -4,57 +4,12 @@ import { Utils } from "@arkecosystem/core-kernel"; import Hoek from "@hapi/hoek"; import Qs from "querystring"; -interface IRoute { - method: string; - path: string; -} - export class Ext { private readonly routePathPrefix = "/api"; - private readonly routes: IRoute[] = [ - { method: "get", path: "/blocks" }, - { method: "get", path: "/blocks/{id}/transactions" }, - { method: "post", path: "/blocks/search" }, - { method: "get", path: "/bridgechains" }, - { method: "post", path: "/bridgechains/search" }, - { method: "get", path: "/businesses" }, - { method: "get", path: "/businesses/{id}/bridgechains" }, - { method: "post", path: "/businesses/search" }, - { method: "get", path: "/delegates" }, - { method: "get", path: "/delegates/{id}/blocks" }, - { method: "get", path: "/delegates/{id}/voters" }, - { method: "post", path: "/delegates/search" }, - { method: "get", path: "/locks" }, - { method: "post", path: "/locks/search" }, - { method: "post", path: "/locks/unlocked" }, - { method: "get", path: "/peers" }, - { method: "get", path: "/transactions" }, - { method: "post", path: "/transactions/search" }, - { method: "get", path: "/transactions/unconfirmed" }, - { method: "get", path: "/votes" }, - { method: "get", path: "/wallets" }, - { method: "get", path: "/wallets/top" }, - { method: "get", path: "/wallets/{id}/locks" }, - { method: "get", path: "/wallets/{id}/transactions" }, - { method: "get", path: "/wallets/{id}/transactions/received" }, - { method: "get", path: "/wallets/{id}/transactions/sent" }, - { method: "get", path: "/wallets/{id}/votes" }, - { method: "post", path: "/wallets/search" }, - ]; - public constructor(private readonly config) {} public isValidRoute(request) { - if (!this.hasPagination(request)) { - return false; - } - - const { method, path } = request.route; - - return ( - this.routes.find((route) => route.method === method && `${this.routePathPrefix}${route.path}` === path) !== - undefined - ); + return this.hasPagination(request); } public onPreHandler(request, h) { @@ -155,12 +110,16 @@ export class Ext { } public hasPagination(request) { - const routeOptions = this.getRouteOptions(request); + const pagination = this.getRoutePaginationOptions(request); + + if (!pagination) { + return false; + } - return Object.prototype.hasOwnProperty.call(routeOptions, "pagination") ? routeOptions.pagination : true; + return pagination.enabled !== undefined ? pagination.enabled : true; } - private getRouteOptions(request) { - return request.route.settings.plugins.pagination || {}; + private getRoutePaginationOptions(request) { + return request.route.settings.plugins.pagination; } } diff --git a/packages/core-api/src/routes/blocks.ts b/packages/core-api/src/routes/blocks.ts index 9b96a40b33..7ce7cde870 100644 --- a/packages/core-api/src/routes/blocks.ts +++ b/packages/core-api/src/routes/blocks.ts @@ -20,6 +20,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -81,6 +86,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -99,6 +109,11 @@ export const register = (server: Hapi.Server): void => { ...server.app.schemas.blockCriteriaSchemas, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); }; diff --git a/packages/core-api/src/routes/delegates.ts b/packages/core-api/src/routes/delegates.ts index 5a73b7c30b..b3d9ad605a 100644 --- a/packages/core-api/src/routes/delegates.ts +++ b/packages/core-api/src/routes/delegates.ts @@ -30,6 +30,11 @@ export const register = (server: Hapi.Server): void => { }, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -62,6 +67,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -95,6 +105,11 @@ export const register = (server: Hapi.Server): void => { }, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -127,6 +142,11 @@ export const register = (server: Hapi.Server): void => { voteBalance: server.app.schemas.integerBetween, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); }; diff --git a/packages/core-api/src/routes/locks.ts b/packages/core-api/src/routes/locks.ts index 578e800d39..ff5f8b983c 100644 --- a/packages/core-api/src/routes/locks.ts +++ b/packages/core-api/src/routes/locks.ts @@ -29,6 +29,11 @@ export const register = (server: Hapi.Server): void => { }, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -79,6 +84,11 @@ export const register = (server: Hapi.Server): void => { isExpired: Joi.bool(), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -96,6 +106,11 @@ export const register = (server: Hapi.Server): void => { ids: Joi.array().unique().min(1).max(25).items(Joi.string().hex().length(64)), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); }; diff --git a/packages/core-api/src/routes/peers.ts b/packages/core-api/src/routes/peers.ts index aa941e3097..51a85ab14e 100644 --- a/packages/core-api/src/routes/peers.ts +++ b/packages/core-api/src/routes/peers.ts @@ -22,6 +22,11 @@ export const register = (server: Hapi.Server): void => { }, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); diff --git a/packages/core-api/src/routes/transactions.ts b/packages/core-api/src/routes/transactions.ts index de02f0c4bc..c0ec0f65f6 100644 --- a/packages/core-api/src/routes/transactions.ts +++ b/packages/core-api/src/routes/transactions.ts @@ -21,6 +21,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -83,6 +88,11 @@ export const register = (server: Hapi.Server): void => { }, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -114,6 +124,11 @@ export const register = (server: Hapi.Server): void => { ...server.app.schemas.transactionCriteriaSchemas, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); diff --git a/packages/core-api/src/routes/votes.ts b/packages/core-api/src/routes/votes.ts index ce5568355e..7c2f66edc4 100644 --- a/packages/core-api/src/routes/votes.ts +++ b/packages/core-api/src/routes/votes.ts @@ -20,6 +20,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); diff --git a/packages/core-api/src/routes/wallets.ts b/packages/core-api/src/routes/wallets.ts index 76c74901d5..200e90d611 100644 --- a/packages/core-api/src/routes/wallets.ts +++ b/packages/core-api/src/routes/wallets.ts @@ -28,6 +28,11 @@ export const register = (server: Hapi.Server): void => { }, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -52,6 +57,11 @@ export const register = (server: Hapi.Server): void => { }, }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -84,6 +94,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -103,6 +118,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -122,6 +142,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -141,6 +166,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -160,6 +190,11 @@ export const register = (server: Hapi.Server): void => { transform: Joi.bool().default(true), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); @@ -197,6 +232,11 @@ export const register = (server: Hapi.Server): void => { }), }), }, + plugins: { + pagination: { + enabled: true, + }, + }, }, }); };