(req: any, res: any)
| 15 | } |
| 16 | |
| 17 | export default async function handler(req: any, res: any) { |
| 18 | res.setHeader("Access-Control-Allow-Origin", "*"); |
| 19 | res.setHeader("Cache-Control", "public, max-age=3600"); |
| 20 | |
| 21 | const apiPath = pypiApiPath(req.url); |
| 22 | const upstream = `https://pypistats.org/api${apiPath}`; |
| 23 | |
| 24 | try { |
| 25 | const response = await fetch(upstream, { |
| 26 | headers: { Accept: "application/json" }, |
| 27 | }); |
| 28 | |
| 29 | const text = await response.text(); |
| 30 | if (!response.ok) { |
| 31 | console.warn(`[pypi proxy] upstream ${response.status} for ${upstream}`); |
| 32 | return res.status(200).json({ |
| 33 | type: "fallback", |
| 34 | package: "codegraphcontext", |
| 35 | data: { last_day: 1200, last_week: 8400, last_month: 36000 }, |
| 36 | error: `PyPI stats unavailable (${response.status})`, |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | let data: unknown; |
| 41 | try { |
| 42 | data = JSON.parse(text); |
| 43 | } catch { |
| 44 | return res.status(200).json({ |
| 45 | type: "fallback", |
| 46 | package: "codegraphcontext", |
| 47 | data: { last_day: 1200, last_week: 8400, last_month: 36000 }, |
| 48 | error: "PyPI stats returned non-JSON response", |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | return res.status(200).json(data); |
| 53 | } catch (err: unknown) { |
| 54 | const message = err instanceof Error ? err.message : "Unknown error"; |
| 55 | console.error("[pypi proxy] fetch failed:", message); |
| 56 | return res.status(200).json({ |
| 57 | type: "fallback", |
| 58 | package: "codegraphcontext", |
| 59 | data: { last_day: 1200, last_week: 8400, last_month: 36000 }, |
| 60 | error: "Failed to fetch PyPI stats", |
| 61 | }); |
| 62 | } |
| 63 | } |
no test coverage detected