JavaScript Class that creates form data from complex objects.
npm install object-2-formdata> Form Data Class For Transforming complex Objects into Form Data
With O2F, you can:
- Create FormData from Complex Data Structures for use in HTTP Methods.
- Convert Files nested within Objects/Arrays
- POST/PATCH Objects of any size
O2F is an Object Class that recursively traverses Objects neatly into FormData in such a way that they are exactly represented when parsed on the backend. This includes: Files, Arrays, Objects, & Primitive Data-Types.
``sh`NPM
npm install object-2-formdata --save
`js`
import { ObjectToForm } from "object-2-formdata";
##### Front-End
`js`
const form = new ObjectToForm(
#### Object:
- Required
- Object to parse into Form Data
#### String:
- Not Required
- Base String eg.('payload')
#### Back-End:
`js`
var payload = serializeObject(JSON.parse(JSON.stringify(payload)));
`json
payload: {
metadata: { createdBy: 'test-admin', uploaded: '2022-08-27T02:06:33.678Z' },
id: '519dc76b-3b2f-47cd-9958-8518066318aa',
title: 'Test',
cover: {
contentsUrl: 'https://www.test.com'
},
volumes: 1,
summary: 'Some Summary',
authors: [ 'Howardbleu' ],
themes: [ 'Combat Sports' ],
genre: [ 'Award Winning' ],
status: 'Publishing',
chapters: [
{ volume: 0, number: 0, releaseDate: '2022-09-06T00:00:00Z' },
{ volume: 0, number: 0, releaseDate: '2022-09-21T00:00:00Z' }
],
__v: 0
}
files: [
{
fieldname: 'patch[chapters][0][file]',
originalname: 'Test1.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
buffer:
size: 3589096
},
{
fieldname: 'patch[chapters][1][file]',
originalname: 'Test2.png',
encoding: '7bit',
mimetype: 'image/png',
buffer:
size: 3382098
}
]
``
MIT