141 lines
4.5 KiB
TypeScript
141 lines
4.5 KiB
TypeScript
const API_BASE = "https://api.dongfeng-systems.org";
|
|
|
|
// ── Response types ───────────────────────────────────────────────────────────
|
|
interface UserInterestResponse {
|
|
items: string[];
|
|
}
|
|
|
|
interface SymbolCheckResponse {
|
|
found: boolean;
|
|
message: string;
|
|
}
|
|
|
|
interface AddInterestResponse {
|
|
success: boolean;
|
|
message: string;
|
|
}
|
|
|
|
// ── Fetch user symbols ───────────────────────────────────────────────────────
|
|
export async function getUserSymbols(user: string = "frontend"): Promise<string[]> {
|
|
const res = await fetch(
|
|
`${API_BASE}/api/user-interest/fetch?user=${encodeURIComponent(user)}&type=symbol`,
|
|
{ cache: "no-store" }
|
|
);
|
|
if (!res.ok) throw new Error("Failed to fetch user symbols");
|
|
const data: UserInterestResponse = await res.json();
|
|
return data.items;
|
|
}
|
|
|
|
// ── Fetch user topics ────────────────────────────────────────────────────────
|
|
export async function getUserTopics(user: string = "frontend"): Promise<string[]> {
|
|
const res = await fetch(
|
|
`${API_BASE}/api/user-interest/fetch?user=${encodeURIComponent(user)}&type=topic`,
|
|
{ cache: "no-store" }
|
|
);
|
|
if (!res.ok) throw new Error("Failed to fetch user topics");
|
|
const data: UserInterestResponse = await res.json();
|
|
return data.items;
|
|
}
|
|
|
|
// ── Check if symbol is valid ─────────────────────────────────────────────────
|
|
export async function checkSymbol(symbol: string): Promise<SymbolCheckResponse> {
|
|
const normalized = symbol.trim().toUpperCase();
|
|
|
|
if (!normalized) {
|
|
return {
|
|
found: false,
|
|
message: "Symbol cannot be empty",
|
|
};
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(
|
|
`${API_BASE}/api/market/symbol-check/${encodeURIComponent(normalized)}`,
|
|
{ cache: "no-store" }
|
|
);
|
|
|
|
const raw = await res.text();
|
|
|
|
let data: any = null;
|
|
try {
|
|
data = raw ? JSON.parse(raw) : null;
|
|
} catch {
|
|
data = null;
|
|
}
|
|
|
|
if (!res.ok) {
|
|
return {
|
|
found: false,
|
|
message:
|
|
data?.message ||
|
|
raw ||
|
|
"Unable to verify this symbol right now. Please try again.",
|
|
};
|
|
}
|
|
|
|
return {
|
|
found: Boolean(data?.found),
|
|
message:
|
|
data?.message ||
|
|
(data?.found ? "Symbol found." : "This symbol is invalid."),
|
|
};
|
|
} catch {
|
|
return {
|
|
found: false,
|
|
message: "Unable to verify this symbol right now. Please try again.",
|
|
};
|
|
}
|
|
}
|
|
|
|
// ── Delete a user interest (symbol or topic) ─────────────────────────────────
|
|
export async function deleteUserInterest(
|
|
type: "symbol" | "topic",
|
|
value: string,
|
|
user: string = "frontend"
|
|
): Promise<AddInterestResponse> {
|
|
const normalized = value.trim();
|
|
if (!normalized) throw new Error(`${type} cannot be empty`);
|
|
|
|
const body = {
|
|
user,
|
|
type,
|
|
value: type === "symbol" ? normalized.toUpperCase() : normalized,
|
|
};
|
|
|
|
const res = await fetch(
|
|
`${API_BASE}/api/user-interest/remove?user=${encodeURIComponent(user)}&type=${encodeURIComponent(type)}&value=${encodeURIComponent(
|
|
type === "symbol" ? normalized.toUpperCase() : normalized
|
|
)}`,
|
|
{ cache: "no-store" }
|
|
);
|
|
|
|
if (!res.ok) throw new Error("Failed to delete interest");
|
|
return res.json();
|
|
}
|
|
|
|
// ── Add a user interest (symbol or topic) ────────────────────────────────────
|
|
export async function addUserInterest(
|
|
type: "symbol" | "topic",
|
|
value: string,
|
|
user: string = "frontend"
|
|
): Promise<AddInterestResponse> {
|
|
const normalized = value.trim();
|
|
if (!normalized) throw new Error(`${type} cannot be empty`);
|
|
|
|
const body = {
|
|
user,
|
|
type,
|
|
value: type === "symbol" ? normalized.toUpperCase() : normalized,
|
|
};
|
|
|
|
const res = await fetch(
|
|
`${API_BASE}/api/user-interest/add?user=${encodeURIComponent(user)}&type=${encodeURIComponent(type)}&value=${encodeURIComponent(
|
|
type === "symbol" ? normalized.toUpperCase() : normalized
|
|
)}`,
|
|
{ cache: "no-store" }
|
|
);
|
|
|
|
if (!res.ok) throw new Error("Failed to add interest");
|
|
return res.json();
|
|
}
|