Umi: [Feature Request] 希望 umi-request 的 request 的拦截器添加 Promise 支持

Created on 18 Jun 2019  ·  4Comments  ·  Source: umijs/umi

Background

发送 request 前需要验证授权是否过期,如果过期需要更新授权信息后提交 request

希望添加 Promise 支持,支持以下语法

// request拦截器, 改变url 或 options.
request.interceptors.request.use( async (url, options) => {
  // 异步获取值
 const testVal = await testFn();
  return (
    {
      url: `${url}&interceptors=yes`,
      options: { ...options, interceptors: true, testVal },
    }
  );
});

pkg(umi-request) wontfix

All 4 comments

近期会支持

这个问题在typescript下还会存在

request.interceptors.request.use( async (url:string, options: RequestOptionsInit) => ( {url, options} ));

在index.d.ts中 RequestInterceptor 看起来并不支持 Promise

request: { use: (handler: RequestInterceptor) => void; };

export type RequestInterceptor = ( url: string, options: RequestOptionsInit ) => { url?: string; options?: RequestOptionsInit; };

错误信息如下:

Argument of type '(url: string, options: RequestOptionsInit) => Promise<{ url: string; options: RequestOptionsInit; }>' is not assignable to parameter of type 'RequestInterceptor'.
Type 'Promise<{ url: string; options: RequestOptionsInit; }>' has no properties in common with type '{ url?: string | undefined; options?: RequestOptionsInit | undefined; }'. TS2345

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

这个问题在typescript下还会存在

request.interceptors.request.use( async (url:string, options: RequestOptionsInit) => ( {url, options} ));

在index.d.ts中 RequestInterceptor 看起来并不支持 Promise

request: { use: (handler: RequestInterceptor) => void; };

export type RequestInterceptor = ( url: string, options: RequestOptionsInit ) => { url?: string; options?: RequestOptionsInit; };

错误信息如下:

Argument of type '(url: string, options: RequestOptionsInit) => Promise<{ url: string; options: RequestOptionsInit; }>' is not assignable to parameter of type 'RequestInterceptor'.
Type 'Promise<{ url: string; options: RequestOptionsInit; }>' has no properties in common with type '{ url?: string | undefined; options?: RequestOptionsInit | undefined; }'. TS2345

我也遇到这问题了,请问修复了吗?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nguyenhuutinh picture nguyenhuutinh  ·  3Comments

sorrycc picture sorrycc  ·  4Comments

tauruswang picture tauruswang  ·  3Comments

ddzy picture ddzy  ·  3Comments

zemzheng picture zemzheng  ·  3Comments