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

imba-request

v1.0.25

Published

请求封装 request

Downloads

56

Readme

安装

# pnpm
pnpm i imba-request

使用

初始化和初始配置

import { ImbaRequest } from 'imba-request'
import Axios from 'axios'

const axios = Axios.create({
  baseURL: 'https://api.example.com',
  timeout: 3000,
  headers: { Authorization: 'AUTH_TOKEN' },
})

const http = new ImbaRequest(
	axios,
	{
	/**
   * 缓存&SWR 是否开启
   * 默认 true
   */
    cacheBool: true,
    /**
   * 缓存&SWR 缓存时间 默认分单位 mm
   * 默认 -1
   */
    cacheTime: 1,
    /**
   * 缓存&SWR 缓存单位 mm | ss
   * 默认 mm
   */
    cacheUnit: 'mm',
    /**
   * 是否重复请求合并
   * 默认 true
   */
    repeatMergeBool: true,
    /**
   * 是否请求错误后重试
   * 默认 true
   */
    retryBool: true,
    /**
   * 请求重试错误次数
   * 默认 1
   */
    retryCount: 1,
    /**
   * 重试内时间定位 单位秒
   * 默认 3
   */
    retryInterval: 3,
    /**
   * 分页字段设置
   */
    pageKey: ['page', 'size'],
    /**
  * 打印API接口地址是否MD5化
  */
    printMD5: false,
    /**
   * 是否开启打印请求数据
   */
    printConsole: true,
})
console.log('%c [ http ]-86', 'font-size:14px; background:#41b883; color:#ffffff;', http)

test

const testAsync = (config: any) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(config)
    }, 300)
  })
}

axios.interceptors.request.use((config) => {
  console.log('%c [ config ]-79', 'font-size:14px; background:#41b883; color:#ffffff;', config)
  config.headers.Authorization = 'Bearer 11111'
  return config
})

axios.interceptors.request.use(async (config) => {
  const result = await testAsync({ Cookie: '222222' })
  config.headers = Object.assign(config.headers || {}, result)
  return config
})

axios.interceptors.request.use((config) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      config.headers = Object.assign(config.headers || {}, { Cookie: '333333' })
      resolve(config)
    }, 300)
  })
})

axios.interceptors.response.use((res) => {
  return res.data
})

http.request('/testA', { _method: 'POST', _body: { name: '111' } }).then((res) => {
  console.log('%c [ res ]-60', 'font-size:14px; background:#41b883; color:#ffffff;', res)
})
// GET 请求 api/xxx/:id/:idnext 形式 => api/xxx/111/next?name=wtf
http.request(['/api/test/:id/:idnext', 'GET'], {
	_param: { name: 'wtf' },
	_id: '111',
	_idnext: 'next'
})

// POST 请求 api/xxx/:id 形式 => api/xxx/2?id=2 请求体 row json => { body: 'i am body man' }
http.request(['/api/test/:id', 'POST'], {
	_param: { name: 'xxx' },
	_body: { title: 'i am body man' },
	_id: '2'
})

// POST 请求修改为 PUT 请求
http.request(['/api/test/put', 'POST'], {
	_body: { id: 3 },
	_method: 'PUT'
})

// POST 请求分页
http.request(['/api/test/post', 'POST'], {
	_body: { id: 4 },
	_page: [1, 10]
})

// GET 请求分页
 http.request(['/api/test/get', 'GET'], {
	_param: { id: 5 },
	_page: [1, 10]
})

// POST 请求分页
http.request(['/api/test/post', 'POST'],
{
	_body: { id: 6 },
	_page: [1, 10]
})

// GET 请求分页 缓存&SWR
http.request(['/api/test/get', 'GET'],
{
	_param: { id: 7 },
	_cache: 10,
	_cacheUnit: 'ss'
})

// 多个重复请求 并列为一个请求返回
http.request(['/api/test/get', 'GET'], { _param: { id: 8 } })
http.request(['/api/test/get', 'GET'], { _param: { id: 8 }, _repeatMergeBool: false })
http.request(['/api/test/get', 'GET'], { _param: { id: 8 } })
http.request(['/api/test/get', 'GET'], { _param: { id: 8 } })

// 尝试错误请求 自动重试请求
http.request('/api/test', { _retryInterval: 0 }, { baseURL: '//error.com' })

// 设定 axios 原参数
http.request('/api/ddd',{
	 // 出错后不重试请求
    _retryCount: 0,
}, {
		axiosOptions: {
			
		}
	}
)