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': '*', }, } ); } // Call RapidAPI LinkedIn Scraper const rapidApiUrl = `https://linkedin-data-api.p.rapidapi.com/get-post?url=${encodeURIComponent(postUrl)}`; const response = await fetch(rapidApiUrl, { method: 'GET', headers: { 'X-RapidAPI-Key': apiKey, 'X-RapidAPI-Host': 'linkedin-data-api.p.rapidapi.com' } }); if (!response.ok) { const errorText = await response.text(); return new Response( JSON.stringify({ error: `RapidAPI Error: ${response.status}`, details: errorText }), { status: response.status, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, } ); } const data = await response.json(); // Check if API call was successful if (!data.success) { return new Response( JSON.stringify({ error: 'API returned unsuccessful response', details: data.message || 'Unknown error' }), { status: 400, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, } ); } // Return the data return new Response(JSON.stringify(data), { 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': '*', }, } ); } }, };