API

可以直接向导入的 un 传递相关配置来发起请求。

un(config)

import { un } from '@uni-helper/uni-network'

// 发起 POST 请求
un({
  method: 'POST',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone',
  },
})

un(url, config)

import { un } from '@uni-helper/uni-network'

// 发起 GET 请求(默认请求方法)
un('/user/12345')

请求别名

为了方便起见,已经为所有支持的请求方法提供了别名。在使用别名方法时,url、method、data 不需要在 config 中指定。如果同时指定,config 中指定的字段会被覆盖掉。

  • un.request(config)
  • un.download(config)
  • un.upload(config)
  • un.get(url[, config])
  • un.delete(url[, config])
  • un.head(url[, config])
  • un.options(url[, config])
  • un.trace(url[, config])
  • un.connect(url[, config])
  • un.post(url[, data[, config]])
  • un.put(url[, data[, config]])
  • un.patch(url[, data[, config]])

静态成员

un 还带有以下静态成员,可以直接使用:

成员说明
un.create(config?)用给定配置创建新实例
un.Un核心 Un 类,便于继承扩展
un.CanceledError取消错误的构造函数
un.CancelToken旧版取消令牌的构造函数
un.isCancel(value)判断值是否为取消错误
un.UnError请求错误的构造函数
un.isUnError(value)判断值是否为请求错误
un.all(values)Promise.all 的别名
un.mergeConfig(config1, config2)合并两份配置
un.HttpStatusCodeHTTP 状态码常量表
un.VERSION库的版本号
import { un } from '@uni-helper/uni-network'

const instance = un.create({ timeout: 10000 })

const results = await un.all([
  un.get('/a'),
  un.get('/b'),
])

if (un.isUnError(error)) {
  console.log(error.message)
}