Sifa professional network API (Fastify, AT Protocol, Jetstream) sifa.id/
at main 54 lines 1.7 kB view raw
1export function normalizeUrl(url: string): string { 2 let normalized = url.toLowerCase(); 3 normalized = normalized.replace(/^https?:\/\//, ''); 4 normalized = normalized.replace(/^www\./, ''); 5 normalized = normalized.replace(/\/+$/, ''); 6 return normalized; 7} 8 9const PLATFORM_EXTRACTORS: Record<string, (url: URL) => string | null> = { 10 github: (url) => { 11 const parts = url.pathname.split('/').filter(Boolean); 12 return parts[0] ?? null; 13 }, 14 linkedin: (url) => { 15 const parts = url.pathname.split('/').filter(Boolean); 16 if (parts[0] === 'in' && parts[1]) return parts[1]; 17 return null; 18 }, 19 twitter: (url) => { 20 const parts = url.pathname.split('/').filter(Boolean); 21 return parts[0] ?? null; 22 }, 23 instagram: (url) => { 24 const parts = url.pathname.split('/').filter(Boolean); 25 return parts[0] ?? null; 26 }, 27 youtube: (url) => { 28 const parts = url.pathname.split('/').filter(Boolean); 29 if (parts[0] === 'channel' && parts[1]) return parts[1]; 30 if (parts[0]?.startsWith('@')) return parts[0]; 31 return null; 32 }, 33 fediverse: (url) => { 34 const parts = url.pathname.split('/').filter(Boolean); 35 if (parts[0]?.startsWith('@')) return `${parts[0]}@${url.hostname}`; 36 return null; 37 }, 38}; 39 40export function extractPlatformUsername(platform: string, url: string): string | null { 41 const normalizedPlatform = platform.toLowerCase(); 42 const extractor = PLATFORM_EXTRACTORS[normalizedPlatform]; 43 if (!extractor) return null; 44 45 try { 46 let parsableUrl = url; 47 if (!/^https?:\/\//i.test(parsableUrl)) { 48 parsableUrl = `https://${parsableUrl}`; 49 } 50 return extractor(new URL(parsableUrl)); 51 } catch { 52 return null; 53 } 54}