axios-enhance
Enhance axios features, use it like axios but more powerful.
Extension features
cache,
contentType,
beforeRequest,
afterResponse,
proxyPath,
onError,
prepare,
helpers,
...
Install
npm install --save axios-enhance
Usage
Example
;;
Setup global options
;// need setup before invoke http()http;
Instance
;// default options with instancevar instance = http; ;// orinstance;
Handle file stream
;
Preprocess request data
Use for preproccess request options, return a object, it will not send request.
;// request: { url, method, headers, params, data }var request = ;
Use for open or download file URL.
var request = ;// request.toString() = url + params(need to set paramsSerializer)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.
;; { return ;} { const request = ; return <Upload name="file" action=requesturl ></Upload> ;}
upload with header token.
{ return ;} { const request = ; return <Upload name="file" action=requesturl headers=requestheaders ></Upload> ;}
Use proxy
proxyPath use baseURL
;// with none baseURL will request current location host, like '/http://localhost:8080/setUser'var promise = ; // with baseURL will request specific host, like '/http://www.beancharts.com/setUser'var promise = ;
proxyPath use String
;// will request '/api/setUser'var promise = ;
proxyPath use Function
// will request '/api/setUser'var promise = ;
Use other xhr lib.
;// use other xhr lib, will request '/http://www.beancharts.com/setUser'$; // or, will request '/api/setUser'$;
Interceptors
request interceptor
;// or;
response interceptor
;// or;
Asynchronize Interceptors
beforeRequest
;
afterResponse
;
Transform
transformRequest
; ;
transformResponse
;
Serializer
Serialize parameters.
; ;// or;
default paramsSerializer handler
Set to false or rewrite it could change default behavior.
{ return helpersqs;}
API
/** * @desc wrap and extension axios lib, suport all options with axios. * @param {axios.options...} axios options. * @param * @param * @param * @param * @param * @param * @param * @param * @param * @param * @param * @param {string | function} proxyPath proxy path, can be string or function, the function receive a options args and return a string. * @param * @param * @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://www.beancharts.com' to '/http://www.beancharts.com' for proxy matching * @param */
helpers.qs
refer to https://www.npmjs.com/package/qs
helpers.util
export function isString(obj) export function isDate(obj) export function isObject(obj) export function isNumber(obj) export function isFunction(obj) export function isFormData(obj) export function isIE() /** * @desc 判断参数是否为空, 包括null, undefined, [], '', * @param */export function isEmpty(obj) /** * @desc 判断参数是否不为空 */export function isNotEmpty(obj) /** * @desc 判断参数是否为空字符串, 比isEmpty()多判断字符串中全是空格的情况, 如: ' '. * @param */export function isBlank(str) /** * @desc 判断参数是否不为空字符串 */export function isNotBlank(obj) /** * @desc 函数节流 * @url http://underscorejs.org/#throttle * @param * @param * @param */export function throttle(func, wait, options)
axios options
The following options are provided by the underlying axios library.
// `timeout` specifies the number of milliseconds before the request times out.// If the request takes longer than `timeout`, the request will be aborted.timeout: 1000 // default is `0` (no timeout) // `withCredentials` indicates whether or not cross-site Access-Control requests// should be made using credentialswithCredentials: false // default // `auth` indicates that HTTP Basic auth should be used, and supplies credentials.// This will set an `Authorization` header, overwriting any existing// `Authorization` custom headers you have set using `headers`.// Please note that only HTTP Basic auth is configurable through this parameter.// For Bearer tokens and such, use `Authorization` custom headers instead.auth: username: 'janedoe' password: 's00pers3cret' // `responseType` indicates the type of data that the server will respond with// options are: 'arraybuffer', 'document', 'json', 'text', 'stream'// browser only: 'blob'responseType: 'json' // default // `responseEncoding` indicates encoding to use for decoding responses// Note: Ignored for `responseType` of 'stream' or client-side requestsresponseEncoding: 'utf8' // default // `xsrfCookieName` is the name of the cookie to use as a value for xsrf tokenxsrfCookieName: 'XSRF-TOKEN' // default // `xsrfHeaderName` is the name of the http header that carries the xsrf token valuexsrfHeaderName: 'X-XSRF-TOKEN' // default // `onUploadProgress` allows handling of progress events for uploads {// Do whatever you want with the native progress event} // `onDownloadProgress` allows handling of progress events for downloads {// Do whatever you want with the native progress event} // `maxContentLength` defines the max size of the http response content in bytes allowedmaxContentLength: 2000 // `validateStatus` defines whether to resolve or reject the promise for a given// HTTP response status code. If `validateStatus` returns `true` (or is set to `null`// or `undefined`), the promise will be resolved; otherwise, the promise will be// rejected. { return status >= 200 && status < 300; // default}