koa-better-body
Version:
Full-featured [koa][] body parser! Support parsing text, buffer, json, json patch, json api, csp-report, multipart, form and urlencoded bodies. Works for koa@1, koa@2 and will work for koa@3.
19 lines (17 loc) • 477 B
JavaScript
import { defaultOptions, setParsers, isValid, parseBody } from './utils';
export default function koaBetterBody(options) {
const opts = defaultOptions(options);
return function* plugin(next) {
if (opts.strict && !isValid(this.method)) {
return yield* next;
}
try {
setParsers(this, opts);
yield* parseBody(this, opts, next);
} catch (err) {
if (!opts.onError) throw err;
opts.onError(err, this);
}
yield* next;
};
}