Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

koa-short-body

Ragg-30MIT1.1.0

More short API for koa-better-body.

api, body, bodyparser, csp, csp-report, data, form, formdata, formidable, json, koa, middleware, multipart, multiple, parse, parser, report, request, upload, urlencoded

readme

npm mit license build status coverage status deps status

More short API for koa-better-body.

A koa body parser middleware with support for multipart, json, csp-report and urlencoded request bodies. Via formidable and co-better-body. It's fork of koa-better-body

Install

npm i --save koa-short-body
npm test

Usage

For more use-cases see the tests or examples folder.

Extending default types

Every new defined type, will be appended to defaults, see defaults

app.use(koaBody({
  extendTypes: {
    // will parse application/x-javascript type body as a JSON string
    json: ['application/x-javascript'],
    multipart: ['multipart/mixed']
  }
}));

will expects request bodies

application/json
application/json-patch+json
application/vnd.api+json
application/csp-report
application/x-javascript

application/x-www-form-urlencoded

multipart/form-data
multipart/mixed

get field or file via this

app.use(function * (next) {
    // Get specify fields
    console.log(this.field("field_name"));

    // Get received all fields.
    console.log(this.field());

    // Get specify fields file (it's must be returns Array<File>)
    console.log(this.file("file"));

    // Get all received files
    console.log(this.file());
})

.koaBetterBody

However, koa-short-body have few custom options, see also co-better-body, raw-body and formidable

  • [options] {Object}
    • jsonLimit {String|Number} The byte limit of the JSON body, default 1mb
    • formLimit {String|Number} The byte limit of the form body, default 56kb
    • encoding {String} Sets encoding for incoming form fields, default utf-8
    • encode {String} alias of opts.encoding
    • multipart {Boolean} Support multipart/form-data request bodies, default false
    • extendTypes {Object} extending request types, see defaults
      • multipart {Array} array with multipart types, default ['multipart/form-data']
      • json {Array} array with json types, default ['application/x-www-form-urlencoded']
      • form {Array} array with form types
    • qs {Object} Options that are passing to qs
    • formidable {Object} Options that are passing to formidable
  • return {GeneratorFunction} That you can use with koa or co

formidable options

See node-formidable for a full list of options

  • bytesExpected {Integer} The expected number of bytes in this form, default null
  • maxFields {Integer} Limits the number of fields that the querystring parser will decode, default 1000
  • maxFieldsSize {Integer} Limits the amount of memory a field can allocate in bytes, default 2mb
  • uploadDir {String} Sets the directory for placing file uploads in, default os.tmpDir()
  • hash {String} If you want checksums calculated for incoming files - 'sha1' or 'md5', default false
  • multiples {Boolean} Multiple file uploads or no, default false

qs options

See qs for a full list of options

  • depth {Integer} The parsed object maximum hierarchy depth, default 5
  • delimiter {String|Regex} The query string delimeter, default &
  • arrayLimit {Integer} Maximum array size or -1 to disable arrays, default 20

Author

Charlike Mike Reagent

License MIT license

Copyright (c) 2014-2015 Charlike Mike Reagent, contributors.
Released under the MIT license.


Powered and automated by kdf, February 6, 2015

changelog

v1.0.17 / February 6, 2015

  • Release v1.0.17 / npm@v1.0.17
  • fix license range
  • run update readme
  • update keywords
  • bump deps, actually ranges to ~ only which means only latest patch version

v1.0.16 / January 19, 2015

v1.0.15 / January 19, 2015

  • Release v1.0.15 / npm@v1.0.15
  • add encode alias for encoding option

v1.0.14 / January 18, 2015

  • Release v1.0.14 / npm@v1.0.14
  • istanbul ignore
  • coverage tweaks
  • increase max statements to 20
  • closes #10, update/add tests

v1.0.13 / January 17, 2015

  • Release v1.0.13 / npm@v1.0.13
  • update dotfiles and jscs rules
  • revert back filesKey option

v1.0.12 / November 27, 2014

  • Release v1.0.12 / npm@v1.0.12
  • bump to jscs >= 1.8.0 and jscs-doc >= 0.2.0
  • update semver range

v1.0.11 / November 27, 2014

  • Release v1.0.11 / npm@v1.0.11
  • fix code style collisions

v1.0.10 / November 27, 2014

  • Release v1.0.10 / npm@v1.0.10
  • docs, readme, coveralls
  • edit safeContext in .jscsrc

v1.0.9 / November 27, 2014

  • Release v1.0.9 / npm@v1.0.9
  • bump jscs-jsdoc to v0.1.0
  • update jscs config .jscsrc

v1.0.8 / November 26, 2014

  • Release v1.0.8 / npm@v1.0.8
  • normalize (dot)files
  • update all to apply jshint/jscs code style
    • add .jscsrc and .jshintignore
  • update makefile and scripts in package.json

v1.0.7 / October 26, 2014

  • Release v1.0.7 / npm@v1.0.7
  • update names of some tests (rfc7231) "Request Entity Too Large" -> "Payload Too Large"
  • add doc blocks

v1.0.6 / October 25, 2014

  • Release v1.0.6 / npm@v1.0.6
  • update automation
  • improve code coverage
  • add Makefile
  • add npm run scripts

v1.0.5 / October 25, 2014

v1.0.4 / October 21, 2014

v1.0.3 / July 03, 2014

  • Release v1.0.3 / npm@v1.0.3
  • Pretty styling
  • auto badges
  • add history
  • add extend, because of options merging bug.
  • add better tests - only 7, for all use cases.
  • need suggestions for error 413 handling, maybe raw-body problem/logic?
  • when upload, always returned type is application/octet-stream, not image/png, image/gif, etc - maybe formidable problem/logic?
  • deprecation message also comes from formidable
  • always json and urlencoded bodies will be pushed to request .body.fields object. (fixed in v1.0.4)

v1.0.0 / June 08, 2014