Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
basic-uniapp-v3
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-uniapp-v3
Commits
1c92a9e4
提交
1c92a9e4
authored
5月 09, 2023
作者:
方治民
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 修复 AxiosRequestConfig 类型使用问题
上级
b91927e3
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
24 行增加
和
26 行删除
+24
-26
Axios.ts
src/utils/http/axios/Axios.ts
+11
-10
axiosCancel.ts
src/utils/http/axios/axiosCancel.ts
+4
-4
axiosTransform.ts
src/utils/http/axios/axiosTransform.ts
+6
-9
index.ts
src/utils/http/axios/index.ts
+1
-1
global.d.ts
types/global.d.ts
+2
-2
没有找到文件。
src/utils/http/axios/Axios.ts
浏览文件 @
1c92a9e4
import
type
{
AxiosInstance
,
AxiosPromise
,
AxiosResponse
,
InternalAxiosRequestConfig
}
from
'axios'
import
type
{
AxiosInstance
,
AxiosPromise
,
AxiosRe
questConfig
,
AxiosRe
sponse
,
InternalAxiosRequestConfig
}
from
'axios'
import
axios
,
{
AxiosError
}
from
'axios'
import
type
{
RequestOptions
,
Result
,
UploadFileParams
}
from
'/#/axios'
import
type
{
CreateAxiosOptions
}
from
'./axiosTransform'
...
...
@@ -13,7 +13,7 @@ import { ContentTypeEnum, RequestEnum } from '/@/enums/httpEnum'
* @param config 请求配置
* @returns 请求结果
*/
axios
.
defaults
.
adapter
=
function
(
config
:
CreateAxiosOptions
):
AxiosPromise
<
any
>
{
axios
.
defaults
.
adapter
=
function
(
config
:
InternalAxiosRequestConfig
):
AxiosPromise
<
any
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
request
=
uni
.
request
({
method
:
config
.
method
.
toUpperCase
()
as
RequestEnum
,
...
...
@@ -125,6 +125,7 @@ export class VAxios {
!
ignoreCancel
&&
axiosCanceler
.
addPending
(
config
)
if
(
requestInterceptors
&&
isFunction
(
requestInterceptors
))
{
// @ts-expect-error
config
=
requestInterceptors
(
config
,
this
.
options
)
}
return
config
...
...
@@ -156,7 +157,7 @@ export class VAxios {
/**
* @description: File Upload
*/
uploadFile
<
T
=
any
>
(
config
:
Internal
AxiosRequestConfig
,
params
:
UploadFileParams
)
{
uploadFile
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
params
:
UploadFileParams
)
{
const
formData
=
new
window
.
FormData
()
const
customFilename
=
params
.
name
||
'file'
...
...
@@ -221,7 +222,7 @@ export class VAxios {
}
// support form-data
supportFormData
(
config
:
Internal
AxiosRequestConfig
)
{
supportFormData
(
config
:
AxiosRequestConfig
)
{
const
headers
=
config
.
headers
||
this
.
options
.
headers
const
contentType
=
headers
?.[
'Content-Type'
]
||
headers
?.[
'Content-Type'
]
...
...
@@ -239,24 +240,24 @@ export class VAxios {
}
}
get
<
T
=
any
>
(
config
:
Internal
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
get
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
return
this
.
request
({
...
config
,
method
:
'GET'
},
options
)
}
post
<
T
=
any
>
(
config
:
Internal
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
post
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
return
this
.
request
({
...
config
,
method
:
'POST'
},
options
)
}
put
<
T
=
any
>
(
config
:
Internal
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
put
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
return
this
.
request
({
...
config
,
method
:
'PUT'
},
options
)
}
delete
<
T
=
any
>
(
config
:
Internal
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
delete
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
return
this
.
request
({
...
config
,
method
:
'DELETE'
},
options
)
}
request
<
T
=
any
>
(
config
:
Internal
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
let
conf
:
CreateAxiosOptions
=
cloneDeep
(
config
)
request
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
let
conf
=
cloneDeep
(
config
)
as
CreateAxiosOptions
const
transform
=
this
.
getTransform
()
const
{
requestOptions
}
=
this
.
options
...
...
src/utils/http/axios/axiosCancel.ts
浏览文件 @
1c92a9e4
import
type
{
Canceler
,
InternalAxiosRequestConfig
}
from
'axios'
import
type
{
AxiosRequestConfig
,
Canceler
}
from
'axios'
import
axios
from
'axios'
import
{
isFunction
}
from
'/@/utils/is'
// Used to store the identification and cancellation function of each request
let
pendingMap
=
new
Map
<
string
,
Canceler
>
()
export
function
getPendingUrl
(
config
:
Internal
AxiosRequestConfig
)
{
export
function
getPendingUrl
(
config
:
AxiosRequestConfig
)
{
return
[
config
.
method
,
config
.
url
].
join
(
'&'
)
}
...
...
@@ -14,7 +14,7 @@ export class AxiosCanceler {
* Add request
* @param {Object} config
*/
addPending
(
config
:
Internal
AxiosRequestConfig
)
{
addPending
(
config
:
AxiosRequestConfig
)
{
this
.
removePending
(
config
)
const
url
=
getPendingUrl
(
config
)
config
.
cancelToken
=
...
...
@@ -41,7 +41,7 @@ export class AxiosCanceler {
* Removal request
* @param {Object} config
*/
removePending
(
config
:
Internal
AxiosRequestConfig
)
{
removePending
(
config
:
AxiosRequestConfig
)
{
const
url
=
getPendingUrl
(
config
)
if
(
pendingMap
.
has
(
url
))
{
...
...
src/utils/http/axios/axiosTransform.ts
浏览文件 @
1c92a9e4
/**
* Data processing class, can be configured according to the project
*/
import
type
{
AxiosRe
sponse
,
InternalAxiosRequestConfig
}
from
'axios'
import
type
{
AxiosRe
questConfig
,
AxiosResponse
}
from
'axios'
import
type
{
RequestOptions
,
Result
}
from
'/#/axios'
export
interface
CreateAxiosOptions
extends
Internal
AxiosRequestConfig
{
export
interface
CreateAxiosOptions
extends
AxiosRequestConfig
{
authenticationScheme
?:
string
transform
?:
AxiosTransform
requestOptions
?:
RequestOptions
...
...
@@ -16,15 +16,15 @@ export abstract class AxiosTransform {
* 允许动态修改本次请求的 config 和 options
*/
inject
?:
(
config
:
Internal
AxiosRequestConfig
,
config
:
AxiosRequestConfig
,
options
:
RequestOptions
,
)
=>
{
config
:
Internal
AxiosRequestConfig
;
options
:
RequestOptions
}
)
=>
{
config
:
AxiosRequestConfig
;
options
:
RequestOptions
}
/**
* @description: Process configuration before request
* @description: Process configuration before request
*/
beforeRequestHook
?:
(
config
:
InternalAxiosRequestConfig
,
options
:
RequestOptions
)
=>
Internal
AxiosRequestConfig
beforeRequestHook
?:
(
config
:
AxiosRequestConfig
,
options
:
RequestOptions
)
=>
AxiosRequestConfig
/**
* @description: Request successfully processed
...
...
@@ -39,10 +39,7 @@ export abstract class AxiosTransform {
/**
* @description: 请求之前的拦截器
*/
requestInterceptors
?:
(
config
:
InternalAxiosRequestConfig
,
options
:
CreateAxiosOptions
,
)
=>
InternalAxiosRequestConfig
requestInterceptors
?:
(
config
:
AxiosRequestConfig
,
options
:
CreateAxiosOptions
)
=>
AxiosRequestConfig
/**
* @description: 请求之后的拦截器
...
...
src/utils/http/axios/index.ts
浏览文件 @
1c92a9e4
...
...
@@ -268,7 +268,7 @@ export const defHttp = createAxios({
// other api url
// export const otherHttp = createAxios({
// transform: {
// inject: (config:
Internal
AxiosRequestConfig<any>, options: http.RequestOptions) => {
// inject: (config: AxiosRequestConfig<any>, options: http.RequestOptions) => {
// // TODO: 根据实际项目配置
// return { config, options }
// },
...
...
types/global.d.ts
浏览文件 @
1c92a9e4
import
type
{
Internal
AxiosRequestConfig
}
from
'axios'
import
type
{
AxiosRequestConfig
}
from
'axios'
declare
global
{
type
Recordable
<
T
=
any
>
=
Record
<
string
,
T
>
...
...
@@ -49,7 +49,7 @@ declare global {
retryRequest
?:
RetryRequest
}
type
RequestConfig
<
T
=
any
>
=
Internal
AxiosRequestConfig
<
T
>
type
RequestConfig
<
T
=
any
>
=
AxiosRequestConfig
<
T
>
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论