Skip to content
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

Add jsxmlhttprequest #57

Merged
merged 23 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7638e77
Update changelog
juancarlospaco Dec 14, 2020
e51da62
Add jsxmlhttprequest
juancarlospaco Dec 14, 2020
2a5d219
Copy stdlib and use a javascript dedicated folder
juancarlospaco Dec 14, 2020
5cb44a9
Copy stdlib and use a javascript dedicated folder
juancarlospaco Dec 14, 2020
e7d38c6
Fix doc
juancarlospaco Dec 14, 2020
7510b43
Fix doc
juancarlospaco Dec 14, 2020
79c6610
This should use testament instead :|
juancarlospaco Dec 14, 2020
ac03e82
Clean out
juancarlospaco Dec 14, 2020
2fbec44
Fixes inferred from other fixes
juancarlospaco Dec 19, 2020
d1348a8
Fixes inferred from other fixes
juancarlospaco Dec 19, 2020
48ec6f6
More Nim like naming
juancarlospaco Dec 21, 2020
cabedd4
Merge branch 'master' of https://github.com/nim-lang/fusion into js2
juancarlospaco Dec 30, 2020
aa2a0bc
remove export
juancarlospaco Dec 31, 2020
5a5b711
Merge branch 'master' of https://github.com/nim-lang/fusion into js2
juancarlospaco Jan 3, 2021
9419573
Fix
juancarlospaco Jan 3, 2021
1658426
add import
juancarlospaco Jan 4, 2021
6bfbfeb
simplify open proc
juancarlospaco Jan 4, 2021
9e5e5d6
Fixes
juancarlospaco Jan 4, 2021
68b0447
Fix doc, fix overload
juancarlospaco Jan 4, 2021
ef79b37
:O
juancarlospaco Jan 4, 2021
5c304ca
Merge branch 'master' of https://github.com/nim-lang/fusion into js2
juancarlospaco Jan 5, 2021
6ec40c5
Update src/fusion/js/jsxmlhttprequest.nim
juancarlospaco Jan 6, 2021
df6fdfd
Update src/fusion/js/jsxmlhttprequest.nim
juancarlospaco Jan 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Added `jsxmlserializer` module for [`XMLSerializer`](https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer) for the JavaScript target.


- Added `jsxmlhttprequest` module for [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) for the JavaScript target.

- Added `jsformdata` module for [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) for the JavaScript target.
- Added `jssets` module, Set for the JavaScript target
Expand Down
57 changes: 57 additions & 0 deletions src/fusion/js/jsxmlhttprequest.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## `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; metod, 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:
juancarlospaco marked this conversation as resolved.
Show resolved Hide resolved
from std/dom import Node
if defined(fusionJsXmlhttprequestTests):
let request: XMLHttpRequest = 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()
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()