Created: 2017-05-16 Tue 23:30
yada 2.0"Companies don't want to update their code every time you decide to rename a function in your standard library. Its role in Java's dominance can't be overstated. This culture … made its way into Clojure and ClojureScript, who haven't made breaking changes in the five years I've used them."
– Zach Oakes – ClojureScript: The industry's favorite functional frontend language – https://sekao.net/blog/industry.html
new java.util.Date(12,12,12)
yada API(ns yada
(:require [yada.yada :as yada]))
["/yada" (yada/yada "yada")]
"Computer Science is the belief that all problems can be solved by adding one more layer of indirection" – Derek deBruler
(ns yada.yada
(:require
[potemkin :refer (import-vars)]
[lots of other stuff]))
(potemkin/import-vars
[yada.aleph listener server]
[yada.context content-type charset language]
[yada.handler handler yada interceptor-chain]
[yada.swagger swaggered]
[yada.resource resource as-resource])
yada extensions & bundles
yada extensions & bundles
yada extensions & bundles
yada can move forwards without breaking compatibility (hopefully!)yada now has a proper extension mechanism for features that fall
outside the HTTP specifications.yada 1.2 !yada
(defn handler [request]
{:status 200
:headers {"Content-Type" "text/html"}
:body "Hello World!!!!!!!!!!!!!!!1
"})
(defresource parameter [txt]
:available-media-types ["text/plain"]
:handle-ok (fn [_] (format "The text is %s" txt)))
(defnk $entries$POST
"Add a new entry to the guestbook"
{:responses {200 schemas/ClientEntry}}
[[:request body :- schemas/EntryData]
[:resources guestbook index]]
(let [entry-id (swap! index inc)
indexed-entry (assoc body :index entry-id)]
(swap! guestbook assoc entry-id indexed-entry)
{:body indexed-entry}))
(GET "/plus" []
:return Total
:query-params [x :- Long, y :- Long]
:summary "x+y with query-parameters"
(ok {:total (+ x y)}))
(POST "/minus" []
:return Total
:body-params [x :- Long, y :- Long]
:summary "x-y with body-parameters"
(ok {:total (- x y)}))
yadayada: A revolution in HTTP library designyada: resource model(defn head-request
"Turns a HEAD request into a GET."
{:added "1.2"}
[request]
(if (= :head (:request-method request))
(assoc request :request-method :get)
request))
(defn head-response
"Returns a nil body if original request was a HEAD."
{:added "1.2"}
[response request]
(if (and response (= :head (:request-method request)))
(assoc response :body nil)
response))
(routes
(GET "/hello" [] (fn [req] "Hello World!"))
(POST "/hello" [] (fn [req] (launch-missiles!))))
PUT request to /hello?5.2. Conditionals
The HTTP conditional request header fields [RFC7232] allow a client to place a precondition on the state of the target resource, so that the action corresponding to the method semantics will not be applied if the precondition evaluates to false.
― RFC 7231
(defn wrap-not-modified
"Middleware that returns a 304 Not Modified from the wrapped handler
if the handler response has an ETag or Last-Modified header, and the
request has a If-None-Match or If-Modified-Since header that matches
the response."
{:added "1.2"}
[handler]
(fn [request]
(-> (handler request) ; WAT?
(not-modified-response request))))
yada: resources as data{:access-control
{:authentication ...}
:methods
{:get
{:produces [{:media-type "text/html" :charset "UTF-8"}
"application/json" "application/edn"]
:response (fn [ctx] {:foo :bar})}
:put
{:consumes "multipart/form-data"
:parameters {:form {:foo Keyword}}
:response (fn [ctx] ...)}}}
[["/accounts/" :accno]
(yada/resource {:id :account})]
["/my-account"
(yada/redirect :account
{:route-params {:accno "1234"}})]
(yada/resource
{:methods
{:get
{:response
(fn [ctx]
(java.net.URI.
(yada/href-for
ctx :account
{:route-params
{:accno
(get-in ctx [:authentication "default"
:account-id])}})))}}})
yada content negotiationyada request parameter coercion{:parameters {:query {:id UUID :from Date}}}
yada response body coercion(yada/resource
{:methods
{:get
{:produces #{"text/html" "application/json"}
:response {:just :an :ordinary :map}}}})
yada resource coercion(yada "yada")
(yada {:do "a deer, a female deer"
:re "(let's stop this now)"})
(yada (new-template-resource
"page.html" {:title "yada"}))
(yada (map->PostgresTable {:table "ACCOUNTS"}))
(-> "Let's go meta!" yada yada yada)
yada securityyada async(yada/resource
{:methods
{:get
{:produces "text/event-stream"
:response (chan)}}})
yada async{:parameters {:get {:query {"q" String}}}
:methods
{:get
{:response
(fn [ctx]
(http/get
(str "https://www.google.com/q=clojure+"
(get-in ctx
[:parameters :query "q"]))))}}}
yada asyncyada methods & headersyada flexibilityyada challengesyada has broadly met its initial goals, but more to be done.yada in ClojureScript?yada startup)