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: 支持设置responseHeader等属性 #453

Open
wants to merge 2 commits into
base: refactoring
Choose a base branch
from

Conversation

happysong
Copy link

目的: 支持设置xhr的responseHeader等属性

方式: 在xhr的open方法中增加Util.extend(this.custom, MockXMLHttpRequest._settings), 将用户设置的属性拓展到custom对象上

用法:

import Mock from 'mockjs';

const initMock = () => {
  Mock.setup({
    responseHeaders: {
      'content-type': 'json'
    }
  });

// mock请求
  Mock.mock('/upload/get-token', 'get', data => {
    return {
      token: 'token'
    };
  });
}

export default initMock;

最终response将包含headers:

image

@wll8
Copy link

wll8 commented Jun 19, 2021

这个是对所有 mockjs 实例生效吗?

@happysong
Copy link
Author

是的

@wll8
Copy link

wll8 commented Jun 19, 2021

当需要配置单个的时候如何做?

@happysong
Copy link
Author

如果需要支持单个, 应该还需要修改Mock.mock方法支持配置config, xhr再二次拓展custom对象

image

image

@wll8
Copy link

wll8 commented Jun 19, 2021

收到,三口。

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.

2 participants