20 lines
702 B
TypeScript
20 lines
702 B
TypeScript
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() }
|