fastify / fastify-plugin

Plugin helper for Fastify
MIT License
203 stars 43 forks source link

passing route specific arguments to body parser plugin #27

Closed amitguptagwl closed 6 years ago

amitguptagwl commented 6 years ago

I was trying to write a body parser to support multiple content types. To parse nimn to json, I need to pass simplified object structure to the parser which is accepted by particular route.

var nimnInstance = new nimn();
nimnInstance.addSchema(schema);
//some code here
var nimndata = nimnInstance.encode(jObj);
var jObj2 = nimnInstance.decode(nimndata);
mcollina commented 6 years ago

I do not understand what is the problem, what do you want to achieve, and how we can help. Can you please articulate further?

amitguptagwl commented 6 years ago

Sorry for the confusing language. I've got the answer while exploring other examples. Many thanks for your quick response.

amitguptagwl commented 6 years ago

Reopening and updating the question

I'm trying to write a body parser which converts nimn data format to js object. For this, I need to know the object structure of target json which can be different for each route. So how can I pass options with every route so that body parser can parse request payload to js obj accordingly?

const opts = {
  schema: {}
};
fastify.get('/', opts, (request, reply) => {
  //write some code here
})

As per above snippets, I want to access opts in body parser plugin.

mcollina commented 6 years ago

You should really post this issue in the Fastify repo. This is just a little utility to help writing plugins.

amitguptagwl commented 6 years ago

sure thanks. I'll do

amitguptagwl commented 6 years ago

should I ask in fastify or fastify/help?

Issues should only be posted in this repository after you have been able to reproduce them and confirm that they are a bug or incorrect/missing information in the docs.

For all other questions, requests, help resolving an issue, or if you are not sure if this is the right place, please do not open an issue here. Instead, ask a question in our help repository or try to find a solution in the community Gitter Chat.

mcollina commented 6 years ago

I would go for the main repo, this functionality is not there. We only support schema-based behavior through https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md#schema-compiler, but it's JSON-only.

amitguptagwl commented 6 years ago

Exactly. I believe both functionalities should be implemented in the same way, so it can facilitate 3rd party plugins too.