Initial commit
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import type { ServerSettings, ServerPaths, ConfigInfo, Modlist, ModlistListItem, ModEntry } from '../types'
|
||||
|
||||
const BASE = '/api'
|
||||
|
||||
async function request<T>(path: string, options?: RequestInit): Promise<T> {
|
||||
const res = await fetch(`${BASE}${path}`, {
|
||||
headers: { 'Content-Type': 'application/json', ...options?.headers },
|
||||
...options,
|
||||
})
|
||||
if (res.status === 204) return undefined as T
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({ error: res.statusText }))
|
||||
throw new Error(err.error || res.statusText)
|
||||
}
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export const settingsApi = {
|
||||
get: () => request<ServerSettings>('/server/settings'),
|
||||
update: (data: Partial<ServerSettings>) => request<ServerSettings>('/server/settings', { method: 'PUT', body: JSON.stringify(data) }),
|
||||
start: () => request<{ status: string }>('/server/start', { method: 'POST' }),
|
||||
stop: () => request<{ status: string }>('/server/stop', { method: 'POST' }),
|
||||
restart: () => request<{ status: string }>('/server/restart', { method: 'POST' }),
|
||||
status: () => request<{ running: boolean }>('/server/status'),
|
||||
steamcmd: () => request<{ branch: string; user: string; platform: string }>('/server/steamcmd'),
|
||||
paths: () => request<ServerPaths>('/server/paths'),
|
||||
}
|
||||
|
||||
export const configsApi = {
|
||||
list: () => request<ConfigInfo[]>('/configs'),
|
||||
get: async (name: string) => {
|
||||
const r = await fetch(`${BASE}/configs/${name}`)
|
||||
if (!r.ok) {
|
||||
const err = await r.json().catch(() => ({ error: r.statusText }))
|
||||
throw new Error(err.error || r.statusText)
|
||||
}
|
||||
return r.text()
|
||||
},
|
||||
create: (name: string, content?: string) => request<{ name: string }>('/configs', { method: 'POST', body: JSON.stringify({ name, content: content || '' }) }),
|
||||
update: (name: string, content: string) => request<{ status: string }>(`/configs/${name}`, { method: 'PUT', body: JSON.stringify({ content }) }),
|
||||
delete: (name: string) => request<void>(`/configs/${name}`, { method: 'DELETE' }),
|
||||
duplicate: (name: string, newName: string) => request<{ name: string }>(`/configs/${name}/duplicate`, { method: 'POST', body: JSON.stringify({ new_name: newName }) }),
|
||||
}
|
||||
|
||||
export interface ModStatus {
|
||||
id: string
|
||||
name: string
|
||||
enabled: boolean
|
||||
downloaded: boolean
|
||||
}
|
||||
|
||||
export const modlistsApi = {
|
||||
list: () => request<ModlistListItem[]>('/modlists'),
|
||||
get: (id: string) => request<Modlist>(`/modlists/${id}`),
|
||||
create: (name: string) => request<Modlist>('/modlists', { method: 'POST', body: JSON.stringify({ name }) }),
|
||||
update: (id: string, data: { name: string; mods: ModEntry[] }) => request<Modlist>(`/modlists/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||
delete: (id: string) => request<void>(`/modlists/${id}`, { method: 'DELETE' }),
|
||||
duplicate: (id: string, name: string) => request<Modlist>(`/modlists/${id}/duplicate`, { method: 'POST', body: JSON.stringify({ name }) }),
|
||||
importHtml: (file: File) => {
|
||||
const form = new FormData()
|
||||
form.append('file', file)
|
||||
return fetch(`${BASE}/modlists/import`, { method: 'POST', body: form }).then(r => {
|
||||
if (!r.ok) return r.json().then(e => { throw new Error(e.error) })
|
||||
return r.json() as Promise<Modlist>
|
||||
})
|
||||
},
|
||||
checkMods: (id: string) => request<ModStatus[]>(`/modlists/${id}/check`),
|
||||
downloadMissing: (id: string) => request<{ status: string; count?: number }>(`/modlists/${id}/download-missing`, { method: 'POST' }),
|
||||
updateAll: (id: string) => request<{ status: string; count?: number }>(`/modlists/${id}/update-all`, { method: 'POST' }),
|
||||
}
|
||||
|
||||
function wsUrl(path: string): string {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
return `${protocol}//${window.location.host}${path}`
|
||||
}
|
||||
|
||||
export const steamcmdApi = {
|
||||
updateGame: (branch: string, user: string) =>
|
||||
request<{ status: string }>('/server/steamcmd/update-game', { method: 'POST', body: JSON.stringify({ branch, user }) }),
|
||||
downloadMod: (modId: string) =>
|
||||
request<{ status: string }>('/server/steamcmd/download-mod', { method: 'POST', body: JSON.stringify({ mod_id: modId }) }),
|
||||
status: () => request<{ running: boolean }>('/server/steamcmd/status'),
|
||||
}
|
||||
|
||||
export const logsApi = {
|
||||
list: () => request<string[]>('/server/logs'),
|
||||
get: (file: string) => fetch(`${BASE}/server/logs/${file}`).then(r => r.text()),
|
||||
wsUrl: () => wsUrl('/ws/server/logs'),
|
||||
steamcmdWsUrl: () => wsUrl('/ws/steamcmd/logs'),
|
||||
rptWsUrl: () => wsUrl('/ws/server/rpt'),
|
||||
}
|
||||
Reference in New Issue
Block a user