apiGet stability improved
This commit is contained in:
1 parent
460e8fc3cc
commit
21047227b8
1 file changed
+5
@@ -10,11 +10,16 @@ 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>) => {
|
||||||
|
try {
|
||||||
const res = await fetch(url)
|
const res = await fetch(url)
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data = await res.json() as I
|
const data = await res.json() as I
|
||||||
return factory(data)
|
return factory(data)
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed fetch request: ${error}`)
|
||||||
|
return getDefaultResponse()
|
||||||
|
}
|
||||||
|
|
||||||
return getDefaultResponse()
|
return getDefaultResponse()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user