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 Request] requestInterceptor支持异步 #10303

Closed
Hincex opened this issue Jan 16, 2023 · 8 comments · Fixed by #12045
Closed

[Feature Request] requestInterceptor支持异步 #10303

Hincex opened this issue Jan 16, 2023 · 8 comments · Fixed by #12045

Comments

@Hincex
Copy link

Hincex commented Jan 16, 2023

Background

请求拦截前会注入token,但是token的获取是异步的

  async function requestInterceptor(url: string, options: RequestOptionsInit) {
    const token = await StorageUtil.get(TOKEN);
    ...
}
// 这里的类型不匹配
export const request = {
  requestInterceptors : [requestInterceptor]
}

Proposal

希望能修改一下类型

@fz6m
Copy link
Member

fz6m commented Jan 16, 2023

你这里的请求发的时候都要去请求一次 token ,不合理的。

建议在应用启动的时候 getInitialState 中拿到 token ,set 到 valtio 全局数据存储或者 local storage 里,之后从 local storage 或者 valtio 里同步的取。

@Hincex
Copy link
Author

Hincex commented Jan 17, 2023

你这里的请求发的时候都要去请求一次 token ,不合理的。

建议在应用启动的时候 getInitialState 中拿到 token ,set 到 valtio 全局数据存储或者 local storage 里,之后从 local storage 或者 valtio 里同步的取。

token就是启动时候获取的 然后存储到了storage 只是因为用的是localforage这个库 他的获取是异步的 所以 才会有这个需求

@Hincex
Copy link
Author

Hincex commented Jan 30, 2023

你这里的请求发的时候都要去请求一次 token ,不合理的。
建议在应用启动的时候 getInitialState 中拿到 token ,set 到 valtio 全局数据存储或者 local storage 里,之后从 local storage 或者 valtio 里同步的取。

token就是启动时候获取的 然后存储到了storage 只是因为用的是localforage这个库 他的获取是异步的 所以 才会有这个需求

怎么没人回复了呢 目前流程是登录请求会获取一个token然后存储下来 后续其他请求发送的时候就会去读取存储的token然后携带到header里面 主要是读取是异步的 所以需要支持异步

@cp-apple
Copy link

+1,希望支持异步,我们有类似需求,需要在请求前处理异步数据..

@MANNIX-CHEN
Copy link

+1 我们也有数据源是需要异步获取的。

@esymeptoo
Copy link

esymeptoo commented Oct 27, 2023

这种场景可以初始化的时候获取token再初始化request插件
伪代码:

function setJwtToken(token) {
  request.interceptors.request.use(() => {})
}
getToken().then((token) => { 
  setJwtToken(token)
})

@KelleyYe
Copy link

KelleyYe commented Jan 4, 2024

也遇到这个问题,因为请求的时候需要判断token是否过期,如果过期了需要先刷新一次token,拿到最新的token再用来继续完成之前的请求工作

@travisnguyen20
Copy link

+1. I got same issue as you @KelleyYe. Axios accepts Promise type as well. Please fix

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 a pull request may close this issue.

7 participants