cojs / co-body

Parse request bodies with co
MIT License
325 stars 42 forks source link

Add a parameter named transformBody to parse the request body in a user-defined way #76

Closed mstar-kk closed 4 years ago

mstar-kk commented 4 years ago

Add a parameter named transformBody to parse the request body in a user-defined way. For example,

//opts
const opts = {
    transformBody: function(str) {
        const JsonBigInt = require("json-bigint");
        return JsonBigInt.parse(str);
    }
}
codecov-commenter commented 4 years ago

Codecov Report

Merging #76 into master will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #76   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines           91       102   +11     
=========================================
+ Hits            91       102   +11     
Impacted Files Coverage Δ
lib/form.js 100.00% <100.00%> (ø)
lib/json.js 100.00% <100.00%> (ø)
lib/text.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update a847bfd...ff4a6b2. Read the comment docs.

dead-horse commented 4 years ago

we should separate the transform function for three types

dead-horse commented 4 years ago

I don't think add this options is required. If you have something that is not compatible with the standard, you should use https://github.com/stream-utils/raw-body to handle it by yourself.

Another solution is use text type and parse by yourself later.

mstar-kk commented 4 years ago

I think that as long as node provides services to the outside, this problem can not be avoided, because there is no guarantee of what language the client is java,python or whatever. And just like parse form body can use different querystring module in a user-defined, this paramater do the same job for other body type. Axios has interceptors with similar capabilities. From this point of view, this function is required. Thanks for review my code.

@dead-horse

dead-horse commented 4 years ago

Whatever language the client use, they should follow the spec (like https://www.json.org/json-en.html, https://url.spec.whatwg.org/#urlencoded-serializing), and co-body only support the standard version of JSON / X-www-form-urlencoded format.

dead-horse commented 4 years ago

Certainly the phenomenon you describe exists, but there are so few scenarios that I don't want to have to provide new features for the sake of such scenarios, which is not conducive to keeping the code cohesive and streamlined.

mstar-kk commented 4 years ago

ok,I understand.And thanks again. @dead-horse