ajax-interface-checker
v0.3.0
Published
A CLI of npm package publish
Downloads
10
Readme
ajax-interface-checker
Introduction
ajax-interface-checker module would offer your all supports in http request ,
which contains http request methods 、 http error throw and catch module 、
http proxy module and http window module .
When you are developing you program , you could use ajax-interface-checker
to setting mock data 、test your page when different errors happened 、and
use real runtime window to show it .
Note that , i used `Promise` 、 `Map` and other ES6 feathers , but i do NOT
import their compatible code , because i suppose that you would do it in your
project . Right ?
module
HttpAuth
`HttpAuth` module helps you to auth your ajax result , but it aimed to handle
common logic for your ajax code .
Use Pattern
HttpAuth(ajaxRes , <statusKey> , <messageKey> , <dataKey>) ;
How to use
import { HttpAuth } from "ajax-interface-checker" ;
// normal ajax result mock
let mockRes = { code : 0 , message : 'ok' , data : {} } ;
HttpAuth(mockRes) ; // mockRes.data
// error ajax result mock
let mockResError = { code : -1 , message : 'database connect error' } ;
HttpAuth(mockResError) ; // mockRes.data
==================================
Http
Http offers three basic methods , get 、post and jsonp , to get server data .
I use axios and jsonp module to do these . You could use theme as bellow .
And I would offer cache control , but you should be more careful to USE it .
I would also offer http error test , you could use `options.hasOpenError` to run it
as bellow value .
Use Pattern
let http = new Http(<options>) ;
http.auth = function(){} ;
// get
http.get(pathname , <params> , <options> , <baseHost> ) ;
// post
http.post(pathname , <data> , <params> , <options> , <baseHost>) ;
// jsonp
http.jsonp(pathname , <params> , <options> , <baseHost>) ;
You could rewrite
http.auth
to auth your ajax result , especially some same error logic handler .http.get
method overcames a get request .pathname
sets pathname of your get request , link '/getNameList'params
sets params , which your want to catch , it is a object .options
sets basic options of ajaxcache
controls whether to use ajax cachecacheCount
sets the max count of ajax cachehasOpenError
whether to open error test
baseHost
sets base host of your get request , link "//localhost" .
http.post
method overcames a post request .pathname
sets pathname of your post request , link '/getNameList'data
sets data , which your want to catch , it is a object .params
sets params , which your want to catch , it is a object .options
sets basic options of ajax for example cache 、 cacheCount and so on .cache
controls whether to use ajax cachecacheCount
sets the max count of ajax cachehasOpenError
whether to open error test
baseHost
sets base host of your get request , link "//localhost" .
http.jsonp
method overcames a jsonp request .pathname
sets pathname of your jsonp request , link '/fetchNameList'params
sets params , which your want to catch , it is a object .options
sets basic options of ajax for example cache 、 cacheCount and so on .cache
controls whether to use ajax cachecacheCount
sets the max count of ajax cachehasOpenError
whether to open error test
baseHost
sets base host of your jsonp request , link "//localhost" .
How to use
In your config.js file , you should use NODE_ENV to setting different env about
server setting .
config.js
var env = (process.env.NODE_ENV || "development").trim() ;
var config = {
env: env ,
} ;
switch (env){
case 'production' :
Object.assign(config , {
'protocol': 'https' ,
'NODE_ENV': env ,
'apihost' : '' ,
'domain' : ''
}) ;
break ;
default :
Object.assign(config , {
'protocol': 'http' ,
'NODE_ENV': env ,
'apihost' : '' ,
'domain' : ''
}) ;
break ;
} ;
module.exports = config ;
client useage
utils/ajax.js
import { Http } from "ajax-interface-chekcer" ;
let http = new Http({
apihost : "//localhost" ,
jsonpHost : "//localhost"
}) ;
// reset ajax data auth
http.auth = function(res){
let { code = 0 , data = {} , message = 'ok' } = res ;
switch(code){
case 0 :
return data ;
case -1 :
default :
console.log("ajax interface error");
return res ;
}
} ;
http.get('/getList' , { page : 1 , pagesize : 20 } , {cache : true}).then(data => data) ;
http.post('/fetchList' , { page : 1 , pagesize : 20 } , {cache : true}).then(data => data) ;
http.jsonp('/getList' , { page : 1 , pagesize : 20 } , {cache : true}).then(data => data) ;
server useage
I would use `request` module to run server ajax , like form node to java .
import { Http } from "ajax-interface-checker" ;
import config from "../../config/index" ;
import httpCommonFail from "./httpCommonFail" ;
import request from "request" ;
import auth from "./auth" ;
// config http ajax aim source url
let httpInstance = new Http({
apihost : config.apihost ,
jsonpHost : config.jsonphost
}) ;
// default server exception catch ,
// this catching function had been overcomed in ajax-interface-checker ,
// you can rewrite it if you need .
httpInstance.httpCommonFail = httpCommonFail ;
// default interface exception catch ,
// this catching function had been overcomed in ajax-interface-checker ,
// you can rewrite it if you need .
httpInstance.auth = auth ;
// rewrite http request method to run node request
httpInstance.request = function(params = {}){
return new Promise((resolve , reject) => {
request(params , function(err , response , body = ""){
// err reject
if(err){
reject(err) ;
return ;
}
// response
if(response.statusCode * 1 === 200){
let res = null ;
// 兼容返回的不是对象的问题
try {
res = JSON.parse(body) ;
}catch(err){
res = {
state : 0 ,
code : 0 ,
data : body
}
}
resolve({ data : res }) ;
}
}) ;
}) ;
}
==================================
About Browser Compatible
IE9+ and all modern browser
LINCESE
MIT