Skip to content

Conversation

liujiayii
Copy link

在invoke中,同步返回false会拦截当前api的执行,但是异步返回false时,会导致wrapperOptionsoptions[name]报错TypeError: Cannot create property 'success' on boolean 'false',此时options是false了,导致了报错

我在本地测试了return Promisi.resolve(false)return false,同步和异步返回false均能正常拦截当前api的执行

@Otto-J Otto-J requested a review from Copilot October 15, 2025 09:28
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug in the interceptor system where asynchronous hooks returning false caused TypeScript errors when trying to set properties on the boolean value. The changes ensure that both synchronous and asynchronous hooks can properly return false to intercept API execution.

  • Modified wrapperHook to preserve false values instead of coercing them to the original data
  • Updated the queue function to handle asynchronous false returns by creating mock Promise objects
  • Added proper handling for both sync and async false returns to prevent property assignment errors

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

if (isPromise(res)) {
return res.then((res) => {
if (res === false) {
return { then() {}, catch() {} } as Promise<undefined>;
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

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

The mock Promise object with empty then() and catch() methods may cause issues with Promise chaining. Consider using Promise.reject() or a proper rejected Promise to maintain consistent Promise behavior.

Copilot uses AI. Check for mistakes.

});
}
if (res === false) {
return { then() {}, catch() {} } as Promise<undefined>;
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

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

The mock Promise object with empty then() and catch() methods may cause issues with Promise chaining. Consider using Promise.reject() or a proper rejected Promise to maintain consistent Promise behavior.

Copilot uses AI. Check for mistakes.

// 修改这里:处理异步返回 false 的情况
promise = res.then((res) => {
if (res === false) {
return { then() {}, catch() {} } as Promise<undefined>;
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

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

The mock Promise object with empty then() and catch() methods may cause issues with Promise chaining. Consider using Promise.reject() or a proper rejected Promise to maintain consistent Promise behavior.

Copilot uses AI. Check for mistakes.

Comment on lines +25 to +27
const result = hook(data, params);
// 只有当结果为undefined或null时才返回data,保留false值
return result === undefined || result === null ? data : result;
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

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

The logic change from || data to explicit undefined/null checking is significant and may affect other use cases. Consider adding unit tests to verify this doesn't break existing functionality where hooks return other falsy values like 0 or empty string.

Copilot uses AI. Check for mistakes.

@liujiayii
Copy link
Author

@chouchouji @Otto-J 帮忙看下有问题吗?

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.

1 participant