Giter Club home page Giter Club logo

ffetch's Introduction

@pacote/ffetch

Build Status

Installation

yarn add @pacote/ffetch

Usage

In its simplest form, ffetch works a lot like the Fetch API it wraps:

import { ffetch } from '@pacote/ffetch'

const response = await ffetch('https://goblindegook.com/api/kittens/1').run()

console.log(result.value) // yields either an error or the response body

The function conveniently abstracts away concerns like response body parsing and some error handling.

However, the main difference and biggest selling point is that ffetch creates a TaskEither object which doesn't immediately run and allows chaining extra functionality. For example, you could more simply implement features like request cancellation and retrying around such an abstraction.

In the example above, executing ffetch(...).run() returns a Promise like the Fetch API does. But instead of resolving to a successful response (or rejecting on error), this Promise resolves to an Either representation that holds the successfully obtained and parsed response (on the right) or any of the possible errors that can arise (on the left).

Whatever the result, it can be directly accessed via the value property, but Either also allows handling responses safely through mapping functions, all without resorting to (sometimes nested) try ... catch constructs. For example:

import { ffetch } from '@pacote/ffetch'

// Perform the request:
const response = await ffetch('https://goblindegook.com/api/kittens/1').run()

// Handling success and failure responses separately:
response
  .map(body => {
    /* handle successful response */
    return body
  })
  .leftMap(error => {
    /* handle request failure */
    return error
  })
)

// Get the successful response body falling back to some default value:
const okOrDefault = response.getOrElse('some default value')

License

MIT © Luís Rodrigues.

ffetch's People

Contributors

goblindegook avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.