const apiKey = process.env.WEBLINQ_API_KEY;
const base = 'https://api.weblinq.dev/v1';
export async function snapshotStore(storeUrl) {
// ① screenshot
const shot = post('/web/screenshot', {
url: storeUrl,
screenshotOptions: { fullPage: true },
});
// ② internal links
const links = post('/web/links', { url: storeUrl, includeExternal: false });
// ③ quick store schema
const schema = {
type: 'object',
properties: {
storeName: { type: 'string' },
totalProducts: { type: 'number' },
},
};
const meta = post('/web/extract-json', {
url: storeUrl,
response_format: { type: 'json_schema', json_schema: schema },
prompt: 'Store overview',
});
const [screenshot, linkData, storeInfo] = await Promise.all([
shot,
links,
meta,
]);
const productLinks = (linkData.data.links || [])
.filter((l) => /\/(product|item|p)\//.test(l.url))
.slice(0, 20);
return {
url: storeUrl,
screenshot: screenshot.data.permanentUrl,
storeInfo: storeInfo.data.extracted,
productLinks,
};
}
function post(p, b) {
return fetch(`${base}${p}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(b),
}).then((r) => r.json());
}