;; This Source Code Form is subject to the terms of the Mozilla Public ;; License, v. 2.0. If a copy of the MPL was not distributed with this ;; file, You can obtain one at http://mozilla.org/MPL/2.0/. ;; ;; Copyright (c) UXBOX Labs SL (ns app.http.doc "API autogenerated documentation." (:require [app.common.data :as d] [app.config :as cf] [app.util.services :as sv] [app.util.template :as tmpl] [clojure.java.io :as io] [clojure.spec.alpha :as s] [pretty-spec.core :as ps])) (defn get-spec-str [k] (with-out-str (ps/pprint (s/form k) {:ns-aliases {"clojure.spec.alpha" "s" "clojure.core.specs.alpha" "score" "clojure.core" nil}}))) (defn prepare-context [rpc] (letfn [(gen-doc [type [name f]] (let [mdata (meta f)] ;; (prn name mdata) {:type (d/name type) :name (d/name name) :auth (:auth mdata true) :docs (::sv/docs mdata) :spec (get-spec-str (::sv/spec mdata))}))] {:query-methods (into [] (map (partial gen-doc :query)) (->> rpc :methods :query (sort-by first))) :mutation-methods (into [] (map (partial gen-doc :mutation)) (->> rpc :methods :mutation (sort-by first)))})) (defn handler [rpc] (let [context (prepare-context rpc)] (if (contains? cf/flags :backend-api-doc) (fn [_] {:status 200 :body (-> (io/resource "api-doc.tmpl") (tmpl/render context))}) (constantly {:status 404 :body ""}))))