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

@nitinrajput/request

v1.0.17

Published

fetch data

Downloads

37

Readme

Request-wrapper

Super simple to use

Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default..

const request = require("@nitinrajput/request");
request("http://www.google.com", function (error, response, body) {
  console.error("error:", error); // Print the error if one occurred
  console.log("statusCode:", response && response.statusCode); // Print the response status code if a response was received
  console.log("body:", body); // Print the HTML for the Google homepage.
});

Promise based HTTP client for the browser and node.js

Installing

Using npm:

npm i @nitinrajput/request

Table of contents

Request also offers convenience methods like request.get , request.post, request.put, request.patch and request.Delete


Performing a GET request

request.get("http://www.google.com", function (error, response, body) {
  console.error("error:", error);
  // Print the error if one occurred
  console.log("statusCode:", response && response.statusCode);
  // Print the response status code if a response was received
  console.log("body:", body);
  // Print the HTML for the Google homepage.
});

Performing a post request

request.post("http://www.google.com", data, function (error, response, body) {
  console.error("error:", error);
  // Print the error if one occurred
  console.log("statusCode:", response && response.statusCode);
  // Print the response status code if a response was received
  console.log("body:", body);
  // Print the HTML for the Google homepage.
});

data for PATCH, POST and PUT requests. Must be a String or object If jsonistrue, then body` must be a JSON-serializable object.

Promises & Async/Await

request supports both streaming and callback interfaces natively. If you'd like request to return a Promise instead, you can use an alternative interface wrapper for request. These wrappers can be useful if you prefer to work with Promises, or if you'd like to use async/await in ES2017.

The basics of async/await

There are two parts to using async/await in your code.

The async keyword

First of all we have the async keyword, which you put in front of a function declaration to turn it into an async function. An async function is a function that knows how to expect the possibility of the await keyword being used to invoke asynchronous code.

The await keyword

The advantage of an async function only becomes apparent when you combine it with the await keyword. await only works inside async functions within regular JavaScript code, however it can be used on its own with JavaScript modules.

await can be put in front of any async promise-based function to pause your code on that line until the promise fulfills, then return the resulting value.

You can use await when calling any function that returns a Promise, including web API functions.

Error handling, "try...catch"

No matter how great we are at programming, sometimes our scripts have errors. They may occur because of our mistakes, an unexpected user input, an erroneous server response, and for a thousand other reasons.

Usually, a script “dies” (immediately stops) in case of an error, printing it to console.

But there’s a syntax construct try...catch that allows us to “catch” errors so the script can, instead of dying, do something more reasonable.

The “try…catch” syntax

The try...catch construct has two main blocks: try, and then catch:

try {
  // code...
} catch (err) {
  // error handling
}

It works like this:

  1. First, the code in try {...} is executed.
  2. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch.
  3. If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err). The err variable (we can use any name for it) will contain an error object with details about what happened.

So, an error inside the try {...} block does not kill the script – we have a chance to handle it in catch.

Example

In order to gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with require() use the following approach:

const  request = require("@nitinrajput/request");

application/x-www-form-urlencoded (URL-Encoded Forms)

URL-encoded forms are simple.

request.post('http://service.com/upload', {form:{key:'value'}}
// or
request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })

request(url, callback)

The first argument can be either a url or an options object. The only required option is uri; all others are optional.

  • url - a parsed url object from url.parse()
  • baseUrl - fully qualified uri string used as the base url. Most useful with request.defaults, for example when you want to do many requests to the same domain. If baseUrl is https://example.com/api/, then requesting /end/point?test=true will fetch https://example.com/api/end/point?test=true.
  • method - http method (default: "GET")

  • body - entity body for PATCH, POST and PUT requests. Must be a Buffer, String . If jsonistrue, then body` must be a JSON-serializable object.

Request method aliases

For convenience aliases have been provided for all supported request methods.

request.Delete(url,callback )
request.get(url, callback)
request.post(url,data,callback)
request.put(url ,data, callback)
request.patch(url, data, callback)