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

关于 umi-request 拦截器的执行顺序 #2776

Closed
garrett12138 opened this issue Jul 8, 2019 · 10 comments
Closed

关于 umi-request 拦截器的执行顺序 #2776

garrett12138 opened this issue Jul 8, 2019 · 10 comments

Comments

@garrett12138
Copy link

umi-request v1.0.8
自定义的response拦截器是在结果转成json数据之前执行的吗,如果我要对结果的json作统一处理,那要怎么做呢?

@patrickmao93
Copy link

@garrett12138 目前貌似只能自行再封装一层, 像这样

export interface CustomRequest {
  <T = any>(url: string, options?: RequestOptionsInit): Promise<T>;
  get: RequestMethod;
  post: RequestMethod;
  delete: RequestMethod;
  put: RequestMethod;
  patch: RequestMethod;
}

// wrap original request instance to filter out "data" for successful requests
const wrappedRequest = async (url: string, options: ExtendOptionsInit) => {
  const response = await request(url, options);
  if (response && response.success) {
    return response.data;
  }
  return response;
};

const methods = ['get', 'post', 'delete', 'put', 'patch'];
// add syntactic sugar for request methods
methods.forEach(method => {
  wrappedRequest[method] = (url: string, options: ExtendOptionsInit) =>
    wrappedRequest(url, { ...options, method });
});

export default wrappedRequest as CustomRequest;

我之前提过一个关于这个的 Pull Request 但是没有被接受

@clock157
Copy link
Contributor

@chenjsh36 你增加的中间件能力是否能解决这个问题?

@garrett12138
Copy link
Author

@这样并不完美,如果这样封装一层,所有的调request的地方都得改成这个封装

@chenjsh36
Copy link
Contributor

@chenjsh36 你增加的中间件能力是否能解决这个问题?

可以,中间件能力是在将结果做转换后执行的,提供了对结果做统一处理的能力,如下:

import request from 'umi-request';

request.use(async (ctx, next) => {
  await next();
  // 统一对结果进行处理
  ctx.res.hello = 'world';
});
const data = await request(('/api/middleware'));
console.log(data.hello); // world

目前改造的版本正在 beta 测试中

@huangbao21
Copy link

huangbao21 commented Jul 29, 2019

@chenjsh36 你增加的中间件能力是否能解决这个问题?

可以,中间件能力是在将结果做转换后执行的,提供了对结果做统一处理的能力,如下:

import request from 'umi-request';

request.use(async (ctx, next) => {
  await next();
  // 统一对结果进行处理
  ctx.res.hello = 'world';
});
const data = await request(('/api/middleware'));
console.log(data.hello); // world

目前改造的版本正在 beta 测试中

现在用中间件 报错 request.use is not function.
"umi-request": "^1.1.0" @chenjsh36

@huangbao21
Copy link

看到了tag 有1.1.1 有这个方法了

@chenjsh36
Copy link
Contributor

看到了tag 有1.1.1 有这个方法了

1.1.1 版本已经回退,目前中间件机制在 beta 测试中,可以使用 1.2.0-beta.4 版本看能否解决

@clock157
Copy link
Contributor

clock157 commented Aug 14, 2019

这样可以

/**
 * 将结果的状态层剥掉
 */
request.use(async(ctx, next) => {
  await next();
  if(ctx.res && ctx.res.code === 200) {
    ctx.res = ctx.res.data;
  }
});

@stale
Copy link

stale bot commented Oct 13, 2019

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.

@stale stale bot added the wontfix label Oct 13, 2019
@garrett12138
Copy link
Author

现在可以按 @huangbao21 @clock157 的解决了

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

No branches or pull requests

6 participants