Locomon ·
WHAT IS LOCOMON
Locomon is a fictional creature from the "Digimon" series. A mechanical digimon of locomotives that exists to transport network data. It thinks the most important thing is how to send the data. But as a result of that, so occasionally there are violent things.
Installation
npm i locomon
Documentation
LocomonBody
辅助对象,用于对http返回信息的封装
// http status status: 200 // body // 在success时,boby是一个对象,由Response.json()得来, // 在error时,body是一个字符串,由Response.text()得来。 body: {} || ""
LocomonError
辅助对象,用于对http错误的封装
// 错误名称 name: "error name" // response.text() body: LocomonBody // 错误信息,若未设置,由错误名+response.text()中的报错信息拼接而成 message: "error message"
LocomonSetting
辅助对象,包含设置信息
defaultConfig
对请求配置进行全局设置
默认值:
// 默认值不对config做任何设置{}
// 若对所有方法进行设置,则使用"default"作为属性 "default": headers: ... credentials: "include" // 若要对具体的方法进行设置,则使用method的小写作为属性 "get": headers: ...
statusValidation
statusValidation会对http status进行判断,对不符合要求的status进行报错处理,进入onError回调,默认合法的status范围是[200, 400);
/** * function statusValidation * @params status * @return boolean */
onError
当statusValidation返回false时,表明http status未通过校验,则进入onError流程
onError方法调用时传入一个LocomonError对象作为参数
{ ... throw ...}
onSuccess
当statusValidation返回true时,表明http status已通过校验,进入onSuccess流程 onSuccess方法调用时传入一个LocomonBody对象作为参数
{ ... return ...}
Locomon.setup(LocomonSetting);
setup 方法接受一个参数LocomonSetting,用于对请求进行全局设置
Locomon.request(url, config, LocomonSetting)
config.params用于拼接get 请求的query config.data用于拼接其他请求的body
config.body会被忽略
注意 使用request时,LocomonSetting中的defaultConfig字段会被忽略 fetch中实际使用的config为{...defaultMethodConfig, ...config} 实际使用的setting为{...defaultSetting, ...LocomonSetting}
Locomon.get
等同于 Locomon.request(url, {method: "get", ...}, LocomonSetting);
Locomon.post
等同于 Locomon.request(url, {method: "post", ...}, LocomonSetting);
注意!
目前只有get方法和post方法被单独抽出,其他方法还需使用request
Example
const defaultSettings = defaultConfig: default: headers: "content-type": "application/json" get: headers: {} { return status >= 200 && status < 400; } { if errorstatus === 500 ... ; else if errorstatus === 404 ... } { if resstatus === 200 // 某些自定义特殊条件 if resret === 303 throw ... } Locomon; Locomon