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.24 kB
JavaScript
function e(e){return{GET:async t=>{let n=await e.GET(t);return n},POST:async t=>{let n=await e.POST(t);return n}}}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),s=await e[a](o);await r(s,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`,n=e.headers.host||`localhost:3000`,r=`${t}://${n}${e.url}`,i=new Headers;Object.entries(e.headers).forEach(([e,t])=>{if(t){let n=Array.isArray(t)?t.join(`, `):t;i.set(e,n)}});let a;return(e.method===`POST`||e.method===`PUT`||e.method===`PATCH`)&&(a=typeof e.body==`string`?e.body:JSON.stringify(e.body)),new Request(r,{method:e.method||`GET`,headers:i,body:a})}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),s=await e[r](i);await o(s,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`,n=e.get(`host`)||`localhost:3000`,r=`${t}://${n}${e.originalUrl||e.url}`,i=new Headers;Object.entries(e.headers).forEach(([e,t])=>{if(t){let n=Array.isArray(t)?t.join(`, `):t;i.set(e,n)}});let a;return(e.method===`POST`||e.method===`PUT`||e.method===`PATCH`)&&(a=typeof e.body==`string`?e.body:JSON.stringify(e.body)),new Request(r,{method:e.method||`GET`,headers:i,body:a})}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),a=await e[r](i);await l(a,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`,n=e.headers.host||`localhost:3000`,r=`${t}://${n}${e.url}`,i=new Headers;Object.entries(e.headers).forEach(([e,t])=>{if(t){let n=Array.isArray(t)?t.join(`, `):t;i.set(e,n)}});let a;return(e.method===`POST`||e.method===`PUT`||e.method===`PATCH`)&&(a=typeof e.body==`string`?e.body:JSON.stringify(e.body)),new Request(r,{method:e.method||`GET`,headers:i,body:a})}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){let t=s(e);return{method:[`GET`,`POST`],handler:t}}exports.toExpressHandler=i,exports.toFastifyHandler=s,exports.toFastifyRouteHandler=u,exports.toNextJsHandler=e,exports.toNextJsPagesHandler=t;