This commit is contained in:
StrangeD0s
2026-02-07 20:11:26 +01:00
parent 817cdc7906
commit 81a53e441e
15 changed files with 1069 additions and 86 deletions
+35
View File
@@ -0,0 +1,35 @@
import { SWAPIPlanet, SWAPIStarship, Planet, Starship } from "@/types/swapi";
const BASE_URL = "https://swapi.dev/api";
export async function fetchPlanets(): Promise<Planet[]> {
const res = await fetch(`${BASE_URL}/planets/`);
if (!res.ok) throw new Error("Galaxy data unavailable");
const data = await res.json();
return data.results.map(
(p: SWAPIPlanet): Planet => ({
...p,
id: p.url,
// Add random x/y coordinates for galaxy map
x: Math.floor(Math.random() * 80) + 10,
y: Math.floor(Math.random() * 70) + 15,
}),
);
}
export async function fetchStarships(): Promise<Starship[]> {
const res = await fetch(`${BASE_URL}/starships/`);
if (!res.ok) throw new Error("Imperial records unavailable");
const data = await res.json();
return data.results.map((s: SWAPIStarship): Starship => {
// Add difficulty score based on ship cost
const cost = parseInt(s.cost_in_credits);
let ds = 8;
if (isNaN(cost) || cost > 1000000) ds = 12;
else if (cost > 100000) ds = 10;
return { ...s, id: s.url, ds };
});
}