hapijs / h2o2

Proxy handler for hapi.js
Other
165 stars 66 forks source link

onResponse return value? #103

Closed datvong-wm closed 4 years ago

datvong-wm commented 5 years ago

According to the documentation:

It is not clear what onResponse should return. In this example, it doesn't return anything.

   onResponse: function (err, res, request, h, settings, ttl) {

                console.log('receiving the response from the upstream.');
                Wreck.read(res, { json: true }, function (err, payload) {

                    console.log('some payload manipulation if you want to.')
                    const response = h.response(payload);
                    response.headers = res.headers;
                    return response;
                });
            }
hueniverse commented 5 years ago

It's an override. Whatever it returns will be the return value of the handler.

kanongil commented 5 years ago

The example from the docs is wrong though. It just returns undefined and performs some non-sensical async operation. As it is, H2o2 doesn't support any kind of async response handling. Maybe it should await the result of the onResponse() call?

datvong-wm commented 5 years ago

So in this example, it should return Wreck.read()?

onResponse: function (err, res, request, h, settings, ttl) {
  return Wreck.read(res, {json:true}, function(err, payload) {
    return response;
  });
}
antony commented 4 years ago

I've just run into this too.

@hapi/wreck now supports async/await, and h2o2 won't return a value if you use the callback syntax, even if you add return onto the front of it. You need to use the async/await syntax.

just a doc update for the new wreck, I'll do it once I've got my syntax working 100%.