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

Feature: ctx.http需要暴露interceptors或者提供拦截器功能 #1266

Open
Tracked by #1358
mzzsfy opened this issue Nov 15, 2023 · 6 comments
Open
Tracked by #1358

Feature: ctx.http需要暴露interceptors或者提供拦截器功能 #1266

mzzsfy opened this issue Nov 15, 2023 · 6 comments
Labels
feature 新特性

Comments

@mzzsfy
Copy link

mzzsfy commented Nov 15, 2023

Describe the problem related to the feature request

检查到特定返回值自动重新登陆,

Describe the solution you'd like

ctx.http需要暴露interceptors或者提供拦截器功能

Describe alternatives you've considered

创建新的http

Additional context

No response

@mzzsfy mzzsfy added the feature 新特性 label Nov 15, 2023
@shigma
Copy link
Member

shigma commented Nov 15, 2023

之后可能会迁移到 fetch API,迁移之后再做考虑(我也不知道 fetch 有没有 interceptor 对应的实现)

ref: #1181

@MaikoTan
Copy link
Member

In paticular your case, I think you could just catch the exception and then resolve the re-login process.

try {
  const res = ctx.http.get(url)
  // ...
} catch (e) {
  if (Quester.isAxiosError(e) {
    if (e.statusCode === 401) {
      // re-login and re-request
    }
  }
}

Well, there're no RAII or something in JS, the code might be ugly but usable for now.

@Hieuzest
Copy link

我倾向于在目前的实现上直接提供一套api来做。因为其实并不需要使用axios的interceptor,只需要我们wrap一些东西

@mzzsfy
Copy link
Author

mzzsfy commented Nov 15, 2023

In paticular your case, I think you could just catch the exception and then resolve the re-login process.

try {
  const res = ctx.http.get(url)
  // ...
} catch (e) {
  if (Quester.isAxiosError(e) {
    if (e.statusCode === 401) {
      // re-login and re-request
    }
  }
}

Well, there're no RAII or something in JS, the code might be ugly but usable for now.

目前我也是这样实现的,但这开发起来很不友好

@shigma
Copy link
Member

shigma commented Feb 14, 2024

请问能提供一些使用 axios interceptor 的示例代码吗?我有意实现相关的功能,不过想深入了解你的需求。

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

No branches or pull requests

4 participants