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

toget

v0.5.0

Published

Simple Node.js RESTful API builder to make requests easy.

Downloads

4

Readme

toget

npm version Build Status

Simple Node.js RESTful API builder for request.

Install

$ npm install toget --save
$ npm install request --save

Note: request 2+ required.

Overview

toget makes available through its API the most used properties of request.

const toget = require('toget')(require('request'));

// All request made with `request` instance will have
// `http://domain.com` as base URL.
let request = toget('http://domain.com');

request('/user/:id', { id: 1 })
.then((response) => {
  console.log(response.body);
})
.catch((err) => {
  console.trace(err);
});

Request API

Request is the class in charge to build and execute requests.

const Request = require('toget/lib/Request');

constructor(base)

Creates Request instance.

  • base (String) - protocol+auth+hostname+port to be taken as base for request.
let request = new Request('http://localhost:3000');

// Set request module for API to work.
request.upon(require('request'));

.get(path, params)

Set request method, path and optional params.

  • path (String) - The pathname property consists of the entire path section of the URL.
  • params (Object) - Possible object to replace values in path.

Note: The same for .post(), .put() and .delete() methods.

request.get('/user/:id', { id: 1 })

.query(qs)

Used to specify query string with an Object.

  • qs (Object) - Object to be parsed into a URL query string.
request.query({ sort: 'createdAt DESC' })

.headers(values)

Set HTTP request headers.

  • values (Object) - HTTP Headers as Object to be append in request.
request.headers({ 'Accept-Encoding': 'gzip, deflate' })

.json()

Adds Content-type: application/json header and parses the response body as JSON.

request.json()

.gzip()

Add an Accept-Encoding header to request compressed content encodings from the server and decode supported content encodings in the response.

request.gzip()

.jar(value)

Define your custom cookie jar.

const url = 'http://localhost';
const req = new Request(url);
const j = req.request.jar();
j.setCookie(req.request.cookie('key1=value1'), url);

request.jar(j)

.timeout(value)

Integer containing the number of milliseconds to wait for a server to send response before aborting the request.

request.timeout(1500)

.body(data)

Entity body for POST and PUT requests.

  • data (Buffer|ReadStream|String|Object) - Entity body.

Note: If json is true, then body must be a JSON-serializable object.

request.body({ firstName: 'Foo', lastName: 'Bar', age: 25 })

.encoding(value)

Set encoding code to the response body.

  • value (Object) - Represent the enconding code.

Note: If value is undefined means this is 'utf-8' by default. In the case for null is used as encoding code, the body is returned as a Buffer.

request.encoding('utf-8')

.toPromise()

Make request and returns Promise instance to handle response and possible errors.

request.toPromise()
.then((response) => {
  console.log(response.body);
})
.catch(console.error);

Note: response resolved in promise is an instance of Response.

.toOptions()

Returns request options Object ready to be used in request(options).

request.get('/user').toOptions(); // { method: 'GET', url: 'http://localhost:3000/user' }

.exec(cb)

Shortcut for request(req.get('/user').toOptions(), cb) to use as a normal request call. This method returns request instance.

  • cb (Function) - Callback as specified in request docs; request(options, callback).
request.get('/doodle.png').exec((err, response, body) => {
  if (err) {
    console.error(err);
    return;
  }

  // ...
})
.pipe(fs.createWriteStream('doodle.png'));

Note: The callback here is handled by request so this means response is not the response you would expect returned by a promise but the response.rawResponse instead.

Response API

Response is the class in charge to handle response from request made.

const Response = require('toget/lib/Response');

constructor(rawResponse)

Creates Response instance.

  • rawResponse (Object) - Response Object received in request callback.
let request = new Response(rawResponse);

.body Buffer|ReadStream|String

Response body extracted from rawResponse.body.

response.body

.headers Object

Response headers extracted from rawResponse.headers.

response.headers

.statusCode Number

Response status code extracted from rawResponse.statusCode.

response.statusCode

.url String

URL used in request.

response.url

Method used in request (GET, POST, PUT, DELETE).

.method String

response.method

.status Object

Response status code dictionary available as string-boolean as defined in statusCodes.json

response.status

.type Number

Response status range type code (1, 2, 3, 4, 5, etc).

response.type

.info Boolean

Response status code range from 100 to 199.

response.info

.ok Boolean

Response status code range from 200 to 299.

response.ok

.clientError Boolean

Response status code range from 400 to 499.

response.clientError

.serverError Boolean

Response status code range from 500 to 599.

response.serverError

.error Error|Object|Boolean

Response error, false if no error or Error|Body instance otherwise.

if (response.error) {
  throw response.error;
}

Test

$ npm test

License

MIT