Skip to content

Commit

Permalink
feat: add request time
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Dec 5, 2020
1 parent 7b79442 commit ccbb14a
Show file tree
Hide file tree
Showing 8 changed files with 501 additions and 323 deletions.
31 changes: 28 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
## [1.0.9](https://github.com/anncwb/vite-plugin-mock/compare/1.0.9...v1.0.9) (2020-12-05)


### Bug Fixes

* Fix local development post request proxy to https ([7965604](https://github.com/anncwb/vite-plugin-mock/commit/79656046377f501da796d1be9752522a2203d69b))
* remove unnecessary and wrong usage of "try catch" ([815abde](https://github.com/anncwb/vite-plugin-mock/commit/815abde26f8f9a19322916ae01a9896a9aced33a))


### Features

* add request time ([ce29f0a](https://github.com/anncwb/vite-plugin-mock/commit/ce29f0a08f20756f28ce2e0db77785c81e92243e))



## [1.0.2](https://github.com/anncwb/vite-plugin-mock/compare/1.0.1...1.0.2) (2020-09-14)


### Features

- add examples ([2c5a86b](https://github.com/anncwb/vite-plugin-mock/commit/2c5a86bb75e39b6c7c9e08b1691c0541aeb104d9))
* add examples ([2c5a86b](https://github.com/anncwb/vite-plugin-mock/commit/2c5a86bb75e39b6c7c9e08b1691c0541aeb104d9))


- add supportTs options,Support es6 and commonjs modules in .js folder ([37f83f5](https://github.com/anncwb/vite-plugin-mock/commit/37f83f54c3a34e049f967b0db0ac2ade401cbf58))

## [1.0.1](https://github.com/anncwb/vite-plugin-mock/compare/58ad7cd57e3fd0daa92e0fc59c00e09cf6ba45ad...1.0.1) (2020-09-14)


### Features

* add supportTs options,Support es6 and commonjs modules in .js folder ([37f83f5](https://github.com/anncwb/vite-plugin-mock/commit/37f83f54c3a34e049f967b0db0ac2ade401cbf58))


### Performance Improvements

- Replace typescript plug-in with a faster esbuild plug-in ([58ad7cd](https://github.com/anncwb/vite-plugin-mock/commit/58ad7cd57e3fd0daa92e0fc59c00e09cf6ba45ad))
* Replace typescript plug-in with a faster esbuild plug-in ([58ad7cd](https://github.com/anncwb/vite-plugin-mock/commit/58ad7cd57e3fd0daa92e0fc59c00e09cf6ba45ad))



8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ Set the data entry that the mock reads. When the file exists and is located in t

The project uses glob to read the folder set by mockPath. This parameter is used as the parameter of the glob module

### ignoreFiles

**type** boolean|string

**default** `YYYY-MM-DD HH:mm:ss`

Whether to display the request time. If it is a string, use dayjs to format

## Mock file example

`/path/mock`
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-mock",
"version": "1.0.6",
"version": "1.0.9",
"description": "A mock plugin for vite",
"main": "dist/index.js",
"files": [
Expand Down Expand Up @@ -29,27 +29,28 @@
},
"homepage": "https://github.com/anncwb/vite-plugin-mock/tree/master/#readme",
"dependencies": {
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"chalk": "^4.1.0",
"chokidar": "^3.4.2",
"esbuild": "^0.7.19",
"chokidar": "^3.4.3",
"esbuild": "^0.8.18",
"glob": "^7.1.6",
"koa-bodyparser": "^4.3.0",
"rollup": "^2.32.1",
"rollup-plugin-esbuild": "^2.5.2"
"rollup": "^2.34.1",
"rollup-plugin-esbuild": "^2.6.0",
"dayjs": "^1.9.6"
},
"peerDependencies": {
"mockjs": ">=1.1.0",
"vite": ">=1.0.0-rc.4"
"vite": ">=1.0.0-rc.13"
},
"devDependencies": {
"@types/koa-bodyparser": "^4.3.0",
"@types/mockjs": "^1.0.3",
"@types/node": "^14.0.3",
"conventional-changelog-cli": "^2.1.0",
"@types/node": "^14.14.10",
"conventional-changelog-cli": "^2.1.1",
"mockjs": "^1.1.0",
"rimraf": "^3.0.2",
"typescript": "^4.0.3",
"vite": "^1.0.0-rc.6"
"vite": "^1.0.0-rc.13"
}
}
28 changes: 25 additions & 3 deletions src/createMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import chokidar from 'chokidar';
import chalk from 'chalk';
import glob from 'glob';
import Mock from 'mockjs';
import { isArray, isFunction, sleep, isRegExp } from './utils';
import { isArray, isFunction, sleep, isRegExp, isBoolean } from './utils';
import { loadConfigFromBundledFile } from './loadConfigFromBundledFile';
import { rollup } from 'rollup';
import esbuildPlugin from 'rollup-plugin-esbuild';
import dayjs from 'dayjs';
// @ts-ignore
import bodyParser from './koaBodyparse';
const pathResolve = require('@rollup/plugin-node-resolve');
Expand Down Expand Up @@ -38,8 +39,27 @@ export function getMockData() {
return mockData;
}

function getInvokeTime(opt: CreateMock): string {
const { showTime } = opt;
const defTime = ` - ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`;
if (isBoolean(showTime)) {
if (!showTime) {
return '';
} else {
return defTime;
}
}
if (!showTime) return '';

try {
return dayjs().format(showTime);
} catch (error) {
return defTime;
}
}

// request match
export function requestMiddle(app: any) {
export function requestMiddle(app: any, opt: CreateMock) {
return async (ctx: ParameterizedContext<DefaultState, Context>, next: any) => {
const path = ctx.path;
const req = mockData.find((item) => item.url === path);
Expand All @@ -53,7 +73,9 @@ export function requestMiddle(app: any) {
const body = await bodyParserFn(ctx);
const mockRes = isFunction(response) ? response({ body, query }) : response;
console.log(
`${chalk.green('[vite:mock-server]:request invoke: ' + ` ${chalk.cyan(path)} `)}`
`${chalk.green(
`[vite:mock-server${getInvokeTime(opt)}]:request invoke: ` + ` ${chalk.cyan(path)} `
)}`
);
ctx.type = 'json';
ctx.status = 200;
Expand Down
2 changes: 1 addition & 1 deletion src/mockServerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export const createMockServerPlugin = (opt: CreateMock): ServerPlugin => {
return ({ app }) => {
// app.use(bodyParser());
createMockServer(opt);
app.use(requestMiddle(app));
app.use(requestMiddle(app, opt));
};
};
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface CreateMock {
watchFiles?: boolean;
localEnabled?: boolean;
supportTs?: boolean;
showTime?: boolean | string;
}

export type MethodType = 'get' | 'post' | 'put' | 'delete' | 'patch';
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export function isRegExp(val: unknown): val is RegExp {
return is(val, 'RegExp');
}

export function isBoolean(val: unknown): val is boolean {
return is(val, 'Boolean');
}

export function sleep(time: number) {
return new Promise((resolve) => {
setTimeout(() => {
Expand Down
Loading

0 comments on commit ccbb14a

Please sign in to comment.