archived-servertest icon indicating copy to clipboard operation
archived-servertest copied to clipboard

Add shorthand for setting JSON encoding as default.

Open timoxley opened this issue 10 years ago • 4 comments

A large number of APIs are JSON only: passing {encoding: 'json'} to every request is likely to get very repetitive.

This patch adds a .json property to the exported servertest function, which simply sets the encoding to json by default. This does not strongarm json encoding though, if another encoding is specified in the request options, that encoding will be honored.

I suspect users will likely want the following pattern, setting json format on by default at require-time:

var servertest = require('servertest').json

Not sure how to best add this to the readme though.

timoxley avatar Aug 17 '14 20:08 timoxley

Been thinking a lot about this and initially I had a negative response but now I'm thinking of going all-out. It's not just JSON that's common but also UTF-8 will be a logical choice most of the time. In fact, you might want to do the same with post and get to minimise the need to

What do you reckon about having the following things hanging off the main exported object:

  • json
  • utf8
  • post
  • get

And perhaps even make it so you can double-up:

var servertest = require('servertest').json.post

That's not too "fluent" (bleh!) is it?

I'd also like to do a type check on options and if it's a string then interpret "json", "utf8", "POST" and "GET" specially and turn them into appropriate options objects. Then you'd have to do much less typing to get what you want and still have the flexibility where you need it.

Thoughts?

rvagg avatar Aug 19 '14 12:08 rvagg

That's not too "fluent" (bleh!) is it?

Nope, sounds perfect to me.

timoxley avatar Aug 19 '14 14:08 timoxley

This is all cool, so long as you can configure it with an object too; fluent apis are clunky if you need to configure them dynamically.

timoxley avatar Aug 19 '14 14:08 timoxley

maybe expand to get/post/put/del/etc

timoxley avatar Aug 19 '14 14:08 timoxley