index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*!
  2. * koa-better-body <https://github.com/tunnckoCore/koa-better-body>
  3. *
  4. * Copyright (c) 2014 Charlike Mike Reagent, Daryl Lau, contributors.
  5. * Released under the MIT license.
  6. */
  7. 'use strict';
  8. /*!
  9. * Module dependencies.
  10. */
  11. var buddy = require('co-body');
  12. var forms = require('formidable');
  13. var xtend = require('extend');
  14. var defaultOptions = {
  15. patchNode: false,
  16. patchKoa: true,
  17. multipart: false,
  18. encoding: 'utf-8',
  19. jsonLimit: '1mb',
  20. formLimit: '56kb',
  21. formidable: {
  22. multiples: true,
  23. keepExtensions: true,
  24. maxFields: 10,
  25. }
  26. };
  27. /**
  28. * Doneable formidable
  29. *
  30. * @param {Stream} ctx
  31. * @param {Object} opts
  32. * @return {Function} Node-style callback, ready for yielding
  33. * @api private
  34. */
  35. function formy(ctx, opts) {
  36. return function(done) {
  37. var form = new forms.IncomingForm(opts);
  38. form.parse(ctx.req, function(err, fields, files) {
  39. if (err) {return done(err);}
  40. done(null, {fields: fields, files: files});
  41. });
  42. };
  43. }
  44. /**
  45. * ## Examples
  46. * > For a more comprehensive examples, see [examples](./examples) folder.
  47. *
  48. * - [`examples/multer`](./examples/multer.js) - usage like Express's bodyParser - [multer][multer-url] `npm run examples-multer`
  49. * - [`examples/koa-router`](./examples/koa-router.js) - usage with Alex's [koa-router][koa-router-url] `npm run examples-koa-router`
  50. *
  51. *
  52. * ## Options
  53. * > However, `koa-better-body` have few custom options, see also [co-body][cobody-url], [raw-body][rawbody-url], [formidable][formidable-url]
  54. *
  55. * @param {Boolean} `patchNode` Patch request body to Node's `ctx.req` object, default `false`
  56. * @param {Boolean} `patchKoa` Patch request body to Koa's `ctx.request` object, default `true`
  57. * @param {String|Number} `jsonLimit` The byte limit of the JSON body, default `1mb`
  58. * @param {String|Number} `formLimit` The byte limit of the form body, default `56kb`
  59. * @param {String} `encoding` Sets encoding for incoming form fields, default `utf-8`
  60. * @param {Boolean} `multipart` Support `multipart/form-data` request bodies, default `false`
  61. * @param {Object} `formidable` Options that are passing to `formidable`
  62. * @param {Number} `formidable.maxFields` See [formidable-options](./readme.md#formidable-options). our default `10`
  63. * @param {Boolean} `formidable.multiples` See [formidable-options](./readme.md#formidable-options), our default `true`
  64. * @param {Boolean} `formidable.keepExtensions` See [formidable-options](./readme.md#formidable-options), our default `true`
  65. * @return {GeneratorFunction} That you can use with [koa][koa-url] or [co][co-url]
  66. * @api public
  67. */
  68. module.exports = function koaBody(options) {
  69. var opts = xtend(true, defaultOptions, options || {});
  70. return function* koaBody(next){
  71. var body = {}, json, form;
  72. if (this.request.is('json')) {
  73. json = yield buddy.json(this, {encoding: opts.encoding, limit: opts.jsonLimit});
  74. body.fields = json;
  75. }
  76. else if (this.request.is('urlencoded')) {
  77. form = yield buddy.form(this, {encoding: opts.encoding, limit: opts.formLimit});
  78. body.fields = form;
  79. }
  80. else if (this.request.is('multipart') && opts.multipart) {
  81. body = yield formy(this, opts.formidable);
  82. }
  83. if (opts.patchNode) {
  84. this.req.body = body;
  85. }
  86. if (opts.patchKoa) {
  87. this.request.body = body;
  88. }
  89. yield next;
  90. };
  91. };