Lint rules and flow annotations for rest of the files
This commit is contained in:
@ -1,8 +1,13 @@
|
||||
// @flow
|
||||
import httpErrors from 'http-errors';
|
||||
import querystring from 'querystring';
|
||||
import { type Context } from 'koa';
|
||||
|
||||
export default function pagination(options) {
|
||||
return async function paginationMiddleware(ctx, next) {
|
||||
export default function pagination(options?: Object) {
|
||||
return async function paginationMiddleware(
|
||||
ctx: Context,
|
||||
next: () => Promise<void>
|
||||
) {
|
||||
const opts = {
|
||||
defaultLimit: 15,
|
||||
maxLimit: 100,
|
||||
@ -11,7 +16,9 @@ export default function pagination(options) {
|
||||
|
||||
let query = ctx.request.query;
|
||||
let body = ctx.request.body;
|
||||
// $FlowFixMe
|
||||
let limit = parseInt(query.limit || body.limit, 10);
|
||||
// $FlowFixMe
|
||||
let offset = parseInt(query.offset || body.offset, 10);
|
||||
limit = isNaN(limit) ? opts.defaultLimit : limit;
|
||||
offset = isNaN(offset) ? 0 : offset;
|
||||
@ -27,9 +34,13 @@ export default function pagination(options) {
|
||||
offset: offset,
|
||||
};
|
||||
|
||||
// $FlowFixMe
|
||||
query.limit = ctx.state.pagination.limit;
|
||||
// $FlowFixMe
|
||||
query.offset = ctx.state.pagination.offset + query.limit;
|
||||
ctx.state.pagination.nextPath = `/api${ctx.request.path}?${querystring.stringify(query)}`;
|
||||
ctx.state.pagination.nextPath = `/api${
|
||||
ctx.request.path
|
||||
}?${querystring.stringify(query)}`;
|
||||
|
||||
return next();
|
||||
};
|
||||
|
Reference in New Issue
Block a user