0.0.1 • Published 3 years ago

ccs-http-axios v0.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

http-service

增强axios功能,像axios一样使用它,但更方便。

安装

npm install --save http-service

用法

import $http from 'http-service';
$http({
    baseURL: 'http://www.demo.com',
    url: '/demo',
    params: {
        id: 1
    }
}).then((data) => {
 
}, (error) => {
 
}).finally(() => {

});

settings

用于设置全局选项

import $http from 'http-service';
// 需要在http()方法之前注册
$http.settings({
    baseURL: 'http://www.demo.com',
    method: "POST",  // default is 'GET'
    contentType: ContentType.APPLICATION_X_WWW_FORM_URLENCODED  // default is 'json'
});

异步拦截器

beforeRequest

$http({
    baseURL: 'http://www.demo.com',
    url: '/demo',
    beforeRequest(resolve, reject, options) {
        // Do something before request is sent
        setTimeout(() => {
            resolve(options);                   // will continue to process.
            // or
            reject('some error message.');      // will abort http request.
        }, 2000)
    }
});

afterResponse

$http({
    baseURL: 'http://www.demo.com',
    url: '/demo',
    afterResponse(resolve, reject, options) {
        var { data, status } = response;
        switch (status) {
            case 200:   // continue to process.
                resolve(data);
            case 401:   // need log in
                reject(response);
                break;
            case 500:   // throw a error.
                reject(response);
                break;
        }
    }
});

cancel

var abort;
 
$http({
    baseURL: 'http://www.demo.com',
    url: '/demo',
    cancel(c) {
        abort = c;
    }
});
 
setTimeout(() => abort());

API

/**
 * @desc 包装和扩展axios库,用axios支持所有选项。
 * @param {function} cancel axios的cancelToken,函数接收一个cancel参数。
 * @param {string} contentType HTTP请求标头Content-Type,默认为“ application / json”
 * @param {function|array} requestInterceptor 包装axios.interceptors.request.use(成功,错误)方法
 * @param {function|array} responseInterceptor 包装axios.interceptors.response.use(成功,错误)方法
 * @param {function} beforeRequest 异步处理请求拦截器,函数接收(解析,拒绝,选项)参数
 * @param {function} afterResponse 使进程响应拦截器异步,函数接收(解析,拒绝,响应,选项)参数
 * @param {function} onError发生错误时,它将在promise.catch()之前调用,函数接收到一个错误对象,其中包括(配置,请求,响应,消息,堆栈)
 * @other 参考axios api: https://github.com/axios/axios
 * @return {object} - 返回一个promise
 */
$http(options)

/ **
 *  @desc 设置全局选项
 * /
$http.settings(options)

/**
 * @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