Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nanorequest

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nanorequest

Sometimes you just want to make a small request with a callback

  • 5.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
33
increased by65%
Maintainers
2
Weekly downloads
 
Created
Source

nanorequest stability

npm version build status downloads js-standard-style

Sometimes you just want to make a small request with a callback.

About

Sometimes, when you're testing an API for example, request is too large to install as as devDependency. Or might you don't need hawk authentication? Perhaps you're just sending JSON so you don't really care about multipart/mime (although this can handle that, you just gotta do the work yourself...).

This is not a full-featured library for sending and working with HTTP requests though. If you need something that isn't supported, you're likely looking at the wrong library. The idea here to really just wrap the built-in http.request method so you can use a callback and not deal with all the events crap.

Version 2.0.0 and higher requires node 8. If you want to use node 7 or earlier, you can continue to use ^1.0.0.

Installation

npm install nanorequest

Usage

const request = require('nanorequest')
// or!
import { nanorequest } from 'nanorequest'

const opts = {
  url: 'https://google.com'
}

const req = request(opts, (err, res, body) => {
  // do something with these things
})

// or with promises
request(opts)
  .then(({res, body}) => {}) // do something with these things
  .catch((err) => {}) // error out!

// or with async/await
try {
  const {res, body} = request(opts)
} catch (err) {
  console.log(err, err.res.statusCode, err.body)
}

API

nanorequest(opts:object(key:string), cb(err:Error, res:Response, body:(mixed)):function):Request|Promise

The opts object matches the options used in http.request, but accepts an optional url field.

  • opts.url:string - the URL you want to request. Will be parsed with url.parse.

The final parameter to the callback will depend on the content-type header returned from the server:

  • application/json: this will be parsed and the object will returned
  • text/*: the buffer will be converted to a string and returned
  • otherwise a buffer will be returned

If the callback is omitted, the function with return a Promise instead of returning the request.

License

Apache-2.0 © 2018 Todd Kennedy

Keywords

FAQs

Package last updated on 27 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc