{"id":22030965,"url":"https://github.com/ts-stack/body-parser","last_synced_at":"2026-01-11T01:06:03.515Z","repository":{"id":247976217,"uuid":"827386287","full_name":"ts-stack/body-parser","owner":"ts-stack","description":"Node.js body parser writen in TypeScript, in promise style, in ESM format, without support Node.js version \u003c 20.6.0","archived":false,"fork":false,"pushed_at":"2024-09-25T10:43:17.000Z","size":837,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-31T19:46:08.367Z","etag":null,"topics":["body-parser"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ts-stack.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-11T14:55:21.000Z","updated_at":"2024-09-25T10:43:21.000Z","dependencies_parsed_at":"2024-08-14T13:01:23.083Z","dependency_job_id":"59635a7f-b49e-4267-bb19-3727b2abd563","html_url":"https://github.com/ts-stack/body-parser","commit_stats":null,"previous_names":["ts-stack/body-parser"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fbody-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fbody-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fbody-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fbody-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ts-stack","download_url":"https://codeload.github.com/ts-stack/body-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227299217,"owners_count":17760462,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["body-parser"],"created_at":"2024-11-30T08:12:43.261Z","updated_at":"2026-01-11T01:06:03.445Z","avatar_url":"https://github.com/ts-stack.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @ts-stack/body-parser\n\nNode.js body parser writen in TypeScript, in [promise][2] style, in ESM format, without support Node.js version \u003c 20.6.0. This library is a fork of the well-known [ExpressJS body parser library][0] (from [this commit][1]).\n\n**Note** As request body's shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting. For example, `body.foo.toString()` may fail in multiple ways, for example the `foo` property may not be there or may not be a string, and `toString` may not be a function and instead a string or other user input.\n\n[Learn about the anatomy of an HTTP transaction in Node.js](https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/).\n\n_This does not handle multipart bodies_, due to their complex and typically large nature. For multipart bodies, you may be interested in [@ts-stack/multer][3].\n\nThis module provides the following parsers: JSON, Raw, Text, URL-encoded body parsers.\n\n## Installation\n\n```sh\nnpm install @ts-stack/body-parser\n```\n\nPlease make sure that Node.js (version \u003e= 20.6.0) is installed on your operating system.\n\n## Usage\n\nThe `@ts-stack/body-parser` module exposes various factories to create parsers. All parsers return parsed body in [Promise][2] when the `Content-Type` request header matches the `type` option, or an empty object (`{}`) if there was no body to parse, the `Content-Type` was not matched.\n\nThe various errors returned by this module are described in the\n[errors section](#errors).\n\n```ts\nimport http from 'http';\nimport { getJsonParser } from '@ts-stack/body-parser';\n\nimport { InterfaceOfBody } from './types.js';\n\nconst jsonParser = getJsonParser({ limit: '1kb' });\n\nhttp.createServer(async function (req, res) {\n  try {\n    const body = await jsonParser\u003cInterfaceOfBody\u003e(req, req.headers);\n    res.statusCode = 200;\n    res.setHeader('Content-Type', 'text/plain')\n    res.write('you posted:\\n');\n    res.end(JSON.stringify(body));\n  } catch (err: any) {\n    // handling an error\n  }\n});\n```\n\nAlternatively, you can use the `BodyParserGroup` helper. It is designed for cases when you do not know which parser is needed for a specific route. When creating an instance of the `BodyParserGroup` class, you can pass options for the corresponding parsers, after which you can use the `parse` method as follows:\n\n```ts\nimport { BodyParserGroup } from '@ts-stack/body-parser';\n\nconst bodyParserGroup = new BodyParserGroup({\n  jsonOptions: config.jsonOptions,\n  textOptions: config.textOptions,\n  urlencodedOptions: config.urlencodedOptions,\n  rawOptions: config.rawOptions,\n});\n\nconst body = await bodyParserGroup.parse(req, req.headers, {});\n```\n\n### Change accepted type for parsers\n\nAll the parser factories accept a `type` option which allows you to change the `Content-Type` that the parser will parse.\n\n```ts\nimport { getJsonParser, getRawParser, getTextParser } from '@ts-stack/body-parser';\n\n// parse various different custom JSON types as JSON\nconst jsonParser = getJsonParser({ type: 'application/*+json' });\n\n// parse some custom thing into a Buffer\nconst rawParser = getRawParser({ type: 'application/vnd.custom-type' });\n\n// parse an HTML body into a string\nconst textParser = getTextParser({ type: 'text/html' });\n```\n\n## Errors\n\nThe parsers provided by this module create errors using the\n[`http-errors` module](https://www.npmjs.com/package/http-errors). The errors\nwill typically have a `status`/`statusCode` property that contains the suggested\nHTTP response code, an `expose` property to determine if the `message` property\nshould be displayed to the client, a `type` property to determine the type of\nerror without matching against the `message`, and a `body` property containing\nthe read body, if available.\n\nThe following are the common errors created, though any error can come through\nfor various reasons.\n\n### content encoding unsupported\n\nThis error will occur when the request had a `Content-Encoding` header that\ncontained an encoding but the \"inflation\" option was set to `false`. The\n`status` property is set to `415`, the `type` property is set to\n`'encoding.unsupported'`, and the `charset` property will be set to the\nencoding that is unsupported.\n\n### entity parse failed\n\nThis error will occur when the request contained an entity that could not be\nparsed by the parser. The `status` property is set to `400`, the `type`\nproperty is set to `'entity.parse.failed'`, and the `body` property is set to\nthe entity value that failed parsing.\n\n### entity verify failed\n\nThis error will occur when the request contained an entity that could not be\nfailed verification by the defined `verify` option. The `status` property is\nset to `403`, the `type` property is set to `'entity.verify.failed'`, and the\n`body` property is set to the entity value that failed verification.\n\n### request aborted\n\nThis error will occur when the request is aborted by the client before reading\nthe body has finished. The `received` property will be set to the number of\nbytes received before the request was aborted and the `expected` property is\nset to the number of expected bytes. The `status` property is set to `400`\nand `type` property is set to `'request.aborted'`.\n\n### request entity too large\n\nThis error will occur when the request body's size is larger than the \"limit\"\noption. The `limit` property will be set to the byte limit and the `length`\nproperty will be set to the request body's length. The `status` property is\nset to `413` and the `type` property is set to `'entity.too.large'`.\n\n### request size did not match content length\n\nThis error will occur when the request's length did not match the length from\nthe `Content-Length` header. This typically occurs when the request is malformed,\ntypically when the `Content-Length` header was calculated based on characters\ninstead of bytes. The `status` property is set to `400` and the `type` property\nis set to `'request.size.invalid'`.\n\n### stream encoding should not be set\n\nThis error will occur when something called the `req.setEncoding` method prior\nto this parser. This module operates directly on bytes only and you cannot\ncall `req.setEncoding` when using this module. The `status` property is set to\n`500` and the `type` property is set to `'stream.encoding.set'`.\n\n### stream is not readable\n\nThis error will occur when the request is no longer readable when this parser\nattempts to read it. This typically means something other than a parser from\nthis module read the request body already and the parser was also configured to\nread the same request. The `status` property is set to `500` and the `type`\nproperty is set to `'stream.not.readable'`.\n\n### too many parameters\n\nThis error will occur when the content of the request exceeds the configured\n`parameterLimit` for the `urlencoded` parser. The `status` property is set to\n`413` and the `type` property is set to `'parameters.too.many'`.\n\n### unsupported charset \"BOGUS\"\n\nThis error will occur when the request had a charset parameter in the\n`Content-Type` header, but the `iconv-lite` module does not support it OR the\nparser does not support it. The charset is contained in the message as well\nas in the `charset` property. The `status` property is set to `415`, the\n`type` property is set to `'charset.unsupported'`, and the `charset` property\nis set to the charset that is unsupported.\n\n### unsupported content encoding \"bogus\"\n\nThis error will occur when the request had a `Content-Encoding` header that\ncontained an unsupported encoding. The encoding is contained in the message\nas well as in the `encoding` property. The `status` property is set to `415`,\nthe `type` property is set to `'encoding.unsupported'`, and the `encoding`\nproperty is set to the encoding that is unsupported.\n\n## License\n\n[MIT](LICENSE)\n\n\n[0]: https://github.com/expressjs/body-parser\n[1]: https://github.com/expressjs/body-parser/commit/83db46a1e5512135ce01ed90b9132ee16a2657a8\n[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise\n[3]: https://github.com/ts-stack/multer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fts-stack%2Fbody-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fts-stack%2Fbody-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fts-stack%2Fbody-parser/lists"}