') !== '7';
});
// @@replace logic
fixRegExpWellKnownSymbolLogic('replace', function (_, nativeReplace, maybeCallNative) {
var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
return [
// `String.prototype.replace` method
// https://tc39.es/ecma262/#sec-string.prototype.replace
function replace(searchValue, replaceValue) {
var O = requireObjectCoercible(this);
var replacer = searchValue == undefined ? undefined : getMethod(searchValue, REPLACE);
return replacer
? replacer.call(searchValue, O, replaceValue)
: nativeReplace.call(toString(O), searchValue, replaceValue);
},
// `RegExp.prototype[@@replace]` method
// https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
function (string, replaceValue) {
var rx = anObject(this);
var S = toString(string);
if (
typeof replaceValue === 'string' &&
replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1 &&
replaceValue.indexOf('$<') === -1
) {
var res = maybeCallNative(nativeReplace, rx, S, replaceValue);
if (res.done) return res.value;
}
var functionalReplace = isCallable(replaceValue);
if (!functionalReplace) replaceValue = toString(replaceValue);
var global = rx.global;
if (global) {
var fullUnicode = rx.unicode;
rx.lastIndex = 0;
}
var results = [];
while (true) {
var result = regExpExec(rx, S);
if (result === null) break;
results.push(result);
if (!global) break;
var matchStr = toString(result[0]);
if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
}
var accumulatedResult = '';
var nextSourcePosition = 0;
for (var i = 0; i < results.length; i++) {
result = results[i];
var matched = toString(result[0]);
var position = max(min(toIntegerOrInfinity(result.index), S.length), 0);
var captures = [];
// NOTE: This is equivalent to
// captures = result.slice(1).map(maybeToString)
// but for some reason `nativeSlice.call(result, 1, result.length)` (called in
// the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and
// causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it.
for (var j = 1; j < result.length; j++) captures.push(maybeToString(result[j]));
var namedCaptures = result.groups;
if (functionalReplace) {
var replacerArgs = [matched].concat(captures, position, S);
if (namedCaptures !== undefined) replacerArgs.push(namedCaptures);
var replacement = toString(replaceValue.apply(undefined, replacerArgs));
} else {
replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue);
}
if (position >= nextSourcePosition) {
accumulatedResult += S.slice(nextSourcePosition, position) + replacement;
nextSourcePosition = position + matched.length;
}
}
return accumulatedResult + S.slice(nextSourcePosition);
}
];
}, !REPLACE_SUPPORTS_NAMED_GROUPS || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);
const baseURL = config.baseURL;
const $ = function (selector) {
return document.querySelector(selector);
};
const $$ = function (selector) {
return document.querySelectorAll(selector);
};
/**
* 把
* @param {*} newNode
* @param {*} existingNode
*/
function insertAfter(newNode, existingNode) {
existingNode.parentNode.insertBefore(newNode, existingNode.nextSibling);
}
function addStyle(css) {
var style = document.createElement("style");
style.textContent = css;
document.head.appendChild(style);
}
function copyText(text) {
if (navigator.clipboard) {
// clipboard api 复制
navigator.clipboard.writeText(text);
} else {
var textarea = document.createElement("textarea");
document.body.appendChild(textarea);
// 隐藏此输入框
textarea.style.position = "fixed";
textarea.style.clip = "rect(0 0 0 0)";
textarea.style.top = "10px";
// 赋值
textarea.value = text;
// 选中
textarea.select();
// 复制
document.execCommand("copy", true);
// 移除输入框
document.body.removeChild(textarea);
}
}
/**
* 根据请求路径和请求方法构建一个函数名称
* @param {String} url
* @param {String} method
* @returns 函数名称
*/
function generateFnNameByUrl(url, method) {
/* 处理 url 中类似 {id} 的部分*/
// 如果 url 中出现了 - 也要去掉
const wordAry = url.replace(/{(.+)}/g, "by/$1").replace("-", "/").split("/").map(word => {
return word.replace(word.charAt(0), word.charAt(0).toUpperCase());
});
if (method.toUpperCase() === "GET") {
return "get" + wordAry.join("");
} else {
// 其他类型的请求,一般会把动词放在最后
// 所以把动词提到最前面
const action = wordAry.pop();
wordAry.unshift(action.toLowerCase());
return wordAry.join("");
}
}
function buildFunction(url, method, desc) {
console.log("%c Line:65 🍡 url", "color:#ea7e5c", url);
url = url.replace(baseURL, "");
const fnName = generateFnNameByUrl(url, method);
const {
url: $url,
params
} = parseUrl(url);
let paramsStr = "data";
if (params.length > 0) {
paramsStr = "(" + [...params, "data"].join(", ") + ")";
}
console.log("%c Line:85 🎂 config.methodFields", "color:#ea7e5c", config.methodFields);
return `
// ${desc}${"\n// 接口文档:" + location.href }
${"export " }const ${fnName} = ${paramsStr} => ${config.methodFields[method]}(${$url}, data)
`;
}
function parseUrl(url) {
var reg = /{([^{]+)}/g;
const ret = url.match(reg);
const params = ret ? ret.map(curr => curr.replace(/{|}/g, "")) : [];
return {
url: "`" + url.replace("{", "${") + "`",
params
};
}
function appendDom(baseURL, fnString) {
const div = document.createElement("div");
const divClassName = "yapi-script-dom";
div.classList.add(divClassName);
// 判断已经有这个 div 了,如果有先删除
const self = $("." + divClassName);
if (self) {
self.parentElement.removeChild(self);
}
addStyle(`
pre,
.${divClassName} pre {
margin-bottom: 0;
position: relative;
}
pre code,
.${divClassName} pre code {
overflow-x: auto;
color: #525252;
white-space: pre;
padding: 1.2em 1.4em;
font-size: 1em;
line-height: inherit;
display: block;
padding: 1em;
overflow: auto;
background-color: #f6f8fa;
border-radius: 6px;
}
.icon {
position: absolute;
top: 2em;
right: 1em;
display: inline-block;
width: 2em;
height: 2em;
background-repeat: no-repeat;
z-index: 10;
}
.icon-hide {
display: none;
}
.icon-copy {
background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0NDggNTEyIj4gPHBhdGggZmlsbD0iIzVBNjE2QSIgZD0iTTQzMy45NDEgNjUuOTQxbC01MS44ODItNTEuODgyQTQ4IDQ4IDAgMCAwIDM0OC4xMTggMEgxNzZjLTI2LjUxIDAtNDggMjEuNDktNDggNDh2NDhINDhjLTI2LjUxIDAtNDggMjEuNDktNDggNDh2MzIwYzAgMjYuNTEgMjEuNDkgNDggNDggNDhoMjI0YzI2LjUxIDAgNDgtMjEuNDkgNDgtNDh2LTQ4aDgwYzI2LjUxIDAgNDgtMjEuNDkgNDgtNDhWOTkuODgyYTQ4IDQ4IDAgMCAwLTE0LjA1OS0zMy45NDF6TTI2NiA0NjRINTRhNiA2IDAgMCAxLTYtNlYxNTBhNiA2IDAgMCAxIDYtNmg3NHYyMjRjMCAyNi41MSAyMS40OSA0OCA0OCA0OGg5NnY0MmE2IDYgMCAwIDEtNiA2em0xMjgtOTZIMTgyYTYgNiAwIDAgMS02LTZWNTRhNiA2IDAgMCAxIDYtNmgxMDZ2ODhjMCAxMy4yNTUgMTAuNzQ1IDI0IDI0IDI0aDg4djIwMmE2IDYgMCAwIDEtNiA2em02LTI1NmgtNjRWNDhoOS42MzJjMS41OTEgMCAzLjExNy42MzIgNC4yNDMgMS43NTdsNDguMzY4IDQ4LjM2OGE2IDYgMCAwIDEgMS43NTcgNC4yNDNWMTEyeiI+PC9wYXRoPiA8L3N2Zz4=);
cursor: pointer;
transition: all .3s ease-in-out;
}
.icon-copy:hover {
transform: scale(1.2);
}
.icon-check {
background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MTIgNTEyIj4gPHBhdGggZD0iTTE3My44OTggNDM5LjQwNGwtMTY2LjQtMTY2LjRjLTkuOTk3LTkuOTk3LTkuOTk3LTI2LjIwNiAwLTM2LjIwNGwzNi4yMDMtMzYuMjA0YzkuOTk3LTkuOTk4IDI2LjIwNy05Ljk5OCAzNi4yMDQgMEwxOTIgMzEyLjY5IDQzMi4wOTUgNzIuNTk2YzkuOTk3LTkuOTk3IDI2LjIwNy05Ljk5NyAzNi4yMDQgMGwzNi4yMDMgMzYuMjA0YzkuOTk3IDkuOTk3IDkuOTk3IDI2LjIwNiAwIDM2LjIwNGwtMjk0LjQgMjk0LjQwMWMtOS45OTggOS45OTctMjYuMjA3IDkuOTk3LTM2LjIwNC0uMDAxeiI+PC9wYXRoPiA8L3N2Zz4=);
}
`);
div.innerHTML = `
生成的代码
当前 baseURL 为: ${baseURL}
`;
insertAfter(div, $(".panel-view"));
}
function enableCopy() {
$$(".js-copy-btn").forEach(btn => {
btn.addEventListener("click", e => {
copyCode(e);
});
});
}
function copyCode(e) {
console.log("%c Line:75 🧀 e", "color:#465975", e);
const code = e.target.getAttribute("data-code");
copyText(code);
e.target.classList.add("icon-hide");
e.target.nextElementSibling.classList.remove("icon-hide");
setTimeout(() => {
e.target.classList.remove("icon-hide");
e.target.nextElementSibling.classList.add("icon-hide");
}, 800);
}
function tableColumns(columns) {
const div = document.createElement("div");
const divClassName = "yapi-script-columns-dom";
div.classList.add(divClassName);
// 判断已经有这个 div 了,如果有先删除
const self = $("." + divClassName);
if (self) {
self.parentElement.removeChild(self);
}
if (columns === "") {
return;
}
const code = "export const columns = " + JSON.stringify(columns, null, 2);
console.log("%c Line:92 🍉 code", "color:#2eafb0", code);
div.innerHTML = `
根据「返回数据」中的 data
字段生成表格的 columns
数据,请根据实际需求进行调整使用。
`;
$(".caseContainer").append(div);
}
// import style from './styles/global.module.css'
customXMLHttpRequest({
eventHandlers: {
onloadend(e) {
const xhr = e.target;
if (xhr.readyState === 4 && xhr.responseURL.includes("api/interface/get?") && xhr.status === 200) {
const res = JSON.parse(e.target.response).data;
console.log("%c Line:61 🍋 res", "color:#3f7cff", res);
const data = {
desc: res.title,
path: res.path,
method: res.method,
// req_body: JSON.parse(res.req_body_other),
res_body: JSON.parse(res.res_body)
};
console.log("%c Line:58 🥕 data", "color:#33a5ff", data);
const resBody = JSON.parse(res.res_body)?.properties?.data?.items?.properties;
if (resBody) {
// console.log("%c Line:68 🍧 resBody", "color:#93c0a4", resBody);
const columns = Object.keys(resBody).map(key => {
const desc = resBody[key].description;
return {
title: desc || key,
dataIndex: key
// type: resBody[key].type,
};
});
console.log("%c Line:71 🌰 columns", "color:#33a5ff", JSON.stringify(columns, null, 2));
tableColumns(columns);
} else {
tableColumns("");
}
const fnString = buildFunction(data.path, data.method, data.desc);
// 具体的接口页面
appendDom(config.baseURL, fnString);
enableCopy();
}
}
}
});
})();
//# sourceMappingURL=main.dev.user.js.map