/** * Welcome to your Persian Poets API Worker! * * This script connects to your D1 database and serves the data * to your frontend application. * * How to use: * 1. Create a new Worker in your Cloudflare dashboard. * 2. Paste this code into the Worker's editor. * 3. Go to the Worker's Settings -> Variables -> D1 Database Bindings. * 4. Add a binding: * - Variable name: DB * - D1 Database: persianlove * 5. Save and Deploy the Worker. Note the URL of your deployed worker. */ export default { async fetch(request, env) { const { pathname } = new URL(request.url); const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', 'Content-Type': 'application/json', }; // Handle preflight requests for CORS if (request.method === 'OPTIONS') { return new Response(null, { headers: corsHeaders }); } try { if (pathname.startsWith('/api/poets')) { // Endpoint to get the list of all poets for the initial selector if (pathname === '/api/poets') { const { results } = await env.DB.prepare( 'SELECT id, name, dates, title, short_bio FROM Poets ORDER BY name' ).all(); return new Response(JSON.stringify(results), { headers: corsHeaders }); } // Endpoint to get all data for a single poet const poetId = pathname.split('/')[3]; if (poetId) { const poet_stmt = env.DB.prepare('SELECT * FROM Poets WHERE id = ?'); const translations_stmt = env.DB.prepare(` SELECT t.*, (SELECT json_group_array(json_object('title', al.title, 'url', al.url)) FROM ArchiveLinks al WHERE al.translation_id = t.id) as archiveLinks, (SELECT json_group_array(json_object('label', ul.label, 'url', ul.url)) FROM UserLinks ul WHERE ul.translation_id = t.id) as userLinks FROM Translations t WHERE t.poet_id = ? ORDER BY t.translator_name `); const manuscripts_stmt = env.DB.prepare('SELECT * FROM Manuscripts WHERE poet_id = ? ORDER BY date_written'); const editio_princeps_stmt = env.DB.prepare('SELECT * FROM EditioPrinceps WHERE poet_id = ? ORDER BY publication_year'); const auction_data_stmt = env.DB.prepare('SELECT * FROM AuctionData WHERE poet_id = ?'); const [ poet, translations, manuscripts, editioPrinceps, auctionData ] = await env.DB.batch([ poet_stmt.bind(poetId), translations_stmt.bind(poetId), manuscripts_stmt.bind(poetId), editio_princeps_stmt.bind(poetId), auction_data_stmt.bind(poetId) ]); const responseData = { poet: poet.results[0], translations: translations.results.map(t => ({ ...t, archiveLinks: t.archiveLinks ? JSON.parse(t.archiveLinks) : [], userLinks: t.userLinks ? JSON.parse(t.userLinks) : [] })), manuscripts: manuscripts.results, editioPrinceps: editioPrinceps.results, auctionData: auctionData.results }; return new Response(JSON.stringify(responseData), { headers: corsHeaders }); } } return new Response(JSON.stringify({ error: 'Not Found' }), { status: 404, headers: corsHeaders }); } catch (e) { console.error(e); return new Response(JSON.stringify({ error: e.message }), { status: 500, headers: corsHeaders }); } }, };