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

js.poke

v1.3.4

Published

A lightweight and handy JS HTTP client

Downloads

8

Readme

Poke

A lightweight and handy JS HTTP Client.

npm package

Build-status Tests esLint Dependency Status Known Vulnerabilities

Why use Poke?

  • 😌   Very easy to use
  • 👨🏻‍💻   Written in TypeScript
  • 🚀   Lightweight
  • ⼮   Stream support
  • 💾   Easy to download file

Installation

npm i js.poke --save

or

yarn js.poke

Easy to use!

Poke allows you to make http request in the simplest way. See the following example:

const poke = require('js.poke')

// Using promise
poke('https://foo.api.com/candys')
.promise()
.then(result => {
    // response body here
    console.log('> body(string): ', result.body)
    // get result in json format
    return result.json()
})
.then(json => {
    // here is the json object
    console.log('> json: ', json)
})
.catch(err => {
    console.log('> Error: ', err)
})

Usage

How to use poke

Poke accepts hostname, pokeOptions and the callback function as the inputs. then it returns some methods for you to apply in different scenarios. Omit the callback function if you would like to handle the result with Promise.

promise

const poke = require('js.poke')

// Using promise
poke(hostname , pokeOptions)
.promise()
.then(result => { 
    // do your handling here
    ... 
})
.catch(error => {
    // handle error
    ...
})

callback

const poke = require('js.poke')
// Using callback
poke(hostname , pokeOptions, result => {
    // status code
    console.log(result.statusCode)
    // body
    console.log(result.body)
    // in json format
    result.json(json => {
        console.log(json)
    })
    // error
    console.log(result.error)
})

Form Data

poke(
    'https://httpbin.org/post',
    {
        method : 'POST',
        // Specify your form data with `boby`
        body : JSON.stringify({
            name : 'kfhk!sdkm!'
        })
    }
)
// ...

Listening to events by using on

const poke = require('js.poke')
// Using callback
poke(hostname , pokeOptions)
// listen to response retrived
.on('response', result => {
    console.log(result)
})
// on chunk is recieved
.on('data', (chunk) => {
    // handle your data here
    console.log(chunk)
})
// on request eneded
.on('end', () => {
    console.log('Request is finished')
})
// listening to error
.on('error', (result) => {
    // handle error
    console.log(result.error)
})

Stream

const fs = require('fs')
const poke = require('js.poke')

// get image
poke('https://via.placeholder.com/100x100')
// write data as an image file
.pipe(fs.createWriteStream('image.png'))

Even we can using Poke as a proxy!

const poke = require('js.poke')

const serv = http.createServer((req, res) => {
    if (req.url === '/placeholder') {
        // get image or whatever you want
        poke('https://via.placeholder.com/100x100')
        // pipe response to res
        .pipe(res)
    } else {
        res.end('Bye!')
    }
})
serv.listen(4321)

The Poke Options

The pokeOptions allows you to customize your request.

// The poke option
const options = {
    // POST, PUT, DELETE
    method : "GET", 
    path : "/", 
    port : 3001,
    // will set automatically, you may override the auto-detect value by specify this boolean
    gzip : true, 
    // in millisecond, if timeout is set and theer is no response return before timeout, request will abort
    timeout : 1000
    // username and password for Basic Auth
    username : 'foo_user',
    password : 'foo_secret'
    // customize your header here
    headers : {
        "content-type" : "application/json"
    },
    // will parse your query object to query string automatically
    query : { page : 3 }
    // form body
    body : JSON.stringify({
        first_name : "Poke",
        last_name : "You"
    })
}

poke('https://foo.api.com', options)

PokeResult

The PokeResult is returned with one of the following types PokeSuccess and PokeError.

PokeSuccess

PokeSuccess contains the req, body, statusCode, json() and headers. call json() returns a promise and parse the body into json object.

PokeError

if the request is failed, PokeError contains error(type: Error) will be returned.

poke('https://foo.api.com', options)
.promise()
.then(result => {
    // status code
    console.log(result.statusCode)
    // body:string
    console.log(result.body)
    // headers
    console.log(result.headers)
    // parse json
    return result.json()
})
.then(json => {
    // handle with parsed json
    console.log(json)
})
.catch(result => {
    // handler error
    console.log(result.error);
})

Authentication

Basic-auth

Simply put your basic-auth's username and password in PokeOption.

const options = {
    method : "GET", 
    // username and password for Basic Auth
    username : 'foo_user',
    password : 'foo_secret',
}

poke('https://foo.api.com', options)
.promise()
.then(result => result.json())
.then(json => console.log(result))
.catch(result => console.log(result.error))

Bearer auth

Put the bearer token into header for doing bearer authentication.

const options = {
    method : "GET", 
    headers : {
        'authorization' : `Bearer ${your_bearer_token}`
    }
}

poke('https://foo.api.com', options)
.promise()
.then(result => result.json())
.then(json => console.log(result))
.catch(result => console.log(result.error))

Customize header

Put custom headers attributes into headers of PokeOption.

poke('https://foo.api.com', {
    ...
    headers : {
        // put your headers here....
        'content-type' : 'application/json',
    }
})

Become a contirbutor!

Million thanks to whom contributes to this project!❤️

Pull requests are welcome! Please see CONTRIBUTING.md

What's Next?

  • 🔌   WebSocket (Upcoming)