std/jsheaders

  Source   Edit

Example: cmd: -r:off

import std/jsheaders

block:
  let header: Headers = newHeaders()
  header.add("key", "value")
  assert header.hasKey("key")
  assert header.keys() == @["key".cstring]
  assert header.values() == @["value".cstring]
  assert header["key"] == "value".cstring
  header["other"] = "another".cstring
  assert header["other"] == "another".cstring
  assert header.entries() == @[("key".cstring, "value".cstring), ("other".cstring, "another".cstring)]
  assert header.toCstring() == """[["key","value"],["other","another"]]""".cstring
  header.delete("other")
  assert header.entries() == @[("key".cstring, "value".cstring)]
  header.clear()
  assert header.entries() == @[]
  assert header.len == 0

block:
  let header: Headers = newHeaders()
  header.add("key", "a")
  header.add("key", "b")  ## Duplicated.
  header.add("key", "c")  ## Duplicated.
  assert header["key"] == "a, b, c".cstring
  header["key"] = "value".cstring
  assert header["key"] == "value".cstring

block:
  let header: Headers = newHeaders()
  header["key"] = "a"
  header["key"] = "b"  ## Overwrites.
  assert header["key"] == "b".cstring

Types

Headers = ref object of JsRoot
HTTP Headers API.   Source   Edit

Procs

func `$`(self: Headers): string {....raises: [], tags: [].}
  Source   Edit
func `[]=`(self: Headers; key: cstring; value: cstring) {.
    importjs: "#.set(#, #)", ...raises: [], tags: [].}
Do not allow duplicated keys, overwrites duplicated keys. https://developer.mozilla.org/en-US/docs/Web/API/Headers/set   Source   Edit
func `[]`(self: Headers; key: cstring): cstring {.importjs: "#.get(#)",
    ...raises: [], tags: [].}
Get all items with key from the headers, including duplicated values. https://developer.mozilla.org/en-US/docs/Web/API/Headers/get   Source   Edit
func add(self: Headers; key: cstring; value: cstring) {.
    importjs: "#.append(#, #)", ...raises: [], tags: [].}
Allows duplicated keys. https://developer.mozilla.org/en-US/docs/Web/API/Headers/append   Source   Edit
func clear(self: Headers) {.importjs: "(() => { const header = #; Array.from(header.keys()).forEach((key) => header.delete(key)) })()",
                            ...raises: [], tags: [].}
Convenience func to delete all items from Headers.   Source   Edit
func delete(self: Headers; key: cstring) {.importjs: "#.$1(#)", ...raises: [],
    tags: [].}
https://developer.mozilla.org/en-US/docs/Web/API/Headers/delete
Warning: Delete all items with key from the headers, including duplicated keys.
  Source   Edit
func entries(self: Headers): seq[tuple[key, value: cstring]] {.
    importjs: "Array.from(#.$1())", ...raises: [], tags: [].}
https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries   Source   Edit
func hasKey(self: Headers; key: cstring): bool {.importjs: "#.has(#)",
    ...raises: [], tags: [].}
https://developer.mozilla.org/en-US/docs/Web/API/Headers/has   Source   Edit
func keys(self: Headers): seq[cstring] {.importjs: "Array.from(#.$1())",
    ...raises: [], tags: [].}
https://developer.mozilla.org/en-US/docs/Web/API/Headers/keys   Source   Edit
func len(self: Headers): int {.importjs: "Array.from(#.entries()).length",
                               ...raises: [], tags: [].}
  Source   Edit
func newHeaders(): Headers {.importjs: "new Headers()", ...raises: [], tags: [].}
https://developer.mozilla.org/en-US/docs/Web/API/Headers   Source   Edit
func toCstring(self: Headers): cstring {.
    importjs: "JSON.stringify(Array.from(#.entries()))", ...raises: [], tags: [].}
Returns a cstring representation of Headers.   Source   Edit
func values(self: Headers): seq[cstring] {.importjs: "Array.from(#.$1())",
    ...raises: [], tags: [].}
https://developer.mozilla.org/en-US/docs/Web/API/Headers/values   Source   Edit