提交 1ba1f8c2 作者: chuangker

feat: 支持 RESTful

上级 b43df22e
import instance from './instance';
import { convertRESTAPI } from '../util';
/** swagger test */
function swagger(opts) {
function swagger_get(opts) {
return instance({
method: 'get',
url: '/swagger',
url: '/swagger',
opts: opts
});
}
/** 支持接口代理的mock,试试在 url 上加 ?s={数字} */
function proxy(opts) {
function proxy_get(opts) {
return instance({
method: 'get',
url: '/proxy',
url: '/proxy',
opts: opts
});
}
/** 带随机数据的mock */
function mock(opts) {
function mock_get(opts) {
return instance({
method: 'get',
url: '/mock',
url: '/mock',
opts: opts
});
}
/** 根据请求参数返回指定数据,试试在 url 上加 ?name={任意值} */
function query(opts) {
function query_get(opts) {
return instance({
method: 'get',
url: '/query',
url: '/query',
opts: opts
});
}
/** 支持 restful 的mock,替换 id 试试 */
function restful_id_list(opts) {
function restful_id_list_get(opts) {
return instance({
method: 'get',
url: '/restful/:id/list',
url: convertRESTAPI('/restful/:id/list', opts),
opts: opts
});
}
export {
swagger,
proxy,
mock,
query,
restful_id_list
swagger_get,
proxy_get,
mock_get,
query_get,
restful_id_list_get
};
......@@ -13,6 +13,20 @@ function createAPI(baseURL) {
};
}
function convertRESTAPI(url, opts) {
if (!opts || !opts.path) return url;
const pathKeys = Object.keys(opts.path);
pathKeys.forEach((key) => {
const r = new RegExp('(:' + key + '|{' + key + '})', 'g');
url = url.replace(r, opts.path[key]);
});
return url;
}
export {
createAPI
createAPI,
convertRESTAPI
};
import * as api from './api';
api.demo.swagger();
api.demo.restful_id_list_g({
path: {
id: 1
}
}).then((res) => {
console.log(res.data);
});
exports.convertUrl = function (url) {
const convertUrl = exports.convertUrl = function (url) {
// /restful/:id/:list/{id} -> restful_id_list_id
// /restful/:id/:list/{id}.json -> restful_id_list_id
const _url = url
......@@ -7,3 +7,23 @@ exports.convertUrl = function (url) {
.filter(value => !!value).join('_');
return _url.split('.')[0];
};
exports.convertMethod = function (mock) {
// 防止重名
// restful_id_list_id => restful_id_list_id_g
// or
// restful_id_list_id => restful_id_list_id_p
return convertUrl(mock.url) + '_' + mock.method.toLowerCase();
};
exports.joinUrl = function () {
// https://www.easy-mock.com//mock/.... => https://www.easy-mock.com/mock/....
let url = [].slice.call(arguments, 0).join('/');
url = url.replace(/:\//g, '://');
url = url.replace(/([^:\s\%\3\A])\/+/g, '$1/');
return url;
}
exports.isREST = function (url) {
return /(:|{|})/.test(url);
}
......@@ -13,6 +13,20 @@ function createAPI(baseURL) {
};
}
function convertRESTAPI(url, opts) {
if (!opts || !opts.path) return url;
const pathKeys = Object.keys(opts.path);
pathKeys.forEach((key) => {
const r = new RegExp('(:' + key + '|{' + key + '})', 'g');
url = url.replace(r, opts.path[key]);
});
return url;
}
export {
createAPI
createAPI,
convertRESTAPI
};
import instance from './instance';
import { convertRESTAPI } from '<%- $$.relative("util") %>';
<% _.forEach(data.mocks, function(mock){ %>/** <%- mock.description %> */
function <%- $$.convertUrl(mock.url) %>(opts) {
function <%- $$.convertMethod(mock) %>(opts) {
return instance({
method: '<%- mock.method %>',
url: '<%- mock.url %>',
url: <% if($$.isREST(mock.url)) {%>convertRESTAPI('<%- mock.url %>', opts)<%} else {%> '<%- mock.url %>'<% } %>,
opts: opts
});
}
<% }) %>export {<% _.forEach(data.mocks, function(mock, i){ %>
<%- $$.convertUrl(mock.url) %><% if(data.mocks.length - 1 !== i) { %>,<% } %><% }) %>
<%- $$.convertMethod(mock) %><% if(data.mocks.length - 1 !== i) { %>,<% } %><% }) %>
};
......@@ -2,7 +2,7 @@ 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 %>',
mock: '<%- $$.joinUrl(config.host, "mock", data.project._id, data.project.url) %>',
dev: '',
pre: '',
prod: ''
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论