Fork me on GitHub
#yada
<
2016-05-12
>
ag18:05:09

@malcolmsparks: I’m following this tutorial https://juxt.pro/blog/posts/course-notes-2.html got almost everything working, except this:

(yada/resource
    {:methods
     {:post
      {:consumes transit
       :produces transit
       :response (fn [ctx]
                   (let [res (:response ctx)]
                     (println (str "res -> " res))

ag18:05:03

so it prints out res -> yada.context.Response@2dad0846

ag18:05:59

so it’s not a map, can you give me a hint what should I return here?

malcolmsparks19:05:03

yada.context.Response is actually a record, so you can assoc :status and :headers into it, and other things too

malcolmsparks19:05:34

If you want to print it, try (println (str "res -> " (into {} res))

malcolmsparks19:05:22

The reason you must return this record (or a derivative you've just modified) is to let yada distinguish between a response and a real map

malcolmsparks19:05:45

There are times when you want to return real maps (and yada might consider them the body, say in an application/json representation)

malcolmsparks19:05:04

@ag hope this helps - I'll check the channel later in case you're having problems

malcolmsparks19:05:03

In other words, you usually return a body in a :response function - yada fills in the rest

ag19:05:34

yeah apparently I'm doing something wrong.

malcolmsparks19:05:51

But in rare occasions you might want to have full control over what is returned - perhaps you need to add a header or set a particular status code - that's the purpose of Response - it's an escape hatch to let you have fine-grained control

ag19:05:17

other than that. yada, bidi. and that tutorial are truly fantastic!!! thank you very much!

malcolmsparks19:05:27

if you just println, then you're effectively returning nil, which will produce a 404, (or the POST equivalent)

malcolmsparks19:05:36

oh thank you @ag, that's kind of you

ag19:05:05

I'm very new to Clojure and Clojurescript, Om Next etc. despite the difficulties. I love it

ag20:05:38

@malcolmsparks: what I’m seeing is this:

java.lang.ClassCastException: manifold.deferred.SuccessDeferred cannot be cast to clojure.lang.Associative

ag20:05:12

and I don’t know what’s causing this

mccraigmccraig20:05:42

@ag yada responses are generally promises (aka Deferreds) of maps, rather than naked maps

mccraigmccraig20:05:46

you can read about the things you can do with Deferreds here - https://github.com/ztellman/manifold/blob/master/docs/deferred.md

mccraigmccraig20:05:51

in short, you can use manifold.deferred/chain to compose a pipeline of functions to process the value when it is delivered to the promise

ag20:05:58

@mccraigmccraig: I’m trying to follow Malcolm’s tutorial. https://juxt.pro/blog/posts/course-notes-2.html but it doesn’t work. I feel figwheel is doing something strange

mccraigmccraig20:05:00

if you are seeing yada errors, i would guess it's probably not figwheel

danburton20:05:07

(I'm with Ag). wrap-cors is showing up in the stack trace and seems to be the culprit.

danburton20:05:29

And fighwheel sidecar's server is what is using wrap-cors

mccraigmccraig20:05:47

ah, @ag @danburton is sidecar a ring-handler ?

mccraigmccraig20:05:21

my (second) guess is that you are using a synchronous ring-handler which is trying to modify one of yada's asynchronous responses, which won't work

mccraigmccraig20:05:54

if that's what's happening the simplest thing to do might be to run your api server and figwheel server separately... alternatively you could use a routing lib like bidi to separate sync and async handler paths, so that sidecar never sees a yada response