Open source candidates with configured browser
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
||||
loadProductByEan,
|
||||
loadProductInfo,
|
||||
getProductPictures,
|
||||
openProductSource,
|
||||
} from "./services/catalog-products.ts";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
@@ -122,6 +123,18 @@ const server = http.createServer(async (request, response) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/catalog-maker/open-source") {
|
||||
await sendJson(
|
||||
response,
|
||||
await openProductSource(requestConfig, {
|
||||
manufacturerId: url.searchParams.get("manufacturerId"),
|
||||
sourceKey: url.searchParams.get("sourceKey"),
|
||||
url: url.searchParams.get("url"),
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/catalog-maker/product-info") {
|
||||
await sendJson(
|
||||
response,
|
||||
|
||||
@@ -240,6 +240,31 @@ export async function getProductPictures(
|
||||
};
|
||||
}
|
||||
|
||||
export async function openProductSource(config, { manufacturerId, sourceKey, url }) {
|
||||
const sourceUrl = normalizeSourceUrl(url);
|
||||
const mapping = await listMappingSources(config, { manufacturerId });
|
||||
const source = mapping.items.find(
|
||||
(item) => item.enabled && String(item.key ?? "").toLowerCase() === String(sourceKey ?? "").toLowerCase(),
|
||||
);
|
||||
|
||||
if (!source) {
|
||||
throw new Error("Source mapping was not found for selected manufacturer.");
|
||||
}
|
||||
|
||||
await openInBrowser(sourceUrl, { engine: source.browserEngine });
|
||||
|
||||
return {
|
||||
configured: mapping.configured,
|
||||
opened: true,
|
||||
source: {
|
||||
key: source.key,
|
||||
name: source.name,
|
||||
browserEngine: source.browserEngine,
|
||||
},
|
||||
url: sourceUrl,
|
||||
};
|
||||
}
|
||||
|
||||
async function findSourceCandidates(source, product, { includePictures = false } = {}) {
|
||||
const searchAttempts = buildAdapterSearchUrls(source, product);
|
||||
let lastSearchUrl = searchAttempts[0]?.url || normalizeSourceUrl(source.url);
|
||||
|
||||
Reference in New Issue
Block a user