kriskowal / q-io

Interfaces for IO using Q promises in JavaScript on Node
http://documentup.com/kriskowal/q-io
MIT License
317 stars 73 forks source link

Normalize CookieJar requests internally, document normalizeRequest #109

Open crocket opened 10 years ago

crocket commented 10 years ago

request supports it, but there is no documentation about cookies in q-io.

Stuk commented 10 years ago

Yep, this isn't documented, sorry. You can use the http-apps Chain to create a request function that stores cookies:

var HTTP = require("q-io/http");
var HttpApps = require("q-io/http-apps");

var cookieRequest = HttpApps.Chain()
    .use(function (app) { return function (request) { return app(HTTP.normalizeRequest(request)); }; })
    .use(HttpApps.CookieJar)
    // you can add other middleware here too
    .end(HTTP.request);

cookieRequest("http://www.google.com")
.done(function (response) {
    console.log(response);
});
crocket commented 10 years ago

This is more complicated than I thought. Can you make API simpler?

Stuk commented 10 years ago

The need for normalizeRequest here is an oversight and shouldn't be needed, otherwise the API is designed to be very composable, which unfortunately does lead to some complexity.

If you don't think you will need the chain then you can just do:

var cookieRequest = HttpApps.CookieJar(HTTP.request);
cookieRequest(HTTP.normalizeRequest("http://www.google.com")).then( ... );
kriskowal commented 10 years ago

Let’s treat the need for normalizeRequest as a bug with two sides.

  1. Need to document HttpApps.Normalize.
  2. HttpApps.CookieJar should normalize internally.
var HTTP = require("q-io/http");
var HttpApps = require("q-io/http-apps");

var cookieRequest = HttpApps.Chain()
    .use(HttpApps.Normalize)
    .use(HttpApps.CookieJar)
    .end(HTTP.request);

cookieRequest("http://www.google.com")
.done(function (response) {
    console.log(response);
});

On Mon, Jul 14, 2014 at 3:49 PM, Stuart Knightley notifications@github.com wrote:

The need for normalizeRequest here is an oversight and shouldn't be needed, otherwise the API is designed to be very composable, which unfortunately does lead to some complexity.

If you don't think you will need the chain then you can just do:

var cookieRequest = HttpApps.CookieJar(HTTP.request);cookieRequest(HTTP.normalizeRequest("http://www.google.com")).then( ... );

— Reply to this email directly or view it on GitHub https://github.com/kriskowal/q-io/issues/109#issuecomment-48970254.

Stuk commented 10 years ago

:+1: