mirror of
https://github.com/WonderAliceUwU/OmniArr.git
synced 2026-11-20 11:35:56 +00:00
⭐️ feature: first app steps
This commit is contained in:
commit
7d9343ead3
51 files changed
+8226
No files matched your search
+89
@@ -0,0 +1,89 @@
|
||||
import type {
|
||||
AppConfig,
|
||||
AuthPayload,
|
||||
AuthResponse,
|
||||
DashboardResponse,
|
||||
DownloadItem,
|
||||
HubPageResponse,
|
||||
ServiceOptionsResponse,
|
||||
SessionResponse
|
||||
} from '../shared/contracts'
|
||||
|
||||
async function request<T>(input: string, init?: RequestInit): Promise<T> {
|
||||
let response: Response
|
||||
|
||||
try {
|
||||
response = await fetch(input, {
|
||||
...init,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(init?.headers ?? {}),
|
||||
},
|
||||
})
|
||||
} catch {
|
||||
throw new Error('Could not reach the OmniArr backend. Check that the server is running.')
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const payload = (await safeJson(response)) as { error?: string } | null
|
||||
throw new Error(payload?.error || `${response.status} ${response.statusText}`)
|
||||
}
|
||||
|
||||
if (response.status === 204) {
|
||||
return undefined as T
|
||||
}
|
||||
|
||||
return (await response.json()) as T
|
||||
}
|
||||
|
||||
async function safeJson(response: Response) {
|
||||
try {
|
||||
return await response.json()
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export const api = {
|
||||
getSession: () => request<SessionResponse>('/api/auth/session'),
|
||||
register: (payload: AuthPayload) =>
|
||||
request<AuthResponse>('/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
}),
|
||||
login: (payload: AuthPayload) =>
|
||||
request<AuthResponse>('/api/auth/login', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
}),
|
||||
logout: () =>
|
||||
request<AuthResponse>('/api/auth/logout', {
|
||||
method: 'POST',
|
||||
}),
|
||||
getSettings: () => request<AppConfig>('/api/settings'),
|
||||
saveSettings: (payload: AppConfig) =>
|
||||
request<{ config: AppConfig }>('/api/settings', {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(payload),
|
||||
}),
|
||||
getOptions: () => request<ServiceOptionsResponse>('/api/settings/options'),
|
||||
loadJellyfinUsers: (payload: { baseUrl: string; apiKey: string }) =>
|
||||
request<{ jellyfinUsers: ServiceOptionsResponse['jellyfinUsers'] }>('/api/settings/jellyfin-users', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
}),
|
||||
getDashboard: () => request<DashboardResponse>('/api/dashboard'),
|
||||
getHub: (page: HubPageResponse['page']) => request<HubPageResponse>(`/api/hubs/${page}`),
|
||||
getDownloads: () => request<DownloadItem[]>('/api/downloads'),
|
||||
requestMovie: (tmdbId: number) =>
|
||||
request<{ ok: boolean }>('/api/requests/movie', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ tmdbId }),
|
||||
}),
|
||||
requestSeries: (tmdbId: number) =>
|
||||
request<{ ok: boolean }>('/api/requests/series', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ tmdbId }),
|
||||
}),
|
||||
}
|
||||
Reference in new issue
Block a user