mock-patch
v1.1.0
Published
an parser for mock data config
Downloads
2
Readme
mock-patch
Installation
npm install --save-dev mock-patch
// or
yarn add -D mock-patch
// or
pnpm add -D mock-patch
Usage
- create
demo.js
import mockjs from 'mockjs';
const getUserInfo = function (params) {
return {
id: 123,
name: 'tom'
}
}
const getNotice = [
{
id: 'xxx1',
title: titles[0],
logo: avatars[0],
updatedAt: new Date(),
href: '',
memberLink: '',
},
{
id: 'xxx2',
title: titles[1],
logo: avatars[1],
updatedAt: new Date('2017-07-24'),
href: '',
memberLink: '',
},
// ...
];
const getActivities = [
{
id: 'trend-1',
updatedAt: new Date(),
user: {
name: 'Tom',
avatar: avatars2[0],
},
group: {
name: 'github',
link: 'https://github.com/',
},
project: {
name: 'gitlab',
link: 'https://gitlab.com/',
},
},
// ...
];
export default {
'/api/user/:id': getUserInfo,
'/api/project/notice': getNotice,
'GET /api/tags': mockjs.mock({
'list|100': [{name: '@city', 'value|1-100': 150, 'type|0-2': 1}],
}),
'POST /api/forms': () => {
return {code: 200, message: 'Ok'};
},
'GET|POST /api/activities': getActivities
// ...
};
- create
mock-server.js
for setup mock server
import Mock from 'mockjs';
import buildMockData, {MockData} from 'mock-patch';
const mocks: MockData[] = [];
const mockContext = import.meta.globEager('./mock/*.ts');
Object.keys(mockContext).forEach(v => {
mocks.push(...buildMockData(mockContext[v].default));
});
mocks.forEach(item => {
console.log(item);
if (item.path) {
if (item.method) {
Mock.mock(item.pathRule, item.method, item.template);
} else {
Mock.mock(item.pathRule, item.template);
}
} else {
Mock.mock(item.template);
}
});
Mock.setup({
timeout: '600-1000',
});
License
MIT Copyright (c) 2022