Reform FormData to Object or Array format.
import { reform } from "https://deno.land/x/reformdata/mod.ts";
const form = new FormData();
form.append("multiple[]", "1");
form.append("multiple[]", "2");
form.append("multiple[]", "3");
form.append("multiple[]", "4");
reform(form); // { multiple: ["1", "2", "3", "4"] }
Install
npm install reformdata
import { reform } from "reformdata";
// Usage is as above :-)
const form = new FormData();
form.append("text", "plain text");
form.append("blob1", new Blob());
form.append("blob2", new Blob([], { type: "application/json" }), "data.json");
form.append("file", new File([], "data.json", { type: "application/json" }));
reform(form);
/*
{
text: "plain text",
blob1: File
blob2: File
file: File
}
*/
const form = new FormData();
form.append("multiple[]", "1");
form.append("multiple[]", "2");
form.append("multiple[]", "3");
form.append("multiple[]", "4");
reform(form); // { multiple: ["1", "2", "3", "4"] }
const form = new FormData();
form.append("multiple[0][]", "1");
form.append("multiple[0][]", "2");
form.append("multiple[1][]", "3");
form.append("multiple[1][]", "4");
reform(form), // { multiple: [["1", "2"], ["3", "4"]] }
const form = new FormData();
// dot notation
form.append("users[1].name", "wan3land");
form.append("users[0].id", "10");
// bracket notation
form.append("users[1][id]", "11");
form.append("users[0][name]", "wan2land");
reform(form);
/*
{
users: [
{ id: "10", name: "wan2land" },
{ id: "11", name: "wan3land" },
],
}
*/
import { serve } from "https://deno.land/std@0.161.0/http/server.ts";
import reform from "https://deno.land/x/reformdata/mod.ts";
serve(async (request) => {
if (request.method === "POST") {
const formData = reform(await request.formData());
// do something
}
return new Response(":D");
});