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

Non JSON body parser #191

Closed
AlexGalays opened this issue Jun 23, 2021 · 4 comments
Closed

Non JSON body parser #191

AlexGalays opened this issue Jun 23, 2021 · 4 comments

Comments

@AlexGalays
Copy link

Hello! Thank you for the good work. It makes coding with express almost pleasant :D (its middleware system is still crazily untyped but it's now manageable)

Am I correct to assume the scope of this lib so far is to provide a parser only for JSON based bodies? Do you have any plan to add parsing for multipart, binary, etc? If nope, perhaps you can add some pointers in the documentation as to how to quickly plug an express middleware to parse a file, etc? Perhaps that could be a real life example of how to use https://github.com/akheron/typera#using-express-middleware ?

Cheers

@grizio
Copy link

grizio commented Jul 9, 2021

I think this lib is not meant to provide a parser for JSON based body. You need to add your own json body parser:

Note: You must use a Express or Koa body parsing middleware for Parser.body to work.
https://github.com/akheron/typera#parserbodytcodec-ttypet-middleware-body-t--responsebadrequeststring

You can add any body parser in the system (json, form-data, custom):

express()
  .use(jsonBodyParserMiddleware) // body parser for content-type = application/json
  .use(formDataBodyParserMiddleware) // body parser for content-type = multipart/form-data
  .use(router(
    // Route with application/json
    route.post('/create')
      .use(Parser.body(t.type({ action: t.string }))),

    // Route with multipart/form-data
    route.post('/upload')
      .use(Parser.body(t.type({
        filename: t.string, // A value got from a text field
        file: myCustomFileType // The temporary file info of type { path: string, size: number, … }
      })))
  ).handler())

@akheron
Copy link
Owner

akheron commented Jul 10, 2021

Correct. typera-express just takes the value in req.body and applies the io-ts codec to it.

@akheron akheron closed this as completed Aug 22, 2021
@dev-m1-macbook
Copy link

dev-m1-macbook commented Nov 6, 2021

BYO body-parser is understandable, and it's what a lot of libs actually do.

But from ergonomics perspective, wouldn't custom-body-parser help in providing request-body types for typera-openapi?

eg).

route
  .useBodyParser(Parser.json) // --> "application/json"
  .useBodyParser(Parser.formDataNoFiles) // --> "application/x-www-form-urlencoded"
  .useBodyParser(Parser.formDataWithFiles) // --> "multipart/form-data"

Or is it possible to create a 'body-parser wrapper'?:

const jsonMarkedBodyParser = markedBodyParser(jsonBodyParser, 'text/json')

(* sorry the examples are horrible)

@akheron
Copy link
Owner

akheron commented Nov 7, 2021

This is actually a very good idea! I opened a new issue: #306

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants