src/nodejs/jsurl

Example:

import src/nodejs/jsurl
requireUrl()
block:
  let u: URL = newURL("https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash".cstring)
  doAssert u.toCstring is cstring
  doAssert u.len == 64
  # echo u.toJSON
block:
  let para: URLSearchParams = newURLSearchParams("?user=abc&query=xyz".cstring)
  doAssert para.toCstring is cstring
  para.add "key".cstring, "value".cstring
  para.sort()
  doAssert para.hasKey "key".cstring
  doAssert para.keys == @["key".cstring, "query", "user"]
  doAssert para.values == @["value".cstring, "xyz", "abc"]
  doAssert para.get("key".cstring) == "value".cstring
  doAssert para.getAll("key".cstring) == @["value".cstring]
  doAssert $para == "key=value&query=xyz&user=abc"
  para.delete "key".cstring
block:
  doAssert domainToASCII("中文.com".cstring) == "xn--fiq228c.com".cstring
  doAssert domainToUnicode("xn--fiq228c.com".cstring) == "中文.com".cstring
  when defined(linux):  ## May or may not be OS and platform dependent.
    doAssert fileURLToPath("file:///C:/path/".cstring) == "/C:/path/".cstring

Types

URL = ref object of JsRoot
  hash*, host*, hostname*, href*, origin*, password*, pathname*, port*,
  protocol*, search*, searchParams*, username*: cstring
https://nodejs.org/api/url.html#url_class_url

Procs

func `$`(self: URL or URLSearchParams): string
func add(self: URLSearchParams; name: cstring; value: cstring) {.
    importjs: "#.append(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_urlsearchparams_append_name_value
func delete(self: URLSearchParams; name: cstring) {.importjs: "#.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_urlsearchparams_delete_name
func domainToASCII(domain: cstring): cstring {.importjs: "url.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_url_domaintoascii_domain
func domainToUnicode(domain: cstring): cstring {.importjs: "url.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_url_domaintounicode_domain
func fileURLToPath(domain: cstring or URL): cstring {.importjs: "url.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_url_fileurltopath_url
func format(self: URL; options: JsObject): cstring {.importjs: "url.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_url_format_url_options
func get(self: URLSearchParams; name: cstring): cstring {.importjs: "#.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_urlsearchparams_get_name
func getAll(self: URLSearchParams; name: cstring): seq[cstring] {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_urlsearchparams_getall_name
func hasKey(self: URLSearchParams; name: cstring): bool {.importjs: "#.has(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_urlsearchparams_has_name
func importUrl() {.importjs: "import * as url from \'url\'@", ...raises: [],
                   tags: [].}
Alias for import * as module_name from 'module_name';. Must be called once before using the module
func keys(self: URLSearchParams): seq[cstring] {.importjs: "Array.from(#.$1())",
    ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_urlsearchparams_keys
func len(self: URL): int {....raises: [], tags: [].}
func newURL(input: cstring): URL {.importjs: "(new URL(#))", ...raises: [],
                                   tags: [].}
func newURL(input: cstring; base: cstring or URL): URL {.
    importjs: "(new URL(#, #))", ...raises: [], tags: [].}
func newURLSearchParams(input: cstring or JsObject or openArray[cstring]): URLSearchParams {.
    importjs: "(new URLSearchParams(#))", ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_new_urlsearchparams
func requireUrl() {.importjs: "const url = require(\'url\')@", ...raises: [],
                    tags: [].}
Alias for const module_name = require('module_name');. Must be called once before using the module
func sets(self: URLSearchParams; name: cstring; value: cstring) {.
    importjs: "#.set(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_urlsearchparams_set_name_value
func sort(self: URLSearchParams) {.importjs: "#.$1()", ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_urlsearchparams_sort
func toJSON(self: URL): cstring {.importjs: "#.toJSON()", ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_url_tojson
func urlToHttpOptions(self: URL): JsObject {.importjs: "url.$1(#)", ...raises: [],
    tags: [].}
https://nodejs.org/api/url.html#url_url_urltohttpoptions_url
func values(self: URLSearchParams): seq[cstring] {.
    importjs: "Array.from(#.$1())", ...raises: [], tags: [].}
https://nodejs.org/api/url.html#url_urlsearchparams_values