Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
monorepo
/
venus
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
1
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit fddfc6d4
authored
Aug 21, 2025
by
lighua
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix: 增加对不支持的 HTTP 方法的错误处理
1 parent
df655015
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
2 deletions
packages/effects/request/src/request-client/modules/downloader.test.ts
packages/effects/request/src/request-client/modules/downloader.ts
packages/effects/request/src/request-client/modules/downloader.test.ts
View file @
fddfc6d
...
@@ -30,6 +30,7 @@ describe('fileDownloader', () => {
...
@@ -30,6 +30,7 @@ describe('fileDownloader', () => {
expect
(
result
).
toBeInstanceOf
(
Blob
);
expect
(
result
).
toBeInstanceOf
(
Blob
);
expect
(
result
).
toEqual
(
mockBlob
);
expect
(
result
).
toEqual
(
mockBlob
);
expect
(
mockAxiosInstance
.
get
).
toHaveBeenCalledWith
(
url
,
{
expect
(
mockAxiosInstance
.
get
).
toHaveBeenCalledWith
(
url
,
{
method
:
'GET'
,
responseType
:
'blob'
,
responseType
:
'blob'
,
responseReturn
:
'body'
,
responseReturn
:
'body'
,
});
});
...
@@ -51,6 +52,7 @@ describe('fileDownloader', () => {
...
@@ -51,6 +52,7 @@ describe('fileDownloader', () => {
expect
(
result
).
toEqual
(
mockBlob
);
expect
(
result
).
toEqual
(
mockBlob
);
expect
(
mockAxiosInstance
.
get
).
toHaveBeenCalledWith
(
url
,
{
expect
(
mockAxiosInstance
.
get
).
toHaveBeenCalledWith
(
url
,
{
...
customConfig
,
...
customConfig
,
method
:
'GET'
,
responseType
:
'blob'
,
responseType
:
'blob'
,
responseReturn
:
'body'
,
responseReturn
:
'body'
,
});
});
...
@@ -84,3 +86,72 @@ describe('fileDownloader', () => {
...
@@ -84,3 +86,72 @@ describe('fileDownloader', () => {
);
);
});
});
});
});
describe
(
'fileDownloader use other method'
,
()
=>
{
let
fileDownloader
:
FileDownloader
;
it
(
'should call request using get'
,
async
()
=>
{
const
url
=
'https://example.com/file'
;
const
mockBlob
=
new
Blob
([
'file content'
],
{
type
:
'text/plain'
});
const
mockResponse
:
Blob
=
mockBlob
;
const
mockAxiosInstance
=
{
request
:
vi
.
fn
(),
}
as
any
;
fileDownloader
=
new
FileDownloader
(
mockAxiosInstance
);
mockAxiosInstance
.
request
.
mockResolvedValueOnce
(
mockResponse
);
const
result
=
await
fileDownloader
.
download
(
url
);
expect
(
result
).
toBeInstanceOf
(
Blob
);
expect
(
result
).
toEqual
(
mockBlob
);
expect
(
mockAxiosInstance
.
request
).
toHaveBeenCalledWith
(
url
,
{
method
:
'GET'
,
responseType
:
'blob'
,
responseReturn
:
'body'
,
});
});
it
(
'should call post'
,
async
()
=>
{
const
url
=
'https://example.com/file'
;
const
mockAxiosInstance
=
{
post
:
vi
.
fn
(),
}
as
any
;
fileDownloader
=
new
FileDownloader
(
mockAxiosInstance
);
const
customConfig
:
AxiosRequestConfig
=
{
method
:
'POST'
,
data
:
{
name
:
'aa'
},
};
await
fileDownloader
.
download
(
url
,
customConfig
);
expect
(
mockAxiosInstance
.
post
).
toHaveBeenCalledWith
(
url
,
{
name
:
'aa'
},
{
method
:
'POST'
,
responseType
:
'blob'
,
responseReturn
:
'body'
,
},
);
});
it
(
'should handle errors gracefully'
,
async
()
=>
{
const
url
=
'https://example.com/file'
;
const
mockAxiosInstance
=
{
post
:
vi
.
fn
(),
}
as
any
;
fileDownloader
=
new
FileDownloader
(
mockAxiosInstance
);
await
expect
(()
=>
fileDownloader
.
download
(
url
,
{
method
:
'postt'
}),
).
rejects
.
toThrow
(
'RequestClient does not support method "POSTT". Please ensure the method is properly implemented in your RequestClient instance.'
,
);
});
});
packages/effects/request/src/request-client/modules/downloader.ts
View file @
fddfc6d
...
@@ -33,9 +33,27 @@ class FileDownloader {
...
@@ -33,9 +33,27 @@ class FileDownloader {
responseType
:
'blob'
,
responseType
:
'blob'
,
};
};
const
response
=
await
this
.
client
.
request
<
T
>
(
url
,
finalConfig
);
// Prefer a generic request if available; otherwise, dispatch to method-specific calls.
const
method
=
(
finalConfig
.
method
||
'GET'
).
toUpperCase
();
const
clientAny
=
this
.
client
as
any
;
return
response
;
if
(
typeof
clientAny
.
request
===
'function'
)
{
return
await
clientAny
.
request
(
url
,
finalConfig
);
}
const
lower
=
method
.
toLowerCase
();
if
(
typeof
clientAny
[
lower
]
===
'function'
)
{
if
([
'POST'
,
'PUT'
].
includes
(
method
))
{
const
{
data
,
...
rest
}
=
finalConfig
as
Record
<
string
,
any
>
;
return
await
clientAny
[
lower
](
url
,
data
,
rest
);
}
return
await
clientAny
[
lower
](
url
,
finalConfig
);
}
throw
new
Error
(
`RequestClient does not support method "
${
method
}
". Please ensure the method is properly implemented in your RequestClient instance.`
,
);
}
}
}
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment