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

fetchy-request

v0.8.7

Published

Request done right

Downloads

17

Readme

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. _