Fetch API wrapped in a TaskEither.
yarn add @pacote/ffetch
The returned Either can be handled with fp-ts mapping functions, keeping
success and failure paths explicit:
import { ffetch } from '@pacote/ffetch'
import { pipe } from 'fp-ts/function'
import { map, mapLeft, getOrElse } from 'fp-ts/lib/Either'
const response = await ffetch('https://goblindegook.com/api/kittens/1')()
pipe(
response,
map((body) => {
/* handle successful response */
return body
}),
mapLeft((error) => {
/* handle request failure */
return error
}),
)
const okOrDefault = pipe(
response,
getOrElse(() => 'some default value'),
)
MIT © Luís Rodrigues.