-
Notifications
You must be signed in to change notification settings - Fork 8.1k
feat: axios request 根据外部函数判断是否需要刷新 #6638
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
Conversation
|
WalkthroughReplaced a fixed 401-based re-authentication trigger in authenticateResponseInterceptor with a configurable predicate enableRefreshFunc(response). Updated the interceptor’s public signature to include this predicate and added the RequestResponse type import to support it. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant Interceptor
participant enableRefreshFunc
participant AuthRefresh
Caller->>Interceptor: handle response/error
Interceptor->>enableRefreshFunc: evaluate(response)
enableRefreshFunc-->>Interceptor: boolean
alt enableRefreshFunc == false
Interceptor-->>Caller: throw original error
else enableRefreshFunc == true
Interceptor->>AuthRefresh: perform refresh & retry
AuthRefresh-->>Interceptor: retry result
Interceptor-->>Caller: return retried response or error
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested reviewers
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these settings in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/effects/request/src/request-client/preset-interceptors.ts
(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/effects/request/src/request-client/preset-interceptors.ts (1)
packages/effects/request/src/request-client/types.ts (2)
RequestResponse
(79-79)ResponseInterceptorConfig
(80-80)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: post-update (windows-latest)
- GitHub Check: post-update (ubuntu-latest)
🔇 Additional comments (1)
packages/effects/request/src/request-client/preset-interceptors.ts (1)
2-6
: Importing RequestResponse type looks correctGood addition; it aligns with the new predicate’s signature usage below.
enableRefreshFunc, | ||
formatToken, | ||
}: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Make enableRefreshFunc optional with a safe default (preserve backward compatibility and avoid runtime errors)
Requiring callers to always provide enableRefreshFunc
is a breaking change and risks a runtime TypeError if omitted. Also, a default of “status === 401” preserves prior behavior.
Apply this diff to make the predicate optional and provide a default:
export const authenticateResponseInterceptor = ({
client,
doReAuthenticate,
doRefreshToken,
enableRefreshToken,
- enableRefreshFunc,
+ enableRefreshFunc = (resp?: RequestResponse) => resp?.status === 401,
formatToken,
}: {
client: RequestClient;
doReAuthenticate: () => Promise<void>;
doRefreshToken: () => Promise<string>;
enableRefreshToken: boolean;
- enableRefreshFunc: (response: RequestResponse) => boolean;
+ enableRefreshFunc?: (response?: RequestResponse) => boolean;
formatToken: (token: string) => null | string;
}): ResponseInterceptorConfig => {
Also applies to: 63-65
🤖 Prompt for AI Agents
In packages/effects/request/src/request-client/preset-interceptors.ts around
lines 56-58 (and similarly 63-65), the enableRefreshFunc parameter is currently
required; make it optional and supply a safe default predicate that preserves
prior behavior (status === 401). Update the function signature/destructuring to
provide a default value for enableRefreshFunc (e.g. enableRefreshFunc = (status)
=> status === 401) and adjust the type to be optional (or include the default in
the parameter type), ensuring all usages within the file call the predicate
safely without causing runtime errors when callers omit it.
// 根据外部传入的函数判断是否需要 重新认证 | ||
if (!enableRefreshFunc(response)) { | ||
throw error; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard against undefined error.response and canceled requests to prevent crashes
error.response
can be undefined (e.g., network errors, CORS, aborts). Calling enableRefreshFunc(response)
unguarded can throw. Also, skip refresh logic for canceled requests.
Apply this diff to make the check robust:
- // 根据外部传入的函数判断是否需要 重新认证
- if (!enableRefreshFunc(response)) {
+ // 根据外部传入的函数判断是否需要重新认证;对取消请求和无响应场景进行保护
+ if (axios.isCancel?.(error) || !response || !enableRefreshFunc(response)) {
throw error;
}
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In packages/effects/request/src/request-client/preset-interceptors.ts around
lines 69-72, the code calls enableRefreshFunc(response) without guarding against
error.response being undefined and without handling canceled requests; update
the logic to first detect canceled/aborted requests (e.g. error.code ===
'ERR_CANCELED' or use axios.isCancel(error) if axios is used) and immediately
rethrow in that case, then check that error.response exists before calling
enableRefreshFunc(response) and only invoke it when a response is present;
otherwise rethrow the original error.
有点破坏性更新的问题 |
Description
Type of change
Please delete options that are not relevant.
pnpm-lock.yaml
unless you introduce a new test example.Checklist
pnpm run docs:dev
command.pnpm test
.feat:
,fix:
,perf:
,docs:
, orchore:
.Summary by CodeRabbit