Add request interceptor#177
Add request interceptor#177dhh merged 4 commits intohotwired:mainfrom kirillplatonov:request-interceptor
Conversation
|
Could this same seam for modifying headers be achieved with a addEventListener("turbo:before-fetch-request", ({ detail: { fetchOptions }) => fetchOptions.headers["..."] = "...")) |
|
Changing headers with |
|
@kirillplatonov Nice work on this PR! Great that we won't need to fetch JWT every x seconds with your |
|
Until it's merged I published my fork to npm: |
|
Great stuff @kirillplatonov. Could you add some documentation for this on https://turbo.hotwire.dev/reference/drive? It's in the turbo-site repo. Thanks! |
| @@ -0,0 +1,15 @@ | |||
| export class RequestInterceptor { | |||
There was a problem hiding this comment.
Could we not set this like we do progressBarDelay? Where it's set on the session, not as a singleton.
There was a problem hiding this comment.
I think here in Turbo it would be completely possible
There was a problem hiding this comment.
I was trying to put it in session initially. But we use interceptor in FetchRequest class where we don't have an access to the session. Add fetching session from delegates not worked as well since not all delegates have access to the session. That's why I end up using a singleton. Seems like the cleanest solution available.
Added documentation here: hotwired/turbo-site#53 |
|
Thanks for getting this through @kirillplatonov @dhh - useful for JWT / Shopify apps! |
|
@kirillplatonov Upon further reflection, I think it might be worth exploring an approach similar to #28 (comment). Which basically let's the turbo:before-fetch-request pause the request. Then we could do it all in a callback instead. |
|
@dhh having the same pausable API for both |
The problem
The modern server-side embedded Shopify apps are built with JWT session tokens which are obtained on the client-side and then passed to every server request.
Turboworks pretty well with it since it allows to navigate application without doing page reloading on every click or page submission. The biggest issue is passing JWT token to everyTurborequest.The solution
This PR introduces request interceptor support. It allows to insert async function between every fetch request. Inside this function you can do some preparation for request and add custom headers.
Example usage