Giter Club home page Giter Club logo

immutable-request's Introduction

Immutable Request

Combines superagent, bluebird, immutable and lru-cache for sane isomorphic HTTP POST/GET abstraction.

Example

// Simply use the static factory
Request.GET('http://localhost:8888/todoList')
.then((immutableMap) => {
  // do something with immutableMap
})
.catch((err) => {
  // handle error
});

// send a POST request with { seed: 1337 } as its form body
const post = Request.POST('http://localhost:8888/shuffleTodoList', { seed: 1337 }, { timeout: 10000 });
// nevermind, abort it
post.cancel(new Error('because reasons'));

// you can specify timeout (in ms) and
// return type ('immutable', 'json', 'object' or 'null')
Request.GET('http://localhost:8888/todoList', { timeout: 10000, type: 'json' })
.then((json) => {
  // do something with json
});

// leverage automatic, clever userland caching
const requester = new Request.Requester('http://localhost:8888', {
  max: 100, // cache no more than 100 HTTP requests
  maxAge: 60000, // cache expires after 60s
  timeout: 10000, // request timeout after 10000
});

requester.GET('/todoList')
.then((immutableMap) => {
  // do something with immutableMap
  // you can access the raw json with immutableMap.json
});
// cache is done by full path, so querystring can be used to force recaching
requester.GET('/todoList?try=2')
.then((immutableMap2) {
  // do something with immutableMap2
});
// POST requests are not cached, the request is sent twice
requester.POST('/shuffleTodoList', { seed: 42 });
requester.POST('/shuffleTodoList', { seed: 42 });
// cancel all pending requests
requester.cancelAll();
// clear the cache and destroy references
requester.reset();

Usage

This module is written in ES6/7. You will need babel to use it.

immutable-request's People

Contributors

antca avatar elierotenberg avatar glaciuspgm avatar

Stargazers

 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.