export default { async fetch(request, env, ctx) { // Handle CORS preflight if (request.method === 'OPTIONS') { return new Response(null, { headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', }, }); } // Only allow POST requests if (request.method !== 'POST') { return new Response(JSON.stringify({ error: 'Method not allowed' }), { status: 405, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, }); } try { const { postUrl, apiKey } = await request.json(); if (!postUrl || !apiKey) { return new Response( JSON.stringify({ error: 'Missing postUrl or apiKey' }), { status: 400, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, } ); } // Extract post URN from LinkedIn URL // LinkedIn post URLs look like: https://www.linkedin.com/posts/username_activity-1234567890123456789-abcd const postUrnMatch = postUrl.match(/activity[:-](\d+)/); if (!postUrnMatch) { return new Response( JSON.stringify({ error: 'Invalid LinkedIn post URL format' }), { status: 400, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, } ); } const activityId = postUrnMatch[1]; const postUrn = `urn:li:activity:${activityId}`; // Call Lix API const lixUrl = `https://api.lix-it.com/v1/enrich/post?post_urn=${encodeURIComponent(postUrn)}`; const lixResponse = await fetch(lixUrl, { method: 'GET', headers: { 'Authorization': apiKey, 'Content-Type': 'application/json', }, }); if (!lixResponse.ok) { const errorText = await lixResponse.text(); return new Response( JSON.stringify({ error: `Lix API Error: ${lixResponse.status}`, details: errorText }), { status: lixResponse.status, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, } ); } const data = await lixResponse.json(); // DEBUG: Return the raw API response so we can see what Lix is actually returning return new Response(JSON.stringify({ debug: true, rawApiResponse: data, message: 'This is debug mode - showing raw Lix API response' }), { status: 200, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, }); } catch (error) { return new Response( JSON.stringify({ error: 'Internal server error', message: error.message }), { status: 500, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, } ); } }, };