fetchie
enhanced fetch api
Author: Ash Zhang
基本用法
;
支持的方法:get、post、put、del(delete)、head
最后必须用.then
才会正式发出请求
fetchie ;
.query(params)
以下请求会转化成:/gift?pageNum=1&pageSize=16
fetchie ;
.send(data)
发送 json 数据
fetchie ;
.append(name, file, fileName)
添加文件,用在 then 之前,send 之后
fetchie
.set(header)
添加 header
.setType(contentType)
设置 Content-Type,支持json
、html
、text
或xml
.accept(type)
设置 Accept
.cors(needCors)
是否需要跨域
.prefix(host)
为 url 添加前缀,以下请求的 url 是://localhost:9090/gifts
fetchie prefix'//localhost:9000' ;
.timeout(ms)
设置超时(毫秒)
.handleError(cb)
处理请求的错误返回,必须在 then 之前
fetchie ;
全局设置
.use(middleware)
用 use 添加全局的 middleware
fetchie
所有的请求都会添加前缀、跨域、并在 1 秒内 timeout
.success(cb)
和 .error(cb)
全局性的成功和失败回调
fetchie ;
Mock 数据
可以添加本地 mock,配合 hot-loader 实现即时刷新的数据 mock,非常方便
;; fetchie
mock-data.json 的格式如下:路径-请求方法-数据
路径的格式和 express 相同
"gifts/:id?": "get": "id": 1 "name": "Gift 1" "id": 2 "name": "Gift 2" "post": "name": "$$name$$" "put": "id": 3 "error": "error": true
也可以用函数
"gifts/:id?": { if paramid return "id": paramid "name": "Gift 1" ; return "id": 1 "name": "Gift 1" "id": 2 "name": "Gift 2" ; }
注:error
下为请求错误时的返回数据,要 mock 一个错误回调,只需插入一个.mockError()
:
fetchie ;