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

不支持restful形式的api结构 #4

Closed
carl-jin opened this issue Mar 8, 2021 · 8 comments
Closed

不支持restful形式的api结构 #4

carl-jin opened this issue Mar 8, 2021 · 8 comments

Comments

@carl-jin
Copy link

carl-jin commented Mar 8, 2021

问题描述

不支持restful形式的api结构, 如果mock模拟数据按以下restful api结构书写, 永远只会返回第一个

export default [
  {
    url: '/admin-api/api-permission',
    timeout: 1000,
    method: 'post',
    response: () => {
      return resultSuccess({});
    },
  },
  {
    url: '/admin-api/api-permission',
    timeout: 1000,
    method: 'get',
    response: ({ query }) => {
      const { page = 1, pageSize = 20 } = query;
      return resultPageSuccess(page, pageSize, apiPermissionList);
    },
  },
] as MockMethod[];

版本

最新版本

解决方法

https://github.com/anncwb/vite-plugin-mock/blob/bee76e432663c7bd6968fe0670dfa0eb8ab0946a/src/createMockServer.ts#L61
在这里需要加个method判断

    const matchReq = mockData.find((item) => {
      if (isGet) {
        return item.url === queryParams.pathname && item.method === req.method.toLowerCase();
      }
      return item.url === req.url  && item.method === req.method.toLowerCase();
    });

我不确定是我调用的方式不对, 还是程序判断不准确

@carl-jin
Copy link
Author

carl-jin commented Mar 8, 2021

另外也不支持, /admin-api/api-permission/:id 这种写法, 匹配不到 :id

@carl-jin
Copy link
Author

carl-jin commented Mar 8, 2021

只能先写死了, 不会排查.. 我之前看vue-router好像有个什么库, 专门处理这种路径匹配的

  {
    url: '/admin-api/api-permission/0',
    timeout: 1000,
    method: 'delete',
    response: () => {
      return resultSuccess({});
    },
  },

@anncwb
Copy link
Collaborator

anncwb commented Mar 9, 2021

会支持

@carl-jin
Copy link
Author

carl-jin commented Mar 9, 2021

会支持

我刚好准备提交一个pull request

@carl-jin
Copy link
Author

carl-jin commented Mar 9, 2021

会支持

#5

@anncwb anncwb closed this as completed in 236393e Mar 9, 2021
@anncwb
Copy link
Collaborator

anncwb commented Mar 9, 2021

试试v2.2.1

@anncwb
Copy link
Collaborator

anncwb commented Mar 9, 2021

2.2.2

@carl-jin
Copy link
Author

carl-jin commented Mar 9, 2021

yezir

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

No branches or pull requests

2 participants