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

lg-http

v0.0.2

Published

基于 axios 二次封装,内置加载, 提示, 取消重复请求, 刷新 token

Downloads

4

Readme

lg-http

注:只支持 get、post

介绍

基于 axios 二次封装,内置加载, 提示, 取消重复请求, 刷新 token

使用

引入

import Http, { useHttp } from 'lg-http'

演示 1

import Http from 'lg-http'
/*
 * option option配置属性
 */
const $http = new Http(option)
/*
 * url 接口地址
 * data 接口参数
 * attaches attaches配置属性
 * axiosConfig axios配置属性
 */
$http
    .get(url, data, attaches, axiosConfig)
    .then((result) => {})
    .catch((error) => {})

$http.get(
    '/login',
    {
        account: 'admin',
        password: '123456'
    },
    {
        showLoading: false,
        showToast: false,
        codeCallback: {
            400: (result) => {
                console.log(result)
            }
        }
    },
    {
        header: {
            'Content-Type': 'application/json'
        }
    }
)

演示 2

注:useHttp 只适用 Vue3

根据 loading , 方便处理独立的加载效果

import {
    useHttp
} from 'lg-http'

/*
 * option option配置属性
 * url 接口地址
 * data 接口参数
 * attaches attaches配置属性
 * axiosConfig axios配置属性
 * return resultAttr useHttp结果属性
 */
const resultAttr = useHttp(option, {
    url,
    data,
    attaches,
    axiosConfig
})

const {
    response,
    data,
    error,
    loading
} = useHttp(option, {
    '/login',
    {
        account: 'admin',
        password: '123456'
    },
    {
        showLoading: true,
        showToast: false,
        codeCallback: {
            400: (result) => {
                console.log(result)
            }
        }
    },
    {
        header: {
            'Content-Type': 'application/json'
        }
    }
})

option 配置属性

| 属性名 | 类型 | 默认值 | 说明 | | ------------------------ | ---------------- | --------------------------------------- | -------------------------- | | baseURL | string | - | axios baseURL | | timeout | number | 20000 | axios timeout | | headers | object | {} | axios headers | | showLoading | boolean | true | 是否开启加载效果 | | loadingMethods | object | {open: console.log, close: console.log} | 加载效果的配置 | | showToast | boolean | true | 是否开启提示 | | toastMethods | function, object | console.log | 提示配置 | | isRefresh | boolean | false | 是否刷新 token | | refreshRequestAssert | function | (res)=>res.code===401 | 刷新 token 的状态 | | refreshRequest | () => Promise | - | 刷新 token 的函数 | | isOptimization | boolean | false | 是否开启重复请求拦截 | | successRequestAssert | function | (res)=>res.success | 请求正确的状态 | | defaultErrorToastMessage | string | '服务异常,请重新再试' | 错误提示文案 | | codeCallback | object | {} | 某种 code 状态下的特殊处理 |

attaches 配置属性(可选)

| 属性名 | 类型 | 默认值 | 说明 | | -------------- | ------- | ------ | -------------------------- | | showLoading | boolean | - | 是否开启加载效果 | | showToast | boolean | - | 是否开启提示 | | isRefresh | boolean | - | 是否刷新 token | | isOptimization | boolean | - | 是否开启重复请求拦截 | | codeCallback | object | {} | 某种 code 状态下的特殊处理 | | successMessage | string | - | 成功提示文案 |

axios 配置属性

axios 官网

useHttp 结果属性

| 属性名 | 类型 | 默认值 | 说明 | | -------- | ------- | ------ | ------------------------------------------- | | response | obeject | - | 请求成功结果 | | data | obeject | - | response.data | | error | obeject | - | 请求错误结果 | | loading | boolean | - | 是否加载中。默认为 true, 请求完成为 false | | finished | boolean | - | 是否请求完成。默认为 false, 请求完成为 true |