Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented onConfig to by pass the request if certain conditions are matched #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cyfung1031
Copy link
Contributor

@cyfung1031 cyfung1031 commented Aug 16, 2023

close #118

Remarks

Since most requests are async, it might not be suitable to do the bypassing in onRequest.
onConfig is synchronously triggered in xhr.open, so it can completely bypass the entire open+setRequestHeader(POST request)+send.

Usage

const { unProxy, originXhr } = proxy({
    onConfig: (config, xhrProxy) => {
        console.log(`url: ${config.url}`)
        if (config.url.indexOf('google.com') >= 0) return false;
    },
    onRequest: (config, handler) => {
        handler.next(config);
    },
    onError: (err, handler) => {
        handler.next(err)
    },
    onResponse: (response, handler) => {
        handler.next(response)
    }
})

cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
@cyfung1031
Copy link
Contributor Author

Combined Distribution Script ajaxhook.js for Testing (PR 119, 120, 121, 122)
https://cdn.jsdelivr.net/gh/cyfung1031/ajax-hook@1ebe48e08108449669290a226e52fc6fbf7ec9ef/dist/ajaxhook.js

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

@@ -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.

改了

@DAHUIAAAAA
Copy link
Collaborator

新增 api 需要完善一下 markdown 文档

@cyfung1031
Copy link
Contributor Author

新增 api 需要完善一下 markdown 文档

新增了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proxy 的 onRequest 有沒有方法像 hook 一樣整個bypass?
2 participants