Skip to content

Commit

Permalink
implemented onConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfung1031 committed Aug 16, 2023
1 parent 69f240b commit 8a8869f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
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,
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;
},
send: function (args, xhr) {
Expand Down

0 comments on commit 8a8869f

Please sign in to comment.