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

mp-request-promise

v2.0.1

Published

Promise based HTTP client for miniprogram. 基于promise的小程序网络请求库,实现拦截器、取消请求等功能,用法类似axios.

Downloads

7

Readme

mp-request-promise

Promise based HTTP client for miniprogram.

基于 promise 的小程序网络请求库,实现拦截器、取消请求等功能,用法类似 axios
支持微信、QQ、支付宝、百度、头条、京东、360、Taro.js、Uni-App等小程序平台。

Install

npm install mp-request-promise

Usage

用法和 axios 一致, config 配置信息同小程序平台。

请求API

请求方式别名:

  • request(config)
  • request(url[, config])
  • request.get(url[, config])
  • request.post(url[, config])
  • request.put(url[, config])
  • request.delete(url[, config])
  • request.head(url[, config])
  • request.options(url[, config])
  • request.trace(url[, config])
  • request.connect(url[, config])
import request from 'mp-request-promise'

// 设置通用配置
request.defaults.header['Content-Type'] = 'application/json'

// 1. get请求 
request('https://api.com')
request({ url: '', method: 'get' })
request.get('https://api.com')

// 2. post请求
request({ url: '', method: 'post', data: {} })
request.post('url', { data: {} })

新建实例

  • request.create(api[, config])
import request from 'mp-request-promise'
import Taro from '@tarojs/taro'


// 创建一个 `Taro` 平台的新对象
const instance = request.create(Taro, {
  baseURL: 'https://api.com',
  timeout: 6000,
  header: {
    'X-Request-With': 'XMLHttpRequest'
  }
})

拦截器

在请求或响应被 then 或 catch 处理前拦截它们。

import request from 'mp-request-promise'

// 添加请求拦截器
request.interceptors.request.use(
  config => {
    // 在发送请求之前做些什么
    return config
  },
  error => {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)

// 添加响应拦截器
request.interceptors.response.use(
  response => {
    // 对响应数据做点什么
    return response
  },
  error => {
    // 对响应错误做点什么
    return Promise.reject(error)
  }
)

给自定义的实例添加拦截器。

import request from 'mp-request-promise'

const instance = request.create()
instance.interceptors.request.use(function () {/*...*/})

取消请求

import request from 'mp-request-promise'

let cancel

request.get('https://api.com/users', {
  data: { userId: 9527 },
  // 创建一个 cancel token
  cancelToken: new request.CancelToken(function(_cancel) {
    // 接收一个 cancel 函数作为参数
    cancel = _cancel
  })
})

// 取消请求
cancel()