Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@wya/http

wya-team14MIT1.5.3-alpha.2

wya-http for js

readme

@wya/http

npm changelog

安装

npm install @wya/http --save

用法例子

import createHttpClient, { ajax } from '@wya/http';

let cancelCb;

ajax({
    url: 'http://***.com/{id}?userName={user.name}', // 支持动态路由
    type: "GET",
    param: {
        id: '2',
        user: {
            name: 'wya-team',
            age: ''
        }
    },
    requestType: "form-data:json",
    getInstance: ({ xhr, cancel }) => cancelCb = cancel,
    debug: true
}).then((res) => {
    console.log(res, 0);
}).catch((res) => {
    console.log(res);
});

setTimeout(() => {
    cancelCb();
}, 100);

API

属性 说明 类型 默认值
createHttpClient 注册函数 (globalOptions = {}) => Func -
ajax ajax函数 (userOptions = {}) => Promise -
const { ajax } = createHttpClient();
  • createHttpClient - 参数说明
属性 说明 类型 默认值
globalOptions 可以给下面的userOptions设置些默认值 obj -
  • 参数配置原则: userOptions(single) > registerOptions(HOF) > defaultOptions(single)

  • ajax - 参数说明 - 属性

属性 说明 类型 默认值
url 请求地址path str -
type 请求类型 str GET
param 参数 obj -
async 请求是否是异步 bool true
debug 测试 bool false
requestType form-datajsonform-data:json(POST方式以 data: JSON.stringify(data)传递) str form-data
allowEmptyString 是否接收空字符串 bool false
loading 执行loadingFnloadedFn boolean true
localData 假如数据有缓存,不请求ajax obj -
apis - - -
responseType - - -
credentials - - -
headers - - -
useXHR - - -
restful - - -
timeout 单位s - -
delay 单位s - -
  • ajax - 参数说明 - 方法
属性 说明 类型 默认值
onLoading 请求时回调 ({ options }) => void -
onLoaded 请求完回调,可以把loading移除 ({ options }) => void -
onBefore 在调用前改变options - 拦截options ({ options }) => Promise -
onAfter 在调用后改变response - 拦截response ({ response, options }) => Promise -
onOther status !1或!0,以外的情 ({ response, options }) => void -
onProgress 上传进度回调 (e) => void -
getInstance 获取XHR实例 ({ xhr, options, cancel }) => void -

changelog

历史版本

1.0.12 (2019-03-18)

  • xhr.responseText为空时, 返回 -> { httpStatus }
  • emptyStr -> allowEmptyString

1.0.4 (2019-03-18)

  • 移除 restful API -> /{books_id}/{article_id}
  • emptyStr -> allowEmptyString

1.0.0 (2019-03-10)

  • wya-fetch -> @wya/http

  • ajaxFn -> createHTTPClient

  • 所有的错误状态都会进入catch
  • 除onProgress, 其他hook参数均接受对象, 参考文档

0.3.0 (2018-08-07)

  • ajaxFn 只接收 defaultOptions

0.2.0 (2018-08-06)

  • 增加APIemptyStr
  • 增加APIrestful
  • 增加APIasync
  • 去除APIsetCb

0.1.12 (2018-01-23)

  • 增加APIonBefore, 类型func, 返回值必须是Promise; async/await去兼容RN/AsyncStorage返回是Promise的情况;即(opts) => Promise
  • 增加APIonAfter, 类型func, 返回值必须是Promise;

0.1.8 (2018-01-23)

  • requestType添加选项'form-data:json',form-data形式传递的数据再包装成 data: JSON.stringify(data)

0.1.5 (2018-01-22)

  • 处理Android Native, '' -> undefined, 后者undefined更加友好;
  • 添加主动取消xhr.__ABORTED__ = true

0.1.3 (2018-01-15)

  • 使用XMLHttpRequest取代fetch, 方便跨域

0.1.0 (2018-01-02)

  • 添加仓库