2.3.0 • Published 6 months ago

@routup/body v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@routup/body

npm version main codecov Known Vulnerabilities Conventional Commits

This is a plugin for reading and parsing the request payload.

Table of Contents

Installation

npm install @routup/body --save

Documentation

To read the docs, visit https://routup.net

Usage

For standard use, the package is installed as a plugin, as shown below.

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 | boolean
  • Default: true
router.use(body({
    json: {
        limit: '100kb'
    }
}));

urlEncoded

To parse application/x-www-form-urlencoded input data, enable the url-encoded handler.

  • Type: Options | boolean
  • Default: true
router.use(body({
    urlEncoded: {
        extended: false
    }
}));

raw

To parse any input data as Buffer, enable the raw handler.

  • Type: Options | boolean
  • Default: false
router.use(body({
    raw: {
        inflate: false
    }
}));

text

To parse any input data as string, enable the text handler.

  • Type: Options | boolean
  • Default: false
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.

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.

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 library, but this might change in the near future.

License

Made with 💚

Published under MIT License.

1.0.2

8 months ago

1.0.3

8 months ago

2.3.0

6 months ago

2.0.0-alpha.3

8 months ago

2.2.0

6 months ago

2.0.0-alpha.0

8 months ago

2.1.0

7 months ago

2.0.0

8 months ago

1.0.1

12 months ago

1.0.0

1 year ago

1.0.0-alpha.0

1 year ago

0.7.4

1 year ago

0.7.3

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.7.0

1 year ago

0.3.4

1 year ago

0.6.0

1 year ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.0-alpha.0

2 years ago

0.1.0

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago