@alova/wormhole
Version:
More modern openAPI generating solution for alova.js
106 lines (105 loc) • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStandardOperationId = getStandardOperationId;
exports.getStandardTags = getStandardTags;
exports.getRandomVariable = getRandomVariable;
exports.getStandardRefName = getStandardRefName;
const utils_1 = require("../../../utils");
function getStandardOperationId(pathObject, options) {
const { url, method, map, standardLoader } = options;
if (standardLoader.validate(pathObject.operationId)) {
return pathObject.operationId;
}
let operationId = '';
if (pathObject.operationId) {
operationId = standardLoader.transform(pathObject.operationId, {
style: 'camelCas',
});
}
if (!operationId) {
operationId = standardLoader.transform(`${method}/${url}`, {
style: 'snakeCase',
});
}
if (map.has(operationId)) {
let num = 1;
while (map.has(`${operationId}${num}`)) {
num += 1;
}
operationId = `${operationId}${num}`;
}
map.add(operationId);
return operationId;
}
function getStandardTags(tags, options) {
const { standardLoader } = options;
const tagsSet = new Set();
if (!tags || !tags.length) {
return ['general'];
}
return tags.map((tag) => {
tag = tag.trim();
if (standardLoader.validate(tag)) {
tagsSet.add(tag);
return tag;
}
let newTag = '';
if (tag) {
newTag = standardLoader.transform(tag, {
style: 'camelCas',
});
}
if (tagsSet.has(newTag)) {
let num = 1;
while (tagsSet.has(`${newTag}${num}`)) {
num += 1;
}
newTag = `${newTag}${num}`;
}
if (!newTag) {
newTag = 'general';
}
tagsSet.add(newTag);
return newTag;
});
}
function getRandomVariable(value) {
return `${(0, utils_1.strHashCode)(value)}`
.split('')
.map((code) => {
let numberCode = Number(code);
numberCode = Number.isNaN(numberCode) ? 10 : numberCode;
return String.fromCharCode(numberCode + 97);
})
.join('');
}
const refPathMap = new Map();
const refNameSet = new Set();
function getStandardRefName(refPath, options) {
const { toUpperCase = true, standardLoader } = options;
if (refPathMap.has(refPath)) {
return refPathMap.get(refPath) ?? '';
}
const refName = (0, utils_1.get$refName)(refPath, toUpperCase);
if (standardLoader.validate(refName)) {
refNameSet.add(refName);
refPathMap.set(refPath, refName);
return refName;
}
let newRefName = standardLoader.transform(refName, {
style: 'snakeCase',
}) || getRandomVariable(refName);
if (toUpperCase) {
newRefName = (0, utils_1.capitalizeFirstLetter)(newRefName);
}
if (refNameSet.has(newRefName)) {
let num = 1;
while (refNameSet.has(`${newRefName}${num}`)) {
num += 1;
}
newRefName = `${newRefName}${num}`;
}
refNameSet.add(newRefName);
refPathMap.set(refPath, newRefName);
return newRefName;
}