FoalTS / foal

Full-featured Node.js framework, with no complexity. 🚀 Simple and easy to use, TypeScript-based and well-documented.
https://foalts.org/
MIT License
1.89k stars 139 forks source link

Change json parser #863

Closed Stargateur closed 3 years ago

Stargateur commented 3 years ago

How to select the json parsing ? I need to use json extended from mongodb: https://www.npmjs.com/package/bson but I don't find any doc about how to handle this situation.

Related https://github.com/FoalTS/foal/issues/791, but I don't like at all the solution proposed.

LoicPoullain commented 3 years ago

It looks like the library can take an object as parameter. Why not creating a hook for this?

const BSON = require('bson');

@Hook(ctx => {
  ctx.request.body = BSON.serialize(ctx.request.body);
})
Stargateur commented 3 years ago

it was my understanding that hook are more about data validation than parsing also, I try using the body in the controller, but it's already parsed, I don't think the body in the hook is not parsed too, but I will verify.

I don't think your solution work. Also I need to parsed extended JSON not BSON

LoicPoullain commented 3 years ago

it was my understanding that hook are more about data validation than parsing also

Hooks can be used in many situations. They are just mere functions executed before the controller method.

I don't think the body in the hook is not parsed too

You're right, the body in the hook is already parsed. I thought that you wanted to convert the JS object (JSON parsed) into a buffer.

If I understand correctly, you're dealing with an extended specification of JSON which is why it's not working.

In this case unfortunately, the only thing that I can suggest is to use the solution that your mentioned in #791 (i.e. using an Express pre-middleware)

LoicPoullain commented 3 years ago

I'm closing the issue as there has been no activity in the last three weeks. Feel free to reach back if you still need help on this 👍

Stargateur commented 3 years ago

I'm closing the issue as there has been no activity in the last three weeks. Feel free to reach back if you still need help on this 👍

I'm not really ok with closing this issue, cause the solution proposed remove feature like ValidateBody and more. Having to manually parse the body make using foalts almost pointless.

I think it should be possible to override the json parser.