reformdata
v0.2.2
Published
Reform FormData to Object or Array format.
Downloads
35
Readme
reformdata (re:FormData)
Reform FormData to Object or Array format.
Usage
with Deno
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"] }
with Node.js & Browser
Install
npm install reformdata
import { reform } from "reformdata";
// Usage is as above :-)
Support File or Blob
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
}
*/
Array
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"] }
n-depth Array
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"]] }
Object
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" },
],
}
*/
Example with Deno
import { serve } from "https://deno.land/[email protected]/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");
});