@marblejs/middleware-body
HTTP request body parser middleware for Marble.js.

Installation

1
yarn add @marblejs/middleware-body
Copied!
Requires @marblejs/core to be installed.

Importing

1
import { bodyParser$ } from '@marblejs/middleware-body';
Copied!

Type declaration

1
bodyParser$ :: BodyParserOptions -> HttpMiddlewareEffect
Copied!

Parameters

parameter
definition
options
<optional> BodyParserOptions
BodyParserOptions
parameter
definition
parser
<optional> RequestBodyParser
type
<optional> Array<string>
The type option is used to determine what media type the middleware will parse. It is passed directly to the type-is library and this can be an extension name (like json), a mime type (like application/json), or a mime type with a wildcard (like */* or */json). Defaults to */*.

Basic usage

app.ts
1
import { bodyParser$ } from '@marblejs/middleware-body';
2
3
export default httpListener({
4
middlewares: [bodyParser$()],
5
effects: [/* ... */],
6
});
Copied!
Lets assume that we have the following CURL command, which triggers POST /api/login endpoint:
1
$ curl --header "Content-Type: application/json" \
2
--request POST \
3
--data '{ "username": "foo", "password": "bar" }' \
4
http://localhost:3000/api/login
Copied!
Using previously connected bodyParser$ middleware, the app will intercept the following payload object:
1
req.body = {
2
username: 'foo',
3
password: 'bar',
4
};
Copied!
The POST request body can be intercepted like follows.
login.effect.ts
1
import { r } from '@marblejs/core';
2
3
export const login$ = r.pipe(
4
r.matchPath('/login'),
5
r.matchType('POST'),
6
r.useEffect(req$ => req$.pipe(
7
map(req => req.body as { username: string, password: string }),
8
map(body => ({ body: `Hello, ${body.username}!` }))
9
)));
Copied!
All properties and values in req.body object are untrusted (unknown) and should be validated before trusting.
This middleware does not handle multipart bodies.

Advanced usage

The middleware does nothing if request Content-Type is not matched, which makes a possibility for chaining multiple parsers one after another. For example, we can register multiple middlewares that will parse only a certain set of possible Content-Type headers.
Default parser:
1
import { bodyParser$ } from '@marblejs/middleware-body';
2
3
bodyparser$();
Copied!
Parses application/json (to JSON), application/x-www-form-urlencoded (to JSON), application/octet-stream (to Buffer) and text/plain (to string) content types
JSON parser:
1
import { bodyParser$, jsonParser } from '@marblejs/middleware-body';
2
3
bodyParser$({
4
parser: jsonParser,
5
type: ['*/json', 'application/vnd.api+json'],
6
})
Copied!
Parses req.body to JSON object.
URLEncoded parser:
1
import { bodyParser$, urlEncodedParser } from '@marblejs/middleware-body';
2
3
bodyParser$({
4
parser: urlEncodedParser,
5
type: ['*/x-www-form-urlencoded'],
6
});
Copied!
Parses encoded URLs req.body to JSON object.
Text parser:
1
import { bodyParser$, textParser } from '@marblejs/middleware-body';
2
3
bodyParser$({
4
parser: textParser,
5
type: ['text/*'],
6
});
Copied!
Parses req.body to string.
Raw parser:
1
import { bodyParser$, rawParser } from '@marblejs/middleware-body';
2
3
bodyParser$({
4
parser: rawParser,
5
type: ['application/octet-stream'],
6
}),
Copied!
Parses req.body to Buffer.

Custom parsers

If the available parsers are not enough, you can create your own body parsers by conforming to RequestBodyParser interface. The example below shows how the jsonParser looks underneath.
1
export const jsonParser: RequestBodyParser = req => body =>
2
JSON.parse(body.toString());
Copied!
Last modified 4mo ago