diff --git a/frontend/src/lib/httpClient.ts b/frontend/src/lib/httpClient.ts index 9ddb98a..d9ef272 100644 --- a/frontend/src/lib/httpClient.ts +++ b/frontend/src/lib/httpClient.ts @@ -1,6 +1,9 @@ import { tryCatch } from 'fp-ts/TaskEither' +import * as J from 'fp-ts/Json' +import * as E from 'fp-ts/Either' +import { pipe } from 'fp-ts/lib/function' -async function fetcher(url: string, opt?: RequestInit): Promise { +async function fetcher(url: string, opt?: RequestInit, controller?: AbortController): Promise { const jwt = localStorage.getItem('token') if (opt && !opt.headers) { @@ -14,17 +17,27 @@ async function fetcher(url: string, opt?: RequestInit): Promise { headers: { ...opt?.headers, 'X-Authentication': jwt ?? '' - } + }, + signal: controller?.signal }) if (!res.ok) { throw await res.text() } - return res.json() as T + + + return res.text() } -export const ffetch = (url: string, opt?: RequestInit) => tryCatch( - () => fetcher(url, opt), +export const ffetch = (url: string, opt?: RequestInit, controller?: AbortController) => tryCatch( + async () => pipe( + await fetcher(url, opt, controller), + J.parse, + E.match( + (l) => l as T, + (r) => r as T + ) + ), (e) => `error while fetching: ${e}` )