2.0.0 • Published 6 years ago
see-ajax v2.0.0
see-ajax
An ajax wrapper, with response refactoring, pre handling, post handling, etc.
requirements
related
note
Only for json response.
quick start
npm install see-ajax --saveimport seeAjax from 'see-ajax';
// configure application
seeAjax.config(name, {
method,
stringify,
settings,
url,
req,
refactor,
pre,
post,
implement,
});
// make a request
seeAjax(name, params, successCallback);config options
method: which http method to use
type:stringdefault:get
'post/put/delete'stringify: whether to stringify request params
type:booldefault:false
If true, the server will receive string, but not key-value pairs.
If GET method, request params will not stringify at any time.
settings: extra reqwest options
type:mapdefault:{}
url: url to request
type:stringdefault: empty string
req/requestKeys: keys mapping of request params
type:mapdefault:{}
{sourceKey: 'newKey'}refactor/responseRefactor: rules to refactor response using json-refactor
type:mapdefault:{}
refactor: rulesrules: see json-refactor
pre/preHandle: more handling to request params
type:function
(params, name) => {... modify params, or return a new params ...}post/postHandle: more handling to response data
type:function
(result, params, name) => {... modify result, or return a new result }implement: custom implementing instead of ajax
type:function
(cb, params) => { ... cb(result) }Sometimes, you have to not use ajax, but other ways, like html templates.
api
seeAjax.config: configure application
// one
seeAjax.config(name, options);
// multiple
seeAjax.config({
name1: options1,
name2: options2,
...
});seeAjax.setEnv: set current environment(index to get config options)
seeAjax.setEnv(0/1/2/3);If you need multiple environments supports, you can configure all config options by array, and then set a env.
If you don't set an environment, 0 will be the default.
seeAjax.config(name, {
method: [method1, method2, ...],
stringify: [stringify1, stringify2, ...],
settings: [settings1, settings2, ...],
url: [url1, url2, ...],
req: [req1, req2, ...],
refactor: [refactor1, refactor2, ...],
pre: [pre1, pre2, ...],
post: [post1, post2, ...],
implement: [implement1, implement2, ...],
});
seeAjax.setEnv(0); // method1, stringify1, url1, ...
seeAjax.setEnv(1); // method2, stringify2, url2, ...seeAjax.getEnv: get current environment
const env = seeAjax.getEnv(); // 0/1/2/3seeAjax: make a request
seeAjax(name, params, successCallback);name: defined request namenote:commonis a special request name, and it will apply to all requests
params: request paramstype:mapexample:{a: 1, b: '2'}
successCallback: callback when ajax successexample:result => { ... }result: handled response data. But if response's status code is3XX, 4XX, 5XX,resultwill be like:{error: true, response: Response}error: mark response having an error, and you can customize it byseeAjax.set({errorField: 'yourErrorField'})response: original XMLHttpRequest Object
seeAjax.set: set custom config
seeAjax.set({
errorField: 'error',
debug: !0,
disableCache: !0,
disableCacheField: '_',
});errorField:type: stringdefault: errorconfigure your own error fielddebug:type: booldefault: truewhether in debug modedisableCache:type: booldefault: truedisable request cache forGET, HEADmethodsdisableCacheField:type: stringdefault: _field name for appending timestamp to original url whendisableCacheistrue
handlers sequences while processing
method: check which http method to use, default isGETstringify: check whether to stringify request paramssettings: check extra reqwest settingsurl: get request urlreq: get real request paramspre: more handling before send a requestcommon: common handling, if havename: named handling
implement: if have,see-ajaxwill not send anajaxrefactor: refactoring response datacommon: common handling, if havename: named handling
post: more handling after refactoring response datacommon: common handling, if havename: named handling