HS: fetch do-not-throw modifier (+1 test)
This commit is contained in:
@@ -874,12 +874,30 @@
|
||||
(define
|
||||
hs-fetch
|
||||
(fn
|
||||
(url format)
|
||||
(url format do-not-throw)
|
||||
(let
|
||||
((fmt (cond ((nil? format) "text") ((or (= format "json") (= format "JSON") (= format "Object")) "json") ((or (= format "html") (= format "HTML")) "html") ((or (= format "response") (= format "Response")) "response") ((or (= format "text") (= format "Text")) "text") (true format))))
|
||||
((fmt (cond ((nil? format) "text") ((or (= format "json") (= format "JSON") (= format "Object")) "json") ((or (= format "html") (= format "HTML")) "html") ((or (= format "response") (= format "Response")) "response") ((or (= format "text") (= format "Text")) "text") ((or (= format "number") (= format "Number")) "number") (true format))))
|
||||
(let
|
||||
((raw (perform (list "io-fetch" url fmt))))
|
||||
(cond ((= fmt "json") (hs-host-to-sx raw)) (true raw))))))
|
||||
((raw (perform (list "io-fetch" url "response" (dict)))))
|
||||
(do
|
||||
(when (get raw :_network-error) (raise {:response raw :message "Network error" :_hs-error "FetchError"}))
|
||||
(when
|
||||
(and (not (get raw :ok)) (not (= fmt "response")) (not do-not-throw))
|
||||
(raise {:response raw :status (get raw :status) :message "Fetch error" :_hs-error "FetchError"}))
|
||||
(cond
|
||||
((= fmt "response") raw)
|
||||
((= fmt "json")
|
||||
(let
|
||||
((parsed (perform (list "io-parse-json" (get raw :_json)))))
|
||||
(hs-host-to-sx parsed)))
|
||||
((= fmt "html")
|
||||
(perform (list "io-parse-html" (get raw :_html))))
|
||||
((= fmt "number")
|
||||
(or
|
||||
(parse-number (get raw :_number))
|
||||
(parse-number (get raw :_body))
|
||||
0))
|
||||
(true (get raw :_body))))))))
|
||||
|
||||
(define
|
||||
hs-json-escape
|
||||
@@ -970,6 +988,8 @@
|
||||
(true (str value))))
|
||||
((= type-name "JSON")
|
||||
(cond
|
||||
((and (dict? value) (dict-has? value :_json))
|
||||
(guard (_e (true value)) (json-parse (get value :_json))))
|
||||
((string? value) (guard (_e (true value)) (json-parse value)))
|
||||
((dict? value) (hs-json-stringify value))
|
||||
((list? value) (hs-json-stringify value))
|
||||
|
||||
Reference in New Issue
Block a user