api: reconfigered api handling
This commit is contained in:
@@ -16,7 +16,8 @@ export async function getAutoComplete(
|
||||
const url = `${DISCOVER_BASE}/autoComplete?q=${encodeURIComponent(
|
||||
query
|
||||
)}&limit=${limit}`;
|
||||
const res = await fetch(url, { signal });
|
||||
const apiKey = process.env.EXPO_PUBLIC_API_KEY;
|
||||
const res = await fetch(url, { signal, headers: { 'Content-Type': 'application/json', "X-API-Key": apiKey ?? "", } });
|
||||
if (!res.ok) throw new Error("AutoComplete failed " + res.status);
|
||||
const data: unknown = await res.json();
|
||||
if (!Array.isArray(data)) return [];
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
export type PersonHistorySeasonRaw = {
|
||||
seasonId: number;
|
||||
show: number;
|
||||
seasonNumber: number;
|
||||
startDate?: string;
|
||||
endDate?: string | null;
|
||||
seasonParticipants: any[];
|
||||
};
|
||||
|
||||
export type PersonHistoryEntry = {
|
||||
showId: number;
|
||||
seasonId: number;
|
||||
seasonNumber: number;
|
||||
partner?: {
|
||||
id: number;
|
||||
name: string;
|
||||
birthDate?: string;
|
||||
imageUri: string | null;
|
||||
} | null;
|
||||
};
|
||||
|
||||
const PERSON_API_BASE = "http://45.157.177.99:8080/persons";
|
||||
|
||||
export async function getPersonHistory(
|
||||
personId: number
|
||||
): Promise<PersonHistoryEntry[]> {
|
||||
const url = `${PERSON_API_BASE}/${personId}/history`;
|
||||
try {
|
||||
console.log("[getPersonHistory] Fetch:", url);
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) throw new Error("History fetch failed " + res.status);
|
||||
const data: unknown = await res.json();
|
||||
if (!Array.isArray(data)) {
|
||||
console.warn("History expected array, got:", data);
|
||||
return [];
|
||||
}
|
||||
return (data as PersonHistorySeasonRaw[]).map((season) => {
|
||||
const me = season.seasonParticipants.find(
|
||||
(p) => p.person.personId === personId
|
||||
);
|
||||
let partner: PersonHistoryEntry["partner"] = null;
|
||||
if (me?.partner?.person) {
|
||||
partner = {
|
||||
id: me.partner.person.personId,
|
||||
name: me.partner.person.name,
|
||||
imageUri: me.partner.person.imageUrl ?? null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
showId: season.show,
|
||||
seasonId: season.seasonId,
|
||||
seasonNumber: season.seasonNumber,
|
||||
partner,
|
||||
};
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("getPersonHistory error:", e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,8 @@ export async function getSearchResults(
|
||||
filteredTags.join(",")
|
||||
)}&limit=${limit}`;
|
||||
|
||||
const res = await fetch(url, { signal });
|
||||
const apiKey = process.env.EXPO_PUBLIC_API_KEY;
|
||||
const res = await fetch(url, { signal, headers: { 'Content-Type': 'application/json', "X-API-Key": apiKey ?? "", } });
|
||||
if (!res.ok) throw new Error("AutoComplete failed " + res.status);
|
||||
|
||||
const data: unknown = await res.json();
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
|
||||
export type RawSeasonParticipant = {
|
||||
id: { seasonId: number; personId: number };
|
||||
person: {
|
||||
personId: number;
|
||||
name: string;
|
||||
birthDate?: string;
|
||||
imageUrl?: string | null;
|
||||
partner?: {
|
||||
personId: number;
|
||||
name: string;
|
||||
birthDate: string;
|
||||
imageUrl: string | null;
|
||||
birthDate?: string;
|
||||
imageUrl?: string | null;
|
||||
};
|
||||
partner: unknown | null;
|
||||
};
|
||||
|
||||
export type RawSeason = {
|
||||
seasonId: number;
|
||||
show: number;
|
||||
seasonNumber: number;
|
||||
startDate?: string;
|
||||
endDate?: string | null;
|
||||
moderators: unknown[];
|
||||
seasonParticipants: RawSeasonParticipant[];
|
||||
show?: number;
|
||||
moderators?: unknown[];
|
||||
};
|
||||
|
||||
export type SeasonParticipant = {
|
||||
@@ -43,27 +46,37 @@ export async function getSeason(
|
||||
): Promise<Season | null> {
|
||||
const url = `${SEASON_BASE_URL}/${showId}/seasons/${seasonNumber}`;
|
||||
try {
|
||||
const apiKey = process.env.EXPO_PUBLIC_API_KEY;
|
||||
console.log("[getSeason] Fetch:", url);
|
||||
const res = await fetch(url);
|
||||
const res = await fetch(url, {
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"X-API-Key": apiKey ?? "",
|
||||
},
|
||||
});
|
||||
console.log("[getSeason] Status:", res.status);
|
||||
if (res.status === 404) return null;
|
||||
if (!res.ok) throw new Error("Season fetch failed " + res.status);
|
||||
|
||||
const raw: RawSeason = await res.json();
|
||||
const participants: SeasonParticipant[] = raw.seasonParticipants.map(
|
||||
(p) => ({
|
||||
id: p.person.personId,
|
||||
name: p.person.name,
|
||||
birthYear: p.person.birthDate
|
||||
? Number(p.person.birthDate.slice(0, 4))
|
||||
: undefined,
|
||||
imageUri:
|
||||
p.person.imageUrl ??
|
||||
`https://i.pravatar.cc/300?img=${Math.random() * 70}`,
|
||||
})
|
||||
);
|
||||
|
||||
const participants: SeasonParticipant[] = Array.isArray(
|
||||
raw.seasonParticipants
|
||||
)
|
||||
? raw.seasonParticipants.map((p) => ({
|
||||
id: p.personId,
|
||||
name: p.name,
|
||||
birthYear: p.birthDate ? Number(p.birthDate.slice(0, 4)) : undefined,
|
||||
imageUri:
|
||||
p.imageUrl ??
|
||||
`https://i.pravatar.cc/300?img=${Math.floor(Math.random() * 70) + 1}`,
|
||||
}))
|
||||
: [];
|
||||
|
||||
return {
|
||||
id: raw.seasonId,
|
||||
showId: raw.show,
|
||||
showId,
|
||||
seasonNumber: raw.seasonNumber,
|
||||
startDate: raw.startDate,
|
||||
endDate: raw.endDate,
|
||||
@@ -74,3 +87,4 @@ export async function getSeason(
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user