sitefox.ui
Utility functions that can be used in client-side code.
csrf-token
(csrf-token)
Returns the CSRF token passed by Sitefox in the XSRF-TOKEN
cookie. Pass this value when doing POST requests via ajax:
(js/fetch "/api/endpoint"
#js {:method "POST"
:headers #js {:Content-Type "application/json"
:XSRF-Token (csrf-token)}
:body (js/JSON.stringify (clj->js data))})
Note: passing the token via client side cookie is now deprecated. If you want to re-enable it set the client side environment variable SEND-CSRF-COOKIE
, and then this function will work again.
See the README for more details: https://github.com/chr15m/sitefox/#csrf-protection
fetch-csrf-token
(fetch-csrf-token)
Returns a promise which resolves to a string containing the CSRF token fetched from the Sitefox backend server. Pass this value as the X-XSRF-TOKEN when doing POST requests via ajax fetch:
(-> (fetch-csrf-token)
(.then (fn [token]
(js/fetch "/api/endpoint"
#js {:method "POST"
:headers #js {:Content-Type "application/json"
:X-XSRF-TOKEN token} ; <- use token here
:body (js/JSON.stringify (clj->js some-data))}))))
get-cookie
(get-cookie cookie-name & [cookies])
Returns the value of the named cookie from js/document.cookies (or optionally the cookies
argument).
json-post
(json-post url data & [options])
Use HTTP post to send data to /url in JSON format. Will convert clj to js datastructures. 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.
simple-date-time
(simple-date-time dt)
Returns a simple string representation of the date and time in YYYY-MM-DD HH:MM:SS format.
time-since
(time-since date-string & [from-date-string])
Returns a string describing how long ago the date described in date-string
was.