Merge branch 'hs-e40-fetch' into loops/hs
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 16s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 16s
This commit is contained in:
@@ -869,12 +869,33 @@
|
||||
(define
|
||||
hs-fetch
|
||||
(fn
|
||||
(url format)
|
||||
(url format do-not-throw target)
|
||||
(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))))
|
||||
(let
|
||||
((raw (perform (list "io-fetch" url fmt))))
|
||||
(cond ((= fmt "json") (hs-host-to-sx raw)) (true raw))))))
|
||||
((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))))
|
||||
(do
|
||||
(when (not (nil? target))
|
||||
(dom-dispatch target "hyperscript:beforeFetch" nil))
|
||||
(let
|
||||
((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
|
||||
@@ -965,6 +986,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