feat: scraper registry + adapter interface
This commit is contained in:
19
src/lib/scrapers/index.ts
Normal file
19
src/lib/scrapers/index.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { detectShop, type Shop } from '@/lib/shops'
|
||||
import type { PriceScraper, ScrapeResult } from './types'
|
||||
|
||||
const registry = new Map<Shop, PriceScraper>()
|
||||
|
||||
export function registerScraper(scraper: PriceScraper) {
|
||||
registry.set(scraper.shop, scraper)
|
||||
}
|
||||
|
||||
export async function scrapeUrl(url: string): Promise<ScrapeResult> {
|
||||
const shop = detectShop(url)
|
||||
if (!shop) throw new Error(`Unsupported URL: ${url}`)
|
||||
const scraper = registry.get(shop)
|
||||
if (!scraper) throw new Error(`No scraper registered for shop: ${shop}`)
|
||||
return scraper.scrape(url)
|
||||
}
|
||||
|
||||
export function registerScraperForTest(s: PriceScraper) { registry.set(s.shop, s) }
|
||||
export function resetScrapersForTest() { registry.clear() }
|
||||
Reference in New Issue
Block a user