Skip to content

feat: response filter#56

Open
patrickmao1 wants to merge 1 commit into
umijs:masterfrom
patrickmao1:master
Open

feat: response filter#56
patrickmao1 wants to merge 1 commit into
umijs:masterfrom
patrickmao1:master

Conversation

@patrickmao1
Copy link
Copy Markdown

Added a feature that enables user to pass in a user defined responseFilter function in request init option.

This function allows user to:

  1. post-process response data so they won't need to deal with it after each call of request.
  2. not struggle with writing another wrapper around the request instance & lose request's syntactic sugar (i.e. .get .post ...)

@patrickmao1 patrickmao1 changed the title feat: added response filter & tests feat: response filter Jul 5, 2019
@yesmeck
Copy link
Copy Markdown
Contributor

yesmeck commented Jul 5, 2019

Why not use response interceptor?

@patrickmao1
Copy link
Copy Markdown
Author

patrickmao1 commented Jul 6, 2019

How do you use response interceptor to achieve this? _parseRequest method in wrapped-fetch is called after the user defined response interceptor calls and is excepting raw response. I can't return anything but the original response type in the interceptor

@clock157
Copy link
Copy Markdown
Contributor

you can do it like this:

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

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 this pull request may close these issues.

3 participants