fix encoding url in archive
This commit is contained in:
@@ -1,13 +1,24 @@
|
||||
export async function ffetch<T>(
|
||||
url: string,
|
||||
onSuccess: (res: T) => void,
|
||||
onError: (err: string) => void,
|
||||
opt?: RequestInit,
|
||||
) {
|
||||
import { tryCatch } from 'fp-ts/TaskEither'
|
||||
import { flow } from 'fp-ts/lib/function'
|
||||
|
||||
export const ffetch = <T>(url: string, opt?: RequestInit) => flow(
|
||||
tryCatch(
|
||||
() => fetcher<T>(url, opt),
|
||||
(e) => `error while fetching: ${e}`
|
||||
)
|
||||
)
|
||||
|
||||
const fetcher = async <T>(url: string, opt?: RequestInit) => {
|
||||
const res = await fetch(url, opt)
|
||||
if (!res.ok) {
|
||||
onError(await res.text())
|
||||
return
|
||||
|
||||
if (opt && !opt.headers) {
|
||||
opt.headers = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
}
|
||||
onSuccess(await res.json() as T)
|
||||
|
||||
if (!res.ok) {
|
||||
throw await res.text()
|
||||
}
|
||||
return res.json() as T
|
||||
}
|
||||
Reference in New Issue
Block a user