apiGet update
This commit is contained in:
1 parent
42fdf429e2
commit
794281c02c
3 files changed
+14
-10
No files matched your search
+7
-5
@@ -40,14 +40,16 @@ export function createApiUrl(path: string) {
|
||||
export type FactoryFunction<I, O> =
|
||||
| ((response: I) => O)
|
||||
| ((response: I) => Promise<O>)
|
||||
export type ApiDefaultResponse<O> = () => O
|
||||
|
||||
export function apiGet<I, O>(factory: FactoryFunction<I, O>) {
|
||||
return async (url: string, defaultResponse: O) => {
|
||||
const res = await requestGet<I>(url, null as any)
|
||||
if (!res) {
|
||||
return defaultResponse
|
||||
return async (url: string, getDefaultResponse: ApiDefaultResponse<O>) => {
|
||||
const res = await fetch(url)
|
||||
if (res.ok) {
|
||||
const data = await res.json() as I
|
||||
return factory(data)
|
||||
}
|
||||
|
||||
return factory(res)
|
||||
return getDefaultResponse()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user