Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ interface XhrErrorHandler extends XhrHandler {
}

interface Proxy {
onConfig?: (config: XhrRequestConfig, xhrProxy: Hooks) => boolean | void,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成 onFilter 吧,语义化更好,第二个参数没什么太大用处,可以不传,因为过滤 url 后就可以直接走原生方法了,不用手动调用,切莫过度设计

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改了

onRequest?: (config: XhrRequestConfig, handler: XhrRequestHandler) => void,
onResponse?: (response: XhrResponse, handler: XhrResponseHandler) => void,
onError?: (err: XhrError, handler: XhrErrorHandler) => void,
Expand Down
8 changes: 7 additions & 1 deletion src/xhr-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ var ErrorHandler = makeHandler(function (error) {
});

function proxyAjax(proxy, win) {
var onRequest = proxy.onRequest,
var onConfig = proxy.onConfig,
onRequest = null,
onRequest_ = proxy.onRequest,
onResponse = proxy.onResponse,
onError = proxy.onError;

Expand Down Expand Up @@ -187,6 +189,10 @@ function proxyAjax(proxy, win) {
// 所以我们在send拦截函数中再手动调用open,因此返回true阻止xhr.open调用。
//
// 如果没有请求拦截器,则不用阻断xhr.open调用
onRequest = onRequest_;
if (onConfig) {
if (onConfig(config, this) === false) onRequest = null;
}
if (onRequest) return true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面这段代码直接改成这样就可以了:

if (onFilter && onFilter(config)) {
    return;
}

if (onRequest) return true;

可以看到 hookFunction 里面,当函数没有 ret 的时候,就会直接走原生方法:
image

},
send: function (args, xhr) {
Expand Down