Fork me on GitHub
#juxt
<
2018-05-09
>
martinklepsch13:05:19

I really like it when my bidi routes are plain data but when working with yada it seems common to just replace the route-id keywords with resources. Is there some way to maintain that separation?

martinklepsch13:05:01

(defrecord DocPage [page-type]
  yada.resource/ResourceCoercion
  (as-resource [_]
    (yada/resource
     {:id page-type
      :description "The description to this example resource"
      :summary "An example resource"
      :produces "text/html"
      :methods {:get (fn [_] "hello world")}}))) 

(->DocPage :my-id)
does something like this seem reasonable? This way my route leaves would be basic records…

martinklepsch13:05:15

hm, that doesn’t work:

No implementation of method: :resolve-handler of protocol: #’bidi.bidi/Matched found for class: cljdoc.server.doc_pages.DocPage

martinklepsch13:05:20

Extending it with bidi’s protocols gets me to

No implementation of method: :request of protocol: #’bidi.ring/Ring found for class: cljdoc.server.doc_pages.DocPage

dominicm13:05:07

same kind of thing, bidi has a protocol for "how to make a handler from this"

martinklepsch13:05:05

@dominicm I think I found some relevant code…

martinklepsch13:05:23

(defrecord DocPage [page-type]
  yada.resource/ResourceCoercion
  (as-resource [_]
    (yada/resource
     {:id page-type
      :description “The description to this example resource”
      :summary “An example resource”
      :produces “text/html”
      :methods {:get (fn [_] (str “hello world ” page-type))}}))

  bidi.bidi/Matched
  (resolve-handler [this m]
    (let [r (yada.resource/as-resource this)]
      (if (:path-info? r)
        (assoc m :handler r)
        (bidi.bidi/succeed r m))))

  (unresolve-handler [this m]
    (when
        (or (= this (:handler m))
            (and page-type (= page-type (:handler m))))
      “”)))

martinklepsch16:05:17

is there a way to match all URI segments after a given / into a collection? e.g. /article/section1/section2/123 => {:article-path ["section1" "section2" "123"]}?

dominicm16:05:56

There's something about :path-info? in yada, but it's done via a bidi protocol

dominicm16:05:10

Oh, you have that already

dominicm16:05:15

so yeah, just add :path-info? true

martinklepsch16:05:14

ah neat. it’s matching the URI now but additional segments are not part of the route-params. I assume I have to sort this out myself?

dominicm16:05:52

yeah, you just get the whole thing.

martinklepsch16:05:21

ah remainder is a thing