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

@allez/weapp-request

v1.0.2

Published

**参考了 Axios,用于微信小程序发起网络请求。**

Downloads

7

Readme

Weapp Request

参考了 Axios,用于微信小程序发起网络请求。

Axios 官方文档

特性

  • 支持 Promise API
  • 拦截请求和响应
  • 封装 wx.request\wx.uploadFile\wx.downloadFile\wx.connectSocket,一致的语法结构
  • 处理 Task 任务对象
  • release 环境支持请求体的本地打印以及本地日志记录

发起 wx.request 请求(默认为GET请求)

import request from '@allez/weapp-request'

request.request('/user/12345')

request.request({ method: 'post', url: '/user/12345', data: { firstName: 'Fred' } })

使用别名

request.request(config)

request.get(url[, data[, config]])

request.delete(url[, data[, config]])

request.head(url[, data[, config]])

request.options(url[, data[, config]])

request.post(url[, data[, config]])

request.put(url[, data[, config]])

request.patch(url[, data[, config]])

发起 wx.uploadFile 请求

// 传入 name 以及 filePath 参数会自动发起 wx.uploadFile 请求
request({ url: 'www.abc.com', name: 'name', filePath: 'filePath' })

使用别名

request.uploadFile('www.abc.com', 'filePath', 'name', {...config})

request.uploadFile({ url: 'www.abc.com', filePath: 'filePath', name: 'name', ...config })

发起 wx.downloadFile 请求

// 传入 filePath 参数会自动发起 wx.downloadFile 请求(wx.downloadFile中的 filePath 为非必填项,可传入空值)
request({ url: 'www.abc.com', filePath: '' })

使用别名

request.downloadFile('www.abc.com', 'filePath', {...config})

request.downloadFile({ url: 'www.abc.com', filePath: 'filePath', ...config })

发起 wx.connectSocket 请求

request.connectSocket('www.abc.com', {...config})

request.connectSocket({ url: 'www.abc.com', ...config })

拦截请求和响应

request.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  console.log('请求被拦截了')
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
})
request.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  console.log('响应被拦截了')
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
})

处理 Task 任务对象

wx.request\wx.uploadFile\wx.downloadFile\wx.connectSocket 拥有相同的处理方法。

在 config 内传入合法Task方法的 task 对象。

RequestTask

request.downloadFile({
  url: 'www.abc.com',
  task: {
    // task对象键值命名必须与官方文档Task提供的函数名一致
    onProgressUpdate: (res, task) => {
      // 使用自定义函数接收参数
      yourOnProgressUpdateFun(res, task)
    },
    offProgressUpdate: (res, task) => {
      yourOffProgressUpdateFunc(res, task)
    },
    // 如果不是on监听事件的回调函数,request传入的是task对象
    abort: task => {
      yourAbortFunc(task)
    },
  },
});

function yourAbortFunc(task) {
  if (true) {
    task.abort()
  }
}