-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Comments
@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 但是没有被接受 |
@chenjsh36 你增加的中间件能力是否能解决这个问题? |
@这样并不完美,如果这样封装一层,所有的调request的地方都得改成这个封装 |
可以,中间件能力是在将结果做转换后执行的,提供了对结果做统一处理的能力,如下: 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. |
看到了tag 有1.1.1 有这个方法了 |
1.1.1 版本已经回退,目前中间件机制在 beta 测试中,可以使用 1.2.0-beta.4 版本看能否解决 |
这样可以
|
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. |
现在可以按 @huangbao21 @clock157 的解决了 |
umi-request v1.0.8
自定义的response拦截器是在结果转成json数据之前执行的吗,如果我要对结果的json作统一处理,那要怎么做呢?
The text was updated successfully, but these errors were encountered: