nfp_sites/old/parser/middleware.mjs

34 lines
817 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() {
2022-04-05 16:47:24 +00:00
return (ctx) => {
2019-02-19 11:34:52 +00:00
ctx.state.pagination = this.pagination.parsePagination(ctx)
ctx.state.filter = this.pagination.parseFilter(ctx)
}
}
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)
}
}
}
}