File size: 413 Bytes
6aaf221
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
import { fetch } from 'undici';     // Node ≥18 has global fetch; otherwise add undici

export async function fetchAndEncode(url: string) {
  const res = await fetch(url);
  if (!res.ok) throw new Error(`Failed to fetch image: ${url}`);
  const buf = Buffer.from(await res.arrayBuffer());
  const mimeType = res.headers.get('content-type') || 'image/png';
  return { mimeType, data: buf.toString('base64') };
}