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

rpclient

v0.1.0

Published

Remote Package Client

Downloads

4

Readme

usage

npm install rpclient

import RPClient from 'rpclient'

const rpc = new RPClient({
    //todas as requisições passarão por aqui antes de serem enviadas
    beforeRequest(config){
        config.headers['x-custom-head'] = 'value'

        //isso faz com que a requisição assuma essa configuração
        return config
    },

    //todas as respostas passarão por aqui
    beforeResponse(error, response, config){
        //posso modificar a resposta e retornar aqui
        return response
    }
})

//exemplo 01
rpc.get('http://get.param/:p1/:p2', {p1:1, p2:2, q:'xpto'})
//GET http://get.param/1/2/?q=xpto

//exemplo 02
let form = new FormData()
form.append('name', 'myname')
form.append('id', 2)
rpc.put('http://put.form/:id', form) //form pode ser HTMLFormElement
    .then(res => {
        console.log(res)
    })
    .catch(err => {
        console.warn(err)
    })

//exemplo 03
let loginApi = rpc.api({
    beforeRequest(config){
        //posso modificar a requisição aqui, aina que já tenha sido modificada no RPClient
    }    
})
loginApi.post('http://login.api', {username:'myuser', password:'mypass'})
    .then(res => {
        console.log(res)
    })
    .catch(err => {
        console.warn(err)
    })

//exemplo 04
rpc.get('http://my.api.search', {q:'n=argument'})
    .then(res => {
        console.log('api2 ok', res)
    })
    .catch(err => {
        console.warn('api2 error', err)
    })


//exemplo 05
//renovando token expirado
let access_token = 'xYowlkjunlakuh987yap34jnf0987yb0q98ht097yb0397yb097y6bn-028nuv98npouiboiuhoiuhs'
let renew_token = 'rkijnlkjnlasfa-alkjnaf897yaoisuy09iknoab987y-9a8upasjfçlkjs.npaiushdboiu'

const rpc = new RPClient({
    beforeRequest(config){
        config.headers['Authentication'] = access_token
        return config
    },

    beforeResponse(error){
        let config
        let data = error ? error.data : {} //considerendo que esse seja o formato da resposta de erro da sua api
        
        //considerando que este seja o formato de resposta quando o token expirar
        if (data.code == 'TokenExpiredError'){
            config = {
                url: `http://my.api/renew/`,
                method: 'put',
                prevent: true, //todas as outras requisições ficam pendentes desta, inclusive a atual que retornou esse erro, esta será reenviada
                data: {
                    renew_token
                }
            }
            
            rpc.request(config)
                .then((result)=>{
                    access_token = result.data.access_token
                    renew_token = result.data.renew_token
                    console.log('renew ok')
                })
                .catch(err => {
                    console.warn('renew error', err)
                    //bom lugar para chamar tela de autenticação
                })
        }
    }
})