Implement RFC-391 (#18585)
This commit is contained in:
parent
cc08d5c2c6
commit
e65dc36ddd
3 changed files with 89 additions and 91 deletions
|
|
@ -1,121 +1,120 @@
|
||||||
## - 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. jsfetch module requires `-d:nimExperimentalJsfetch`
|
## .. 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.".}
|
||||||
|
|
||||||
when defined(nimExperimentalJsfetch):
|
import std/[asyncjs, jsheaders, jsformdata]
|
||||||
import std/[asyncjs, jsheaders, jsformdata]
|
from std/httpcore import HttpMethod
|
||||||
from std/httpcore import HttpMethod
|
from std/jsffi import JsObject
|
||||||
from std/jsffi import JsObject
|
|
||||||
|
|
||||||
type
|
type
|
||||||
FetchOptions* = ref object of JsRoot ## Options for Fetch API.
|
FetchOptions* = ref object of JsRoot ## Options for Fetch API.
|
||||||
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
|
||||||
|
|
||||||
FetchModes* = enum ## Mode options.
|
FetchModes* = enum ## Mode options.
|
||||||
fmCors = "cors"
|
fmCors = "cors"
|
||||||
fmNoCors = "no-cors"
|
fmNoCors = "no-cors"
|
||||||
fmSameOrigin = "same-origin"
|
fmSameOrigin = "same-origin"
|
||||||
|
|
||||||
FetchCredentials* = enum ## Credential options. See https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
|
FetchCredentials* = enum ## Credential options. See https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
|
||||||
fcInclude = "include"
|
fcInclude = "include"
|
||||||
fcSameOrigin = "same-origin"
|
fcSameOrigin = "same-origin"
|
||||||
fcOmit = "omit"
|
fcOmit = "omit"
|
||||||
|
|
||||||
FetchCaches* = enum ## https://developer.mozilla.org/docs/Web/API/Request/cache
|
FetchCaches* = enum ## https://developer.mozilla.org/docs/Web/API/Request/cache
|
||||||
fchDefault = "default"
|
fchDefault = "default"
|
||||||
fchNoStore = "no-store"
|
fchNoStore = "no-store"
|
||||||
fchReload = "reload"
|
fchReload = "reload"
|
||||||
fchNoCache = "no-cache"
|
fchNoCache = "no-cache"
|
||||||
fchForceCache = "force-cache"
|
fchForceCache = "force-cache"
|
||||||
|
|
||||||
FetchRedirects* = enum ## Redirects options.
|
FetchRedirects* = enum ## Redirects options.
|
||||||
frFollow = "follow"
|
frFollow = "follow"
|
||||||
frError = "error"
|
frError = "error"
|
||||||
frManual = "manual"
|
frManual = "manual"
|
||||||
|
|
||||||
FetchReferrerPolicies* = enum ## Referrer Policy options.
|
FetchReferrerPolicies* = enum ## Referrer Policy options.
|
||||||
frpNoReferrer = "no-referrer"
|
frpNoReferrer = "no-referrer"
|
||||||
frpNoReferrerWhenDowngrade = "no-referrer-when-downgrade"
|
frpNoReferrerWhenDowngrade = "no-referrer-when-downgrade"
|
||||||
frpOrigin = "origin"
|
frpOrigin = "origin"
|
||||||
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
|
Body* = ref object of JsRoot ## https://developer.mozilla.org/en-US/docs/Web/API/Body
|
||||||
bodyUsed*: bool
|
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*: Body
|
||||||
|
|
||||||
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
|
||||||
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*: Body
|
||||||
|
|
||||||
|
|
||||||
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()`.
|
||||||
|
|
||||||
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 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/Body/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/Body/json
|
||||||
|
|
||||||
proc formData*(self: Body): Future[FormData] {.importjs: "#.$1()".}
|
proc formData*(self: Body): Future[FormData] {.importjs: "#.$1()".}
|
||||||
## https://developer.mozilla.org/en-US/docs/Web/API/Body/formData
|
## https://developer.mozilla.org/en-US/docs/Web/API/Body/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): FetchOptions {.importjs:
|
||||||
"{method: #, body: #, mode: #, credentials: #, cache: #, referrerPolicy: #, keepalive: #, redirect: #, referrer: #, integrity: #}".}
|
"{method: #, body: #, mode: #, credentials: #, cache: #, referrerPolicy: #, keepalive: #, redirect: #, referrer: #, integrity: #}".}
|
||||||
## .. 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): FetchOptions =
|
||||||
## Constructor for `FetchOptions`.
|
## Constructor for `FetchOptions`.
|
||||||
result = FetchOptions(
|
result = FetchOptions(
|
||||||
body: body, mode: $mode, credentials: $credentials, cache: $cache, referrerPolicy: $referrerPolicy,
|
body: body, mode: $mode, credentials: $credentials, cache: $cache, referrerPolicy: $referrerPolicy,
|
||||||
keepalive: keepalive, redirect: $redirect, referrer: referrer, integrity: integrity,
|
keepalive: keepalive, redirect: $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
|
||||||
of HttpPost: "POST".cstring
|
of HttpPost: "POST".cstring
|
||||||
of HttpPut: "PUT".cstring
|
of HttpPut: "PUT".cstring
|
||||||
of HttpDelete: "DELETE".cstring
|
of HttpDelete: "DELETE".cstring
|
||||||
of HttpPatch: "PATCH".cstring
|
of HttpPatch: "PATCH".cstring
|
||||||
else: "GET".cstring
|
else: "GET".cstring
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
proc fetch*(url: cstring | Request): Future[Response] {.importjs: "$1(#)".}
|
proc fetch*(url: cstring | Request): Future[Response] {.importjs: "$1(#)".}
|
||||||
## `fetch()` API, simple `GET` only, returns a `Future[Response]`.
|
## `fetch()` API, simple `GET` only, returns a `Future[Response]`.
|
||||||
|
|
||||||
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 | Body | FetchOptions): cstring {.importjs: "JSON.stringify(#)".}
|
||||||
|
|
||||||
func `$`*(self: Request | Response | Body | FetchOptions): string = $toCstring(self)
|
func `$`*(self: Request | Response | Body | FetchOptions): string = $toCstring(self)
|
||||||
|
|
||||||
|
|
||||||
runnableExamples("-d:nimExperimentalJsfetch -r:off"):
|
runnableExamples("-r:off"):
|
||||||
import std/[asyncjs, jsconsole, jsheaders, jsformdata]
|
import std/[asyncjs, jsconsole, jsheaders, jsformdata]
|
||||||
from std/httpcore import HttpMethod
|
from std/httpcore import HttpMethod
|
||||||
from std/jsffi import JsObject
|
from std/jsffi import JsObject
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,6 @@ hint("Processing", off)
|
||||||
# experimental API's are enabled in testament, refs https://github.com/timotheecour/Nim/issues/575
|
# experimental API's are enabled in testament, refs https://github.com/timotheecour/Nim/issues/575
|
||||||
# sync with `kochdocs.docDefines` or refactor.
|
# sync with `kochdocs.docDefines` or refactor.
|
||||||
switch("define", "nimExperimentalAsyncjsThen")
|
switch("define", "nimExperimentalAsyncjsThen")
|
||||||
switch("define", "nimExperimentalJsfetch")
|
|
||||||
switch("define", "nimExperimentalLinenoiseExtra")
|
switch("define", "nimExperimentalLinenoiseExtra")
|
||||||
|
|
||||||
switch("define", "nimPreviewFloatRoundtrip")
|
switch("define", "nimPreviewFloatRoundtrip")
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ const
|
||||||
gaCode* = " --doc.googleAnalytics:UA-48159761-1"
|
gaCode* = " --doc.googleAnalytics:UA-48159761-1"
|
||||||
# errormax: subsequent errors are probably consequences of 1st one; a simple
|
# errormax: subsequent errors are probably consequences of 1st one; a simple
|
||||||
# bug could cause unlimited number of errors otherwise, hard to debug in CI.
|
# bug could cause unlimited number of errors otherwise, hard to debug in CI.
|
||||||
docDefines = "-d:nimExperimentalAsyncjsThen -d:nimExperimentalJsfetch -d:nimExperimentalLinenoiseExtra"
|
docDefines = "-d:nimExperimentalAsyncjsThen -d:nimExperimentalLinenoiseExtra"
|
||||||
nimArgs = "--errormax:3 --hint:Conf:off --hint:Path:off --hint:Processing:off --hint:XDeclaredButNotUsed:off --warning:UnusedImport:off -d:boot --putenv:nimversion=$# $#" % [system.NimVersion, docDefines]
|
nimArgs = "--errormax:3 --hint:Conf:off --hint:Path:off --hint:Processing:off --hint:XDeclaredButNotUsed:off --warning:UnusedImport:off -d:boot --putenv:nimversion=$# $#" % [system.NimVersion, docDefines]
|
||||||
gitUrl = "https://github.com/nim-lang/Nim"
|
gitUrl = "https://github.com/nim-lang/Nim"
|
||||||
docHtmlOutput = "doc/html"
|
docHtmlOutput = "doc/html"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue