diff options
author | bptato <nincsnevem662@gmail.com> | 2023-07-02 14:17:27 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-07-02 14:17:27 +0200 |
commit | 8430c0954798ee1632a933a9ca251be8302f6fb6 (patch) | |
tree | 5369327cf69c744bf418ec38cec2dcfd85d2993f /src/xhr/formdata.nim | |
parent | 341299a491183c7c99f98b0ed3ce13307ab20b17 (diff) | |
download | chawan-8430c0954798ee1632a933a9ca251be8302f6fb6.tar.gz |
Use or type for FormData.append
Diffstat (limited to 'src/xhr/formdata.nim')
-rw-r--r-- | src/xhr/formdata.nim | 38 |
1 files changed, 8 insertions, 30 deletions
diff --git a/src/xhr/formdata.nim b/src/xhr/formdata.nim index a1832274..b2656428 100644 --- a/src/xhr/formdata.nim +++ b/src/xhr/formdata.nim @@ -26,40 +26,18 @@ proc newFormData*(form: HTMLFormElement = nil, this.entries = constructEntryList(form, submitter).get(@[]) return ok(this) -#TODO as jsfunc -proc append*(this: FormData, name: string, svalue: string, filename = "") = - this.entries.add(FormDataEntry( - name: name, - isstr: true, - svalue: svalue, - filename: filename - )) - -proc append*(this: FormData, name: string, value: Blob, - filename = "blob") = - this.entries.add(FormDataEntry( - name: name, - isstr: false, - value: value, - filename: filename - )) - -#TODO hack -proc append(ctx: JSContext, this: FormData, name: string, value: JSValue, - filename = none(string)) {.jsfunc.} = - let blob = fromJS[Blob](ctx, value) - if blob.isSome: +proc append[T: string|Blob](ctx: JSContext, this: FormData, name: string, + value: T, filename = opt(string)) {.jsfunc.} = + when T is Blob: let filename = if filename.isSome: filename.get - elif blob.get of WebFile: - WebFile(blob.get).name + elif value of WebFile: + WebFile(value).name else: "blob" - this.append(name, blob.get, filename) - else: - let s = fromJS[string](ctx, value) - # toString should never fail (?) - this.append(name, s.get, filename.get("")) + this.append(name, value, filename) + else: # string + this.append(name, value, filename.get("")) proc delete(this: FormData, name: string) {.jsfunc.} = for i in countdown(this.entries.high, 0): |