GithubHelp home page GithubHelp logo

xr's Introduction

xr

Really simple wrapper around XHR that provides a few bits of nice functionality, exposes the XHR object wherever relevant, and returns an ES6 Promise (or whatever Promise is set to globally, if you want to use something else).

The idea was to make a pragmatic library that's pre-configured for the 90% use case, but override-able for anyone that wants to do anything a bit off the beaten track.

For instance, the library is by default set up to send/receive JSON (with the associated headers and parser/dumper already set up), but if you wanted to use something like XML, it's easy enough to override that with a few lines.

It's lightweight, has no dependencies (other than having either Promise in the global namespace or provided via xr.config), and adds pretty much no overhead over the standard XHR API.

Quickstart

xr.get('/api/items', {take: 5})
  .then(res => console.log(res.data));
    
xr.post('/api/item', {name: 'hello'})
  .then(res => console.log("new item", res.data));

Extended syntax:

xr({
  method: xr.Methods.GET,
  url: '/api/items',
  params: {take: 5},
  events: {
    [xr.Events.PROGRESS]: (xhr, xhrProgressEvent) => {
      console.log("xhr", xhr);
      console.log("progress", xhrProgressEvent);
    }
  }
});

Custom promise:

xr.get('/url', {}, {
  promise: fn => new myPromiseClass(fn)
});

Raw mode (data is not dumped/loaded):

xr.put('/url', 'some data', {
  raw: true
});

Custom dump/load:

xr.post('/url', {'some': 'data'}, {
  dump: data => msgpack.encode(data),
  load: data => msgpack.decode(data)
});

Global configuration

One thing that I've always found irritating with libraries it that if you want to override the defaults, you have to do it per-request, or wrap the libraries.

With XR, this is simple, as you can globally configure the module for your project.

xr.configure({
  promise: fn => new myPromise(fn)
})

API is simple, for now consult source.

Features

  • Returns ES6 promises.
  • Has query parameter generation.
  • Supports events.

Alias Methods

You can do some quick aliases to requests, for instance:

xr.get('/my-url')

Requirements

There must be a polyfill or browser that supports at least the standard ES6 promise API (xr will use whatever's there), and `Object.assign()'.

License

See LICENSE

xr's People

Contributors

bronzehedwick avatar mtpbzz avatar radiosilence avatar tombyrer avatar wprater avatar

Watchers

 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.