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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vision-uni-request

v0.1.2

Published

### 安装

Readme

Vision Uni Request

安装

npm install vision-uni-request

基础示例

import request from 'vision-uni-request'

// get 请求
request
  .get('/user', { params: { id: '12345' } })
  .then(response => {
    console.log(response)
  })
  .catch(error => {
    console.log(error)
  })

// post 请求
request
  .post('/user', { data: { id: '12345' } })
  .then(response => {
    console.log(response)
  })
  .catch(error => {
    console.log(error)
  })

createInstance

通过 createInstance 创建一个 request 实例

import { createInstance } from 'vision-uni-request'

const request = createInstance({
  baseURL: 'http://www.example.com/api/',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})

Config

vision-uni-request 内部,每个参数都保留了其默认值,因此这些参数都不是必须的。传入的请求参数将在调用 uni.request()uni.upload()uni.download() 之前转换为请求载体实际需要的参数。

| 参数名 | 类型 | 说明 | 默认值 | | ------------------ | -------------------------- | --------------------------------------------------------------------------------------------------------------------- | ----------------------------- | | url | String | 开发者服务器接口地址。 | - | | method | String | 请求方法。 | get | | baseURL | String | baseURL 将自动拼接在 url 前面,除非 url 是一个绝对 URL。 | - | | headers | Object | 自定义请求头。 | {} | | params | Object | 语义化参数,本质与 data 参数相同,只在执行 get 请求时有效,此时指定的 data 参数是无效的。 | {} | | data | Object | 请求发送的数据。 | {} | | timeout | Number | 请求超时时间,单位毫秒(0 表示无超时时间)。 | 30000 | | withCredentials | Boolean | 跨域请求时是否需要使用凭证。 | false | | responseType | String | 服务器响应数据类型,有效值为 'text'、'arraybuffer'。 | text | | fileKey | String | methodupload 时有效,上传文件对应的 key。 | file | | filePath | String | methodupload 时有效,上传文件对应的资源路径。 | - | | onUploadProgress | (progressEvent) => void | 上传进度事件(未实现)。 | - | | onDownloadProgress | (progressEvent) => void | 下载进度事件(未实现)。 | - | | validateStatus | (status:number) => boolean | 定义对于给定的 HTTP 响应状态码请求 Promiseresolve 还是 reject,返回 trueresolve ,反之为 reject。 | status >= 200 && status < 300 |

method

参数值支持大小写,调用请求载体前将自动转换为大写。

  • get / GET
  • post / POST
  • put / PUT
  • delete / DELETE
  • head / HEAD
  • options / OPTIONS
  • trace / TRACE
  • connect / CONNECT
  • upload / UPLOAD
  • download / DOWNLOAD

请求别名

默认导出的请求实例和通过 createInstance 创建的请求实例都可通过实例上的方法别名发起请求。

  • request.get(config)
  • request.post(config)
  • request.put(config)
  • request.delete(config)
  • request.head(config)
  • request.options(config)
  • request.trace(config)
  • request.connect(config)
  • request.upload(config)
  • request.download(config)
request.get({
  params: {
    firstName: '',
    lastName: ''
  }
})

request.post({
  params: {
    firstName: '',
    lastName: ''
  }
})

默认参数

通过实例上的 defaults 可以读取和修改实例默认 Config 值。

import request from 'vision-uni-request'

request.defaults.baseURL = 'http://www.example.com/api/'
request.defaults.timeout = 10000

响应结构

请求成功的响应包含以下信息。

{
  // 服务器提供的响应数据
  "data": {},

  // 服务器响应的 HTTP 状态码
  "status": 200,

  // 服务器响应的头
  "headers": {},

  // 服务器响应的 cookie
  "cookies": []
}

配置优先级

请求的 config 参数 > 实例的 defaults 属性 > vision-uni-request 默认值。

拦截器

通过实例上的 interceptors 添加拦截器。

// 添加请求拦截器
const requestInterceptor = request.interceptors.request.use(
  config => {
    // do something
    return config
  },
  error => {
    // do something
    Promise.reject(error)
  }
)

// 移除请求拦截器
request.interceptors.request.reject(requestInterceptor)

// 添加响应拦截器
const responseInterceptor = request.interceptors.response.use(
  response => {
    // do something
    return response
  },
  error => {
    // do something
    Promise.reject(error)
  }
)

// 移除响应拦截器
request.interceptors.response.reject(responseInterceptor)