/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { FutCoreService } from "../services/FutCoreService.ts"; export class FutUnifiedApi { private core: FutCoreService; constructor() { this.core = new FutCoreService(); } public close(): void { this.core.close(); } /** * Unified HTTP Request Handler for Bun HTTP server, Elysia, Express, or Next.js API routes. */ public async handleRequest(req: Request): Promise { const url = new URL(req.url); const pathname = url.pathname; // Set standard CORS headers const headers = { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization", }; if (req.method === "OPTIONS") { return new Response(null, { status: 204, headers }); } try { // Health check if (pathname === "/api/fut/health") { return new Response( JSON.stringify({ status: "OK", timestamp: Date.now(), service: "@aphrody/fut" }), { status: 200, headers }, ); } // 1. Universal URL Parser Endpoint if (pathname === "/api/fut/parse-url" && req.method === "POST") { const body = (await req.json()) as { url: string; livePriceConsole?: number; livePricePc?: number }; if (!body.url) { return new Response(JSON.stringify({ error: "Missing 'url' parameter" }), { status: 400, headers }); } const result = await this.core.parseAnyUrl(body.url, body); return new Response(JSON.stringify(result), { status: 200, headers }); } // 2. Player Analysis Endpoint if (pathname.startsWith("/api/fut/player/")) { const identifier = decodeURIComponent(pathname.replace("/api/fut/player/", "")); const player = await this.core.getPlayerAnalysis(identifier); return new Response(JSON.stringify(player), { status: 200, headers }); } // 3. SBC Solver Endpoint if (pathname === "/api/fut/sbc/solve" && req.method === "POST") { const body = (await req.json()) as { targetRating: number }; const solution = await this.core.getSbcSolution(body.targetRating || 85); return new Response(JSON.stringify(solution), { status: 200, headers }); } // 4. Fodder Index Endpoint if (pathname === "/api/fut/fodder") { const fodder = this.core.getFodderMarket(); return new Response(JSON.stringify(fodder), { status: 200, headers }); } // 5. Market Bargains Endpoint if (pathname === "/api/fut/bargains") { const position = url.searchParams.get("position") || "CM"; const bargains = await this.core.scanBargains({ position }); return new Response(JSON.stringify(bargains), { status: 200, headers }); } return new Response(JSON.stringify({ error: "Not Found", path: pathname }), { status: 404, headers }); } catch (error: unknown) { const msg = error instanceof Error ? error.message : String(error); return new Response(JSON.stringify({ error: msg }), { status: 500, headers }); } } }