milliparsec 2.2.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { ServerResponse as Response, IncomingMessage } from 'http';
3
4
  import { EventEmitter } from 'events';
4
- declare type NextFunction = (err?: any) => void;
5
- export declare type ReqWithBody<T = any> = IncomingMessage & {
5
+ type NextFunction = (err?: any) => void;
6
+ export type ReqWithBody<T = any> = IncomingMessage & {
6
7
  body?: T;
7
8
  } & EventEmitter;
8
9
  export declare const hasBody: (method: string) => boolean;
package/dist/index.js CHANGED
@@ -18,21 +18,25 @@ const custom = (fn) => async (req, _res, next) => {
18
18
  };
19
19
  const json = () => async (req, res, next) => {
20
20
  if (hasBody(req.method)) {
21
- req.body = await p((x) => JSON.parse(x.toString()))(req, res, next);
21
+ req.body = await p((x) => (x ? JSON.parse(x.toString()) : {}))(req, res, next);
22
22
  next();
23
23
  }
24
24
  else
25
25
  next();
26
26
  };
27
27
  const raw = () => async (req, _res, next) => {
28
- if (hasBody(req.method))
28
+ if (hasBody(req.method)) {
29
29
  req.body = await p((x) => x)(req, _res, next);
30
+ next();
31
+ }
30
32
  else
31
33
  next();
32
34
  };
33
35
  const text = () => async (req, _res, next) => {
34
- if (hasBody(req.method))
36
+ if (hasBody(req.method)) {
35
37
  req.body = await p((x) => x.toString())(req, _res, next);
38
+ next();
39
+ }
36
40
  else
37
41
  next();
38
42
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "milliparsec",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "description": "tiniest body parser in the universe",
5
5
  "repository": "https://github.com/talentlessguy/parsec.git",
6
6
  "author": "talentlessguy <pilll.PL22@gmail.com>",
@@ -18,25 +18,24 @@
18
18
  },
19
19
  "exports": "./dist/index.js",
20
20
  "devDependencies": {
21
- "@rollup/plugin-typescript": "6",
22
- "@tinyhttp/app": "^2.0.16",
23
- "@types/node": "^17.0.12",
24
- "c8": "7.11.0",
25
- "esbuild-node-loader": "^0.6.4",
26
- "rollup": "^2.66.1",
21
+ "@rollup/plugin-typescript": "^11.1.0",
22
+ "@tinyhttp/app": "^2.0.31",
23
+ "@types/node": "^18.16.3",
24
+ "c8": "7.13.0",
25
+ "rollup": "^3.21.4",
27
26
  "supertest-fetch": "^1.5.0",
28
- "tslib": "^2.3.1",
29
- "typescript": "^4.5.5",
30
- "uvu": "^0.5.3"
27
+ "tslib": "^2.5.0",
28
+ "tsm": "^2.3.0",
29
+ "typescript": "^5.0.4",
30
+ "uvu": "^0.5.6"
31
31
  },
32
32
  "files": [
33
33
  "dist"
34
34
  ],
35
35
  "scripts": {
36
- "test": "node --experimental-loader esbuild-node-loader test.ts",
36
+ "test": "uvu -r tsm",
37
37
  "test:coverage": "c8 --include=src pnpm test",
38
38
  "test:report": "c8 report --reporter=text-lcov > coverage.lcov",
39
39
  "build": "rollup -c"
40
- },
41
- "readme": "<div align=\"center\">\n<br /><br /><br />\n<img src=\"logo.png\" width=\"400px\" />\n<br /><br />\n\n![Vulnerabilities][vulns-badge-url]\n[![Version][v-badge-url]][npm-url] [![Coverage][cov-img]][cov-url] [![Github actions][gh-actions-img]][github-actions] [![Downloads][dl-badge-url]][npm-url]\n\n</div>\n<br />\n\nTiniest body parser in the universe. Built for modern Node.js.\n\nCheck out [deno-libs/parsec](https://github.com/deno-libs/parsec) for Deno port.\n\n## Features\n\n- ⏩ built with `async` / `await`\n- 🛠 JSON / raw / urlencoded data support\n- 📦 tiny package size (728B)\n- 🔥 no dependencies\n- ⚡ [tinyhttp](https://github.com/talentlessguy/tinyhttp) and Express support\n\n## Install\n\n```sh\n# pnpm\npnpm i milliparsec\n\n# yarn\nyarn add milliparsec\n\n# npm\nnpm i milliparsec\n```\n\n## Usage\n\n### Basic example\n\nUse a middleware inside a server:\n\n```js\nimport { createServer } from 'http'\nimport { json } from 'milliparsec'\n\nconst server = createServer(async (req: ReqWithBody, res) => {\n await json()(req, res, (err) => void err && console.log(err))\n\n res.setHeader('Content-Type', 'application/json')\n\n res.end(JSON.stringify(req.body))\n})\n```\n\n### Web frameworks integration\n\n#### tinyhttp\n\n```ts\nimport { App } from '@tinyhttp/app'\nimport { urlencoded } from 'milliparsec'\n\nnew App()\n .use(urlencoded())\n .post('/', (req, res) => void res.send(req.body))\n .listen(3000, () => console.log(`Started on http://localhost:3000`))\n```\n\n## API\n\n### `raw(req, res, cb)`\n\nMinimal body parsing without any formatting.\n\n### `text(req, res, cb)`\n\nConverts request body to string.\n\n### `urlencoded(req, res, cb)`\n\nParses request body using `new URLSearchParams`.\n\n### `json(req, res, cb)`\n\nParses request body using `JSON.parse`.\n\n### `custom(fn)(req, res, cb)`\n\nCustom function for `parsec`.\n\n```js\n// curl -d \"this text must be uppercased\" localhost\nawait custom(\n req,\n (d) => d.toUpperCase(),\n (err) => {}\n)\nres.end(req.body) // \"THIS TEXT MUST BE UPPERCASED\"\n```\n\n### What is \"parsec\"?\n\nThe parsec is a unit of length used to measure large distances to astronomical objects outside the Solar System.\n\n[vulns-badge-url]: https://img.shields.io/snyk/vulnerabilities/npm/milliparsec.svg?style=for-the-badge&color=25608B&label=vulns\n[v-badge-url]: https://img.shields.io/npm/v/milliparsec.svg?style=for-the-badge&color=25608B&logo=npm&label=\n[npm-url]: https://www.npmjs.com/package/milliparsec\n[dl-badge-url]: https://img.shields.io/npm/dt/milliparsec?style=for-the-badge&color=25608B\n[github-actions]: https://github.com/talentlessguy/milliparsec/actions\n[gh-actions-img]: https://img.shields.io/github/workflow/status/talentlessguy/milliparsec/CI?style=for-the-badge&color=25608B&label=&logo=github\n[cov-img]: https://img.shields.io/coveralls/github/tinyhttp/milliparsec?style=for-the-badge&color=25608B\n[cov-url]: https://coveralls.io/github/tinyhttp/milliparsec\n"
40
+ }
42
41
  }