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

requshar

v0.1.3

Published

An HTTP client library for use in server (restler) and client side (jQuery)

Downloads

8

Readme

Requshar NPM Version

An HTTP client library for use in server (restler) and client side (jQuery)

Installing

npm install requshar

Running the tests

npm test

Features

  • Equal API in client and server side
  • Easy interface for common operations via http.request and $.ajax
  • Simple service wrapper that allows you to easily put together REST API libraries

API

request(url[, options][, callback])

Basic method to make a request of any type. The function returns a request instance.

get(url[, options][, callback])

GET request.

post(url[, options][, callback])

POST request.

put(url[, options][, callback])

PUT request.

patch(url[, options][, callback])

PATCH request.

delete(url[, options][, callback])

DELETE request.

json(url[, options][, callback])

Parse response to json.

Options

Options :: Shared
  • method Request method, can be get, post, put, patch and delete. Defaults to 'get'.
  • query Query string variables as a javascript object, will override the querystring in the URL. Defaults to empty.
  • data The data to be added to the body of the request. Default to empty.
  • headers Request headers. Default to empty.
  • parseJSON Enabled parse to json responses. Default to false.
  • defaultErrorCode Error code for internal errors. Default to 598.
  • timeout If set, will emit the timeout event when the response does not return within the said value (in ms).
  • debug Enabled debug mode. Default to false.
  • logger Debbuger. Default to console.
Options :: Server
  • onTimeout Function callback for timeout responses.
Options :: Client
  • done Function callback for success responses. Default to _.noop.
  • fail Function callback for fail responses. Default to _.noop.

For more option see Restler and jQuery.ajax

Callback

function callback(err, response, body)

  • response In server side is the restler response Object. In client side is an object with next properties readyState, responseText, statusCode, statusText, status and res (jQuery response)
  • body Response text. If parseJSON options is true, body is an Object.

Request instance

var Requshar = require('requshar');
var requshar = new Requshar();

var request = requshar.get('http://www.google.com.ar', function(err, res, body) {
  if (err instanceof Error) {
    console.log('Error:', err.message);
  } else {
    console.log(body);
  }
});

request.req; // Server side restler request instance.
request.req; // Client side jQuery request instance.

Example usage

Default usage

var Requshar = require('requshar');
var requshar = new Requshar();

requshar.get('http://www.google.com.ar', function(err, res, body) {
  if (err instanceof Error) {
    console.log('Error:', err.message);
  } else {
    console.log(body);
  }
});

requshar.post('http://yourpage.com/users', {
  data: {
    id: 334,
    name: 'pepito'
  }
}, function(err, res, body) {
  if (err instanceof Error) {
    console.log('Error:', err.message);
  } else {
    console.log(body);
  }
});

Usage with default options

var Requshar = require('requshar');
var requshar = new Requshar({
  baseUrl: 'http://www.google.com.ar',
  headers: {
    'content-type': 'text/plain',
    'connection': 'keep-alive',
    'accept': '*/*'
  }
});

requshar.get('/', function(err, res, body) {
  if (err instanceof Error) {
    console.log('Error:', err.message);
  } else {
    console.log(body);
  }
});

Usage in debug mode

var logger = require('debug')('requshar');
var Requshar = require('requshar');
var requshar = new Requshar({
  debug: true,
  logger: logger
});

requshar.get('http://www.google.com.ar', function(err, res, body) {
  if (err instanceof Error) {
    console.log('Error:', err.message);
  } else {
    console.log(body);
  }
});

Usage in client side

<html>
  <head>
    <meta charset="utf-8">
    <title>Requshar in client side</title>
  </head>
  <body>
    <script src="https://cdn.rawgit.com/jquery/jquery/2.1.4/dist/jquery.min.js"></script>
    <script src="https://cdn.rawgit.com/jashkenas/underscore/1.8.3/underscore.js"></script>
    <script src="https://cdn.rawgit.com/comodinx/requshar/v0.1.3/requshar.js"></script>
    <script>
      var requshar = new Requshar({
        baseUrl: 'https://api.github.com',
        debug: true
      });

      requshar.json('/repos/comodinx/requshar', function(err, res, body) {
        if (err instanceof Error) {
          alert('Error:' + err.message);
        } else {
          alert(JSON.stringify(_.pick(body, 'id', 'name', 'full_name', 'language'), null, '  '));
        }
      });
    </script>
  </body>
</html>