Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transforming response body in an interceptor #202

Closed
sinistral opened this issue Dec 21, 2017 · 2 comments
Closed

transforming response body in an interceptor #202

sinistral opened this issue Dec 21, 2017 · 2 comments

Comments

@sinistral
Copy link

I'm confused about how interceptors are intended to work, which means I'm very likely doing something wrong.

I have inferred from the response interceptor example that they may be used to transform the body of a response. However, I find the example misleading because short-circuiting the reduction as described results in the handler receiving something that doesn't look like a response. And if I don't want the short-circuit the response interceptor pipeline, it isn't clear to me how to generate an AjaxResponse with the new transformed body that I want to have propagate through the remaining interceptors...

As a quick hack to handle a similar situation to the one described in #76, I've had to resort to this:

(ajax.core/to-interceptor
   {:name
    "Streaming JSON response transformer"
    :response
    #(let [body (json-stream->json-array (ajax.protocols/-body %))]
       (specify! %
         ajax.protocols/AjaxResponse
         (-body [this] body)))})

but I feel like missing something blindingly obvious.

@JulianBirch
Copy link
Owner

I'd be pretty amazed if you could use interceptors to get around my lack of support for streaming, but it would be absolutely awesome if you could. However, I think that what you want is to use is a response format. (Response formats are actually interceptors, but with a lot of code written for you.)

You can find the exact code in https://github.com/JulianBirch/cljs-ajax/blob/master/src/ajax/interceptors.cljc. Most of the rest of the code calls i/map->ResponseFormat to actually create one but notably a map passed into ajax-request is processed like that to begin with.

I hope this helps, please let me know how you get on, it sounds great!

@JulianBirch
Copy link
Owner

Closing since to the extent it's a problem it's covered by other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants