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

axios-ultra

v0.0.5

Published

一个全平台请求库 支持web、taro、uniapp、小程序,并且支持通用业务拦截器

Downloads

1

Readme

axios-ultra

优点:
1.一个全平台请求库 支持普通web、taro、uniapp、小程序
2.类型友好提示,易扩展
3.通过全局配置实现,直接获取接口data,而不需要每次请求总是判断resp.code == 200再获取resp.data
4.通过全局配置实现,请求生命周期自动loading和message提示
5.内置刷新token逻辑,只需实现processRefreshTokenLogic逻辑即可

Useage

npm i axios-ultra
or
yarn add axios-ultra
import HttpRequest from "axios-ultra";
import type {AxiosUltraErrorMessageOption, AxiosUltraLoadingOption, AxiosUltraSuccessMessageOption, AxiosUltraToast} from "axios-ultra";
import {message} from 'antd'

const httpRequest = new HttpRequest({
    baseURL: '/api',
    processHeaderToken(config) {
        Object.assign(config.headers, {
            Authorization: localStorage.getItem('token') || ''
        })
    },
    //处理刷新token逻辑
    async processRefreshTokenLogic(defaultOptions) {
        await httpRequest.get('/refreshToken', {}, defaultOptions);
        localStorage.setItem('token', '1')
    },
    //实现提供自动提示
    toast: {
        loading(option: AxiosUltraLoadingOption) {
            return message.loading({
                content: option.title,
                duration: 999999
            })
        },
        success(option: AxiosUltraSuccessMessageOption) {
            message.success({
                content: option.title,
            })
        },
        error(option: AxiosUltraErrorMessageOption) {
            message.error({
                content: option.title,
            })
        },
    }
});
//data 直接是接口响应的data 内置默认取接口最后的数据
const data = httpRequest.get('/api1', {}, {message: true, loading: true});
const data1 = httpRequest.get('/api1', {}, {message: {error: false, message: '操作成功'}, loading: '加载中...'});

Type Description

/**
 * by zlm(执着)
 */
import {AxiosRequestConfig} from "axios";

/**
 * 接口响应
 */
export interface AxiosUltraAPIResponse {

}

/**
 * 失败消息提示
 */
export interface AxiosUltraErrorMessageOption {
    title: string
}

/**
 * 成功消息提示
 */
export interface AxiosUltraSuccessMessageOption {
    title: string
}

/**
 * 消息提示
 */
export type AxiosUltraMessageOption = {
    error?: AxiosUltraErrorMessageOption | boolean | string;
    success?: AxiosUltraSuccessMessageOption | boolean | string;
}


/**
 * loading
 */
export interface AxiosUltraLoadingOption {
    title: string
}

/**
 * 消息提示 接口
 */
export type AxiosUltraToast = {
    /**
     * 成功提示
     * @param option
     */
    success(option: AxiosUltraSuccessMessageOption);
    /**
     * 错误提示
     * @param option
     */
    error(option: AxiosUltraErrorMessageOption);
    /**
     * 加载提示,返回关闭方法
     * @param option
     */
    loading(option: AxiosUltraLoadingOption): () => void;
}

export interface AxiosInterceptorObject {
    /**
     * 请求拦截
     * @param config
     */
    requestInterceptor?: (config: any) => Promise<any>;
    requestInterceptorCatch?: (err: any) => Promise<any>;
    /**
     * 响应拦截
     * @param response
     */
    responseInterceptor?: (response: any) => Promise<any>;
    responseInterceptorCatch?: (err: any) => Promise<any>;
}

//请求参数
export interface AxiosUltraRequestConfigOption extends AxiosRequestConfig, Record<string, any> {
    /**
     * 是否loading
     */
    loading?: AxiosUltraLoadingOption | string | boolean;
    /**
     * 是否消息提示
     */
    message?: AxiosUltraMessageOption | string | boolean;
    /**
     * 提示配置 桥接
     */
    toast?: AxiosUltraToast;
    /**
     * 外部传入是否请求成功
     * @param resp
     */
    isSuccess?: (resp: any) => Boolean;
    /**
     * 是否获取api响应的数据
     */
    getApiResponse?: boolean;
    /**
     * 是否含有完整的http响应(一般用于获取头部信息的请求)
     */
    getResponse?: boolean;
    /**
     * 成功情况下获取data
     * @param resp
     */
    getSuccessData?: (resp: any) => any;
    /**
     * 获取接口消息提示
     * @param resp
     */
    getApiMessage?: (resp: any) => string;
    /**
     * 拦截器
     */
    interceptor?: AxiosInterceptorObject;

    /**
     * 是否需要token
     */
    needHeaderToken?: boolean;
    /**
     * 处理
     * @param config
     */
    processHeaderToken?: (config: AxiosUltraRequestConfigOption) => void;
    /**
     * token刷新重试
     */
    refreshTokenRetryCount?: number;
    /**
     * 开启刷新token
     */
    enableRefreshToken?: boolean;
    /**
     * 处理刷新token逻辑
     * @param defaultOptions
     */
    processRefreshTokenLogic?: (defaultOptions: AxiosUltraRequestConfigOption) => void | Promise<void>;

}

Extend Type

//一定要加,不然不知道扩展哪个库
import AxiosUltra from 'axios-ultra'

declare module 'axios-ultra' {
    /**
     * 扩展接口响应参数
     */
    export interface AxiosUltraAPIResponse {
        code: number;
        data: any;
        message: string
    }

    /**
     * 扩展错误消息提示传参
     */
    export interface AxiosUltraErrorMessageOption {

    }

    /**
     * 扩展成功消息提示传参
     */
    export interface AxiosUltraSuccessMessageOption {

    }
}