Fork me on GitHub

what's my best option for making an https request from a joyride script? just requiring https from node?


Here's what I ended up with, would appreciate any feedback:

(defn- fetch-json
  (p/create (fn [resolve reject]
              (https/get url
                         (fn [res]
                           (if (= 200 (.-statusCode res))
                             (let [chunks (js/Array)]
                               (doto res
                                 (.on "data" #(.push chunks %))
                                 (.on "end" #(resolve (js->clj (.parse js/JSON (apply str chunks))
                                                               :keywordize-keys true)))))
                             (reject (.-message res))))))))

(defn add-clojars-dep
  (p/let [search-term (vscode/window.showInputBox #js {:prompt "Search for an artifact by name"
                                                       :title "Add Clojars Dependency"})
          search-results (fetch-json (str "" search-term))
          selected-dep  (vscode/window.showQuickPick
                         (clj->js (mapv (fn [x]
                                          #js {:label (str (:group_name x) "/" (:jar_name x))
                                               :description (:version x)
                                               :detail (:description x)})
                                        (:results search-results))))
          dep-versions (fetch-json (str "" (.-label selected-dep)))
          selected-version (vscode/window.showQuickPick
                            (clj->js (mapv (fn [x]
                                             #js {:label (:version x)
                                                  :description (str (:downloads x) " downloads")})
                                           (:recent_versions dep-versions))))]

    (insert-text!+ (str (.-label selected-dep) " {:mvn/version \"" (.-label selected-version) "\"}"))))


also this one, curious if there's a better way to move the cursor to beginning/end of form

(defn update-clojars-dep []
  (let [editor ^js vscode/window.activeTextEditor
        original-selection (.-selection editor)
        current-form (calva/ranges.currentForm)
        form-end (.-end (first current-form))
        artifact (second current-form)]
    (p/let [search-results (fetch-json (str "" artifact))]
        (set! (.-selection editor) (vscode/Selection. form-end form-end))
        (vscode/commands.executeCommand "paredit.killSexpForward")
        (binding [*print-namespace-maps* false]
          (insert-text!+ (str " " (pr-str {:mvn/version (:latest_version search-results)}))))
        (set! (.-selection editor) original-selection)))))