Files
PlexPlaylistSync/sample-front-end/services/api.ts
T
2025-11-28 03:00:02 +09:00

141 lines
5.3 KiB
TypeScript

import { Playlist, ServerType, ApiResponse, PlexServerConnection, PlexConnectionSettings, RegexReplacement, UiConfig, SyncSettings } from '../types';
const API_PREFIX = '/api';
const parseJson = async <T>(response: Response): Promise<ApiResponse<T>> => {
try {
const data = await response.json();
return data as ApiResponse<T>;
} catch (error) {
console.error('Failed to parse API response', error);
return { data: {} as T, status: 'error', message: 'Invalid response from server' };
}
};
const mapServerType = (type: ServerType) => type === ServerType.LOCAL ? 'local' : 'cloud';
export const apiService = {
getUiConfig: async (): Promise<ApiResponse<UiConfig>> => {
try {
const response = await fetch(`${API_PREFIX}/ui-config`);
const result = await parseJson<UiConfig>(response);
if (!response.ok || result.status !== 'success') {
return { data: { statusCheckIntervalSeconds: 60 }, status: 'error', message: result.message || '无法获取前端配置' };
}
return result;
} catch (error) {
return { data: { statusCheckIntervalSeconds: 60 }, status: 'error', message: '无法获取前端配置' };
}
},
getSettings: async (): Promise<ApiResponse<SyncSettings>> => {
try {
const response = await fetch(`${API_PREFIX}/settings`);
const result = await parseJson<SyncSettings>(response);
if (!response.ok || result.status !== 'success') {
return { data: { syncStrategy: 'LOCAL_OVERWRITE', regexRules: [] }, status: 'error', message: result.message || '无法获取设置' };
}
return result;
} catch (error) {
return { data: { syncStrategy: 'LOCAL_OVERWRITE', regexRules: [] }, status: 'error', message: '无法获取设置' };
}
},
saveStrategy: async (strategy: string): Promise<ApiResponse<SyncSettings>> => {
try {
const response = await fetch(`${API_PREFIX}/settings/strategy`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ strategy })
});
return await parseJson<SyncSettings>(response);
} catch (error) {
return { data: { syncStrategy: strategy, regexRules: [] }, status: 'error', message: '无法保存同步策略' };
}
},
getRegexRules: async (): Promise<ApiResponse<RegexReplacement[]>> => {
try {
const response = await fetch(`${API_PREFIX}/regex-rules`);
return await parseJson<RegexReplacement[]>(response);
} catch (error) {
return { data: [], status: 'error', message: '无法获取正则规则' };
}
},
saveRegexRules: async (rules: RegexReplacement[]): Promise<ApiResponse<RegexReplacement[]>> => {
try {
const response = await fetch(`${API_PREFIX}/regex-rules`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(rules)
});
return await parseJson<RegexReplacement[]>(response);
} catch (error) {
return { data: [], status: 'error', message: '无法保存正则规则' };
}
},
getPlaylists: async (serverType: ServerType): Promise<ApiResponse<Playlist[]>> => {
try {
const response = await fetch(`${API_PREFIX}/playlists/${mapServerType(serverType)}`);
const result = await parseJson<Playlist[]>(response);
if (!response.ok || result.status !== 'success') {
return { data: [], status: 'error', message: result.message || 'Failed to fetch playlists' };
}
return result;
} catch (error) {
console.error(`Error fetching ${serverType} playlists:`, error);
return { data: [], status: 'error', message: 'Failed to fetch playlists' };
}
},
getServerStatus: async (): Promise<ApiResponse<PlexServerConnection>> => {
try {
const response = await fetch(`${API_PREFIX}/server/status`);
const result = await parseJson<PlexServerConnection>(response);
if (!response.ok || result.status !== 'success') {
return { data: { isConnected: false }, status: 'error', message: result.message || 'Failed to connect to server' };
}
return result;
} catch (error) {
return {
data: { isConnected: false },
status: 'error',
message: 'Failed to connect to server'
};
}
},
connectToPlex: async (settings: PlexConnectionSettings): Promise<ApiResponse<{ token: string, serverInfo: PlexServerConnection }>> => {
try {
const response = await fetch(`${API_PREFIX}/server/connect`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(settings)
});
return await parseJson<{ token: string, serverInfo: PlexServerConnection }>(response);
} catch (error: any) {
return {
data: { token: '', serverInfo: { isConnected: false } },
status: 'error',
message: error?.message || 'Connection failed'
};
}
},
selectLibrary: async (library: string): Promise<ApiResponse<{ libraryName: string }>> => {
try {
const response = await fetch(`${API_PREFIX}/server/library`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ library })
});
return await parseJson<{ libraryName: string }>(response);
} catch (error) {
return { data: { libraryName: library }, status: 'error', message: '无法切换媒体库' };
}
}
};