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

@yandex-cloud/axios-wrapper

v1.2.2

Published

Generic helper class for wrapping axios calls

Downloads

26

Readme

Axios Wrapper

This library provides a convenient wrapper around Axios adding automatic cancelling of concurrent requests to its features.

HTTP API

Constructor parameters

config [optional]

The configuration of an axios instance.

collector [optional]

The configuration of requests collector is an object:

{
    "collectErrors": 10,
    "collectRequests": 10
}

Basic methods

Wrapper provides http-methods get, head, put, post, delete.

Methods get and head have the signature (url, params, options); put, post, while the delete method has (url, data, params, options) signature.

The params argument stands for query string parameters while options is a request settings.

Currently 4 request settings are supported:

  • concurrentId (string): optional request id
  • collectRequest (bool): optional flag, telling if the request should be logger (default true)
  • requestConfig (object): optional config with the custom request parameters
  • headers (object): optional object with custom request headers.
  • timeout (number): optional request timeout
  • onDownloadProgress (function): optional callback for processing file download progress

Headers

The setDefaultHeader({name (string), value (string), methods (array)}) method allows to add a default request header.

Arguments name and value are required, optional argument methods specified all methods which get those default headers (by default all methods will get those headers).

CSRF

The setCSRFToken method allows specifying CSRF-token, which will be added to all put, post and delete requests.

Concurrent requests

Sometimes it is better to cancel the request in flight if its results are no longer needed. To make this happen, one should pass to request's options the concurrentId id. When the next request with the same concurrentId occurs the previous request with that id will be cancelled.

One cancel a request manually as well by invoking the cancelRequest(concurrentId) method.

Collecting requests

It is possible to set up requests collection into the local storage using the collector option. It stores all requests and errors separately. The following apiInstance will keep 10 last requests (both successful and not) and 10 last erroneous requests.

const apiInstance = new API({
    collector: {
        collectErrors: 10,
        collectRequests: 10
    }
});

To obtain saved requests one have to invoke the getCollectedRequests method which returns the object {errors: [...], requests: [...]}.

Usage

The suggested usage is to subclass the base AxiosWrapper class:

export class API extends AxiosWrapper {
    getProjects() {
        return this.get('/projects');
    }
    getSensors({project, selectors}) {
        return this.get(`/projects/${project}/sensors`, {selectors, pageSize: 200});
    }
    getNames({project, selectors}) {
        return this.get(`/projects/${project}/sensors/names`, {selectors});
    }
    getLabels({project, names, selectors}) {
        return this.get(`/projects/${project}/sensors/labels`, {names, selectors});
    }
}

When the baseURL parameter is passed into axios config, all requested pathnames will be appended to it.

const apiInstance = new API({
    config: {
        baseURL: '/api/v2'
    }
});