pushduck
Version:
The fastest way to add file uploads to any web application. Enterprise security, edge-ready. Works with 16+ frameworks and 5+ storage providers. No heavy AWS SDK required.
1 lines • 3.16 kB
JavaScript
function e(e){return{GET:async t=>await e.GET(t),POST:async t=>await e.POST(t)}}function t(e){return async(t,i)=>{try{let a=t.method;if(!a||!(a in e))return i.status(405).json({error:`Method not allowed`});let o=n(t);await r(await e[a](o),i)}catch(e){console.error(`Pages Router Handler Error:`,e);let t=e instanceof Error?e.message:`Internal server error`;i.headersSent||i.status(500).json({success:!1,error:t})}}}function n(e){let t=`${e.headers[`x-forwarded-proto`]||`http`}://${e.headers.host||`localhost:3000`}${e.url}`,n=new Headers;Object.entries(e.headers).forEach(([e,t])=>{if(t){let r=Array.isArray(t)?t.join(`, `):t;n.set(e,r)}});let r;return(e.method===`POST`||e.method===`PUT`||e.method===`PATCH`)&&(r=typeof e.body==`string`?e.body:JSON.stringify(e.body)),new Request(t,{method:e.method||`GET`,headers:n,body:r})}async function r(e,t){t.status(e.status),e.headers.forEach((e,n)=>{t.setHeader(n,e)});let n=e.headers.get(`content-type`);if(n&&n.includes(`application/json`)){let n=await e.json();t.json(n)}else{let n=await e.text();t.send(n)}}function i(e){return async(t,n)=>{try{let r=t.method;if(!r||!(r in e))return n.status(405).json({error:`Method not allowed`});let i=a(t);await o(await e[r](i),n)}catch(e){console.error(`Express Handler Error:`,e);let t=e instanceof Error?e.message:`Internal server error`;n.headersSent||n.status(500).json({success:!1,error:t})}}}function a(e){let t=`${e.protocol||`http`}://${e.get(`host`)||`localhost:3000`}${e.originalUrl||e.url}`,n=new Headers;Object.entries(e.headers).forEach(([e,t])=>{if(t){let r=Array.isArray(t)?t.join(`, `):t;n.set(e,r)}});let r;return(e.method===`POST`||e.method===`PUT`||e.method===`PATCH`)&&(r=typeof e.body==`string`?e.body:JSON.stringify(e.body)),new Request(t,{method:e.method||`GET`,headers:n,body:r})}async function o(e,t){t.status(e.status),e.headers.forEach((e,n)=>{t.setHeader(n,e)});let n=e.headers.get(`content-type`);if(n&&n.includes(`application/json`)){let n=await e.json();t.json(n)}else{let n=await e.text();t.send(n)}}function s(e){return async(t,n)=>{try{let r=t.method;if(!r||!(r in e))return n.status(405).send({error:`Method not allowed`});let i=c(t);await l(await e[r](i),n)}catch(e){console.error(`Fastify Handler Error:`,e);let t=e instanceof Error?e.message:`Internal server error`;n.sent||n.status(500).send({success:!1,error:t})}}}function c(e){let t=`${e.protocol||`http`}://${e.headers.host||`localhost:3000`}${e.url}`,n=new Headers;Object.entries(e.headers).forEach(([e,t])=>{if(t){let r=Array.isArray(t)?t.join(`, `):t;n.set(e,r)}});let r;return(e.method===`POST`||e.method===`PUT`||e.method===`PATCH`)&&(r=typeof e.body==`string`?e.body:JSON.stringify(e.body)),new Request(t,{method:e.method||`GET`,headers:n,body:r})}async function l(e,t){t.status(e.status),e.headers.forEach((e,n)=>{t.header(n,e)});let n=e.headers.get(`content-type`);if(n&&n.includes(`application/json`)){let n=await e.json();t.send(n)}else{let n=await e.text();t.send(n)}}function u(e){return{method:[`GET`,`POST`],handler:s(e)}}exports.toExpressHandler=i,exports.toFastifyHandler=s,exports.toFastifyRouteHandler=u,exports.toNextJsHandler=e,exports.toNextJsPagesHandler=t;