npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ccs-http-axios

v0.0.1

Published

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

Downloads

1

Readme

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