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 mcollina

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

useful!
source:https://uonfu.com/
answerer
Matteo Collina mcollina @nearform In the clouds above Italy Technical Director @nearform, TSC member @nodejs, IoT Expert, Conference Speaker, Ph.D.
Github User Rank List