1.0.25 • Published 12 months ago

imba-request v1.0.25

Weekly downloads
-
License
ISC
Repository
-
Last release
12 months ago

安装

# 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: {
			
		}
	}
)
1.0.25

12 months ago

1.0.19

1 year ago

1.0.22

12 months ago

1.0.21

12 months ago

1.0.20

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago