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

axios-for-alipay

v0.0.1

Published

基于axios的小程序请求方法封装,支持axios大部分特性

Downloads

4

Readme

axios-for-alipay

Axios 小程序版本

API 文档

  • 使用方法
npm i axios-for-alipay

支付宝小程序灰度测试中,在 IDE 中进行安装后直接引入即可。

在 App.js 中

const { axios } = require('axios-for-alipay')

my.axios = axios() // 必须调用 axios 才能实例化成功

在其他页面中

Page({
  onLoad(options) {
    my.axios.get('xxxxx').then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  }
})

全局配置列表

| 参数名 | 类型 | 默认值 | 说明 | | ------------ | ------- | ------------ | --------------------------------------------------------------------------------------------------- | | loading | String | 'Loading...' | my.showLoading 的 titile, 存在此项则会开启请求 Loading | | baseUrl | String | '' | my.request 中 url 的前缀拼接,作用和 axios.js 中 baseURL 一样 | | loadingAwait | Number | 800(ms) | 即如果开启 Loading 后,必须接口请求的 pendding 时间大于该时间才会显示 Loading,以获得更好的用户体验 | | loadingMask | Boolean | true | my.showLoading 的 mask 属性,控制遮罩层 | | timeout | Number | 0 | my.request 的 timeout 属性,代表接口超时时间,0 代表不设置 |

const { axios } = require('axios-for-alipay')

my.axios = axios({
  timeout: 30000,
  loadingAwait: 800
})

方法列表

axios.get

使用方法基本同 axios.js 中的 get 方法

my.axios.get(url[, data[, config]])

| 参数名 | 类型 | 默认值 | 说明 | | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------ | | url | String | '' | API 地址, 即 my.request 的 url,注意,该属性会拼接全局设置的 baseUrl,同时,如果 opt 中设置了 baseUrl, 则以 opt 中为准 | | data | Object | {} | my.request 中的 data,无论何种形式的请求,请保证请求体均为一个对象 | | config | Object | {} | 接口独享的额外配置参数,和 axios.js 一样,优先级最高,最终会被合并进 my.request 中 |

axios.post

使用方法基本同 axios.js 中的 post 方法

my.axios.post(url[, data[, config]])

| 参数名 | 类型 | 默认值 | 说明 | | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------ | | url | String | '' | API 地址, 即 my.request 的 url,注意,该属性会拼接全局设置的 baseUrl,同时,如果 opt 中设置了 baseUrl, 则以 opt 中为准 | | data | Object | {} | my.request 中的 data,无论何种形式的请求,请保证请求体均为一个对象 | | config | Object | {} | 接口独享的额外配置参数,和 axios.js 一样,优先级最高,最终会被合并进 my.request 中 |

axios.delete

使用方法基本同 axios.js 中的 delete 方法

my.axios.delete(url[, config])

| 参数名 | 类型 | 默认值 | 说明 | | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------ | | url | String | '' | API 地址, 即 my.request 的 url,注意,该属性会拼接全局设置的 baseUrl,同时,如果 opt 中设置了 baseUrl, 则以 opt 中为准 | | config | Object | {} | 接口独享的额外配置参数,和 axios.js 一样,优先级最高,最终会被合并进 my.request 中 |

axios.options

使用方法基本同 axios.js 中的 options 方法

my.axios.options(url[, config])

| 参数名 | 类型 | 默认值 | 说明 | | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------ | | url | String | '' | API 地址, 即 my.request 的 url,注意,该属性会拼接全局设置的 baseUrl,同时,如果 opt 中设置了 baseUrl, 则以 opt 中为准 | | config | Object | {} | 接口独享的额外配置参数,和 axios.js 一样,优先级最高,最终会被合并进 my.request 中 |

axios.head

使用方法基本同 axios.js 中的 head 方法

my.axios.head(url[, config])

| 参数名 | 类型 | 默认值 | 说明 | | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------ | | url | String | '' | API 地址, 即 my.request 的 url,注意,该属性会拼接全局设置的 baseUrl,同时,如果 opt 中设置了 baseUrl, 则以 opt 中为准 | | config | Object | {} | 接口独享的额外配置参数,和 axios.js 一样,优先级最高,最终会被合并进 my.request 中 |

axios.put

使用方法基本同 axios.js 中的 put 方法

my.axios.put(url[, data[, config]])

| 参数名 | 类型 | 默认值 | 说明 | | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------ | | url | String | '' | API 地址, 即 my.request 的 url,注意,该属性会拼接全局设置的 baseUrl,同时,如果 opt 中设置了 baseUrl, 则以 opt 中为准 | | data | Object | {} | my.request 中的 data,无论何种形式的请求,请保证请求体均为一个对象 | | config | Object | {} | 接口独享的额外配置参数,和 axios.js 一样,优先级最高,最终会被合并进 my.request 中 |

axios.all

执行并发请求,全部请求成功才会进入成功的回调,有任何一个错误均会进入 catch 回调

let v1 = my.axios.get(api1)
let v2 = my.axios.get(api2)
my.axios.all([v1, v2]).then(([r1, r2]) => {
  console.log(r1, r2)
})

原理为 Promise.all

具体可以参考 axios.js 文档

axios.race

执行并发请求,任何一个请求返回后就会进入成功的回调

即无论有多少个请求发出,只会收到第一个返回成功的请求,其他请求返回结果全部丢弃

let v1 = my.axios.get(api1)
let v2 = my.axios.get(api2)
my.axios.all([v1, v2]).then(([r]) => {
  console.log(r1)
})

原理为 Promise.race

具体可以参考 axios.js 文档

axios.spread

并发请求中使用,对返回结果进行展开,具体可以参考 axios.js 文档

let v1 = my.axios.get(api1)
let v2 = my.axios.get(api2)
my.axios.all([v1, v2]).then(my.axios.spread((r1, r2) => {
  console.log(r1, r2)
}))

拦截器 interceptors

分为 request 和 response 拦截器

使用方法和 axios.js 拦截器完全一样,具体可以参考 axios.js