UNPKG

@blazity/next-image-proxy

Version:

Next.js image proxy. Makes it possible to use dynamic domains in next/image component.

3 lines (2 loc) 1.07 kB
import e from"isomorphic-unfetch";import t,{Stream as r}from"stream";import s from"lodash.merge";import o from"user-agents";function a(a){const n=s({whitelistedPatterns:[],fallbackUrl:"",messages:{wrongFormat:"Image url not provided or has wrong format",notWhitelisted:"Provided image url is not whitelisted",imageFetchError:"Couldn't fetch the image"}},a);return async function(s,a){const m=s.query.imageUrl;if(!m||m&&Array.isArray(m))return void a.status(400).send({message:n.messages.wrongFormat});var c;if(c=m,!n.whitelistedPatterns.some(e=>c.match(e)))return void a.status(422).send({message:n.messages.notWhitelisted});const g=await async function(t){return await e(t,{headers:{"user-agent":(new o).toString()}}).then(e=>e.body)}(m);g?function(e,s,o){const a=new r.PassThrough;t.pipeline(s,a,t=>{if(t)return console.log(t),void i(e,o)}),a.pipe(e)}(a,g,n):i(a,n)}}function i(e,t){t.fallbackUrl.trim()?e.redirect(t.fallbackUrl):e.status(422).send({message:t.messages.imageFetchError})}export{a as withImageProxy}; //# sourceMappingURL=next-image-proxy.modern.js.map