npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

twitreq

v0.0.2

Published

Generates a request options object suitable for making signed and authenticated requests to the v1.1 Twitter OAuth REST API using the native Node https library.

Downloads

4

Readme

twitreq

Generates a request options object suitable for making signed and authenticated requests to the v1.1 Twitter OAuth REST API using the native Node https library.

A lot of oAuth libraries try and do too many things at once: signing requests, wrapping the request/response functionality as well as providing a non-standard programmatic interface to the returned streamed data. This can make it hard to debug when your OAuth request (inevitably) fails the first few times.

This module concentrates on one thing only: generating the options object you need when writing native Node code like this:

var https = require("https");

https.request(options,function (res) {
    // handle response
});

Installation

$ npm install twitreq

Usage

var twitreq = require("twitreq");

twitreq(options,[callback]);

The callback function gets two arguments error and reqOptions.

error: An Error object if failed.

reqOptions: A fully populated, signed and authorised request options object ready for use with the native Node https library.

Options

The twitreq function accepts an options object literal with the following schema:

{
    oAuthConsumerKey,           // Required string
    oAuthConsumerSecret,        // Required string
    oAuthToken,                 // Required string
    oAuthTokenSecret,           // Required string
    method,                     // Required string, e.g. "GET"
    path,                       // Required string, e.g. "/1.1/statuses/user_timeline.json"
    host,                       // Optional string, defaults to "api.twitter.com"
    protocol,                   // Optional string, defaults to "https"
    oAuthVersion,               // Optional string, defaults to "1.0"
    oAuthSignatureMethod,       // Optional string, defaults to "HMAC-SHA1"
    queryParams                 // Optional object literal for query parameters, e.g. {screen_name:"jedrichards"}
    verbose                     // Optional boolean, prints out verbose debug info to console.log
}

Example

This example demonstrates how to grab the latest tweet from a user's timeline (API keys, tokens and secrets omitted, get yours from https://dev.twitter.com):

var twitreq = require("twitreq");
var https = require("https");

var options = {
    queryParams: {
        screen_name: "jedrichards",
        count: "1",
        exclude_replies: "true"
    },
    method: "GET",
    path: "/1.1/statuses/user_timeline.json",
    oAuthConsumerKey: "-",
    oAuthConsumerSecret: "-",
    oAuthToken: "-",
    oAuthTokenSecret: "-"
}

twitreq(options,function (err,reqOptions) {
    if ( err ) {
        // twitreq error
    } else {
        var req = https.request(reqOptions,function (res) {
            res.setEncoding("utf8");
            res.on("error",function (err) {
                // request error
            });
            var data = "";
            res.on("data", function (chunk) {
                data += chunk;
            });
            res.on("end",function () {
                console.log(JSON.parse(data));
            })
        })
        req.end();
    }
});

The above code should produce a reqOptions object that looks something like this:

{
    method: 'GET',
    path: '/1.1/statuses/user_timeline.json?screen_name=jedrichards&count=1&exclude_replies=true',
    hostname: 'api.twitter.com',
    headers:
    {
        Authorization: 'OAuth oauth_consumer_key="-", oauth_nonce="NjIyM2Q0OGYtMzc2NC00ZDliLTliM2EtNWJiYzM3N2MxNmI3", oauth_signature="-", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1355169071", oauth_token="-", oauth_version="1.0"',
        Accept: '*/*',
        Connection: 'close',
        'User-Agent': 'Node.JS twitreq v0.0.1',
        'Content-Type': 'application/x-www-form-urlencoded',
        Host: 'api.twitter.com'
    }
}

Limitations

This is a new library, so please bear in mind the following:

  • Requests with POST body data currently not supported. Coming soon.
  • Related to the point above, only tested with read-only GET requests.
  • Untested with streaming API calls, although may well work fine.
  • Only tested with requests authenticated as the app's Twitter user (i.e. the user associated with the app on https://dev.twitter.com), although may well work fine in other scenarios.