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 🙏

© 2025 – Pkg Stats / Ryan Hefner

galaxy-axios-adapter

v0.0.1

Published

```bash npm i axios-adapters -S ```

Readme

axios-adapters

Install

npm i axios-adapters -S

or

yarn add axios-adapters

Usage

import axios from 'axios';
import { compose, defaultRequest, RESTful, pagination, preventDefault } from 'axios-adapters';

const http = axios.create({
    baseURL: '/',
    adapter: compose(preventDefault(), RESTful(), pagination(), defaultRequest())
});

API

compose

组装 axios middleware adapter

compose(...adapters: AxiosMiddlewareAdapter[]): AxiosAdapter

Axios middleware adapter

function customMiddleware(options) {
    return async function customMiddlewareAdapter(config, next) {
        // do something before downstream
        // ...
        const response = await next();
        // do something after upstream
        // ...
        return response;
    };
}

defaultRequest

封装了 axios 默认的请求 adapter

发起真实请求的 axios middleware,正常情况下作为 compose 最后一个参数。

axios.create({
    adapter: compose(defaultRequest())
});

RESTful

支持 Express like style 请求

const http = axios.create({
    adapter: compose(RESTful(), defaultRequest())
});

// GET /foo/baz
http.get('/foo/:bar', {
    params: {
        bar: 'baz'
    }
});

// POST /foo/baz
http.post('/foo/:bar', {
    bar: 'baz'
});

pagination

X-Pagination Header

若请求响应中存在 X-Pagination 头部,则解析并保存到 response.pagination 中。

Axios response

| Param | Type | Default Value | Description | | ---------- | ------------ | ------------- | ----------- | | pagination | Pagination | | 分页类实例 |

const http = axios.create({
    adapter: compose(pagination(), defaultRequest())
});

/**
 * ...
 * < HTTP/1.1 200 OK
 * < Content-Type: application/json; charset=utf-8
 * < X-Pagination: {"size":10,"num":1,"total":21}
 * < Content-Length: 15
 * < Date: Wed, 18 Sep 2019 03:53:38 GMT
 * < Connection: keep-alive
 * ...
 **/
http.get('/foo').then(response => {
    // do something with `response.pagination`
    // ...
});

preventDefault

e.preventDefault()

Options

| Param | Type | Default value | Description | | ------- | ----------------------------- | ------------- | ------------ | | onError | (error: AxiosError) => void | | 请求错误处理 |

cont onError = (e) => {
    console.log('error occured:', e);
};
const http = axios.create({
    adapter: compose(preventDefault({ onError }), defaultRequest())
});

http.get('/foo'); // 如果请求出错,则默认会调用 onError

Axios error

| Param | Type | Default value | Description | | -------------- | ------------ | ------------- | ---------------------------------- | | preventDefault | () => void | | 若调用,则不会触发 options onError |

cont onError = (e) => {
    console.log('error occured:', e);
};
const http = axios.create({
    adapter: compose(preventDefault({ onError }), defaultRequest())
});

http.get('/foo').catch(e => {
    e.preventDefault();
    // do something with error
    // ...
});