Skip to content

layervault/designer_news_js_client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Designer News API

NodeJS client for accessing the Designer News API.

Installation

The library is available on npm:

npm install designer-news

Configuration

var DN = require('designer-news');

// Configure
var config = new DN.Configuration(function () {
  this.oauthKey = 'abc123'
  this.oauthSecret = 'foobar'

  // optional, if you have it
  this.accessToken = 'jfosdf9sud98fds0f8snd0fs8dnf'
});

// Create the client
var client = new DN.Client(config);

Usage

Authentication

// If configured without an access token, you will need
// to authenticate and retrieve one. Here's the password flow:
client.auth.withPassword('username', 'password', function (err, tokens) {
  // We are now logged in. The configuration is automatically updated
  // with the access token. We can store them somewhere persistent
  // if needed.
});

Retrieving Data

The JS function names roughly follow the same convention as the API URLs.

client.me(callback);

client.stories.get(id, callback);
client.stories.frontPage({page: 2}, callback);
client.stories.recent({page: 2}, callback);
client.stories.search(query, callback);
client.stories.upvote(id, callback);
client.stories.reply(id, comment, callback);

client.comments.get(id, callback);
client.comments.upvote(id, callback);
client.comments.reply(id, comment, callback);

client.motd.get(callback);
client.motd.upvote(callback);
client.motd.downvote(callback);

Promises

All asynchronous calls return a promise, which can be used as an alternative to providing a callback.

client.auth.withPassword('username', 'password').then(function (resp) {
  console.log(resp.accessToken);
  return client.stories.frontPage();
}).then(function (resp) {
  return client.stories.upvote(resp.stories[0].id);
}).then(function (story) {
  console.log(story);
});

Links

Author

Ryan LeFevre - GitHub, Twitter, Email

License

Available under the MIT license. See the LICENSE file for more info.