Open WesTyler opened 7 years ago
So there are two different kinds of cases:
toki-method-proxy
- "I want full access to the real underlying request/response"toki-method-http
- "I just want to pass back some data and let you assemble it"Our current case seems fine for (1) but not (2). Could we, instead of passing Request/Response
to the method, bind to it's context a number of different ways of responding?
Regarding my expectation of #2 above, especially in the context of toki-hapi-bridge, this looks a lot like the request decoration that hapi provides to prerequest handlers via the response
object :
Note that prerequisites do not follow the same rules of the normal reply interface. In all other cases, calling reply() with or without a value will use the result as the response sent back to the client. In a prerequisite method, calling reply() will assign the returned value to the provided assign key.... To force the return value as the response and ends the request lifecycle, use the reply().takeover() method.
^^ from here
So it sounds to me like the default behavior should mirror the response decoration behavior that Hapi prereqs follow, and the use case for toki-method-proxy is a special snowflake that would in the above example use the .takeover()
method.
Should we facade that in the same way as Hapi, or divorce it and have it be a different kind of thing?
The facade seems magical. I'm not a big fan of magical. On the other hand, it also lets a method be used in two different fashions - fully and blindly, or as part of a flow.
Keeping the two different kind of responses divorced allows the method to have control and keeps things more explicit.
I'd be tempted to have the context be something like this:
{
server: { request, response }
respond: ()=>{... //pass data back to Toki for it to assemble
}
I like divorcing it. I agree the Hapi-style facade was a bit jarring at first blush. I get that it makes handlers interchangeable between prereqs and req handlers, but that doesn't mirror our needs here.
I would be on board with something like your split context.
My followup concerns would then be delegation of responsibility and the response lifecycle:
In the case of toki-method-proxy
, which of these would occur?
context.server.response.send(payload)
toki
gets the resolved promise / callback / result from method-proxy
and proceeds to the next hypothetical action in the config listOR
context.server.response.send(payload)
toki
gets the resolved promise / callback / result from method-proxy
and proceeds to the next hypothetical action in the config list.actions
list, toki sends the response from step 1.My first thought would be option A, with the risk that you can have an invalid config where future actions try to send data to an already closed request/response. It would be odd behavior for the proxy to pass back to Toki (and if it wanted to do that, it should just use the respond() method instead of server.*)
Sounds good. Just wanted to have a clear understanding of the response lifecycle so I don't get confused again.
Thanks!
Tbh, I don't think we ever really nailed this down until now. I was just as confused.
I'm going to leave this open for reference until toki-method-http
and toki-method-proxy
are done.
Also so that @lamchakchan can chime in
Meh.
Haha, just kidding. Yes, also that.
Pulling this line of discussion into an issue rather than in-line comments in a merged PR.
"Archived" comments below came from this PR
WesTyler 13 minutes ago • edited Member
WesTyler 12 minutes ago Member
dhinklexo 9 minutes ago Member
WesTyler 4 minutes ago Member