@alessiofrittoli/next-api
Version:
Next.js API utility library
1 lines • 861 B
JavaScript
var webUtils=require('@alessiofrittoli/web-utils');var d=async(e,n=false)=>{try{return n?await e.clone().json():await e.json()}catch{return null}},m=async(e,n=false,t)=>{let r=await(n?e.clone():e).formData(),o=webUtils.getTypedMap();return Array.from(r.entries()).map(([a,i],R,l)=>{if(o.has(a))return;let u=l.filter(([s])=>s===a),p=u.length<=1?i:u.map(([,s])=>s);if(t)return o.set(a,t(a,p));o.set(a,p);}),o},y=async(e,n=false)=>Array.from((await(n?e.clone():e).formData()).entries()).map(([,t])=>t).filter(t=>t instanceof File);var w=async e=>{let n=e?.headers||await(await import('next/headers')).headers(),t=n.get("X-Forwarded-For")?.replace(/\s/g,"").split(",").at(-1),r=n.get("X-Real-Ip")?.replace(/\s/g,"").split(",").at(-1);return t||r||null};exports.getRequestFiles=y;exports.getRequestIp=w;exports.readFormDataBody=m;exports.readJsonBody=d;
;