@maksims/form-multipart-encoder.js
v1.0.0
Published
Simple multipart form data encoding and decoding library.
Downloads
26
Readme
multipart-form-encoder.js
Simple multipart form data encoding and decoding library.
Documentation
The library supports encoding and decoding of follow types:
- primitives
- array of primitives
- simple objects.
This means all outside this types will be converted to string with toString, you should either
implement custom toString function for you complex object or first convert you complex object into simple object and then pass it into decode function.
First function formMultipartDecode convert simple object into FormData, in not Browser enviroment this function will throw a error!
import { formMultipartDecode } from "@maksims/form-multipart-encoder.js";
const formData = formMultipartDecode({
name: "maxmusterman",
age: 35,
hobbies: ["Programming"],
auth: {
password: "********"
}
});Second function formMultipartEncode convert flat javascript object into nested object. Since formData
can not represent deeply nested structures, the solution is to first decode the object into
form data, parse (with multer for example) the form data as a flat object and then encode this flat object into deep object.
import { formMultipartEncode } from "@maksims/form-multipart-encoder.js";
// Parse raw buffer
// <Buffer a4 24 45 53 23 55 54...>
// into
// { "a.b": 20, "a.c": 25, "a.d": 30 }
// then you can encode this into
// { a: { b: 20, c: 25, d: 30 }}
const body = formMultipartEncode(req.body);