HotTRDealsBackend/services/productPreview.service.js
2026-02-09 21:47:55 +00:00

42 lines
1.2 KiB
JavaScript

const axios = require("axios")
const { requestProductPreviewOverRedis } = require("./redis/scraperRpc.service")
function buildScraperUrl(baseUrl, targetUrl) {
if (!baseUrl) throw new Error("SCRAPER_API_URL missing")
if (!targetUrl) throw new Error("url parametresi zorunlu")
const normalizedBase = String(baseUrl)
const encoded = encodeURIComponent(String(targetUrl))
if (normalizedBase.includes("{url}")) {
return normalizedBase.replace("{url}", encoded)
}
if (normalizedBase.endsWith("?") || normalizedBase.endsWith("&")) {
return `${normalizedBase}url=${encoded}`
}
return normalizedBase.endsWith("/")
? `${normalizedBase}${encoded}`
: `${normalizedBase}${encoded}`
}
async function getProductPreviewFromUrl(url) {
const transport = String(process.env.SCRAPER_TRANSPORT || "http")
.trim()
.toLowerCase()
if (transport === "redis") {
return requestProductPreviewOverRedis(url, { timeoutMs: 20000 })
}
const baseUrl = process.env.SCRAPER_API_URL
const scraperUrl = buildScraperUrl(baseUrl, url)
const { data } = await axios.get(scraperUrl, { timeout: 20000 })
if (data && typeof data === "object" && data.product) return data.product
return data
}
module.exports = { getProductPreviewFromUrl }