jsheaders.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ## - HTTP Headers for the JavaScript target: https://developer.mozilla.org/en-US/docs/Web/API/Headers
  2. when not defined(js):
  3. {.fatal: "Module jsheaders is designed to be used with the JavaScript backend.".}
  4. type Headers* = ref object of JsRoot ## HTTP Headers API.
  5. func newHeaders*(): Headers {.importjs: "new Headers()".}
  6. ## https://developer.mozilla.org/en-US/docs/Web/API/Headers
  7. func add*(self: Headers; key: cstring; value: cstring) {.importjs: "#.append(#, #)".}
  8. ## Allows duplicated keys.
  9. ## https://developer.mozilla.org/en-US/docs/Web/API/Headers/append
  10. func delete*(self: Headers; key: cstring) {.importjs: "#.$1(#)".}
  11. ## https://developer.mozilla.org/en-US/docs/Web/API/Headers/delete
  12. ##
  13. ## .. warning:: Delete *all* items with `key` from the headers, including duplicated keys.
  14. func hasKey*(self: Headers; key: cstring): bool {.importjs: "#.has(#)".}
  15. ## https://developer.mozilla.org/en-US/docs/Web/API/Headers/has
  16. func keys*(self: Headers): seq[cstring] {.importjs: "Array.from(#.$1())".}
  17. ## https://developer.mozilla.org/en-US/docs/Web/API/Headers/keys
  18. func values*(self: Headers): seq[cstring] {.importjs: "Array.from(#.$1())".}
  19. ## https://developer.mozilla.org/en-US/docs/Web/API/Headers/values
  20. func entries*(self: Headers): seq[tuple[key, value: cstring]] {.importjs: "Array.from(#.$1())".}
  21. ## https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries
  22. func `[]`*(self: Headers; key: cstring): cstring {.importjs: "#.get(#)".}
  23. ## Get *all* items with `key` from the headers, including duplicated values.
  24. ## https://developer.mozilla.org/en-US/docs/Web/API/Headers/get
  25. func `[]=`*(self: Headers; key: cstring; value: cstring) {.importjs: "#.set(#, #)".}
  26. ## Do *not* allow duplicated keys, overwrites duplicated keys.
  27. ## https://developer.mozilla.org/en-US/docs/Web/API/Headers/set
  28. func clear*(self: Headers) {.importjs:
  29. "(() => { const header = #; Array.from(header.keys()).forEach((key) => header.delete(key)) })()".}
  30. ## Convenience func to delete all items from `Headers`.
  31. func toCstring*(self: Headers): cstring {.importjs: "JSON.stringify(Array.from(#.entries()))".}
  32. ## Returns a `cstring` representation of `Headers`.
  33. func `$`*(self: Headers): string = $toCstring(self)
  34. func len*(self: Headers): int {.importjs: "Array.from(#.entries()).length".}
  35. runnableExamples("-r:off"):
  36. block:
  37. let header: Headers = newHeaders()
  38. header.add("key", "value")
  39. assert header.hasKey("key")
  40. assert header.keys() == @["key".cstring]
  41. assert header.values() == @["value".cstring]
  42. assert header["key"] == "value".cstring
  43. header["other"] = "another".cstring
  44. assert header["other"] == "another".cstring
  45. assert header.entries() == @[("key".cstring, "value".cstring), ("other".cstring, "another".cstring)]
  46. assert header.toCstring() == """[["key","value"],["other","another"]]""".cstring
  47. header.delete("other")
  48. assert header.entries() == @[("key".cstring, "value".cstring)]
  49. header.clear()
  50. assert header.entries() == @[]
  51. assert header.len == 0
  52. block:
  53. let header: Headers = newHeaders()
  54. header.add("key", "a")
  55. header.add("key", "b") ## Duplicated.
  56. header.add("key", "c") ## Duplicated.
  57. assert header["key"] == "a, b, c".cstring
  58. header["key"] = "value".cstring
  59. assert header["key"] == "value".cstring
  60. block:
  61. let header: Headers = newHeaders()
  62. header["key"] = "a"
  63. header["key"] = "b" ## Overwrites.
  64. assert header["key"] == "b".cstring