Closed gr0uch closed 9 years ago
This is particularly annoying in the case when a request does not have Content-Type specified.
Gotcha. We should probably add a content-type getter function which returns the content-type or undefined
if it's not there.
P.S. @Fishrock123 , can you update the labels for this repo :)?
@dougwilson if a key on an object is missing, it already returns undefined
and the rest of the function will still throw an error anyways. I just don't want to have to wrap this in a try...catch block because of a missing header :(
@daliwali I know. I'm talking about making a new function exported by this module that doesn't do that :)
@daliwali how does this API look:
var contentType = require('content-type')
var type = contentType(req)
// type is a parsed type or undefined
@dougwilson I like that, very concise.
P.S. @Fishrock123 , can you update the labels for this repo :)?
@daliwali sorry to bug you again :) But for the contentType(req)
API, in your opinion, what should it do if there is a Content-Type
header, but it's invalid?
@dougwilson Ah, perhaps it really should throw an error. The alternative is to fail silently, which wouldn't feel good either. Throwing an error would feel consistent with some other parsing methods, like JSON.parse
. Ok I take it back, errors are fine all around. It was just disorienting to see argument string is required to be a string
when I passed in a request object.
Ok, we can think on it some more. For now, I'm' going to release your changes from this PR; feel free to open a new issue for anything :)
Note: in my opinion it should not throw as many errors as it does, allow it to return null values and then it is up to the programmer to check if it succeeded. This is particularly annoying in the case when a request does not have
Content-Type
specified.