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

[Feature] 可否添加自定义的 http 状态码的功能? #97

Open
nbaxp opened this issue Sep 4, 2022 · 1 comment
Open

[Feature] 可否添加自定义的 http 状态码的功能? #97

nbaxp opened this issue Sep 4, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@nbaxp
Copy link

nbaxp commented Sep 4, 2022

由于 restful api 一般会复用 http 状态码作为响应代码,建议添加自定义 http status code 的功能。
例如:
使用一个类似 _status 的保留属性作为可选的自定义状态码

  Mock.mock('/api/demo', 'DELETE', (request) => {
    const items = JSON.parse(request.body ?? '[]');
    if (items.length) {
      return { code: 200, message: '删除成功', _status: 204 };
    }
    return { code: 400, message: '参数不能为空', _status: 400 };
  });

返回值读取自定义状态码并删除该属性

const result = mocked.convert(this.custom.template, this.custom.options);
this.status = result._status ?? 200;
delete result._status;

以上
谢谢

@nbaxp nbaxp added the enhancement New feature or request label Sep 4, 2022
@lavyun
Copy link
Owner

lavyun commented Sep 4, 2022

可以考虑,随后支持

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants