@binden/body
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

@binden/body CI Status version Known Vulnerabilities Coverage Status code style: prettier Contributor Covenant semantic-release Conventional Commits GitHub top language node version npm downloads License

Binden body parser middleware. Supports the following content types

  • application/json
  • application/x-www-form-urlencoded
  • plain/text

and any combination of the following encodings

  • br
  • x-gzip
  • gzip
  • deflate

Installation

npm install @binden/body

Usage

import BodyParser from "@binden/body";

class MyMiddleware extends Middleware {
  public run(context: Context): void {
    const { body } = context.request;
    context.log.info("Body has been parsed (or not)", { body });
  }
}

app.use("/deposit", new Router().post(BodyParser, new MyMiddleware()));

Custom parse function

import { Binden } from "binden";
import AJV, { JTDDataType } from "ajv/dist/jtd.js";
import BodyParser from "@binden/body";
const ajv = new AJV();
const schema = {
  properties: { username: { type: "string" }, password: { type: "string" } },
  additionalProperties: false,
};
const parse = ajv.compileParser<JTDDataType<typeof schema>>(schema);
const body_parser = new BodyParser({ parse });
const echo = (context) => {
  const { body } = context.request;
  context.log.info("Body has been parsed (or not)", { body });
  return context.json(body);
};
new Binden()
  .use("/login", new Router().post(BodyParser, echo))
  .createServer()
  .listen(port, () => {
    console.log(`Server is listening on the port ${port}`);
  });

Test

npm test

Readme

Keywords

Package Sidebar

Install

npm i @binden/body

Weekly Downloads

1

Version

2.0.1

License

AGPL-3.0-only

Unpacked Size

44.9 kB

Total Files

6

Last publish

Collaborators

  • vansergen