2.0.1 • Published 3 months ago

@binden/body v2.0.1

Weekly downloads
-
License
AGPL-3.0-only
Repository
github
Last release
3 months ago

@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
2.0.1

3 months ago

2.0.0

3 months ago

1.1.1

10 months ago

1.1.0

1 year ago

1.0.0

2 years ago