Skip to content

Commit

Permalink
feat: support restful api close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
vben-admin committed Mar 9, 2021
1 parent bee76e4 commit 7a35b1a
Show file tree
Hide file tree
Showing 30 changed files with 4,759 additions and 404 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
insert_final_newline = false
trim_trailing_whitespace = false

[*.{js,jsx,json,ts,tsx,yml}]
indent_size = 2
indent_style = space
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
example
28 changes: 28 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
root: true,
env: {
browser: true,
es2021: true,
es6: true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended',
'plugin:jest/recommended',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'jest'],
rules: {
'no-console': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
},
};
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: 'yarn'
directory: '/'
schedule:
interval: 'daily'
27 changes: 26 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
node_modules
.DS_Store
dist
es
.npmrc
.cache

test/upload-server/static

.local
# local env files
.env.local
.env.*.local

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# Editor directories and files
.idea
# .vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

coverage
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
6 changes: 6 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

# shellcheck source=./_/husky.sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"
9 changes: 9 additions & 0 deletions .husky/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
command_exists () {
command -v "$1" >/dev/null 2>&1
}

# Workaround for Windows 10, Git Bash and Yarn
if command_exists winpty && test -t 1; then
exec < /dev/tty
fi
6 changes: 6 additions & 0 deletions .husky/lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
'*.{js,jsx,ts,tsx}': ['eslint --fix', 'prettier --write'],
'{!(package)*.json,*.code-snippets,.!(browserslist)*rc}': ['prettier --write--parser json'],
'package.json': ['prettier --write'],
'*.md': ['prettier --write'],
};
10 changes: 10 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
. "$(dirname "$0")/common.sh"

[ -n "$CI" ] && exit 0

# Format and submit code according to lintstagedrc.js configuration
npm run lint:lint-staged

npm run lint:pretty
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
node_modules
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] };
1 change: 1 addition & 0 deletions es/createProdMockServer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function createProdMockServer(mockList: any[]): void;
51 changes: 51 additions & 0 deletions es/createProdMockServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Mock from 'mockjs';
export function createProdMockServer(mockList) {
// @ts-ignore
Mock.XHR.prototype.__send = Mock.XHR.prototype.send;
// @ts-ignore
Mock.XHR.prototype.send = function () {
if (this.custom.xhr) this.custom.xhr.withCredentials = this.withCredentials || false;
this.__send.apply(this, arguments);
};
for (const { url, method, response, timeout } of mockList) {
__setupMock__(timeout);
Mock.mock(new RegExp(url), method || 'get', __XHR2ExpressReqWrapper__(response, timeout));
}
}
function __param2Obj__(url) {
const search = url.split('?')[1];
if (!search) {
return {};
}
return JSON.parse(
'{"' +
decodeURIComponent(search)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')
.replace(/\+/g, ' ') +
'"}'
);
}
function __XHR2ExpressReqWrapper__(handle, timeout = 0) {
return function (options) {
let result = null;
if (handle instanceof Function) {
const { body, type, url } = options;
result = handle({
method: type,
body: JSON.parse(body),
query: __param2Obj__(url),
});
} else {
result = handle;
}
return Mock.mock(result);
};
}
function __setupMock__(timeout = 0) {
timeout &&
Mock.setup({
timeout,
});
}
4 changes: 2 additions & 2 deletions examples/js-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@vitejs/plugin-vue": "^1.1.5",
"@vue/compiler-sfc": "^3.0.7",
"cross-env": "^7.0.3",
"vite": "^2.0.4",
"vite-plugin-mock": "^2.1.5"
"vite": "^2.0.5",
"vite-plugin-mock": "^2.2.0"
}
}
Loading

0 comments on commit 7a35b1a

Please sign in to comment.