HS: fetch response unwrap + do-not-throw + dot-prop + JSON coerce (+19 tests)

This commit is contained in:
2026-04-26 22:04:28 +00:00
parent cc800c3004
commit 2dadb6a521
7 changed files with 96 additions and 18 deletions

View File

@@ -888,14 +888,40 @@
out)))))))))))
(define
hs-fetch
hs-fetch-impl
(fn
(url format)
(url format no-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 "text"))))
(let
((raw (perform (list "io-fetch" url fmt))))
(cond ((= fmt "json") (hs-host-to-sx raw)) (true raw))))))
(begin
(when (= (host-get raw "_network-error") true)
(raise (or (host-get raw "message") "Network error")))
(when (and (not no-throw) (not (= fmt "response")) (= (host-get raw "ok") false))
(raise (str "HTTP Error: " (host-get raw "status"))))
(cond
((= fmt "response") raw)
((= fmt "json")
(hs-host-to-sx (perform (list "io-parse-json" raw))))
((= fmt "number")
(hs-to-number (perform (list "io-parse-text" raw))))
(true (perform (list "io-parse-text" raw)))))))))
(define
hs-fetch
(fn (url format) (hs-fetch-impl url format false)))
(define
hs-fetch-no-throw
(fn (url format) (hs-fetch-impl url format true)))
(define
hs-json-escape
@@ -986,9 +1012,10 @@
(true (str value))))
((= type-name "JSON")
(cond
((string? value) (guard (_e (true value)) (json-parse value)))
((dict? value) (hs-json-stringify value))
((list? value) (hs-json-stringify value))
((string? value) (guard (_e (true value)) (hs-host-to-sx (json-parse value))))
((not (nil? (host-get value "_json")))
(hs-host-to-sx (perform (list "io-parse-json" value))))
((dict? value) value)
(true value)))
((= type-name "Object")
(if