Ensured JSON decodedoes not happen for endpoints returning empty body

This commit is contained in:
Alejandro Celaya
2022-11-19 09:29:29 +01:00
parent bd8ea17c84
commit dd9ee044eb
6 changed files with 53 additions and 51 deletions

View File

@@ -5,13 +5,15 @@ export class HttpClient {
public fetchJson<T>(url: string, options?: RequestInit): Promise<T> {
return this.fetch(url, options).then(async (resp) => {
const parsed = await resp.json();
if (!resp.ok) {
throw parsed;
throw await resp.json();
}
return parsed as T;
try {
return (await resp.json()) as T;
} catch (e) {
return undefined as T;
}
});
}