Skip to content

Commit

Permalink
Override option to json-post, added credentials by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
chr15m committed Sep 30, 2024
1 parent 4928402 commit 36820e9
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/sitefox/ui.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,20 @@

(defn json-post
"Use HTTP post to send data to /url in JSON format. Will convert clj to js datastructures.
Will obtain and use a CSRF token."
[url data]
Will obtain and use a CSRF token. Use `options` to override the options passed to `js/fetch`.
The options will be shallow-merged into the defaults."
[url data & [options]]
(->
(fetch-csrf-token)
(.then (fn [csrf-token]
(js/fetch url
(clj->js {:method "POST"
:headers {:content-type "application/json"
:X-XSRF-TOKEN csrf-token}
:body (js/JSON.stringify (clj->js data))}))))
(js/fetch
url
(clj->js
(merge
{:method "POST"
:headers {:content-type "application/json"
:X-XSRF-TOKEN csrf-token}
:credentials "include"
:body (js/JSON.stringify (clj->js data))}
(js->clj options :keywordize-keys true))))))
(.then (fn [res] (.json res)))))

0 comments on commit 36820e9

Please sign in to comment.