1.2.20 • Published 4 years ago

request-h5 v1.2.20

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

request-h5

一套支持分组拦截器的 ajax框架

更新说明

  • +2019-10-26 增加全局设置 global
  • +2019-10-28 Ajax 增加原型方法 then
  • +2019-11-15 url开头出支持特殊参数传入 比如: 表示此次请求为 post
  • +2019-11-19 ajax.Request.prototype.load 方法
  • +2019-12-19 ajax.Request.prototype.shortcut 方法
  • +2019-12-20 shortcut 增加events参数

特点

  • 事件模型驱动;
  • ajax 分组支持;
  • 基于事件模型的拦截器,支持分组拦截器;
  • 支持 promise;
  • 灵活的默认配置,全局->分组->ajax;
  • 短路径支持,方便统一切换接口路径;
  • 提供实时获取服务器时间方法;
  • jsonp 和 fetch 支持。

npm.io

安装

npm install request-h5 -- save

使用

import request from "request-h5"

三层结构

npm.io

流程图

npm.io

事件说明

事件名称说明参数
timeout请求超时req
callback请求完成res
abort请求中止req
verify验证数据res
send请求发送req
progress上传进度event
opensend 之前,对传入的数据已经格式化完成req
before最先触发的事件,req 中保存的数据相对原始req
path对 url 格式化后触发

req.dataType 只有在 before 中能修改 req.isFormData 在 before 中无此属性

事件流程

npm.io

新建一个分组

let ajaxOne = new ajax.Group()

添加事件拦截器

事件作用在一个分组上或者作用在单个请求上

// 这个事件作用在分组上
ajaxOne.on("open", req => {
    // 这个事件可以看做一个请求拦截器
});

ajaxOne.on("identity-err", () => {
    // 身份异常事件
});

ajaxOne
    .get("url", res => {
        // 这里的回调相当于ajaxOne.on("callback", res => {})

        // 这里可以触发自定义的事件
        res.root.emit("identity-err");
    })
    .on("open", req => {
        // 这个事件仅作用与当前请求,并且上面那个拦截器后触发
    });

// 更多参数设置,直接通过 setConf 来设置
ajaxOne
    .get("url", res => {})
    .setConf({
        type: "json"
    });

promise 支持

// 此时全局拦截器仍然有效
ajaxOne
    .fetch({
        url
    })
    .then(res => {
        // 当res.err 为 null
    })
    .catch(res => {
        // 当res.err 为 非null
    });

全局参数设置

ajax.Group.setConf({
    // 默认使用 fetch请求
    useFetch: true,
    // 返回资源类型为 json
    resType: "json",
    // jsonp的key为 callback
    jsonpKey: "callback"
});

短路径设置

ajaxOne.setConf({
    paths: {
        // 设置短路径
        webapi: "/data/api/"
    }
});

// 使用短路径
// webapi: 被替换为 /data/api/
ajaxOne.get("webapi:getuser.html", res => {}, { token: "----" });

服务器事件自动获取

// 这里返回服务器事件
ajaxOne.getDate();
ajaxOne.get(
    "webapi:getuser.html",
    res => {
        // 这里返回服务器事件
        res.getDate();
    },
    { token: "----" }
);

options 支持参数说明

名称说明默认值
baseURL基础 url 路径空字符串
paths短路径(替换 url 中的短路径)空对象
useFetch是否使用 fetch,如果浏览器不支持,则降级为 xhrtrue
url请求 url,支持短路径空字符串
method请求方法GET
dataType请求数据格式querystring
resType返回数据格式json
param请求参数null
header请求头设置null
jsonpKeyjsonp 请求是需要的 keycallback
cacheget 请求是否有缓存true
withCredentials跨域带上 cookiefalse

事件参数 req 说明

名称说明默认值
outFlagajax 已经中止,内部使用false
method请求方法GET
url请求 url空字符串
orginURL保存的是原始传入的 urlurl
formatURL格式化后(最终使用的)的 url短路径 -> 全路径
path短路径目录截取值短路径 url 开头处 : 前面的字符,默认为 空字符串
cacheget 请求是否有缓存true
resType请求类型空字符串,默认为 querystring
isFormData是否为 FormData 数据,针对非 GET 请求按照 param 类型数据自动判定
param请求参数空对象
header请求头空对象
baseURL基础 url 路径空字符串
isCross是否跨域按照 url 路径自动判断
xhrxhr 请求的对象
withCredentials跨域带上 cookiefalse

事件参数 res 说明

名称说明默认值
withReqreq 的引用req
rootthis 引用Ajax
err错误内容默认为 null,便是正确返回
jsonjson object空对象
text获取到的字符串空字符串
result返回内容
getHeader(key)获取 key 对应的 headernull
cancel验证节点设置为 true,中止回调false
status状态吗0
getDate()获取服务器时间
getJSON(key)获取 res.json 中的子字符串
jsonKeyjsonKey 中的对象json
  • 设置全局参数,作用于全局的默认参数

AjaxGroup 原型方法

setConf(options)

  • 当前分组实例的默认参数

create(options):Ajax

  • 当前分组下创建一个 ajax 请求
  • options 只作用于当前 ajax 请求
  • 返回当前 Ajax 的实例

fetch(options)

  • 当前分组下创建一个 ajax 请求,返回 Promise 对象

setDate(date:Date)

  • 设置服务器时间,一般内部使用

getDate():Date

  • 获取服务器时间

load(url, callback, param):Ajax get(url, callback, param):Ajax post(url, callback, param):Ajax put(url, callback, param):Ajax jsonp(url, callback, param):Ajax

  • 快捷方式加载

Ajax 原型方法

constructor(group, options)

  • 构造方法
  • group 为属于的分组
  • options 为预设的参数

setConf(options)

  • 仅作用于当前 ajax 请求的参数

assign(key, value)

  • 扩展当前 ajax 参数
  • key 可以为 Object

abort()

  • 中止当前 Ajax 请求

timeout(ms, callback)

  • 超时中止

send(param, isOver)

  • 发送请求
  • param 请求参数
  • isOver, 如果当此 ajax 正在请求是,是否中止 默认 false,为新发送的请求丢失

then(resolve:function)

  • 返回 Promise 对象

emit(type, ...args)

  • 触发事件
  • type 事件名称
  • args 事件参数

emit(type)

  • 判断是否有此事件

on(type, eventcall)

  • 添加事件
  • type 事件名称
  • eventcall 事件函数

off(type, eventcall)

  • 移除事件
  • type 事件名称
  • eventcall 事件函数
  • eventcall 不存在的时候,移除 type 对用的所有事件(不包含继承事件)

别名 get|post|put|jsonp(url, callback:function, param:any)

  • ajax 请求别名
  • url 请求地址,支持短路径
  • callback 回调函数
  • param 参数
  • 返回 ajax 实例

全局设置

global.setConf(options) 配置全局默认参数 global.on(type, callback) 配置全局的事件 global.off(type) 销毁全局的事件

1.2.20

4 years ago

1.2.19

4 years ago

1.2.18

4 years ago

1.2.17

4 years ago

1.2.15

4 years ago

1.2.14

4 years ago

1.2.12

4 years ago

1.2.13

4 years ago

1.2.11

4 years ago

1.2.10

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.6

4 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago