's'
This commit is contained in:
@@ -69,15 +69,42 @@ export function saveYoutubeProChannels(channels: YoutubeProChannel[]): void {
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(channels));
|
||||
}
|
||||
|
||||
const YOUTUBE_PRO_SYNC_PATHS = ["/hub/youtube_pro_channels", "/youtube_pro_channels"] as const;
|
||||
|
||||
/** 从服务器拉多频道列表(依次尝试 /hub 与根路径,兼容反代未转发 /hub 的情况) */
|
||||
export async function fetchYoutubeProChannelsRemote(
|
||||
fetchJson: <T,>(path: string, init?: RequestInit) => Promise<T>,
|
||||
): Promise<YoutubeProChannel[]> {
|
||||
for (const path of YOUTUBE_PRO_SYNC_PATHS) {
|
||||
try {
|
||||
const data = await fetchJson<{ channels?: unknown }>(path);
|
||||
const fromServer = parseYoutubeProChannelList(data.channels);
|
||||
if (fromServer.length) return fromServer;
|
||||
} catch {
|
||||
/* try next */
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export async function pushYoutubeProChannelsRemote(
|
||||
fetchJson: <T,>(path: string, init?: RequestInit) => Promise<T>,
|
||||
channels: YoutubeProChannel[],
|
||||
): Promise<void> {
|
||||
await fetchJson("/hub/youtube_pro_channels", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ channels }),
|
||||
});
|
||||
let last: Error | null = null;
|
||||
for (const path of YOUTUBE_PRO_SYNC_PATHS) {
|
||||
try {
|
||||
await fetchJson(path, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ channels }),
|
||||
});
|
||||
return;
|
||||
} catch (e) {
|
||||
last = e instanceof Error ? e : new Error(String(e));
|
||||
}
|
||||
}
|
||||
throw last ?? new Error("youtube_pro_channels sync failed");
|
||||
}
|
||||
|
||||
export function newChannelId(): string {
|
||||
|
||||
Reference in New Issue
Block a user