added comments
This commit is contained in:
@@ -11,18 +11,20 @@ export interface SaveStatusResponse {
|
||||
uploadDateTime: string
|
||||
}
|
||||
|
||||
// Tries to read the response body as string, returning an empty string if that fails.
|
||||
async function getBodyTextOrEmpty(res: Response): Promise<string> {
|
||||
try {
|
||||
return await res.text()
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
export class SaveParserBackendService {
|
||||
private API_BASE = PROXY_PATHS.saveParserBackend
|
||||
|
||||
private async tryGetResponseJson(res: Response): Promise<string> {
|
||||
try {
|
||||
const body = await res.json()
|
||||
return JSON.stringify(body)
|
||||
} catch {}
|
||||
return ''
|
||||
}
|
||||
|
||||
//TODO: save size limit 30 mb
|
||||
// Uploads a save file for the given game and returns its assigned id.
|
||||
async uploadSave(game: string, file: File): Promise<UploadSaveResponse> {
|
||||
const url = `${this.API_BASE}/uploadSave?game=${encodeURIComponent(game)}`
|
||||
const response = await fetch(url, {
|
||||
@@ -34,33 +36,31 @@ export class SaveParserBackendService {
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to upload save: ${response.statusText} ${await this.tryGetResponseJson(response)}`)
|
||||
throw new Error(`Failed to upload save: ${response.statusText} ` + (await getBodyTextOrEmpty(response)))
|
||||
}
|
||||
|
||||
return await response.json()
|
||||
}
|
||||
|
||||
// Fetches the current parsing status of a previously uploaded save.
|
||||
async getSaveStatus(id: string): Promise<SaveStatusResponse> {
|
||||
const url = `${this.API_BASE}/getSaveStatus?id=${encodeURIComponent(id)}`
|
||||
const response = await fetch(url)
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to get save status: ${response.statusText} ${await this.tryGetResponseJson(response)}`
|
||||
)
|
||||
throw new Error(`Failed to get save status: ${response.statusText} ` + (await getBodyTextOrEmpty(response)))
|
||||
}
|
||||
|
||||
return await response.json()
|
||||
}
|
||||
|
||||
// Fetches the fully parsed data for a save once its parsing is done.
|
||||
async getParsedSave(id: string): Promise<Record<string, any>> {
|
||||
const url = `${this.API_BASE}/getParsedSave?id=${encodeURIComponent(id)}`
|
||||
const response = await fetch(url)
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to get parsed save: ${response.statusText} ${await this.tryGetResponseJson(response)}`
|
||||
)
|
||||
throw new Error(`Failed to get parsed save: ${response.statusText} ` + (await getBodyTextOrEmpty(response)))
|
||||
}
|
||||
|
||||
return await response.json()
|
||||
|
||||
Reference in New Issue
Block a user