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

simple-axios-wechat

v0.2.1

Published

小于1kb的简易的axios微信小程序版使用typescript实现

Downloads

28

Readme

simple-axios-wechat

  • 小于1kb的简易的axios微信小程序版使用typescript实现

目录

Installing

Package manager

Using npm:

$ npm install simple-axios-wechat

Using yarn:

$ yarn add simple-axios-wechat

直接调用

import SimpleAxios from 'simple-axios-wechat'

SimpleAxios<{result: any}>(config)
  .then(res => { console.log(res) })
  .catch(err => { console.error(err) })

method别名

  • 与SimpleAxios.create()实例一致
axios.request<{result: any}>(config)
axios.get<{result: any}>(url[, config])
axios.post<{result: any}>(url[, data[, config]])
axios.put<{result: any}>(url[, data[, config]])
axios.delete<{result: any}>(url[, config])
axios.head<{result: any}>(url[, config])
axios.options<{result: any}>(url[, config])
axios.trace<{result: any}>(url[, data[, config]])
axios.connect<{result: any}>(url[, data[, config]])

Example

get请求并定义返回数据类型

import SimpleAxios from 'simple-axios-wechat'
// 默认 get 请求
SimpleAxios<{result: string}>({ url: 'http://example.org/get' })
  .then(res => { console.log(res) })
  .catch(err => { console.error(err) })

SimpleAxios.get<{result: string}>('http://example.org/get')
  .then(res => { console.log(res) })
  .catch(err => { console.error(err) })

post请求并定义返回数据类型

SimpleAxios<{result: string}>({ url: 'http://example.org/get', method: 'POST' })
  .then(res => { console.log(res) })
  .catch(err => { console.error(err) })

SimpleAxios.post<{result: string}>('http://example.org/post', { data: {} })
  .then((res) => { console.log(res) })
  .catch((err) => { console.error(err) });

全局默认值

  • (默认值优先级与axios一致,后设置大于先设置)
import SimpleAxios from 'simple-axios-wechat'

SimpleAxios.defaults.baseURL = 'http://example.org/get'
SimpleAxios.defaults.timeout = 10 * 1000

创建实例

import SimpleAxios from 'simple-axios-wechat'

const simpleAxios = SimpleAxios.create({
  baseURL: 'http://example.org/get',
  timeout: 10 * 1000
})

拦截器

import SimpleAxios from 'simple-axios-wechat'

const simpleAxios = SimpleAxios.create({
  baseURL: 'http://example.org/get'
})

simpleAxios.interceptors.request.use((config) => {
  // 干点啥?
  return config
}, (err) => {
  return Promise.reject(err)
})

simpleAxios.interceptors.response.use((response) => {
  // 总得干点啥吧?
  return response
}, (err) => {
  return Promise.reject(err)
})

获取RequestTask

  • 在请求config配置项中通过回调获取RequestTask(比如用来 abort())
import SimpleAxios from 'simple-axios-wechat'

SimpleAxios<{data: any, state: number}>({
  url: 'http://example.org/get',
  getRequestTask: (task) => { task.abort() }
})
  .catch(err => { console.error(err) })

自定义请求方法

  • (要求返回一个Promise)
import SimpleAxios from 'simple-axios-wechat'

const simpleAxios = SimpleAxios.create({
  baseURL: 'http://example.org/get',
  adapter: function adapter(config) {
    return new Promise((resolve, reject) => {
      const RequestTask = wx.request({
        ...config,
        success: (res) => { resolve(res) },
        fail: (err) => { reject(err) }
      })
      // ! ** 自定义adapter需要通过回调暴露RequestTask **
      config.getRequestTask && config.getRequestTask(RequestTask)
    })
  }
})