🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

restq

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

restq

Declarative way of getting data from RESTful APIs.

0.10.3
latest
Source
npm
Version published
Weekly downloads
7
40%
Maintainers
1
Weekly downloads
 
Created
Source

#RestQ RestQ is a little helper module that allows you to follow RESTful API links in a more declarative style.

Build Status

Why is that cool?

OK, let's say we have a server that exposes a /posts/42.json resource, which contains an {"author": {"person_id": "foo"}} link to a /people/<person_id>.json resource. And we want to get the post author's email.

One way would be to do something like this:

request.get('http://server.com/posts/42.json, (err, res, body) ->
  if err or res.statusCode isnt 200
    # Handle errors here...
  request.get('http://server.com/people/#{body.author.id}.json), (err, res, body) ->
    if err or res.statusCode isnt 200
      # Handle errors here.
    email = body['email-address']
    

Now that's not too terrible, but it's not awesome either. The nesting is kind of annoying (imagine what happens when there are more levels...) and the code is just not as readable as it could be.

So how does RestQ help?

Well, with RestQ, we can call:

emailPromise = RQ.from('http://server.com/posts/42.json')
  .via('/people/{author.id}.json')
  .get('email-address')
  

And we get a 'thenable' (promise) for the post author's email stored in emailPromise.

Now when we need the actual email address, we do:

p.then (email) -> ... email is here ...

et voila!

Installation

npm install restq

Running tests

npm test

Kudos to

Underscore, Underscore.string, Q, Request

Keywords

rest

FAQs

Package last updated on 03 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts