fix encoding url in archive

This commit is contained in:
2023-10-20 18:25:33 +02:00
parent 600475f603
commit 6688bc3977
8 changed files with 70 additions and 55 deletions

View File

@@ -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
}