nim-lang / fusion

Fusion is for now an idea about how to grow Nim's ecosystem without the pain points of more traditional approaches.
MIT License
128 stars 16 forks source link

Add jsform #58

Closed juancarlospaco closed 3 years ago

juancarlospaco commented 3 years ago

See https://github.com/nim-lang/Nim/pull/16339#issue-538769691 and https://github.com/nim-lang/Nim/pull/16339#issuecomment-744485709 and https://github.com/nim-lang/fusion/pull/57#issue-539766026 and https://github.com/nim-lang/fusion/pull/56#issue-539747803 and https://github.com/nim-lang/fusion/pull/55#issue-539726540 and https://github.com/nim-lang/fusion/pull/58#issue-539886249

timotheecour commented 3 years ago

how about:

func newFormData*(): FormData =
  when defined(nodejs):
    asm """
    var tmp = require("form-data")
    `result` = new tmp()
    """
  else:
    asm "`result` = new FormData()"

...

runnableExamples:
  from strutils import startsWith
  if defined(fusionJsFormdataTests):
    let data: FormData = newFormData()
    when defined(nodejs):
      data.add "key0", "value0"
      data.add "key1", "value1"
      var ret: cstring
      asm """`ret` = `data`.getHeaders()["content-type"]"""
      doAssert ($ret).startsWith """multipart/form-data; boundary=""", $ret
    else:
      data["key0".cstring] = "value0".cstring
      data.add("key1".cstring, "value1".cstring)
      data.delete("key1".cstring)
      doAssert data.hasKey("key0".cstring)
      doAssert data["key0".cstring] == "value0".cstring

tested with:

npm i form-data
nim doc -b:js --doccmd:'-d:fusionJsFormdataTests -d:nodejs' src/fusion/js/jsformdata.nim

note: there's no set in that nodejs package apparently, see https://github.com/form-data/form-data/issues/488

EDIT

ignore this comment, looks like require("form-data") is too different or at least this could be done in future work; IMO the better way would be to fix this: https://github.com/timotheecour/Nim/issues/197 (in future work)