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

next-req

v1.3.5

Published

HTTP Promise based client

Downloads

7

Readme

HTTP Promise based client

Prerequisites

  • npm >= 5.5.0
  • node >= 9.3.0

Install

npm install next-req

Features

  • Make HTTP requests
  • Promised based API
  • Request and Response interceptors
  • Transforms Response for JSON payload
  • Abort requests[Cancel requests]
  • Transforms Request and Response payload

Usage

import nextreq from 'next-req'

nextreq.get('www.yourapi.com').then(response => {
    const payload = response.payload

    // TODO - payload
}).catch(error => {
    // TODO - error
}).finally(() => {
    // will always run
})

Generating GET request with PARAMS

import nextreq from 'next-req'

nextreq.get('www.yourapi.com/user?session=true').then(response => {
    const payload = response.payload

    // TODO - payload
}).catch(error => {
    // TODO - error
}).finally(() => {
    // will always run
})

Generating GET request with params of config

import nextreq from 'next-req'

nextreq.get('www.yourapi.com/user', {
    params: {
        session: true
    }
}).then(response => {
    const payload = response.payload

    // TODO - payload
}).catch(error => {
    // TODO - error
}).finally(() => {
    // will always run
})

Generating POST request

import nextreq from 'next-req'

nextreq.post('www.yourapi.com/user', {
    params: {
        setSession: true
    }
}).then(response => {
    const payload = response.payload

    // TODO - payload
}).catch(error => {
    // TODO - error
}).finally(() => {
    // will always run
})

Generating API request using async/await on function using GET

import nextreq from 'next-req'

const getPayload = async () => {
    try {
        const request = await nextreq.get('www.yourapi.com/user?session=true')
        const payload = request.payload
        
        // TODO - with payload
    } catch (error) {
        // TODO - errors
    } finally {
        // will always run
    }
}

Generating API request using async/await on function using POST

import nextreq from 'next-req'

const postPayload = async () => {
    try {
        const request = await nextreq.post('www.yourapi.com/user', {
            params: {
                setSession: true
            }
        })
        const payload = request.payload
        
        // TODO - with payload
    } catch (error) {
        // TODO - errors
    } finally {
        // will always run
    }
}

Interceptors

Generating interceptors

import nextreq from 'next-req'

// Add request interceptor
nextreq.interceptors.request.use(configs => {
    // TODO - with configs
    return configs
})

// Add response interceptor
nextreq.interceptors.response.use(
    response => {
        // TODO - with response
        return response
    },
    error => {
        // TODO - errors
        // Handle error
        return Promise.reject(error)
    }
)

Error handling

import nextreq from 'next-req'

nextreq.post('www.yourapi.com/user', {
    params: {
        setSession: false
    }
}).then(response => {
    // TODO - with response
}).catch (error => {
    // Evaluate error response to proceed

    if (error.response) {
        // Get error payload
        const payload = error.response.payload

        // Get error status
        const status = error.response.status

        // Get headers
        const headers = error.response.headers

        // TODO - errors
    } else {
        // An error occured in request
        // Get message
        const message = error.message
    }

    // Get configs
    const configs = error.config
})

Author

👤 Prince Khanyile

License

MIT