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

@jiangqiang1996/easy-fetch

v1.0.2

Published

基于fetch封装的请求工具,支持请求拦截和响应拦截等基础配置,拥有过滤重复请求的功能。

Downloads

9

Readme

easy-fetch

基于fetch封装的请求工具,支持请求拦截和响应拦截等基础配置,拥有过滤重复请求的功能。

安装步骤

npm install @jiangqiang1996/easy-fetch

使用步骤

  1. 创建一个实例,并配置全局参数,默认请求成功的code为200,可以在此处重新定义,最好全局均使用该实例进行请求
import FetchService from "@jiangqiang1996/easy-fetch/dist";
const service = fetchService.create({
    baseURL: "http://localhost:8080",
    headers: {'Content-Type': 'application/json'},
    timeout: 60000
});
  1. 对实例设置拦截器:
service.requestInterceptor = (config) => {
    // 在发送请求之前做些什么 token
    if (Session.get('token')) {
        config.headers = {
            'Authorization': `${Session.get('token')}`
        }
    }
    return config;
}
service.responseInterceptor = (res) => {
    if (res?.code !== 200) {
        // `token` 过期或者账号已在别处登录
        if (res.code === 401) {
            Session.clear(); // 清除浏览器全部临时缓存
            window.location.href = '/'; // 去登录页
            ElMessageBox.alert('你已被登出,请重新登录', '提示', {})
                .then(() => {
                })
                .catch(() => {
                });
        }
        ElMessage.error(res.message);
        //如果是需要判定为请求失败,这里必须是reject
        return Promise.reject(res);
    } else {
        return res;
    }
}

service.responseErrorInterceptor = (reason) => {
    // 对响应错误做点什么
    ElMessage.error(reason);
    return Promise.reject(reason);
}
  1. 使用该实例 可传入泛型参数,拿到的响应结果即可自动转换为对应类型
logIn: (data: any) => {
    return service.post<string>( {
        url: '/admin/user/login',
        data,
    });
}
userInfo: (params) => {
    return service.get<UserInfo>( {
        url: '/admin/user/userInfo',
        params:params
    });
}