Create koa-router like handler for next.js.
$ npm install next-compose-router
Sometimes not all api need some validation operates, and as my Koa develop experiences, I prefer defining some apis' preprocessing separately and flexibly. So I write this toolkit to handle this
Create a next handler composed with multi middlewares
import {router} from "next-compose-router"
// usage
export default router(checkAuth, checkParams, (req, res) => {
// write handler here
res.send("success")
})
// others
export const FORBIDDEN = {message: "forbidden", status: 403}
export const BAD_REQUEST: IError = {message: "bad request", status: 400}
async function checkAuth(req, res, next) {
const token = req.headers.authorization
if (valid(token)) { // valid token here
await next()
} else {
throw FORBBIDEN
}
}
async function checkParams(req, res, next) {
const params = req.query
if (valid(params)) { // valid params here
await next()
} else {
throw BAD_REQUEST
}
}
Used to define self error handler
import {nextCompose} from "next-compose-router"
// default
const router = nextCompose((req, res, err) => {
throw err
})
const myRouter = nextCompose((req, res, {status, message}) => {
res.statusCode = status || 500
res.send({
error: {
status: status || 500,
message: status ? message : "internal error"
}
})
})
MIT