Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 1.92 KB

README.md

File metadata and controls

42 lines (30 loc) · 1.92 KB

cljs-sync-request

A ClojureScript wrapper around the NPM sync-request module

Note: cljs-sync-request does not bundle with sync-request

Latest Version

Clojars Project cljdoc badge CircleCI

Why?

Sometimes you just need to perform a synchronous HTTP requests without worrying about entering callback hell or propagating core.async go blocks and channels throughout your code. I use cljs-sync-request primarily in AWS Lambda functions where I don't need to do any asynchronous work and am typically wait for a result before continuing processing.

As the base sync-request library suggests, you are unlikely to want to use this library in production.

Example Usage

(ns io.axrs.cljs-sync-request.example
  (:require
    [io.axrs.cljs.sync-request.core :refer [wrap-sync-request]]
    ["sync-request" :as sync-request]])) ; When using shadow-cljs
    
(def transformers {"application/json" {:decode #(js->clj (js/JSON.parse %)) 
                                       :encode #(js/JSON.stringify (clj->js %))}})
(def request (wrap-sync-request sync-request transformers))

(defn check-status []
  (let [{:keys [status body headers] :as response} (request {:body {:id "123"} 
                                                             :content-type "application/json" 
                                                             :url "http://localhost"
                                                             :method "POST"})
    (if (= 200 status)
      (continue-processing body)
      (handle-other-codes response))))