GithubHelp home page GithubHelp logo

nvdnkpr / abrest Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 708 KB

Abstraction for dealing with RESTful services so your can always be abreast.

Home Page: http://jamescostian.github.io/abrest

License: MIT License

abrest's Introduction

Abrest Build Status Dependency Status

Abstraction for dealing with RESTful services so your can always be abreast.

Abrest works on both the client and the server side (assuming your server side is running Node.JS). If you have any AMD setup (e.g. RequireJS or Almond), Abrest will use that. Otherwise, Abrest will just attach itself to the this variable (which is usually going to be window).

Installation

To install Abrest for your client side, you can run bower install abrest and to install it for your server-side (assuming you're using Node.JS) you can run npm install abrest

API

// Creates an instance of Abrest in the variable 'user' and sets '/api' is the
// base URL. In addition, {id: 5379} will be set as the default data that will
// always be sent unless it is overriden. Also, {'user-agent': 'abrest'} is set
// as the default header that will be sent unless it is overriden.
var user = new Abrest('/api/', {id: 5379}, {'user-agent': 'abrest'})

// Sends a GET request from the 'user' instance of Abrest. The URL that it will
// get is '/api/name' (because '/api/' is the base URL for the 'user' instance)
// and all of the default headers will be sent, none of which will be overriden
user.get('name', function (error, result) {
    if (error) {
        throw error
    }
    else {
        // result (should) be the user's name
    }
})

// Sends a POST request from the 'user' instance of Abrest. The URL that it
// will get is '/api/favorite_color' and the data that it will send will be
// a combination of the default data and {foo: 'bar'}. Also, because there
// aren't any headers here, the default ones will be used.
user.delete('favorite_color', {foo: 'bar'}, function (error, result) {
    if (error) {
        throw error
    }
    else {
        // restult (should) be the user's favorite color
    }
})
// And also Abrest (should) support POST and PUT

Advanced Options

When you create an instance of Abrest, you can modify some of that instance's properties. Here's an example:

var user = new Abrest() // Creates a new instance of Abrest

// Sets the request timeout to 0, which means that it will never timeout.
// The default timeout is 1000 (which is 1 second).
user.timeout = 0

// Sets the default headers to {foo: 'bar'}. So, if a request is sent without
// any headers, then Abrest will still send the header 'foo: bar'. In order to
// override this header, one must send a request with a header that
// specifically sets 'foo' to something else.
user.defaultHeaders = {foo: 'bar'}

// Sets the default data to {foo: 'bar'}. So, if a request is sent without
// any data, then Abrest will still send the data 'foo=bar'. In order to
// override this data, one must send a request with a data that specifically
// sets 'foo' to something else.
user.defaultData = {foo: 'bar'}

Side Note for Client-Side

In the client-side, it's almost always good to set the content-type header to application/x-www-form-urlencoded. So that's what Abrest does.

If you don't want that header to be sent, all you have to do is override it by giving a new default content-type header or by giving a new header in the actual request.

abrest's People

Contributors

jamescostian avatar

Watchers

 avatar  avatar

Forkers

evanisnor

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.