about summary refs log tree commit diff stats
path: root/src/types
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-09-13 20:44:55 +0200
committerbptato <nincsnevem662@gmail.com>2022-09-13 20:44:55 +0200
commit51d83224320b8bd4e81332802bb62158ae6deec5 (patch)
tree4bfb320d7960f78a68de857f26d43ff2f59bea57 /src/types
parent51ea622d58bfca19212fac1800cfb033bb85ec39 (diff)
downloadchawan-51d83224320b8bd4e81332802bb62158ae6deec5.tar.gz
More JS bindings
Diffstat (limited to 'src/types')
-rw-r--r--src/types/url.nim49
1 files changed, 42 insertions, 7 deletions
diff --git a/src/types/url.nim b/src/types/url.nim
index 4e258b22..ef5c32ae 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -911,20 +911,57 @@ proc newURLSearchParams*[T: seq[(string, string)]|Table[string, string]|string](
 proc `$`*(params: URLSearchParams): string {.jsfunc.} =
   return serializeApplicationXWWWFormUrlEncoded(params.list)
 
+proc update(params: URLSearchParams) =
+  if params.url.isnone:
+    return
+  let serializedQuery = $params
+  if serializedQuery == "":
+    params.url.get.query = none(string)
+  else:
+    params.url.get.query = some(serializedQuery)
+
+proc append*(params: URLSearchParams, name: string, value: string) {.jsfunc.} =
+  params.list.add((name, value))
+  params.update()
+
+proc delete*(params: URLSearchParams, name: string) {.jsfunc.} =
+  for i in countdown(params.list.high, 0):
+    if params.list[i][0] == name:
+      params.list.delete(i)
+
+proc get*(params: URLSearchParams, name: string): Option[string] {.jsfunc.} =
+  for it in params.list:
+    if it[0] == name:
+      return some(it[1])
+
+proc getAll*(params: URLSearchParams, name: string): seq[string] {.jsfunc.} =
+  for it in params.list:
+    if it[0] == name:
+      result.add(it[1])
+
+proc set*(params: URLSearchParams, name: string, value: string) {.jsfunc.} =
+  var first = true
+  for i in 0..params.list.high:
+    if params.list[i][0] == name:
+      if first:
+        first = false
+        params.list[i][1] = value
+
 #TODO add Option wrapper
 proc newURL*(s: string, base: Option[string] = none(string)): URL {.jserr, jsctor.} =
   if base.issome:
     let baseUrl = parseUrl(base.get)
     if baseUrl.isnone:
-      JS_THROW TypeError, base.get & " is not a valid URL"
+      JS_THROW JS_TypeError, base.get & " is not a valid URL"
     let url = parseUrl(s, baseUrl)
     if url.isnone:
-      JS_THROW TypeError, s & " is not a valid URL"
+      JS_THROW JS_TypeError, s & " is not a valid URL"
     return url.get
   let url = parseUrl(s)
   if url.isnone:
-    JS_THROW TypeError, s & " is not a valid URL"
+    JS_THROW JS_TypeError, s & " is not a valid URL"
   url.get.searchParams = newURLSearchParams()
+  url.get.searchParams.url = url
   url.get.searchParams.initURLSearchParams(url.get.query.get(""))
   return url.get
 
@@ -1030,7 +1067,5 @@ proc hash*(url: URL, s: string) {.jsset.} =
   discard basicParseUrl(s, url = url, stateOverride = some(FRAGMENT_STATE))
 
 proc addUrlModule*(ctx: JSContext) =
-  let global = ctx.getGlobalObject()
-  discard ctx.registerType(URL, addto = some(global))
-  discard ctx.registerType(URLSearchParams, addto = some(global))
-  free(global)
+  ctx.registerType(URL)
+  ctx.registerType(URLSearchParams)