-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add jshttpclient #17373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add jshttpclient #17373
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
2ffffaa
Add jshttpclient
juancarlospaco 4996be6
Add jshttpclient
juancarlospaco 3b51e02
Add jshttpclient
juancarlospaco 07773b9
Add jshttpclient
juancarlospaco ad901fc
Add jshttpclient
juancarlospaco 7d8ddcf
Add jshttpclient
juancarlospaco ebd885b
Add jshttpclient
juancarlospaco 3b6c838
Add jshttpclient
juancarlospaco 72a5a19
Add jshttpclient
juancarlospaco 776d73c
Add jshttpclient
juancarlospaco 3e67862
How is that path
juancarlospaco dc1866b
doc
juancarlospaco b53c403
doc
juancarlospaco 673d4f3
ReSync
juancarlospaco 1c5b97a
Update changelog.md
juancarlospaco c45deae
Merge branch 'devel' of https://github.com/nim-lang/Nim into jshttpcl…
juancarlospaco 37059c6
Typo
juancarlospaco c908664
Changelog
juancarlospaco 05990b4
Changelog
juancarlospaco ae2ae76
Merge branch 'devel' of https://github.com/nim-lang/Nim into jshttpcl…
juancarlospaco 86534fa
ReSync again
juancarlospaco 700a826
Merge branch 'devel' of https://github.com/nim-lang/Nim into jshttpcl…
juancarlospaco 0a0c6fe
update a runnable example
juancarlospaco 8e3a720
feedback
juancarlospaco 46783d7
Merge branch 'devel' of https://github.com/nim-lang/Nim into jshttpcl…
juancarlospaco a7064e3
Moar fixes
juancarlospaco a2f4ffc
Moar fixes
juancarlospaco e2d48a9
Moar fixes
juancarlospaco 437583c
Simplify
juancarlospaco 2ebfee5
https://github.com/nim-lang/Nim/pull/17373#discussion_r599280280
juancarlospaco 90292a4
Fix doc
juancarlospaco 691a320
Merge branch 'devel' of https://github.com/nim-lang/Nim into jshttpcl…
juancarlospaco 81dfeee
https://github.com/nim-lang/Nim/pull/17373#issuecomment-816516217
juancarlospaco bf0c2ce
https://github.com/nim-lang/Nim/pull/17373#issuecomment-816516217
juancarlospaco 7fb5957
https://github.com/nim-lang/Nim/pull/17373#issuecomment-816516217
juancarlospaco 1cf2696
Fix example
juancarlospaco daf311c
minor
juancarlospaco e17b274
minor
juancarlospaco ad7e60f
Merge branch 'devel' of https://github.com/nim-lang/Nim into jshttpcl…
juancarlospaco f64e467
Make examples work
juancarlospaco 9b4d6e4
Merge branch 'devel' of https://github.com/nim-lang/Nim into jshttpcl…
juancarlospaco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| ## `Async <asyncjs.html>`_ `HttpClient <httpclient.html>`_ for JavaScript implemented on top of `jsfetch <jsfetch.html>`_ | ||
| when not defined(js): | ||
| {.fatal: "Module jsasynchttpclient is designed to be used with the JavaScript backend.".} | ||
|
|
||
| import std/[jsfetch, asyncjs] | ||
| from std/uri import Uri | ||
|
|
||
| type JsAsyncHttpClient* = ref object of JsRoot | ||
|
|
||
| func newJsAsyncHttpClient*(): JsAsyncHttpClient = discard | ||
juancarlospaco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func fetchOptionsImpl(body: cstring; `method`: static[cstring]): FetchOptions = | ||
| unsafeNewFetchOptions(metod = `method`, body = body, mode = "cors".cstring, credentials = "include".cstring, | ||
| cache = "default".cstring, referrerPolicy = "unsafe-url".cstring, keepalive = false) | ||
|
|
||
| proc getContent*(self: JsAsyncHttpClient; url: Uri | string): Future[cstring] {.async.} = | ||
| text(await fetch(cstring($url))) | ||
|
|
||
| proc deleteContent*(self: JsAsyncHttpClient; url: Uri | string): Future[cstring] {.async.} = | ||
| text(await fetch(cstring($url), fetchOptionsImpl("".cstring, "DELETE".cstring))) | ||
|
|
||
| proc postContent*(self: JsAsyncHttpClient; url: Uri | string; body = ""): Future[cstring] {.async.} = | ||
| text(await fetch(cstring($url), fetchOptionsImpl(body.cstring, "POST".cstring))) | ||
|
|
||
| proc putContent*(self: JsAsyncHttpClient; url: Uri | string; body = ""): Future[cstring] {.async.} = | ||
| text(await fetch(cstring($url), fetchOptionsImpl(body.cstring, "PUT".cstring))) | ||
|
|
||
| proc patchContent*(self: JsAsyncHttpClient; url: Uri | string; body = ""): Future[cstring] {.async.} = | ||
| text(await fetch(cstring($url), fetchOptionsImpl(body.cstring, "PATCH".cstring))) | ||
|
|
||
| proc get*(self: JsAsyncHttpClient; url: Uri | string): Future[Response] {.async.} = | ||
| fetch(cstring($url)) | ||
|
|
||
| proc delete*(self: JsAsyncHttpClient; url: Uri | string): Future[Response] {.async.} = | ||
| fetch(cstring($url), fetchOptionsImpl("".cstring, "DELETE".cstring)) | ||
|
|
||
| proc post*(self: JsAsyncHttpClient; url: Uri | string; body = ""): Future[Response] {.async.} = | ||
| fetch(cstring($url), fetchOptionsImpl(body.cstring, "POST".cstring)) | ||
|
|
||
| proc put*(self: JsAsyncHttpClient; url: Uri | string; body = ""): Future[Response] {.async.} = | ||
| fetch(cstring($url), fetchOptionsImpl(body.cstring, "PUT".cstring)) | ||
|
|
||
| proc patch*(self: JsAsyncHttpClient; url: Uri | string; body = ""): Future[Response] {.async.} = | ||
| fetch(cstring($url), fetchOptionsImpl(body.cstring, "PATCH".cstring)) | ||
|
|
||
| proc head*(self: JsAsyncHttpClient; url: Uri | string): Future[Response] {.async.} = | ||
| fetch(cstring($url), fetchOptionsImpl("".cstring, "HEAD".cstring)) | ||
|
|
||
|
|
||
| runnableExamples("-d:nimExperimentalJsfetch -d:nimExperimentalAsyncjsThen -r:off"): | ||
| import std/[asyncjs, jsfetch, uri] | ||
|
|
||
| proc example(): Future[void] {.async.} = | ||
| let client = newJsAsyncHttpClient() | ||
| const data = """{"key": "value"}""" | ||
|
|
||
| block: | ||
| let url = parseUri("http://nim-lang.org") | ||
| let content = await client.getContent(url) | ||
| let response = await client.get(url) | ||
|
|
||
| block: | ||
| let url = parseUri("http://httpbin.org/delete") | ||
| let content = await client.deleteContent(url) | ||
| let response = await client.delete(url) | ||
|
|
||
| block: | ||
| let url = parseUri("http://httpbin.org/post") | ||
| let content = await client.postContent(url, data) | ||
| let response = await client.post(url, data) | ||
|
|
||
| block: | ||
| let url = parseUri("http://httpbin.org/put") | ||
| let content = await client.putContent(url, data) | ||
| let response = await client.put(url, data) | ||
|
|
||
| block: | ||
| let url = parseUri("http://httpbin.org/patch") | ||
| let content = await client.patchContent(url, data) | ||
| let response = await client.patch(url, data) | ||
|
|
||
| discard example() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ## HttpClient for JavaScript targets. | ||
| ## * Asyncronous `HttpClient` implemented on top of `jsfetch`. | ||
| ## * Syncronous `HttpClient` implemented on top of `jsxmlhttprequest`. | ||
| import std/js/[jsasynchttpclient, jssynchttpclient] | ||
| export jsasynchttpclient, jssynchttpclient |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| ## Synchronous `HttpClient <httpclient.html>`_ for JavaScript implemented on top of `jsxmlhttprequest <jsxmlhttprequest.html>`_ | ||
| when not defined(js): | ||
| {.fatal: "Module jssynchttpclient is designed to be used with the JavaScript backend.".} | ||
|
|
||
| import std/uri, std/js/jsxmlhttprequest | ||
| from std/uri import Uri | ||
|
|
||
| type JsHttpClient* = ref object of XMLHttpRequest | ||
|
|
||
| func newJsHttpClient*(): JsHttpClient = discard | ||
|
|
||
| proc xmlHttpRequestImpl(self: JsHttpClient; url: Uri | string; body: string; `method`: static[cstring]): cstring = | ||
| self.open(`method` = `method`, url = cstring($url), false) | ||
| self.send(body = body.cstring) | ||
| self.responseText | ||
|
|
||
| proc getContent*(self: JsHttpClient; url: Uri | string): cstring = | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that this API is completely new, should we only allow |
||
| xmlHttpRequestImpl(self, url, "", "GET".cstring) | ||
|
|
||
| proc deleteContent*(self: JsHttpClient; url: Uri | string): cstring = | ||
| xmlHttpRequestImpl(self, url, "", "DELETE".cstring) | ||
|
|
||
| proc postContent*(self: JsHttpClient; url: Uri | string; body = ""): cstring = | ||
| xmlHttpRequestImpl(self, url, body, "POST".cstring) | ||
|
|
||
| proc putContent*(self: JsHttpClient; url: Uri | string; body = ""): cstring = | ||
| xmlHttpRequestImpl(self, url, body, "PUT".cstring) | ||
|
|
||
| proc patchContent*(self: JsHttpClient; url: Uri | string; body = ""): cstring = | ||
| xmlHttpRequestImpl(self, url, body, "PATCH".cstring) | ||
|
|
||
| proc head*(self: JsHttpClient; url: Uri | string): cstring = | ||
| xmlHttpRequestImpl(self, url, "", "HEAD".cstring) | ||
|
|
||
|
|
||
| runnableExamples("-r:off"): | ||
| from std/uri import parseUri, Uri | ||
|
|
||
| let client = newJsHttpClient() | ||
| const data = """{"key": "value"}""" | ||
|
|
||
| block: | ||
| let url = parseUri("http://nim-lang.org") | ||
| let content = client.getContent(url) | ||
|
|
||
| block: | ||
| let url = parseUri("http://httpbin.org/delete") | ||
| let content = client.deleteContent(url) | ||
|
|
||
| block: | ||
| let url = parseUri("http://httpbin.org/post") | ||
| let content = client.postContent(url, data) | ||
|
|
||
| block: | ||
| let url = parseUri("http://httpbin.org/put") | ||
| let content = client.putContent(url, data) | ||
|
|
||
| block: | ||
| let url = parseUri("http://httpbin.org/patch") | ||
| let content = client.patchContent(url, data) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| ## `XMLHttpRequest` for the JavaScript target: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest | ||
| when not defined(js): | ||
| {.fatal: "Module jsxmlhttprequest is designed to be used with the JavaScript backend.".} | ||
|
|
||
| from std/dom import Node | ||
|
|
||
| type XMLHttpRequest* = ref object of JsRoot ## https://xhr.spec.whatwg.org | ||
| responseXML*: Node | ||
| withCredentials*: bool | ||
| status*, timeout*, readyState*: cint | ||
| responseText*, responseURL*, statusText*: cstring | ||
|
|
||
| func newXMLHttpRequest*(): XMLHttpRequest {.importjs: "new XMLHttpRequest()".} | ||
| ## Constructor for `XMLHttpRequest`. | ||
|
|
||
| func open*(this: XMLHttpRequest; `method`, url: cstring; async = true; user = cstring.default; password = cstring.default) {.importjs: "#.$1(#, #, #, #, #)".} | ||
| ## https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open | ||
|
|
||
| func send*(this: XMLHttpRequest; body: cstring | Node = cstring.default) {.importjs: "#.$1(#)".} | ||
| ## https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send | ||
|
|
||
| func abort*(this: XMLHttpRequest) {.importjs: "#.$1()".} | ||
| ## https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort | ||
|
|
||
| func getAllResponseHeaders*(this: XMLHttpRequest): cstring {.importjs: "#.$1()".} | ||
| ## https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders | ||
|
|
||
| func overrideMimeType*(this: XMLHttpRequest; mimeType: cstring) {.importjs: "#.$1(#)".} | ||
| ## https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/overrideMimeType | ||
|
|
||
| func setRequestHeader*(this: XMLHttpRequest; key, value: cstring) {.importjs: "#.$1(#, #)".} | ||
| ## https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader | ||
|
|
||
| func setRequestHeader*(this: XMLHttpRequest; keyValuePairs: openArray[tuple[key, val: cstring]]) {.importjs: | ||
| "(() => { const rqst = #; #.forEach((item) => rqst.$1(item[0], item[1])) })()".} | ||
| ## Same as `setRequestHeader` but takes `openArray[tuple[key, val: cstring]]`. | ||
|
|
||
|
|
||
| runnableExamples("-r:off"): | ||
| from std/dom import Node | ||
| let request = newXMLHttpRequest() | ||
| request.open("GET".cstring, "http://localhost:8000/".cstring, false) | ||
| request.setRequestHeader("mode".cstring, "no-cors".cstring) | ||
| request.setRequestHeader([(key: "mode".cstring, val: "no-cors".cstring)]) | ||
| request.overrideMimeType("text/plain".cstring) | ||
| request.send() | ||
| echo request.getAllResponseHeaders() | ||
juancarlospaco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| echo "responseText\t", request.responseText | ||
| echo "responseURL\t", request.responseURL | ||
| echo "statusText\t", request.statusText | ||
| echo "responseXML\t", request.responseXML is Node | ||
| echo "status\t", request.status | ||
| echo "timeout\t", request.timeout | ||
| echo "withCredentials\t", request.withCredentials | ||
| echo "readyState\t", request.readyState | ||
| request.abort() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.