jsformdata.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ## - `FormData` for the JavaScript target: https://developer.mozilla.org/en-US/docs/Web/API/FormData
  2. when not defined(js):
  3. {.fatal: "Module jsformdata is designed to be used with the JavaScript backend.".}
  4. type FormData* = ref object of JsRoot ## FormData API.
  5. func newFormData*(): FormData {.importjs: "new FormData()".}
  6. func add*(self: FormData; name: cstring; value: SomeNumber | bool | cstring) {.importjs: "#.append(#, #)".}
  7. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
  8. ## Duplicate keys are allowed and order is preserved.
  9. func add*(self: FormData; name: cstring; value: SomeNumber | bool | cstring, filename: cstring) {.importjs: "#.append(#, #, #)".}
  10. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
  11. ## Duplicate keys are allowed and order is preserved.
  12. func delete*(self: FormData; name: cstring) {.importjs: "#.$1(#)".}
  13. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/delete
  14. ##
  15. ## .. warning:: Deletes *all items* with the same key name.
  16. func getAll*(self: FormData; name: cstring): seq[cstring] {.importjs: "#.$1(#)".}
  17. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/getAll
  18. func hasKey*(self: FormData; name: cstring): bool {.importjs: "#.has(#)".}
  19. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/has
  20. func keys*(self: FormData): seq[cstring] {.importjs: "Array.from(#.$1())".}
  21. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/keys
  22. func values*(self: FormData): seq[cstring] {.importjs: "Array.from(#.$1())".}
  23. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/values
  24. func pairs*(self: FormData): seq[tuple[key, val: cstring]] {.importjs: "Array.from(#.entries())".}
  25. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/entries
  26. func put*(self: FormData; name, value, filename: cstring) {.importjs: "#.set(#, #, #)".}
  27. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/set
  28. func `[]=`*(self: FormData; name, value: cstring) {.importjs: "#.set(#, #)".}
  29. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/set
  30. func `[]`*(self: FormData; name: cstring): cstring {.importjs: "#.get(#)".}
  31. ## https://developer.mozilla.org/en-US/docs/Web/API/FormData/get
  32. func clear*(self: FormData) {.importjs:
  33. "(() => { const frmdt = #; Array.from(frmdt.keys()).forEach((key) => frmdt.delete(key)) })()".}
  34. ## Convenience func to delete all items from `FormData`.
  35. func toCstring*(self: FormData): cstring {.importjs: "JSON.stringify(#)".}
  36. func `$`*(self: FormData): string = $toCstring(self)
  37. func len*(self: FormData): int {.importjs: "Array.from(#.entries()).length".}
  38. runnableExamples("-r:off"):
  39. let data: FormData = newFormData()
  40. data["key0"] = "value0".cstring
  41. data.add("key1".cstring, "value1".cstring)
  42. data.delete("key1")
  43. assert data.hasKey("key0")
  44. assert data["key0"] == "value0".cstring
  45. data.clear()
  46. assert data.len == 0