@tuentyfaiv/http
Version:
Library for http requests based on fetch
2 lines (1 loc) • 793 B
JavaScript
import{ContentType as e}from"./content.js";function t(t){const{body:n,headers:o}=t;if((o.get("Content-Type")??e.ApplicationJson)===e.ApplicationFormData){if(o.delete("Content-Type"),n instanceof FormData)return{body:n,headers:o};const e=new FormData,t=(t,n,o)=>{const r=o?`${t}[${o}]`:t;n instanceof File&&e.append(r,n,n.name),(n instanceof Blob||"string"==typeof n)&&e.append(r,n),"number"!=typeof n&&"boolean"!=typeof n||e.append(r,n.toString()),"object"==typeof n&&e.append(r,JSON.stringify(n))};return Object.entries(n).forEach((([e,n])=>{Array.isArray(n)?n.forEach(((n,o)=>{t(e,n,o)})):t(e,n)})),{body:e,headers:o}}if(n instanceof FormData){const e={};return n.forEach(((t,n)=>{e[n]=t})),{body:JSON.stringify(e),headers:o}}return{body:JSON.stringify(n),headers:o}}export{t as parseBody};