nfp_sites/api/parser/middleware.mjs

36 lines
844 B
JavaScript
Raw Normal View History

2019-02-19 11:34:52 +00:00
import format from 'format-link-header'
2019-09-14 19:03:38 +00:00
import * as pagination from './pagination.mjs'
2019-02-19 11:34:52 +00:00
export default class ParserMiddleware {
constructor(opts = {}) {
Object.assign(this, {
pagination: opts.pagination || pagination,
format: opts.format || format,
})
}
contextParser() {
return (ctx, next) => {
ctx.state.pagination = this.pagination.parsePagination(ctx)
ctx.state.filter = this.pagination.parseFilter(ctx)
return next()
}
}
generateLinks() {
return async (ctx, next) => {
await next()
if (ctx.state.pagination.total > 0) {
ctx.set('Link', this.format(this.pagination.generateLinks(ctx, ctx.state.pagination.total)))
}
if (ctx.state.pagination.total != null) {
ctx.set('pagination_total', ctx.state.pagination.total)
}
}
}
}