feat: migrate runtime backend to node server
This commit is contained in:
97
src/services/storageService.ts
Normal file
97
src/services/storageService.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import type {
|
||||
SavedGameSnapshot,
|
||||
SavedGameSnapshotInput,
|
||||
} from '../persistence/gameSaveStorage';
|
||||
import type { SavedGameSettings } from '../persistence/gameSettingsStorage';
|
||||
import type { CustomWorldProfile } from '../types';
|
||||
import { requestJson } from './apiClient';
|
||||
|
||||
const RUNTIME_API_BASE = '/api/runtime';
|
||||
|
||||
type CustomWorldLibraryResponse = {
|
||||
profiles?: CustomWorldProfile[];
|
||||
};
|
||||
|
||||
export async function getSaveSnapshot() {
|
||||
return requestJson<SavedGameSnapshot | null>(
|
||||
`${RUNTIME_API_BASE}/save/snapshot`,
|
||||
{ method: 'GET' },
|
||||
'读取存档失败',
|
||||
);
|
||||
}
|
||||
|
||||
export async function putSaveSnapshot(snapshot: SavedGameSnapshotInput) {
|
||||
return requestJson<SavedGameSnapshot>(
|
||||
`${RUNTIME_API_BASE}/save/snapshot`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(snapshot),
|
||||
},
|
||||
'保存存档失败',
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteSaveSnapshot() {
|
||||
return requestJson<{ ok: true }>(
|
||||
`${RUNTIME_API_BASE}/save/snapshot`,
|
||||
{ method: 'DELETE' },
|
||||
'删除存档失败',
|
||||
);
|
||||
}
|
||||
|
||||
export async function getSettings() {
|
||||
return requestJson<SavedGameSettings>(
|
||||
`${RUNTIME_API_BASE}/settings`,
|
||||
{ method: 'GET' },
|
||||
'读取设置失败',
|
||||
);
|
||||
}
|
||||
|
||||
export async function putSettings(settings: SavedGameSettings) {
|
||||
return requestJson<SavedGameSettings>(
|
||||
`${RUNTIME_API_BASE}/settings`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(settings),
|
||||
},
|
||||
'保存设置失败',
|
||||
);
|
||||
}
|
||||
|
||||
export async function listCustomWorldLibrary() {
|
||||
const response = await requestJson<CustomWorldLibraryResponse>(
|
||||
`${RUNTIME_API_BASE}/custom-world-library`,
|
||||
{ method: 'GET' },
|
||||
'读取自定义世界库失败',
|
||||
);
|
||||
|
||||
return Array.isArray(response?.profiles) ? response.profiles : [];
|
||||
}
|
||||
|
||||
export async function upsertCustomWorldProfile(profile: CustomWorldProfile) {
|
||||
const response = await requestJson<CustomWorldLibraryResponse>(
|
||||
`${RUNTIME_API_BASE}/custom-world-library/${encodeURIComponent(profile.id)}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
profile,
|
||||
}),
|
||||
},
|
||||
'保存自定义世界失败',
|
||||
);
|
||||
|
||||
return Array.isArray(response?.profiles) ? response.profiles : [];
|
||||
}
|
||||
|
||||
export async function deleteCustomWorldProfile(profileId: string) {
|
||||
const response = await requestJson<CustomWorldLibraryResponse>(
|
||||
`${RUNTIME_API_BASE}/custom-world-library/${encodeURIComponent(profileId)}`,
|
||||
{ method: 'DELETE' },
|
||||
'删除自定义世界失败',
|
||||
);
|
||||
|
||||
return Array.isArray(response?.profiles) ? response.profiles : [];
|
||||
}
|
||||
Reference in New Issue
Block a user