Git Product home page Git Product logo

rest.js's Introduction

rest.js

  • Interact with restfull APIs through JavaScript
  • Supports ordinary XHR-requests as well as JSONP
  • No dependencies, minimalistic
  • Compatible with Google closure compiler advanced mode

DEMO

Syntax

  restjs.get(url, callback)
  restjs.post(url, data, callback)
  restjs.put(url, data, callback)
  restjs.delete(url, callback)
  restjs.login(login)       /* set login to use in all requests */
  restjs.password(password) /* set password to use in all requests */
  restjs.baseURL(url)       /* prefix all requests with this url */
  restjs.useJSONP()         /* use JSONP for all requests */
  restjs.useJSON()          /* use normal JSON for all requests */
  restjs.unpack()           /* make get, post, put & del (delete is a reserved word) available as top level methods */

Example

  restjs.get("http://twitter.com/status/user_timeline/ippalix.json?count=10", function(data) {
    alert(data[0].text) /* Text of ippalix latest tweet */
  });

  restjs.get("http://www.flickr.com/services/feeds/photos_public.gne?format=json", function(data) {
    data["items"][0]["media"]["m"] /* => http://farm6.static.flickr.com/5064/xxxxxxxxxx.jpg */
  });

Gotchas

  • Since JSONP is using dynamically injected script-tags, is supports GET (not POST, PUT and DELETE)
  • JSON supports all methods, but doesn't work cross domain(This is why the JSONP "hack" was invented)
  • restjs.unpack() creates the global functions get(), post(), put() and del(). Not delete() since it's a reserved word in javascript
  • No errorhandling just yet, don't make bad API requests ;-)

If You have a REST-api, please consider (enabling cross-origin resource sharing)[http://enable-cors.org/] so we don't have to reside to hacks in the future.

happy apiing,

ippa.

rest.js's People

Stargazers

Ben Hsieh avatar Angus H. avatar Ryan Martin avatar YNP. Jayuda avatar  avatar Joshua Ramirez avatar Yaşar İÇLİ avatar Jorene René avatar  avatar ippa avatar

Watchers

ippa avatar James Cloos avatar  avatar

rest.js's Issues

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.