提交 b43df22e 作者: chuangker

first commit

上级
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
# Created by https://www.gitignore.io/api/macos
dist
### macOS ###
*.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# End of https://www.gitignore.io/api/macos
# Created by https://www.gitignore.io/api/node
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# End of https://www.gitignore.io/api/node
easy-mock-template-axios
---
基于 axios 的接口定义模板。
Documentation
---
[帮助文档](https://easy-mock.github.io/easy-mock-cli/)
module.exports = {
output: "api",
template: "../",
projects: [
{
id: "58fef6ac5e43ae5dbea5eb53",
name: "demo"
}
]
};
export default {
env: 'mock'
};
import instance from './instance';
/** swagger test */
function swagger(opts) {
return instance({
method: 'get',
url: '/swagger',
opts: opts
});
}
/** 支持接口代理的mock,试试在 url 上加 ?s={数字} */
function proxy(opts) {
return instance({
method: 'get',
url: '/proxy',
opts: opts
});
}
/** 带随机数据的mock */
function mock(opts) {
return instance({
method: 'get',
url: '/mock',
opts: opts
});
}
/** 根据请求参数返回指定数据,试试在 url 上加 ?name={任意值} */
function query(opts) {
return instance({
method: 'get',
url: '/query',
opts: opts
});
}
/** 支持 restful 的mock,替换 id 试试 */
function restful_id_list(opts) {
return instance({
method: 'get',
url: '/restful/:id/list',
opts: opts
});
}
export {
swagger,
proxy,
mock,
query,
restful_id_list
};
import { createAPI } from '../util';
import config from '../config';
const baseUrl = {
mock: 'https://www.easy-mock.com/mock/58fef6ac5e43ae5dbea5eb53/example',
dev: '',
pre: '',
prod: ''
}[config.env || 'mock'];
export default createAPI(baseUrl);
import * as demo from './demo';
export {
demo
};
import axios from 'axios';
const instance = axios.create();
function createAPI(baseURL) {
return function (conf) {
conf = conf || {};
return instance(Object.assign({}, {
url: conf.url,
baseURL: baseURL,
method: conf.method
}, conf.opts));
};
}
export {
createAPI
};
import * as api from './api';
api.demo.swagger();
const path = require('path');
const HTMLPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
main: './example/demo.js'
},
output: {
path: path.resolve(__dirname, '../dist'),
publicPath: '/',
filename: 'demo.js'
},
resolve: {
extensions: ['.js']
},
module: {
rules: [{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
}]
},
plugins: [
new HTMLPlugin()
]
};
exports.convertUrl = function (url) {
// /restful/:id/:list/{id} -> restful_id_list_id
// /restful/:id/:list/{id}.json -> restful_id_list_id
const _url = url
.replace(/:|{|}/g, '')
.split('/')
.filter(value => !!value).join('_');
return _url.split('.')[0];
};
{
"name": "easy-mock-template-axios",
"version": "0.1.0",
"description": "基于 axios 的接口定义模板",
"main": "index.js",
"scripts": {
"build": "rimraf dist && webpack --config example/webpack.conf.js --progress --hide-modules",
"server": "serve ./dist -p 3333",
"create-api": "easymock init example",
"dev": "npm run create-api && npm run build"
},
"author": "Chuangker",
"license": "MIT",
"keywords": [
"easy-mock",
"mock"
],
"repository": {
"type": "git",
"url": "git+https://github.com/easy-mock-templates/axios.git"
},
"devDependencies": {
"axios": "^0.16.1",
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"easy-mock-cli": "^0.1.2",
"html-webpack-plugin": "^2.28.0",
"rimraf": "^2.6.1",
"serve": "^5.1.5",
"webpack": "^2.5.1"
}
}
export default {
env: 'mock'
};
<% _.forEach(config.projects, function(project){ %>import * as <%- $$.convertUrl(project.name) %> from './<%- project.name %>';<% }) %>
export {<% _.forEach(config.projects, function(project, i){ %>
<%- $$.convertUrl(project.name) %><% if(config.projects.length - 1 !== i) { %>,<% } %><% }) %>
};
import axios from 'axios';
const instance = axios.create();
function createAPI(baseURL) {
return function (conf) {
conf = conf || {};
return instance(Object.assign({}, {
url: conf.url,
baseURL: baseURL,
method: conf.method
}, conf.opts));
};
}
export {
createAPI
};
import instance from './instance';
<% _.forEach(data.mocks, function(mock){ %>/** <%- mock.description %> */
function <%- $$.convertUrl(mock.url) %>(opts) {
return instance({
method: '<%- mock.method %>',
url: '<%- mock.url %>',
opts: opts
});
}
<% }) %>export {<% _.forEach(data.mocks, function(mock, i){ %>
<%- $$.convertUrl(mock.url) %><% if(data.mocks.length - 1 !== i) { %>,<% } %><% }) %>
};
import { createAPI } from '<%- $$.relative("util") %>';
import config from '<%- $$.relative("config") %>';
const baseUrl = {
mock: 'https://www.easy-mock.com/mock/<%- data.project._id %><%- data.project.url %>',
dev: '',
pre: '',
prod: ''
}[config.env || 'mock'];
export default createAPI(baseUrl);
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论