@routup/body
Version:
Body plugin for routup.
176 lines (128 loc) • 4 kB
Markdown
# @routup/body
[](https://badge.fury.io/js/@routup%2Fbody)
[](https://github.com/Tada5hi/routup/actions/workflows/main.yml)
[](https://codecov.io/gh/tada5hi/routup)
[](https://snyk.io/test/github/Tada5hi/routup)
[](https://conventionalcommits.org)
This is a plugin for reading and parsing the request payload.
**Table of Contents**
- [Installation](#installation)
- [Documentation](#documentation)
- [Usage](#usage)
- [Options](#options)
- [json](#json)
- [urlEncoded](#urlencoded)
- [raw](#raw)
- [text](#text)
- [Helpers](#helpers)
- [setRequestBody](#setrequestbody)
- [useRequestBody](#userequestbody)
- [Credits](#credits)
- [License](#license)
## Installation
```bash
npm install @routup/body --save
```
## Documentation
To read the docs, visit [https://routup.net](https://routup.net)
## Usage
For standard use, the package is installed as a plugin, as shown below.
```typescript
import { createServer } from 'node:http';
import {
createNodeDispatcher,
coreHandler,
Router,
send
} from 'routup';
import { body, useRequestBody } from '@routup/body';
const router = new Router();
// This will parse requests with Content-Type:
// application/json
// application/x-www-form-urlencoded
router.install(body());
router.get('/', coreHandler((req, res) => {
const body = useRequestBody(req);
console.log(body);
// ...
}));
const server = createServer(createNodeDispatcher(router));
server.listen(3000)
```
## Options
The plugin accepts an object as input parameter to modify the default behaviour.
### `json`
To parse `application/json` input data, enable the json handler.
- Type: [Options](https://github.com/expressjs/body-parser#bodyparserjsonoptions) | `boolean`
- Default: `true`
```typescript
router.use(body({
json: {
limit: '100kb'
}
}));
```
### `urlEncoded`
To parse `application/x-www-form-urlencoded` input data, enable the url-encoded handler.
- Type: [Options](https://github.com/expressjs/body-parser#bodyparserurlencodedoptions) | `boolean`
- Default: `true`
```typescript
router.use(body({
urlEncoded: {
extended: false
}
}));
```
### `raw`
To parse `any` input data as Buffer, enable the raw handler.
- Type: [Options](https://github.com/expressjs/body-parser#bodyparserurlencodedoptions) | `boolean`
- Default: `false`
```typescript
router.use(body({
raw: {
inflate: false
}
}));
```
### `text`
To parse `any` input data as string, enable the text handler.
- Type: [Options](https://github.com/expressjs/body-parser#bodyparsertextoptions) | `boolean`
- Default: `false`
```typescript
router.use(body({
raw: {
inflate: false
}
}));
```
## Helpers
### `setRequestBody`
This function sets the parsed request body/payload for the current request.
This method should be implemented by a router middleware/plugin.
```typescript
declare function setRequestBody(
req: Request,
key: string,
value: unknown
) : void;
declare function setRequestBody(
req: Request,
record: Record<string, any>
) : void;
```
## `useRequestBody`
This function returns the parsed request payload.
```typescript
declare function useRequestBody(
req: Request
) : Record<string, any>;
declare function useRequestBody(
req: Request, key: string
) : any | undefined;
```
## Credits
This library is currently based on the [body-parser](https://www.npmjs.com/package/body-parser) library,
but this might change in the near future.
## License
Made with 💚
Published under [MIT License](./LICENSE).