your personal website on atproto - mirror blento.app
at map 38 lines 1.0 kB view raw
1import { json } from '@sveltejs/kit'; 2import { getLinkPreview } from 'link-preview-js'; 3 4export async function GET({ url }) { 5 const link = url.searchParams.get('link'); 6 if (!link) { 7 return json({ error: 'No link provided' }, { status: 400 }); 8 } 9 10 try { 11 new URL(link); 12 } catch { 13 return json({ error: 'Link is not a valid url' }, { status: 400 }); 14 } 15 16 try { 17 const data = await getLinkPreview(link, { 18 followRedirects: `manual`, 19 handleRedirects: (baseURL: string, forwardedURL: string) => { 20 const urlObj = new URL(baseURL); 21 const forwardedURLObj = new URL(forwardedURL); 22 if ( 23 forwardedURLObj.hostname === urlObj.hostname || 24 forwardedURLObj.hostname === 'www.' + urlObj.hostname || 25 'www.' + forwardedURLObj.hostname === urlObj.hostname 26 ) { 27 return true; 28 } else { 29 return false; 30 } 31 } 32 }); 33 return json(data); 34 } catch (error) { 35 console.error('Error fetching link preview:', error); 36 return json({ error: 'Failed to fetch link preview' }, { status: 500 }); 37 } 38}