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

0.5.13 • Public • Published

Wooks Body

!!! This is work-in-progress library, breaking changes are expected !!!


Wooks Body is composable body parser for @wooksjs/event-http.

Supported content types:

  • ✅ application/json
  • ✅ text/*
  • ✅ multipart/form-data
  • ✅ application/x-www-form-urlencoded

Body parser does not parse every request's body. The parsing happens only when you call parseBody function.

Installation

npm install @wooksjs/http-body

Usage

import { useBody } from '@wooksjs/http-body'
app.post('test', async () => {
  const { parseBody } = useBody()
  const data = await parseBody()
})

Additional hooks

import { useBody } from '@wooksjs/http-body'
app.post('test', async () => {
  const {
    isJson, // checks if content-type is "application/json" : () => boolean;
    isHtml, // checks if content-type is "text/html" : () => boolean;
    isXml, // checks if content-type is "application/xml" : () => boolean;
    isText, // checks if content-type is "text/plain" : () => boolean;
    isBinary, // checks if content-type is binary : () => boolean;
    isFormData, // checks if content-type is "multipart/form-data" : () => boolean;
    isUrlencoded, // checks if content-type is "application/x-www-form-urlencoded" : () => boolean;
    isCompressed, // checks content-encoding : () => boolean | undefined;
    contentEncodings, // returns an array of encodings : () => string[];
    parseBody, // parses body according to content-type : <T = unknown>() => Promise<T>;
    rawBody, // returns raw body Buffer : () => Promise<Buffer>;
  } = useBody()

  // the handler got the control, but the body isn't loaded yet
  //...

  console.log(await parseBody())

  // after `await parseBody()` the body was loaded and parsed
  // ...
})

Documentation

To check out docs, visit wooks.moost.org.

Package Sidebar

Install

npm i @wooksjs/http-body

Weekly Downloads

415

Version

0.5.13

License

MIT

Unpacked Size

16.2 kB

Total Files

6

Last publish

Collaborators

  • mav-rik