Skip to content

Commit

Permalink
feat(core-api): enable registering routes with pagination (#3895)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner committed Jul 15, 2020
1 parent b42bfb6 commit 47d3cf2
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 51 deletions.
33 changes: 32 additions & 1 deletion __tests__/unit/core-api/plugins/pagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ describe("Pagination", () => {
handler: () => {
return customResponse;
},
options: {
plugins: {
pagination: {
enabled: true,
},
},
},
};

injectOptions = {
Expand Down Expand Up @@ -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,
},
},
};
Expand Down
59 changes: 9 additions & 50 deletions packages/core-api/src/plugins/pagination/ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
15 changes: 15 additions & 0 deletions packages/core-api/src/routes/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const register = (server: Hapi.Server): void => {
transform: Joi.bool().default(true),
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down Expand Up @@ -81,6 +86,11 @@ export const register = (server: Hapi.Server): void => {
transform: Joi.bool().default(true),
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand All @@ -99,6 +109,11 @@ export const register = (server: Hapi.Server): void => {
...server.app.schemas.blockCriteriaSchemas,
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});
};
20 changes: 20 additions & 0 deletions packages/core-api/src/routes/delegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export const register = (server: Hapi.Server): void => {
},
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down Expand Up @@ -62,6 +67,11 @@ export const register = (server: Hapi.Server): void => {
transform: Joi.bool().default(true),
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down Expand Up @@ -95,6 +105,11 @@ export const register = (server: Hapi.Server): void => {
},
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down Expand Up @@ -127,6 +142,11 @@ export const register = (server: Hapi.Server): void => {
voteBalance: server.app.schemas.integerBetween,
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});
};
15 changes: 15 additions & 0 deletions packages/core-api/src/routes/locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const register = (server: Hapi.Server): void => {
},
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down Expand Up @@ -79,6 +84,11 @@ export const register = (server: Hapi.Server): void => {
isExpired: Joi.bool(),
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand All @@ -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,
},
},
},
});
};
5 changes: 5 additions & 0 deletions packages/core-api/src/routes/peers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export const register = (server: Hapi.Server): void => {
},
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down
15 changes: 15 additions & 0 deletions packages/core-api/src/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export const register = (server: Hapi.Server): void => {
transform: Joi.bool().default(true),
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down Expand Up @@ -83,6 +88,11 @@ export const register = (server: Hapi.Server): void => {
},
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down Expand Up @@ -114,6 +124,11 @@ export const register = (server: Hapi.Server): void => {
...server.app.schemas.transactionCriteriaSchemas,
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down
5 changes: 5 additions & 0 deletions packages/core-api/src/routes/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const register = (server: Hapi.Server): void => {
transform: Joi.bool().default(true),
}),
},
plugins: {
pagination: {
enabled: true,
},
},
},
});

Expand Down
Loading

0 comments on commit 47d3cf2

Please sign in to comment.