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

redel

v1.0.0

Published

Promise based HTTP client for the browser and node.js

Downloads

33

Readme

Redel

npm version install size npm downloads license: MIT Build Status Coverage Status

A middleware library for promise based axios for the browser and nodeJs

Installing

Using npm:

$ npm install redel

Using yarn:

$ yarn add redel

Redel API

Plugins

Example

Performing a basic usage


const Redel = require('redel')
const axios = require('axios')

const config = { log: true }
Redel.use(axios, config)

// ..

axios.get('https://jsonplaceholder.typicode.com/todos')

Performing usage with multiple plugins


const Redel = require('redel')
const axios = require('axios')

const config = { log: true, cancel: true, pending: true }
Redel.use(axios, config)

// ..

axios.get('https://jsonplaceholder.typicode.com/todos')

Performing usage with axios.create


const Redel = require('redel')
const axios = require('axios')
const axiosInstance = axios.create()

const config = { log: true, cancel: true, pending: true }
Redel.use(axiosInstance, config)

// ..

axiosInstance.get('https://jsonplaceholder.typicode.com/todos')

Cancel Plugin

Cancel plugin is a plugin that wrap your requests before firing them to the server with axios cancellation functionality.

The cancel plugin work with 2 different functionality:

  1. Single cancel
  2. Cancel by group key
  • Single Cancel request that still didn't return from the server when a new request with the same method and pathname gonna be fired to the server.

  • Cancel by group key Cancel all requests with the unique group key

Usage - Single


const Redel = require('redel')
const axios = require('axios')

Redel.use(axios, { cancel: true })
let canceledReqeuests = 0

// We can check if the catch function triggered by the Redel cancel plugin
// with the following condition `!!e.isCanceled`
const catchFn = e => {
  if (e.isCanceled) {
    canceledReqeuests += 1
  }
}

const mount = async () => {
  const basicUrl = 'https://jsonplaceholder.typicode.com/todos'
  await Promise.all([
    axios.get(`${basicUrl}?group=3`).catch(catchFn), // canceled
    axios.get(`${basicUrl}?group=124`).catch(catchFn), // canceled
    axios.get(`${basicUrl}?group=1911`).catch(catchFn), // canceled
    axios.get(`${basicUrl}?group=00001`).catch(catchFn) // resolved
  ])
  console.log({ canceledReqeuests }) // { canceledReqeuests: 3 }
}

mount()

Usage - Cancel by group key

const Redel = require('redel')
const axios = require('axios')

Redel.use(axios, { cancel: true })
const cancelGroupKey = 'customCancelGroupKey'

const headers = Redel.getCancelGroupHeader(cancelGroupKey)
const basicUrl = 'https://jsonplaceholder.typicode.com/todos'

let canceledReqeuests = 0

// We can check if the catch function triggered by the Redel cancel plugin
// with the following condition `!!e.isCanceled`
const catchFn = e => {
  if (e.isCanceled) {
    canceledReqeuests += 1
  }
}

const mount = () => {
  axios.get(`${basicUrl}/1`, { headers }).catch(catchFn),
  axios.get(`${basicUrl}/2`, { headers }).catch(catchFn),
  axios.get(`${basicUrl}/3`, { headers }).catch(catchFn),
  axios.get(`${basicUrl}/4`, { headers }).catch(catchFn)
}

mount()

// beforeDestroyed run the commend below to ensure that
// all pending requests would be canceled
Redel.cancelGroupRequests(cancelGroupKey)

Pending Plugin

Monitoring your pending requests. Expose functionality to get your pending requests.

Examples

const Redel = require('redel')
const axios = require('axios')

Redel.use(axios, { pending: true })

axios.get('https://jsonplaceholder.typicode.com/todos/1')
setTimeout(() => {
    console.log(Redel.getPendingRequests()) // ["/todos/1"]
})

A common usage of this functionality can be found in "beforeunload"

// if user has any pending request, display warning message
window.addEventListener("beforeunload", function (e) {
 if (Redel.getPendingRequests().length) {
   // there are pending requests
   // display a warning message
 }
 // unload the page
})

Log Plugin

Monitoring your requests by printing a very informative log about each request.

Examples

const Redel = require('redel')
const axios = require('axios')

const url = 'https://jsonplaceholder.typicode.com/todos/1'

Redel.use(axios, { log: true })

axios.get(url)

The above will print the js object below

{
   isCompletedWithoutError: true,
   maxContentLength: -1,
   method: "get",
   timeout: 0,
   proxy: undefined,
   requestData: {query: {}, data: {}, params: {}},
   requestHeaders: {
       common: {Accept: "application/json", "text/plain": "*/*"},
       delete: {},
       get: {},
       head: {},
       patch: {"Content-Type": "application/x-www-form-urlencoded"},
       post: {"Content-Type": "application/x-www-form-urlencoded"},
       put: {"Content-Type": "application/x-www-form-urlencoded"},
   },
   responseData: {userId: 1, id: 1, title: "delectus aut autem", completed: false},
   endTime: 1571698420250,
   startTime: 1571698420167,
   totalTime: "83ms",
   url: "https://jsonplaceholder.typicode.com/todos/1",
}

Table of content

| Property | Type | Description | | --- | --- | --- | | isCompletedWithoutError | Boolean | The request done with error or not | | maxContentLength | Number | Request max content length | | method | String | Request method | | timeout | number | Request time out | | proxy | object | Request proxy | | requestData | Object | Object that hold the request data (data, query, params)| | requestHeaders | Object | Request headers | | responseData | Object | Response data | | startTime | Number (timestamp) | Request start time | | endTime | Number (timestamp) | Request end time | | totalTime | String | Request total time | | url | String | Request url |

Use

Work as Redel init function. To initialize the function we need 2 params, axios and config.

| Property | Description | | --- | --- | | axios | axios instance | | config | Contains the desire plugins |

Example

const Redel = require('redel')
const axios = require('axios')

Redel.use(axios, { log: true })

add

Add plugin at run time

Example

const Redel = require('redel')
const axios = require('axios')

Redel.use(axios, { log: true })

// ...
// ...
// ...

Redel.add('cancel')

console.log(Redel.getSignedPlugins()) // ['log', 'cancel']

eject

Remove plugin from Redel. This is useful when you want to remove specific plugin at run time from the Redel instance. Example

const Redel = require('redel')
const axios = require('axios')

Redel.use(axios, { log: true })

//...
//...
//...
console.log(Redel.getSignedPlugins()) // ['log']
Redel.eject('log')
console.log(Redel.getSignedPlugins()) // []

ejectAll

Reset the Redel plugins. This is useful when you want to remove all your plugins at once.

Note: The axios instance will be saved.

Redel.ejectAll()

getSignedPlugins

Return Array of singed plugins name.

Exmaple

const Redel = require('redel')
const axios = require('axios')

Redel.use(axios, { log: true, cancel: true })

console.log(Redel.getSignedPlugins()) // ['log', 'cancel']

getPendingRequests

Return Array of string, that each string contain the url of pending request.

Example

const Redel = require('redel')
const axios = require('axios')

Redel.use(axios, { pending: true })

axios.get('https://jsonplaceholder.typicode.com/todos/1')
setTimeout(() => {
    console.log(Redel.getPendingRequests()) // ["/todos/1"]
})

clearPendingRequests

Clear the pending request array.

Redel.clearPendingRequests()

cancelGroupRequests

Cancel all requests that belong to the groupKey. Click here for more information

Redel.cancelGroupRequests('cancelGroupKey')

getCancelGroupHeader

sign request to cancel group.

Redel.getCancelGroupHeader()

You can find examples here