Open scottcorgan opened 9 years ago
(as a note):
var getSomething = request
.get('something')
.parse('json');
getSomething().then(function(jsonResponse) {
});
I think the default case should be to parse application/json
response type, but everything else leave alone. Then you can explicitly say parse('text')
to disable the parsing even if content type is application/json
.
Agreed.
Ok, one more thought and I'm done. Maybe it would make sense to be able to register parsers for different content types? So I could register a text/html
parser or a special application/json+vnd.whatever
parser? Not sure.
like plugins?
var request = ask({
origin: 'http://someapi.com',
parsers: {
'some-parser-name': require('../parsers/some-parser.json')
}
});
request.get('some-endpont').parse('some-parser-name');
Not necessarily called that, but not far off. I'd suggest:
var request = ask({
origin: 'http://someapi.com',
parsers: {
html: {
contentType: "text/html",
parse: function(res){ /* ... */ }
}
}
});
request.get('some-endpont').parse('some-parser-name');
If a contentType
option is specified, anything of that content type will automatically be parsed. This should be able to be a string or regexp. The users parsers should be merged into a default set (which is maybe just 'json').
If the user passes parse(false)
to the chain, that should disable any parsing. Similarly, parse(true)
should enable content-type based parsing (but not prefer a specific one). parse('parser-name')
should mandate that regardless of content-type, parse using the specified parser, and parse(function(res){ })
should be possible for an on-the-fly parser.
I think that covers everything, anything I'm missing?
That covers it.
contentType
wont' be required, but, if it is included, that parser will only act on content-types specified in that field and ignore any content-types that don't match that, correct?
contentType is a way to specify an automatic triggering of the parsing. If it's present, you should still be able to manually call it to force that parser's usage.
On Thu Nov 06 2014 at 12:28:13 PM Scott Corgan notifications@github.com wrote:
That covers it.
contentType wont' be required, but, if it is included, that parser will only act on content-types specified in that field and ignore any content-types that don't match that, correct?
— Reply to this email directly or view it on GitHub https://github.com/divshot/ask/issues/10#issuecomment-62045601.
Maybe look for
application/json
but also have an explicit option e.g.parse('json')
with an optional parsing function likeparse(function(res){ return res.body.toLowerCase() })