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

@duying/axios-plus

v1.0.9

Published

axios扩展类

Downloads

1

Readme

axios-plus

安装

npm install @duying/axios-plus

示例

Http.js文件

import AxiosPlus from '@duying/axios-plus'

export default class Http extends AxiosPlus {
  baseURL = 'https://some-domain.com/api'

  urlPrefix = '/a'

  headers = {
    'custom-name': 'hello',
    'custom-age': 18
  }

  constructor () {
    super()
    this.addCustomInterceptors()
  }

  addCustomInterceptors () {
    // 添加请求拦截器
    this.addInterceptor('request', 'key1', [
      function (config) {
        // 在发送请求之前做些什么
        // this 关键字为 AxiosEngine 实例
        return config
      },
      function (error) {
        // 对请求错误做些什么
        // this 关键字为 AxiosEngine 实例
        return Promise.reject(error)
      }
    ])
    // 添加响应拦截器
    this.addInterceptor('response', 'key2', [
      function (response) {
        // 2xx 范围内的状态码都会触发该函数
        // 对响应数据做点什么
        // this 关键字为 AxiosEngine 实例
        return response
      },
      function (error) {
        // 超出 2xx 范围的状态码都会触发该函数
        // 对响应错误做点什么
        // this 关键字为 AxiosEngine 实例
        return Promise.reject(error)
      }
    ])
  }
}

CustomApi.js文件

import Http from './Http.js'

export default class CustomApi {
  // 发起 get 请求
  // https://some-domain.com/api/a/getList?keyword=abc
  static getAction () {
    const http = new Http()
    const params = { keyword: 'abc' }
    return http.get('/getList', { params })
  }

  // 发起 post 请求
  // https://some-domain.com/api/a/submitOrder
  static submitAction () {
    const http = new Http()
    const data = { title: '我是标题', content: '我是正文' }
    return http.post('/submitOrder', { data })
  }
}

AxiosPlus 类属性

| 属性名 | 类型 | 说明 | 默认值 | |--------------|--------|----------------------------------------------------------------------------|-------| | baseURL | String | 相对于 url 的路径,完整API地址由 baseURL+urlPrefix+url组成 | - | | urlPrefix | String | 相对于 url 的路径 | - | | headers | Object | 自定义请求头 | {} | | timeout | Number | 如果请求时间超过 timeout 的值,则请求会被中断,0 永不超时,单位:毫秒 | 60000 | | responseType | String | 浏览器将要响应的数据类型,可选值包括: arraybuffer document json text stream blob | json |

AxiosPlus 类方法

| 方法名 | 说明 | |----------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------| | get(url: String, { params?: Object, headers?: Object, responseType?: String }): Promise | GET请求 | | delete(url: String, { params?: Object, headers?: Object, responseType?: String }): Promise | DELETE请求 |
| post(url: String, { params?: Object, headers?: Object, responseType?: String, data?: any }): Promise | POST请求 |
| put(url: String, { params?: Object, headers?: Object, responseType?: String, data?: any }): Promise | PUT请求 | | patch(url: String, { params?: Object, headers?: Object, responseType?: String, data?: any }): Promise | PATCH请求 |
| addInterceptor(type: String, key: String, [onFulfilled?: (value: any) => value, onRejected?: (error: any) => error]): void | 添加拦截器。参数type可选值为 request response,不区分大小写 | | removeInterceptor(type: String, key: String): void | 移除拦截器。参数type可选值为 request response,不区分大小写 | | validateStatus(status: Number): Boolean | 定义了对于给定的 HTTP状态码是 resolve 还是 reject promise。默认状态码为 2xx 时 resolve, 其它为 reject promise | | paramsSerializer(params: Object): String | 序列化params |