GithubHelp home page GithubHelp logo

marcin-piela / fetching-library Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 2.0 722 KB

Simple and powerful API client. It's extension for native fetch API. No dependencies!

License: MIT License

TypeScript 94.79% JavaScript 5.21%
fetch fetch-api fetcher rest rest-client

fetching-library's Introduction

Simple and powerful fetch API extension. Use request and response interceptors to deal with API.

Watch on GitHubStar on GitHubTweet


Build Status version downloads MIT License PRs Welcome Code of Conduct Gzip badge codecov

✅ Zero dependencies

✅ SSR support

✅ Uses Fetch API

✅ Request and response interceptors allows to easily customize connection with API

✅ TypeScript support

✅ Less than 2k minizipped

✅ Simple cache provider - easily to extend


fetching-library

Request and response interceptors allows you to easily customize connection with API (add authorization, refresh token, cache, etc). It uses Fetch API so it can be use in SSR apps (ie. with isomorphic-fetch)

Documentation

Full documentation is available at https://marcin-piela.github.io/fetching-library

Installation

Run the following from your project root :

npm install fetching-library

or

yarn add fetching-library

Short example of use with caching

import { createClient, cache, requestJsonInterceptor, responseJsonInterceptor, responseTextInterceptor } from 'fetching-library';

const cache = createCache(
  (action) => {
    return action.method === 'GET';
  },
  (response) => {
    return new Date().getTime() - response.timestamp < 100000;
  },
);

// Options is not required
const client = createClient({
  requestInterceptors: [requestJsonInterceptor],
  responseInterceptors: [responseJsonInterceptor, responseTextInterceptor]
  cacheProvider: cache,
});

const action = { 
  method: 'GET',
  endpoint: '/users',
};

client.query(action).then(response => {
  //response.status
  //response.error
  //response.errorObject
  //response.payload
});

Example of request interceptor

import { createClient, requestJsonInterceptor, responseJsonInterceptor } from 'fetching-library';

const requestHostInterceptor: host => client => async action => {
  return {
    ...action,
    endpoint: `${host}${action.endpoint}`,
  };
};

const client = createClient({
  requestInterceptors: [requestJsonInterceptor, requestHostInterceptor('http://example.com')],
  responseInterceptors: [responseJsonInterceptor]
  cacheProvider: cache,
});

const action = { 
  method: 'GET',
  endpoint: '/users',
};

client.query(action).then(response => {
  //response.status
  //response.error
  //response.errorObject
  //response.payload
});

Do you want to use it in react app?

Check react-fetching-library

Contributing

Fell free to open PRs and issues to make this library better !

When making a PR, make sure all tests pass. If you add a new feature, please consider updating the documentation or codesandbox examples. Thank you!

License

fetching-library is licensed under the MIT license.

fetching-library's People

Contributors

marcin-piela avatar roberuto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.