{"name":"koa-body","version":"8.0.0","keywords":["koa","urlencoded","multipart","json","body","parser","form"],"license":"MIT","_id":"koa-body@8.0.0","maintainers":[{"name":"dlau","email":"dlau00@gmail.com"},{"name":"markherhold","email":"mark.herhold@gmail.com"},{"name":"thedadi","email":"difada1@hotmail.com"}],"dist":{"shasum":"e7248cd4ed9d37fb75bea442df5cc7858146b349","tarball":"http://registry-npm.ywwl.com/koa-body/download/koa-body-8.0.0.tgz","fileCount":48,"integrity":"sha512-gJNvXtezVmS/oAX8dJt5yG+DpnKP9dHsY0RXmynO6iQfY1yPDLMddpkspiBeU2uYExJHqcaHY9Ap2gTi+b7vbQ==","signatures":[{"sig":"MEYCIQChEOdIjsOt5/jIt/geVROMAt1qT+RPzYKAsdcClGTA5wIhALfbY2qaiHV8QwLwfl+LI2N8FnA4TBev4BrNBksnku+F","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/koa-body@8.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":63765,"size":12998,"noattachment":false,"key":"/koa-body/-/koa-body-8.0.0.tgz"},"main":"./dist/commonjs/index.js","tshy":{"exclude":["src/**/*.test.ts"],"exports":{".":"./src/index.ts","./package.json":"./package.json"}},"type":"module","types":"./dist/commonjs/index.d.ts","module":"./dist/esm/index.js","exports":{".":{"import":{"types":"./dist/esm/index.d.ts","default":"./dist/esm/index.js"},"require":{"types":"./dist/commonjs/index.d.ts","default":"./dist/commonjs/index.js"}},"./package.json":"./package.json"},"scripts":{"fix":"biome check --fix","lint":"tsc --noEmit && biome check","test":"node --import tsx --test **/*.test.ts","build":"pnpm clean && pnpm lint && pnpm test && tshy && tshy-after && attw --pack","clean":"rm -rf dist","release":"pnpm build && pnpm changeset publish","examples-multer":"tsx examples/multer.ts","examples-multipart":"tsx examples/multipart.ts","examples-koa-router":"tsx examples/koa-router.ts"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:161163e9-6151-4e61-802e-3d7aa66dd79d"}},"repository":{"url":"git://github.com/koajs/koa-body.git","type":"git"},"description":"A Koa body parser middleware. Supports multipart, urlencoded and JSON request bodies.","directories":{},"_nodeVersion":"26.0.0","dependencies":{"zod":"^4.4.3","co-body":"^6.2.0","type-fest":"^5.6.0","@types/koa":"^3.0.2","formidable":"^3.5.4","@types/co-body":"^6.1.3","@types/formidable":"^3.5.1"},"_hasShrinkwrap":false,"devDependencies":{"koa":"^3.2.0","tsx":"^4.21.0","tshy":"^4.1.2","supertest":"7.2.2","tshy-after":"^1.4.1","typescript":"^6.0.3","@koa/router":"^15.5.0","@types/node":"25.7.0","@biomejs/biome":"^2.4.15","@changesets/cli":"^2.31.0","@tsconfig/node22":"^22.0.5","@types/supertest":"^7.2.0","@types/koa__router":"^12.0.5","@tsconfig/strictest":"^2.0.8","@arethetypeswrong/cli":"^0.18.2","@changesets/changelog-github":"^0.7.0"},"_npmOperationalInternal":{"tmp":"tmp/koa-body_8.0.0_1778638053257_0.9688040385954613","host":"s3://npm-registry-packages-npm-production"},"readme":"# koa-body\n\n[![CI](https://github.com/koajs/koa-body/actions/workflows/ci.yaml/badge.svg)](https://github.com/koajs/koa-body/actions/workflows/ci.yaml)\n[![KoaJs Slack](https://img.shields.io/badge/Koa.Js-Slack%20Channel-Slack.svg?longCache=true)](https://communityinviter.com/apps/koa-js/koajs)\n\n---\n\n> A full-featured [`koa`](https://github.com/koajs/koa) body parser middleware. Supports `multipart`, `urlencoded`, and `json` request bodies. Provides the same functionality as Express's bodyParser - [`multer`](https://github.com/expressjs/multer).\n\n## Install\n\n> Install with [npm](https://github.com/npm/npm)\n\n```\nnpm install koa-body\n```\n\n## Features\n\n- can handle requests such as:\n  - **multipart/form-data**\n  - **application/x-www-form-urlencoded**\n  - **application/json**\n  - **application/json-patch+json**\n  - **application/vnd.api+json**\n  - **application/csp-report**\n  - **application/reports+json**\n  - **text/xml**\n- configurable content-type matchers per body kind (`jsonTypes`, `urlencodedTypes`, `textTypes`, `multipartTypes`) for vendor or custom mime types\n- option for patch to Koa or Node, or either\n- file uploads\n- body, fields and files size limiting\n\n## Hello World - Quickstart\n\n```sh\nnpm install koa koa-body # Requires Node.js 22+\n```\n\nindex.js:\n\n```js\nconst Koa = require('koa');\nconst { koaBody } = require('koa-body');\n\nconst app = new Koa();\n\napp.use(koaBody());\napp.use((ctx) => {\n  ctx.body = `Request Body: ${JSON.stringify(ctx.request.body)}`;\n});\n\napp.listen(3000);\n```\n\n```sh\nnode index.js\ncurl -i http://localhost:3000/users -d \"name=test\"\n```\n\nOutput:\n\n```text\nHTTP/1.1 200 OK\nContent-Type: text/plain; charset=utf-8\nContent-Length: 29\nDate: Wed, 03 May 2017 02:09:44 GMT\nConnection: keep-alive\n\nRequest Body: {\"name\":\"test\"}%\n```\n\n**For a more comprehensive example, see** `examples/multipart.js`\n\n## Usage with [@koa/router](https://github.com/koajs/router)\n\nIt's generally better to only parse the body as needed, if using a router that supports middleware composition, we can inject it only for certain routes.\n\n```js\nconst Koa = require('koa');\nconst app = new Koa();\nconst router = require('@koa/router')();\nconst { koaBody } = require('koa-body');\n\nrouter.post('/users', koaBody(), (ctx) => {\n  console.log(ctx.request.body);\n  // => POST body\n  ctx.body = JSON.stringify(ctx.request.body);\n});\n\napp.use(router.routes());\n\napp.listen(3000);\nconsole.log('curl -i http://localhost:3000/users -d \"name=test\"');\n```\n\n## Usage with unsupported text body type\n\nFor unsupported text body type, for example, `text/xml`, you can use the unparsed request body at `ctx.request.body`. For the text content type, the `includeUnparsed` setting is not required.\n\n```js\n// xml-parse.js:\nconst Koa = require('koa');\nconst { koaBody } = require('koa-body');\nconst convert = require('xml-js');\n\nconst app = new Koa();\n\napp.use(koaBody());\napp.use((ctx) => {\n  const obj = convert.xml2js(ctx.request.body);\n  ctx.body = `Request Body: ${JSON.stringify(obj)}`;\n});\n\napp.listen(3000);\n```\n\n```sh\nnode xml-parse.js\ncurl -i http://localhost:3000/users -H \"Content-Type: text/xml\" -d '<?xml version=\"1.0\"?><catalog id=\"1\"></catalog>'\n```\n\nOutput:\n\n```text\nHTTP/1.1 200 OK\nContent-Type: text/plain; charset=utf-8\nContent-Length: 135\nDate: Tue, 09 Jun 2020 11:17:38 GMT\nConnection: keep-alive\n\nRequest Body: {\"declaration\":{\"attributes\":{\"version\":\"1.0\"}},\"elements\":[{\"type\":\"element\",\"name\":\"catalog\",\"attributes\":{\"id\":\"1\"}}]}%\n```\n\n## Options\n\n> Options available for `koa-body`. Four custom options, and others are from `co-body` and `formidable`.\n\n- `patchNode` **{Boolean}** Patch request body to Node's `ctx.req`, default `false`\n- `patchKoa` **{Boolean}** Patch request body to Koa's `ctx.request`, default `true`\n- `jsonLimit` **{String|Integer}** The byte (if integer) limit of the JSON body, default `1mb`\n- `formLimit` **{String|Integer}** The byte (if integer) limit of the form body, default `56kb`\n- `textLimit` **{String|Integer}** The byte (if integer) limit of the text body, default `56kb`\n- `encoding` **{String}** Sets encoding for incoming form fields, default `utf-8`\n- `multipart` **{Boolean}** Parse multipart bodies, default `false`\n- `urlencoded` **{Boolean}** Parse urlencoded bodies, default `true`\n- `text` **{Boolean}** Parse text bodies, such as XML, default `true`\n- `json` **{Boolean}** Parse JSON bodies, default `true`\n- `jsonStrict` **{Boolean}** Toggles co-body strict mode; if set to true - only parses arrays or objects, default `true`\n- `jsonTypes` **{String[]}** Content-types matched as JSON bodies (passed to `ctx.is(...)`), default `['application/json', 'application/json-patch+json', 'application/vnd.api+json', 'application/csp-report', 'application/reports+json']`\n- `urlencodedTypes` **{String[]}** Content-types matched as urlencoded bodies (passed to `ctx.is(...)`), default `['urlencoded']`\n- `textTypes` **{String[]}** Content-types matched as text bodies (passed to `ctx.is(...)`), default `['text/*']`\n- `multipartTypes` **{String[]}** Content-types matched as multipart bodies (passed to `ctx.is(...)`), default `['multipart']`\n- `includeUnparsed` **{Boolean}** Toggles co-body returnRawBody option; if set to true, for form encoded and JSON requests the raw, unparsed request body will be attached to `ctx.request.rawBody`, default `false`\n- `formidable` **{Object}** Options to pass to the formidable multipart parser\n- `onError` **{Function}** Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throw\n- `parsedMethods` **{String[]}** Declares the HTTP methods where bodies will be parsed, default `['POST', 'PUT', 'PATCH']`. Replaces `strict` option.\n\n## A note about `parsedMethods`\n\n> see [http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#section-6.3](http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#section-6.3)\n\n- `GET`, `HEAD`, and `DELETE` requests have no defined semantics for the request body, but this doesn't mean they may not be valid in certain use cases.\n- koa-body is strict by default, parsing only `POST`, `PUT`, and `PATCH` requests\n- you may use either the enumeration or strings to chose which methods to parse: For example, `HttpMethodEnum.PATCH`\n\n## File Support\n\nUploaded files are accessible via `ctx.request.files`.\n\n## A note about unparsed request bodies\n\nSome applications require cryptographic verification of request bodies, for example webhooks from slack or stripe. The unparsed body can be accessed if `includeUnparsed` is `true` in koa-body's options. Then the unparsed body is available at `ctx.request.rawBody`. This only works for non `multipart` bodies. \n\n## Some options for formidable\n\n> See [node-formidable](https://github.com/node-formidable/formidable) for a full list of options\n\n- `maxFields` **{Integer}** Limits the number of fields that the querystring parser will decode, default `1000`\n- `maxFieldsSize` **{Integer}** Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default `2mb (2 * 1024 * 1024)`\n- `uploadDir` **{String}** Sets the directory for placing file uploads in, default `os.tmpDir()`\n- `keepExtensions` **{Boolean}** Files written to `uploadDir` will include the extensions of the original files, default `false`\n- `hashAlgorithm` **{String}** If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`, default `false`\n- `multiples` **{Boolean}** Multiple file uploads or no, default `true`\n- `onFileBegin` **{Function}** Special callback on file begin. The function is executed directly by formidable. It can be used to rename files before saving them to disk. [See the docs](https://github.com/node-formidable/formidable#filebegin)\n- `onPart` **{Function}** Overrides the default onPart of formidable. The function can be used to filter out parts based on their mimetype. For more use cases. [See the docs](https://github.com/node-formidable/formidable#formonpart)\n\n## Changelog\n\nPlease see the [Changelog](./CHANGELOG.md) for a summary of changes.\n\n## Tests\n\n```\n$ npm test\n```\n\n## License\n\nThe MIT License, 2014 [Charlike Mike Reagent](https://github.com/tunnckoCore) ([@tunnckoCore](https://twitter.com/tunnckoCore)) and [Daryl Lau](https://github.com/dlau) ([@daryllau](https://twitter.com/daryllau))\n","publish_time":1778638053388,"_cnpm_publish_time":1778638053388,"dist-tags":{"latest":"8.0.1"}}