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.
30 lines (23 loc) • 619 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = koaBetterBody;
var _utils = require("./utils");
function koaBetterBody(options) {
const opts = (0, _utils.defaultOptions)(options);
return function* plugin(next) {
if (opts.strict && !(0, _utils.isValid)(this.method)) {
return yield* next;
}
try {
(0, _utils.setParsers)(this, opts);
yield* (0, _utils.parseBody)(this, opts, next);
} catch (err) {
if (!opts.onError) throw err;
opts.onError(err, this);
}
yield* next;
};
}
module.exports = exports.default