jsformdata.nim 3.0 KB

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