axios-enhanced
Enhance axios features, use it like axios but more convenient.
Extension features
- cache
- contentType
- beforeRequest
- afterResponse
- proxyPath
- onError
- prepare
- helpers
Install
npm install --save axios-enhanced
Usage
Example
;;
settings
settings is used for setup global options.
;// need setup before invoke http()http;
instance
instance method is used for set instance options and it will inherit global options.
; var instance = http; ;// orvar request = instance;
prepare
prepare is used for preproccess request options, return a object, it will not send request.
; var request = ;// request: { url, method, headers, params, data }
Use for open or download file URL.
var request = ;// request.toURL() = url + params(rewrite paramsSerializer option to change default serialize behavior)window; // http://file.xxx.com/file?id=1// or<a href=request target="_blank" >Download</a>
Use jQuery ajax lib.
// or use jquery ajax$
Use Antd Upload Component.
;; var request = ; <Upload name="file" action=request headers=requestheaders ></Upload>
Handle file stream
;
proxyPath
proxyPath will proxy the request to local server and add the config proxyPath to the top of url.
proxyPath is true.
var promise = ;// will request ' http://localhost/users'
proxyPath is String.
var promise = ;// will request 'http://localhost/api/users'
proxyPath is Function.
var promise = ;// will request 'http://localhost/proxy/users'
Use internal Function 'proxyBaseURL' to proxy baseURL.
; var promise = ;// will request 'http://localhost/http://api.xxx.com/users'
Interceptors
request interceptor
;// or;
response interceptor
;// or;
Asynchronize Interceptors
beforeRequest
;
afterResponse
;
Transform
transformRequest
; ;
transformResponse
;
paramsSerializer
Serialize parameters.
; ;// or;
default paramsSerializer handler
Set to false or rewrite it could change default behavior.
{ return helpersqs;}
cancel
simplify cancelToken.
var abort; ; ;
API
Extension features
/** * @desc wrap and extension axios lib, suport all options with axios. * @param * @param * @param * @param * @param * @param * @param * @param * @param * @param * @param * @param {string | function} proxyPath proxyPath will proxy the request to local server and add the config proxyPath to the top of url, it could be boolean, string or function, function receive (baseURL, options) args and return a string. * @param * @other refer to https://github.com/axios/axios * @return */ /** * @desc set global options */http /** * @desc create a new instance */http /** * @desc return a preproccess object, includes { method, url, headers, params, data } properties. * @param * @return */ /** * @desc general http method * @props * HEAD: 'head', * GET: 'get', * POST: 'post', * PUT: 'put', * PATCH: 'patch', * DELETE: 'delete', * OPTIONS: 'options', * TRACE: 'trace' */Method /** * @desc general content type * @props * MULTIPART_FORM_DATA: 'multipart/form-data', * APPLICATION_JSON: 'application/json', * APPLICATION_X_WWW_FORM_URLENCODED: 'application/x-www-form-urlencoded', * APPLICATION_X_JAVASCRIPT: 'application/x-javascript', * APPLICATION_PDF: 'application/pdf', * TEXT_PLAIN: 'text/plain', * TEXT_HTML: 'text/html', * TEXT_XML: 'text/xml', * IMAGE_JPEG: 'image/jpeg', * IMAGE_GIF: 'image/gif', * IMAGE_PNG: 'image/png' */ContentType
helpers.proxy
/** * @desc rewrite baseURL like 'http://api.xxx.com' to '/http://api.xxx.com' for proxy matching * @param * @return */
helpers.qs
refer to https://www.npmjs.com/package/qs