Update jsfetch with latest API and fix missing bindings (#19473)
* Update with latest API and fix missing bindings remove deprecated `Body` remove implicit `cstring` convs add `Headers` to `FetchOptions` add `Request` init proc which takes `FetchOptions` * Update lib/std/jsfetch.nim Co-authored-by: Juan Carlos <juancarlospaco@gmail.com> * Update lib/std/jsfetch.nim Co-authored-by: Juan Carlos <juancarlospaco@gmail.com> * remove experimental flag Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>
This commit is contained in:
parent
56a901f9e1
commit
22ae0bef63
1 changed files with 25 additions and 21 deletions
|
|
@ -1,5 +1,4 @@
|
||||||
## - Fetch for the JavaScript target: https://developer.mozilla.org/docs/Web/API/Fetch_API
|
## - Fetch for the JavaScript target: https://developer.mozilla.org/docs/Web/API/Fetch_API
|
||||||
## .. Note:: jsfetch is Experimental.
|
|
||||||
when not defined(js):
|
when not defined(js):
|
||||||
{.fatal: "Module jsfetch is designed to be used with the JavaScript backend.".}
|
{.fatal: "Module jsfetch is designed to be used with the JavaScript backend.".}
|
||||||
|
|
||||||
|
|
@ -12,6 +11,7 @@ type
|
||||||
keepalive*: bool
|
keepalive*: bool
|
||||||
metod* {.importjs: "method".}: cstring
|
metod* {.importjs: "method".}: cstring
|
||||||
body*, integrity*, referrer*, mode*, credentials*, cache*, redirect*, referrerPolicy*: cstring
|
body*, integrity*, referrer*, mode*, credentials*, cache*, redirect*, referrerPolicy*: cstring
|
||||||
|
headers*: Headers
|
||||||
|
|
||||||
FetchModes* = enum ## Mode options.
|
FetchModes* = enum ## Mode options.
|
||||||
fmCors = "cors"
|
fmCors = "cors"
|
||||||
|
|
@ -42,16 +42,13 @@ type
|
||||||
frpOriginWhenCrossOrigin = "origin-when-cross-origin"
|
frpOriginWhenCrossOrigin = "origin-when-cross-origin"
|
||||||
frpUnsafeUrl = "unsafe-url"
|
frpUnsafeUrl = "unsafe-url"
|
||||||
|
|
||||||
Body* = ref object of JsRoot ## https://developer.mozilla.org/en-US/docs/Web/API/Body
|
|
||||||
bodyUsed*: bool
|
|
||||||
|
|
||||||
Response* = ref object of JsRoot ## https://developer.mozilla.org/en-US/docs/Web/API/Response
|
Response* = ref object of JsRoot ## https://developer.mozilla.org/en-US/docs/Web/API/Response
|
||||||
bodyUsed*, ok*, redirected*: bool
|
bodyUsed*, ok*, redirected*: bool
|
||||||
typ* {.importjs: "type".}: cstring
|
typ* {.importjs: "type".}: cstring
|
||||||
url*, statusText*: cstring
|
url*, statusText*: cstring
|
||||||
status*: cint
|
status*: cint
|
||||||
headers*: Headers
|
headers*: Headers
|
||||||
body*: Body
|
body*: cstring
|
||||||
|
|
||||||
Request* = ref object of JsRoot ## https://developer.mozilla.org/en-US/docs/Web/API/Request
|
Request* = ref object of JsRoot ## https://developer.mozilla.org/en-US/docs/Web/API/Request
|
||||||
bodyUsed*, ok*, redirected*: bool
|
bodyUsed*, ok*, redirected*: bool
|
||||||
|
|
@ -59,8 +56,7 @@ type
|
||||||
url*, statusText*: cstring
|
url*, statusText*: cstring
|
||||||
status*: cint
|
status*: cint
|
||||||
headers*: Headers
|
headers*: Headers
|
||||||
body*: Body
|
body*: cstring
|
||||||
|
|
||||||
|
|
||||||
func newResponse*(body: cstring | FormData): Response {.importjs: "(new Response(#))".}
|
func newResponse*(body: cstring | FormData): Response {.importjs: "(new Response(#))".}
|
||||||
## Constructor for `Response`. This does *not* call `fetch()`. Same as `new Response()`.
|
## Constructor for `Response`. This does *not* call `fetch()`. Same as `new Response()`.
|
||||||
|
|
@ -68,30 +64,34 @@ func newResponse*(body: cstring | FormData): Response {.importjs: "(new Response
|
||||||
func newRequest*(url: cstring): Request {.importjs: "(new Request(#))".}
|
func newRequest*(url: cstring): Request {.importjs: "(new Request(#))".}
|
||||||
## Constructor for `Request`. This does *not* call `fetch()`. Same as `new Request()`.
|
## Constructor for `Request`. This does *not* call `fetch()`. Same as `new Request()`.
|
||||||
|
|
||||||
|
func newRequest*(url: cstring; fetchOptions: FetchOptions): Request {.importjs: "(new Request(#, #))".}
|
||||||
|
## Constructor for `Request` with `fetchOptions`. Same as `fetch(url, fetchOptions)`.
|
||||||
|
|
||||||
func clone*(self: Response | Request): Response {.importjs: "#.$1()".}
|
func clone*(self: Response | Request): Response {.importjs: "#.$1()".}
|
||||||
## https://developer.mozilla.org/en-US/docs/Web/API/Response/clone
|
## https://developer.mozilla.org/en-US/docs/Web/API/Response/clone
|
||||||
|
|
||||||
proc text*(self: Response): Future[cstring] {.importjs: "#.$1()".}
|
proc text*(self: Response): Future[cstring] {.importjs: "#.$1()".}
|
||||||
## https://developer.mozilla.org/en-US/docs/Web/API/Body/text
|
## https://developer.mozilla.org/en-US/docs/Web/API/Response/text
|
||||||
|
|
||||||
proc json*(self: Response): Future[JsObject] {.importjs: "#.$1()".}
|
proc json*(self: Response): Future[JsObject] {.importjs: "#.$1()".}
|
||||||
## https://developer.mozilla.org/en-US/docs/Web/API/Body/json
|
## https://developer.mozilla.org/en-US/docs/Web/API/Response/json
|
||||||
|
|
||||||
proc formData*(self: Body): Future[FormData] {.importjs: "#.$1()".}
|
proc formData*(self: Response): Future[FormData] {.importjs: "#.$1()".}
|
||||||
## https://developer.mozilla.org/en-US/docs/Web/API/Body/formData
|
## https://developer.mozilla.org/en-US/docs/Web/API/Response/formData
|
||||||
|
|
||||||
proc unsafeNewFetchOptions*(metod, body, mode, credentials, cache, referrerPolicy: cstring;
|
proc unsafeNewFetchOptions*(metod, body, mode, credentials, cache, referrerPolicy: cstring;
|
||||||
keepalive: bool; redirect = "follow".cstring; referrer = "client".cstring; integrity = "".cstring): FetchOptions {.importjs:
|
keepalive: bool; redirect = "follow".cstring; referrer = "client".cstring; integrity = "".cstring; headers: Headers = newHeaders()): FetchOptions {.importjs:
|
||||||
"{method: #, body: #, mode: #, credentials: #, cache: #, referrerPolicy: #, keepalive: #, redirect: #, referrer: #, integrity: #}".}
|
"{method: #, body: #, mode: #, credentials: #, cache: #, referrerPolicy: #, keepalive: #, redirect: #, referrer: #, integrity: #, headers: #}".}
|
||||||
## .. warning:: Unsafe `newfetchOptions`.
|
## .. warning:: Unsafe `newfetchOptions`.
|
||||||
|
|
||||||
func newfetchOptions*(metod: HttpMethod; body: cstring;
|
func newfetchOptions*(metod: HttpMethod; body: cstring;
|
||||||
mode: FetchModes; credentials: FetchCredentials; cache: FetchCaches; referrerPolicy: FetchReferrerPolicies;
|
mode: FetchModes; credentials: FetchCredentials; cache: FetchCaches; referrerPolicy: FetchReferrerPolicies;
|
||||||
keepalive: bool; redirect = frFollow; referrer = "client".cstring; integrity = "".cstring): FetchOptions =
|
keepalive: bool; redirect = frFollow; referrer = "client".cstring; integrity = "".cstring,
|
||||||
|
headers: Headers = newHeaders()): FetchOptions =
|
||||||
## Constructor for `FetchOptions`.
|
## Constructor for `FetchOptions`.
|
||||||
result = FetchOptions(
|
result = FetchOptions(
|
||||||
body: body, mode: $mode, credentials: $credentials, cache: $cache, referrerPolicy: $referrerPolicy,
|
body: body, mode: cstring($mode), credentials: cstring($credentials), cache: cstring($cache), referrerPolicy: cstring($referrerPolicy),
|
||||||
keepalive: keepalive, redirect: $redirect, referrer: referrer, integrity: integrity,
|
keepalive: keepalive, redirect: cstring($redirect), referrer: referrer, integrity: integrity,
|
||||||
metod: (case metod
|
metod: (case metod
|
||||||
of HttpHead: "HEAD".cstring
|
of HttpHead: "HEAD".cstring
|
||||||
of HttpGet: "GET".cstring
|
of HttpGet: "GET".cstring
|
||||||
|
|
@ -109,9 +109,9 @@ proc fetch*(url: cstring | Request): Future[Response] {.importjs: "$1(#)".}
|
||||||
proc fetch*(url: cstring | Request; options: FetchOptions): Future[Response] {.importjs: "$1(#, #)".}
|
proc fetch*(url: cstring | Request; options: FetchOptions): Future[Response] {.importjs: "$1(#, #)".}
|
||||||
## `fetch()` API that takes a `FetchOptions`, returns a `Future[Response]`.
|
## `fetch()` API that takes a `FetchOptions`, returns a `Future[Response]`.
|
||||||
|
|
||||||
func toCstring*(self: Request | Response | Body | FetchOptions): cstring {.importjs: "JSON.stringify(#)".}
|
func toCstring*(self: Request | Response | FetchOptions): cstring {.importjs: "JSON.stringify(#)".}
|
||||||
|
|
||||||
func `$`*(self: Request | Response | Body | FetchOptions): string = $toCstring(self)
|
func `$`*(self: Request | Response | FetchOptions): string = $toCstring(self)
|
||||||
|
|
||||||
|
|
||||||
runnableExamples("-r:off"):
|
runnableExamples("-r:off"):
|
||||||
|
|
@ -131,7 +131,8 @@ runnableExamples("-r:off"):
|
||||||
keepalive = false,
|
keepalive = false,
|
||||||
redirect = "follow".cstring,
|
redirect = "follow".cstring,
|
||||||
referrer = "client".cstring,
|
referrer = "client".cstring,
|
||||||
integrity = "".cstring
|
integrity = "".cstring,
|
||||||
|
headers = newHeaders()
|
||||||
)
|
)
|
||||||
assert options0.keepalive == false
|
assert options0.keepalive == false
|
||||||
assert options0.metod == "POST".cstring
|
assert options0.metod == "POST".cstring
|
||||||
|
|
@ -143,6 +144,7 @@ runnableExamples("-r:off"):
|
||||||
assert options0.redirect == "follow".cstring
|
assert options0.redirect == "follow".cstring
|
||||||
assert options0.referrer == "client".cstring
|
assert options0.referrer == "client".cstring
|
||||||
assert options0.integrity == "".cstring
|
assert options0.integrity == "".cstring
|
||||||
|
assert options0.headers.len == 0
|
||||||
|
|
||||||
block:
|
block:
|
||||||
let options1: FetchOptions = newFetchOptions(
|
let options1: FetchOptions = newFetchOptions(
|
||||||
|
|
@ -155,7 +157,8 @@ runnableExamples("-r:off"):
|
||||||
keepalive = false,
|
keepalive = false,
|
||||||
redirect = frFollow,
|
redirect = frFollow,
|
||||||
referrer = "client".cstring,
|
referrer = "client".cstring,
|
||||||
integrity = "".cstring
|
integrity = "".cstring,
|
||||||
|
headers = newHeaders()
|
||||||
)
|
)
|
||||||
assert options1.keepalive == false
|
assert options1.keepalive == false
|
||||||
assert options1.metod == $HttpPost
|
assert options1.metod == $HttpPost
|
||||||
|
|
@ -167,6 +170,7 @@ runnableExamples("-r:off"):
|
||||||
assert options1.redirect == $frFollow
|
assert options1.redirect == $frFollow
|
||||||
assert options1.referrer == "client".cstring
|
assert options1.referrer == "client".cstring
|
||||||
assert options1.integrity == "".cstring
|
assert options1.integrity == "".cstring
|
||||||
|
assert options1.headers.len == 0
|
||||||
|
|
||||||
block:
|
block:
|
||||||
let response: Response = newResponse(body = "-. .. --".cstring)
|
let response: Response = newResponse(body = "-. .. --".cstring)
|
||||||
|
|
@ -182,7 +186,7 @@ runnableExamples("-r:off"):
|
||||||
assert response.ok
|
assert response.ok
|
||||||
assert response.status == 200.cint
|
assert response.status == 200.cint
|
||||||
assert response.headers is Headers
|
assert response.headers is Headers
|
||||||
assert response.body is Body
|
assert response.body is cstring
|
||||||
|
|
||||||
discard example()
|
discard example()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue