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

request-options

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

request-options

Default options object generator for the popular NodeJS package Request

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

request-options

Build Status

Default options object generator written in ES6 for the popular NodeJS package Request, providing a generic set of request options for the request package that can be augmented by providing options or defaults to overwrite.

Install

$ npm install -S request-options

Usage

requestOptions( [options][, defaults] )

@param {Object} [options]
@param {Object} [defaults]
@returns {Object}

options

Any valid request options object

defaults

These defaults are intended for communication with APIs/services that accept and return JSON:

  • json: true
  • gzip: true
  • timeout: 10000

The defaults can be overwritten by providing your own defaults parameter object with the new values. Any additional values will be ignored, use the options object for your extended options.

Examples

The basic use case will return true for json and gzip, as well as a timeout of 10 seconds

const requestOptions = require('request-options');
const request = require('request');

function responseHandler (err, res, body) {
  // handle request response
}

request(requestOptions(), responseHandler);

// requestOptions => {
  json: true,
  gzip: true,
  timeout: 10000
}

Adding additional options

const requestOptions = require('request-options');
const request = require('request');

const options = {
  method: 'get',
  url: 'https://api.yoursite.com/endpoint'
};

function responseHandler (err, res, body) {
  // handle request response
}

request(requestOptions(options), responseHandler);

// requestOptions => {
  method: 'get',
  url: 'https://api.yoursite.com/endpoint',
  json: true,
  gzip: true,
  timeout: 10000
}

Overwriting the defaults

const requestOptions = require('request-options');
const request = require('request');

const options = {
  method: 'get',
  url: 'https://api.yoursite.com/endpoint'
};
const defaults = {
  gzip: false,
  timeout: 20000
};

function responseHandler (err, res, body) {
  // handle request response
}

request(requestOptions(options, defaults), responseHandler);

// requestOptions => {
  method: 'get',
  url: 'https://api.yoursite.com/endpoint',
  json: true,
  gzip: false,
  timeout: 20000
}

Notice how the default json value is still present and left unchanged

Tests

The test suite is located in the test/ directory. All new features are expected to have corresponding test cases. Ensure that the complete test suite passes by executing:

$ npm test

License

MIT License

Copyright (c) 2017 Nigel Horton

Keywords

FAQs

Package last updated on 16 Jan 2017

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