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

feat: response filter #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

patrickmao93
Copy link

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 ...)

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

yesmeck commented Jul 5, 2019

Why not use response interceptor?

@patrickmao93
Copy link
Author

patrickmao93 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
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