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

request-sdk

v0.5.0

Published

request-sdk

Downloads

5

Readme

request-sdk 服务端推送技术

此 SDK 用来提供 公共请求 的基础能力,集成了请求公共请求处理,超时时间,异常请求处理,取消请求等功能。

快速开始

安装 request-sdk

$ npm i -S request-sdk

// 注意点: 
由于request-sdk的异常报错alert,依赖于element-plus的message组件,所以安装时候 会检查安装依赖,否者会报warning。

在页面中引入。

// main.js/ts中
import bdAxios from 'request-sdk';

...
const app = createApp(App);
app.use(bdAxios, {
    // axios已有的配置都可以被配置
    baseURL: 'xxx.baidu.com',
    errCode: 1401, // 特殊code
    cb: () => {} // 特殊code的回调函数
})

组件内使用

import {bdRequest} from 'request-sdk'

export const getData = (req: Req) => {
    return bdRequest<Req, Res>({
        url: 'xxx/xx',
        method: 'get/post',
        data
    })
}

注:不论是get请求还是post请求,都可以传递data参数。

bdRequest 实例方法

| 方法名称 | 说明 | 类型 | | -- | -- | -- | | bdRequest | 请求 | () => void | | cancelRequest | 取消请求 | (url: string) => void | | cancelAllRequest | 取消全部请求 | () => void |

请求类型说明

interface RequestConfig<T = AxiosResponse> extends AxiosRequestConfig {
    interceptors?: RequestInterceptors<T>,
    'x-not-tip'?: Boolean | undefined
}

interface RequestInterceptors<T> {
    // 请求拦截
    requestInterceptors?: (config: AxiosRequestConfig) => AxiosRequestConfig
    requestInterceptorsCatch?: (err: any) => any

    // 响应拦截
    responseInterceptors?: (config: T) => T
    responseInterceptorsCatch?: (err: any) => any
}

class Request {
    // 实例
    instance: AxiosInstance
    // 拦截器对象
    interceptorsObj?: RequestInterceptors<AxiosResponse>

    /**
     * 存放取消方法的集合
     */
    cancelRequestSourceList?: CancelRequestSource[]
    requestUrlList?: string[]

    constructor(config: RequestConfig) {
        this.requestUrlList = []
        this.cancelRequestSourceList = []
        this.instance = axios.create(config)
        this.interceptorsObj = config.interceptors
        // 拦截器执行顺序: 接口请求 -> 实例请求 -> 全局请求 -> 实例响应 -> 全局响应 -> 接口响应
        this.instance.interceptors.request.use(
            (res: AxiosRequestConfig) => res,
            (err: any) => err
        )
        // 实例拦截器
        this.instance.interceptors.request.use(
            this.interceptorsObj?.requestInterceptors,
            this.interceptorsObj?.requestInterceptorsCatch
        )
        // 响应拦截器
        this.instance.interceptors.response.use(
            this.interceptorsObj?.responseInterceptors,
            this.interceptorsObj?.responseInterceptorsCatch
        )
    }
}