profile
viewpoint

Ask questionsSupport RegExp for the addContentTypeParser API

<!-- Before you submit an issue we recommend you visit Fastify Help and ask any questions you have or mention any problems you've had getting started with Fastify.

Please read this entire template before posting any issue. If you ignore these instructions and post an issue here that does not follow the instructions, your issue might be closed, locked, and assigned the missing discussion label. -->

🚀 Feature Proposal

Support RegExp for the addContentTypeParser API.

Motivation

Currently, you need to add a new content type parser for each new content type you use. So you have to add 2 parsers if you use the content types application/vnd.user+json and application/vnd.book+json, even though both are valid JSON objects and could use the same content type parser. This could be possible if the addContentTypeParser API supported RegExp in addition to string values.

Example

fastify.addContentTypeParser(/s*\+json$/, { parseAs: "string" }, function (req, body, done) {
  try {
    var json = JSON.parse(body)
    done(null, json)
  } catch (err) {
    err.statusCode = 400
    done(err, undefined)
  }
})
fastify/fastify

Answer questions matthyk

Yes, I will send a PR as soon as I am done.

useful!
source:https://uonfu.com/
answerer
matthyk Würzburg, Germany Software Engineer who loves RESTful api design and music
Github User Rank List