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

fetchy-request

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetchy-request

Request done right

  • 0.8.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

npm version Build Status Dependency Status

A simple way to make simple http requests.

Install

npm install --save fetchy-request
## Notice: if you are using a legacy node version
## which does not contain a native Promise,
## please install and use a polyfill by yourself.
# npm install --save bluebird

Usage

In general, the API is pretty much a mixture of the fetch API and the popular request package.

A simple GET:

let request = require('fetchy-request')

request('http://open.meituan.com/cool-api/?k1=v1&k2=v2')
    .then(function (response) {
        return response.json()
    }).then(function (json) {
        console.log('parsed json', json)
    })

The same GET [1]:

let request = require('fetchy-request')

request({
    uri: 'http://open.meituan.com/cool-api/'
    method: 'GET',
    qs: {
        k1: 'v1',
        k2: 'v2'
    }
})

A simple POST:

let request = require('fetchy-request')

request({
    uri: 'http://open.meituan.com/cool-api/'
    method: 'POST',
    form: {
        username: 'admin',
        password: 'hackmeifyoucan!'
    }
})

Another POST:

let request = require('fetchy-request')

request({
    uri: 'http://open.meituan.com/cool-api/'
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: '{"json is": "Great!"}'
})

qs and form is stringified by qs; POST body overwrites form.

File uploads are not yet supported.

Timeout

Set response timeout in millisecond.

let request = require('fetchy-request')

request({
        uri: 'http://open.meituan.com/cool-api/?k1=v1&k2=v2'
        timeout: 2000
    })

Retry

Retry for any http 5xx server errors or local syscall errors:

let request = require('fetchy-request')

request({
        uri: 'http://open.meituan.com/cool-api/?k1=v1&k2=v2'
        retry: 2
    })

Proxy

Automatically use process.env.HTTP_PROXY for http requests.

HTTP_PROXY='http://127.0.0.1:8888' node server.js

Notice: this is purposely designed for debugging. DO NOT use in production environments.

Events/Timing/Error Reporting

let request = require('fetchy-request')
request({
    uri: 'http://open.meituan.com/poi/12345?k1=v1&k2=v2',
    // .displayName will be used in errors' message,
    // default to the `.uri` if omitted
    displayName: 'open.meituan.com/poi',
    // label is readable in any events callback
    label: ['tag1', 'tag2']
})

request.on('beforeSend', function (requestOptions) {
})

request.on('success', function (incommingMsg, requestOptions) {
    console.log(requestOptions.displayName, 'timing:', incommingMsg.timing.response - incommingMsg.timing.start)
    // => open.meituan.com/poi timing: 200
    console.log(requestOptions.label)
    // => ['tag1', 'tag2']
})

request.on('failure', function (error, requestOptions) {
    console.warn(error.message)
    // => 502 Bad Gateway: open.meituan.com/poi
    console.log(requestOptions.displayName, 'request failed after', err.timing.error - err.timing.start, 'ms')
    // => open.meituan.com/poi request failed after 300 ms
    console.log(requestOptions.label)
    // => ['tag1', 'tag2']
})

Error Handling Sugar

let request = require('fetchy-request')

request('http://unstable.meituan.com/user.xml?user=xiaody')
    .then(function (response) {
        const fallback = { user: '', phone: '', error: true }

        // request failed
        if (!response.ok)
            return fallback

        // in case its not JSON
        return response.safeJson(function (e) {
            send_error_msg('error occurs when parse response as JSON:', e)
            return fallback
            // the error `e` will be returned if this error handler is omitted;
            // you can also pass in the fallback as a second argument, which overwrite the return value of the error handler.
        })
    }).then(function (userInfoOrFallback) {
        // use userInfo or fallback
    })

_ [1] In fact, there is a little difference when they come to error reporting. _

Keywords

FAQs

Package last updated on 20 Jul 2015

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