apiGet stability improved

This commit is contained in:
tmshv committed 2024-11-26 18:54:32 +03:00
1 parent 460e8fc3cc
commit 21047227b8
1 file changed
+9 -4
+9 -4
View File
@@ -10,10 +10,15 @@ export type ApiDefaultResponse<O> = () => O
export function apiGet<I, O>(factory: FactoryFunction<I, O>) { export function apiGet<I, O>(factory: FactoryFunction<I, O>) {
return async (url: string, getDefaultResponse: ApiDefaultResponse<O>) => { return async (url: string, getDefaultResponse: ApiDefaultResponse<O>) => {
const res = await fetch(url) try {
if (res.ok) { const res = await fetch(url)
const data = await res.json() as I if (res.ok) {
return factory(data) const data = await res.json() as I
return factory(data)
}
} catch (error) {
console.error(`Failed fetch request: ${error}`)
return getDefaultResponse()
} }
return getDefaultResponse() return getDefaultResponse()