Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware: Body Parser #6

Closed
4 tasks done
one-aalam opened this issue Aug 19, 2022 · 0 comments
Closed
4 tasks done

Middleware: Body Parser #6

one-aalam opened this issue Aug 19, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request middleware Bun-Tea Middleware

Comments

@one-aalam
Copy link
Collaborator

one-aalam commented Aug 19, 2022

parseBody (utility/parser)

Add a parseBody(parser) utility that can negotiate the content type of the form submissions, and returns the parsed body

Supports:

  • application/json
  • application/text
  • application/x-www-form-urlencoded
  • multipart/form-data (not available in Bun natively)

Example:

// your parsed body
const body = await parseBody(ctx.request!)

Currently, plan to support reading all the content at once and improve it in the future version of the middleware (or when Bun natively supports multipart/form-data

Middleware

As a companion utility, add a body-parser middleware, that attaches the parsed payload to context ctx.body using the above utility.

Supports:
The middleware should support the following multipart/form-data configurations (all optional)

  • extensions - supported file extensions (default: all the extensions)
  • maxFileSizeBytes - supported maximum payload size per file (default: no restrictions)
  • maxSizeBytes - supported maximum payload size for all the files (default: no restrictions)

Example(basic):

import { BunTea } from 'bun-tea'
import { bodyParser } from 'bun-tea/dist/mw/body-parser'

const app = new BunTea()

app.post("/users", [ bodyParser() ], (ctx) => {
    // get your parsed body here...
    // console.log(ctx.body)
    return ctx.json({
        message: "users created",
    });
})

app.listen({
    port: 3000
}, (server) => {
    console.log(`Server started on ${server.port}`)
})

Example(with configuration):

import { BunTea } from 'bun-tea'
import { bodyParser } from 'bun-tea/dist/mw/body-parser'

const app = new BunTea()

app.post("/users", [ bodyParser({
    extensions: ['jpg'],
    maxFileSizeBytes: 2_4000,
    maxSizeBytes: 2_6000
}) ], (ctx) => {
    // get your parsed body here...
    // console.log(ctx.body)
    return ctx.json({
        message: "users created",
    });
})

app.listen({
    port: 3000
}, (server) => {
    console.log(`Server started on ${server.port}`)
})
@one-aalam one-aalam self-assigned this Aug 19, 2022
@one-aalam one-aalam added the enhancement New feature or request label Aug 19, 2022
@one-aalam one-aalam changed the title Body Parser for FormData + Middleware Middleware: Body Parser for FormData + Middleware Sep 26, 2022
@one-aalam one-aalam changed the title Middleware: Body Parser for FormData + Middleware Middleware: Body Parser Sep 26, 2022
@one-aalam one-aalam added the middleware Bun-Tea Middleware label Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request middleware Bun-Tea Middleware
Projects
None yet
Development

No branches or pull requests

1 participant