422 lines
15 KiB
JavaScript
422 lines
15 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const acorn = require('acorn');
|
|
|
|
const baseDir = process.cwd();
|
|
const methodsDir = path.join(baseDir, 'app', 'service', 'systemMethods');
|
|
const jsonPath = path.join(baseDir, 'methods', 'system.json');
|
|
|
|
function walk(node, visitor, parent = null) {
|
|
if (!node || typeof node !== 'object') return;
|
|
visitor(node, parent);
|
|
for (const key of Object.keys(node)) {
|
|
const value = node[key];
|
|
if (!value) continue;
|
|
if (Array.isArray(value)) {
|
|
for (const item of value) walk(item, visitor, node);
|
|
} else if (typeof value === 'object' && value.type) {
|
|
walk(value, visitor, node);
|
|
}
|
|
}
|
|
}
|
|
|
|
function walkFiles(dir) {
|
|
const files = [];
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
const full = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) files.push(...walkFiles(full));
|
|
if (entry.isFile() && entry.name.endsWith('.js')) files.push(full);
|
|
}
|
|
return files;
|
|
}
|
|
|
|
function getPropName(node) {
|
|
if (!node) return null;
|
|
if (node.type === 'Identifier') return node.name;
|
|
if (node.type === 'Literal') return String(node.value);
|
|
return null;
|
|
}
|
|
|
|
function memberPath(node) {
|
|
if (!node) return null;
|
|
if (node.type === 'ChainExpression') return memberPath(node.expression);
|
|
if (node.type === 'Identifier') return node.name;
|
|
if (node.type === 'ThisExpression') return 'this';
|
|
if (node.type !== 'MemberExpression') return null;
|
|
const left = memberPath(node.object);
|
|
if (!left) return null;
|
|
if (node.computed) {
|
|
if (node.property.type === 'Literal') return `${left}.${String(node.property.value)}`;
|
|
if (node.property.type === 'Identifier') return left;
|
|
return left;
|
|
}
|
|
const right = getPropName(node.property);
|
|
return right ? `${left}.${right}` : left;
|
|
}
|
|
|
|
function classifySourceByExpr(expr, signatureSet) {
|
|
const sources = new Set();
|
|
const visit = node => {
|
|
if (!node) return;
|
|
if (node.type === 'Identifier' && signatureSet.has(node.name)) {
|
|
sources.add('body');
|
|
return;
|
|
}
|
|
if (node.type === 'MemberExpression' || node.type === 'ChainExpression') {
|
|
const p = memberPath(node);
|
|
if (p && p.startsWith('ctx.request.body')) sources.add('body');
|
|
else if (p && p.startsWith('ctx.query')) sources.add('query');
|
|
else if (p && p.startsWith('ctx.params')) sources.add('path');
|
|
else if (p && p.startsWith('ctx.headers')) sources.add('header');
|
|
return;
|
|
}
|
|
if (node.type === 'LogicalExpression' || node.type === 'BinaryExpression') {
|
|
visit(node.left);
|
|
visit(node.right);
|
|
return;
|
|
}
|
|
if (node.type === 'ConditionalExpression') {
|
|
visit(node.consequent);
|
|
visit(node.alternate);
|
|
return;
|
|
}
|
|
};
|
|
visit(expr);
|
|
if (!sources.size) return null;
|
|
return Array.from(sources).sort().join('|');
|
|
}
|
|
|
|
function inferTypeByName(name) {
|
|
if (!name) return 'string';
|
|
if (/ids$/i.test(name)) return 'array';
|
|
if (/^(where|pagination|item|template|data|filters?)$/i.test(name)) return 'object';
|
|
if (/^(is_|has_|need|sync)/i.test(name)) return 'boolean';
|
|
if (/(^id$|_id$|^page$|^pageSize$|^limit$|^offset$|^count$|_count$|^x$|^y$|^z$|^rows$|^columns$|^levels$|^width$|^height$|^length$)/i.test(name)) return 'number';
|
|
if (/(^date$|_date$|^time$|_time$|timestamp)/i.test(name)) return 'string';
|
|
return 'string';
|
|
}
|
|
|
|
function inferTypeByNode(node, fallbackName = '') {
|
|
if (!node) return inferTypeByName(fallbackName);
|
|
if (node.type === 'Literal') {
|
|
if (typeof node.value === 'number') return 'number';
|
|
if (typeof node.value === 'boolean') return 'boolean';
|
|
return 'string';
|
|
}
|
|
if (node.type === 'ObjectExpression') return 'object';
|
|
if (node.type === 'ArrayExpression') return 'array';
|
|
return inferTypeByName(fallbackName);
|
|
}
|
|
|
|
function mapValidateType(v) {
|
|
if (!v) return 'string';
|
|
const t = String(v).toLowerCase();
|
|
if (t === 'int' || t === 'integer' || t === 'number') return 'number';
|
|
if (t === 'boolean' || t === 'bool') return 'boolean';
|
|
if (t === 'array') return 'array';
|
|
if (t === 'object') return 'object';
|
|
return 'string';
|
|
}
|
|
|
|
function extractNoNeedLists(classNode) {
|
|
const result = { noNeedLogin: [], noNeedAuth: ['*'] };
|
|
const ctor = classNode.body.body.find(m => m.type === 'MethodDefinition' && m.kind === 'constructor');
|
|
if (!ctor) return result;
|
|
|
|
for (const stmt of ctor.value.body.body || []) {
|
|
if (stmt.type !== 'VariableDeclaration') continue;
|
|
for (const decl of stmt.declarations || []) {
|
|
if (decl.id.type !== 'Identifier' || !decl.init || decl.init.type !== 'Literal') continue;
|
|
const name = decl.id.name;
|
|
const value = String(decl.init.value || '');
|
|
if (name === '_noNeedLogin') result.noNeedLogin = value.split(',').map(s => s.trim()).filter(Boolean);
|
|
if (name === '_noNeedAuth') result.noNeedAuth = value.split(',').map(s => s.trim()).filter(Boolean);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function collectMethodParams(methodNode) {
|
|
const signatureNames = [];
|
|
for (const p of methodNode.value.params || []) {
|
|
if (p.type === 'Identifier') signatureNames.push(p.name);
|
|
else if (p.type === 'AssignmentPattern' && p.left.type === 'Identifier') signatureNames.push(p.left.name);
|
|
}
|
|
const signatureSet = new Set(signatureNames);
|
|
const aliasMap = new Map();
|
|
const entries = new Map();
|
|
const listFields = [];
|
|
|
|
function resolveName(localName) {
|
|
return aliasMap.get(localName) || localName;
|
|
}
|
|
|
|
function ensure(name) {
|
|
if (!entries.has(name)) {
|
|
entries.set(name, {
|
|
name,
|
|
type: inferTypeByName(name),
|
|
required: false,
|
|
source: new Set(),
|
|
});
|
|
}
|
|
return entries.get(name);
|
|
}
|
|
|
|
function addParam(name, patch = {}) {
|
|
if (!name) return;
|
|
if ([ 'ctx', 'service', 'app', 'result', 'error', 'e' ].includes(name)) return;
|
|
const item = ensure(name);
|
|
if (patch.type) item.type = patch.type;
|
|
if (patch.required === true) item.required = true;
|
|
if (patch.source) item.source.add(patch.source);
|
|
}
|
|
|
|
function addRequiredFromTest(testNode) {
|
|
if (!testNode) return;
|
|
if (testNode.type === 'UnaryExpression' && testNode.operator === '!') {
|
|
if (testNode.argument.type === 'Identifier') {
|
|
const local = testNode.argument.name;
|
|
const resolved = resolveName(local);
|
|
if (resolved !== local || entries.has(resolved)) {
|
|
addParam(resolved, { required: true });
|
|
}
|
|
}
|
|
if (testNode.argument.type === 'MemberExpression' || testNode.argument.type === 'ChainExpression') {
|
|
const p = memberPath(testNode.argument);
|
|
if (!p) return;
|
|
const parts = p.split('.');
|
|
if (parts.length === 2 && signatureSet.has(parts[0])) {
|
|
addParam(parts[1], { required: true, source: 'body' });
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
if (testNode.type === 'LogicalExpression') {
|
|
addRequiredFromTest(testNode.left);
|
|
addRequiredFromTest(testNode.right);
|
|
return;
|
|
}
|
|
if (testNode.type === 'CallExpression') {
|
|
const callee = memberPath(testNode.callee);
|
|
if (callee === 'Array.isArray' && testNode.arguments[0] && testNode.arguments[0].type === 'Identifier') {
|
|
const local = testNode.arguments[0].name;
|
|
const resolved = resolveName(local);
|
|
if (resolved !== local || entries.has(resolved)) {
|
|
addParam(resolved, { required: true, type: 'array' });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
walk(methodNode.value.body, (node, parent) => {
|
|
if (node.type === 'VariableDeclarator') {
|
|
if (node.id.type === 'ObjectPattern' && node.init) {
|
|
const source = classifySourceByExpr(node.init, signatureSet);
|
|
if (!source) return;
|
|
for (const prop of node.id.properties || []) {
|
|
if (prop.type !== 'Property') continue;
|
|
const keyName = getPropName(prop.key);
|
|
if (!keyName) continue;
|
|
|
|
let localName = keyName;
|
|
let type = inferTypeByName(keyName);
|
|
|
|
if (prop.value.type === 'Identifier') {
|
|
localName = prop.value.name;
|
|
} else if (prop.value.type === 'AssignmentPattern') {
|
|
if (prop.value.left.type === 'Identifier') localName = prop.value.left.name;
|
|
type = inferTypeByNode(prop.value.right, keyName);
|
|
}
|
|
|
|
aliasMap.set(localName, keyName);
|
|
addParam(keyName, { source, type });
|
|
}
|
|
}
|
|
|
|
if (node.id.type === 'Identifier' && node.init) {
|
|
const local = node.id.name;
|
|
|
|
if (node.init.type === 'MemberExpression' || node.init.type === 'ChainExpression') {
|
|
const p = memberPath(node.init);
|
|
if (p) {
|
|
const parts = p.split('.');
|
|
if (parts.length === 2 && signatureSet.has(parts[0])) {
|
|
aliasMap.set(local, parts[1]);
|
|
addParam(parts[1], { source: 'body' });
|
|
}
|
|
if (parts.length >= 3 && parts[0] === 'ctx' && parts[1] === 'query') {
|
|
aliasMap.set(local, parts[2]);
|
|
addParam(parts[2], { source: 'query' });
|
|
}
|
|
if (parts.length >= 3 && parts[0] === 'ctx' && parts[1] === 'params') {
|
|
aliasMap.set(local, parts[2]);
|
|
addParam(parts[2], { source: 'path' });
|
|
}
|
|
if (parts.length >= 4 && parts[0] === 'ctx' && parts[1] === 'request' && parts[2] === 'body') {
|
|
aliasMap.set(local, parts[3]);
|
|
addParam(parts[3], { source: 'body' });
|
|
}
|
|
}
|
|
}
|
|
|
|
if ((/allowedFields|eqFields/i).test(local) && node.init.type === 'ArrayExpression') {
|
|
const arr = [];
|
|
for (const el of node.init.elements || []) {
|
|
if (el && el.type === 'Literal' && typeof el.value === 'string') arr.push(el.value);
|
|
}
|
|
if (arr.length) listFields.push(...arr);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (node.type === 'IfStatement') {
|
|
addRequiredFromTest(node.test);
|
|
}
|
|
|
|
if (node.type === 'CallExpression') {
|
|
const callee = memberPath(node.callee);
|
|
|
|
if (callee === 'ctx.validate' && node.arguments[0] && node.arguments[0].type === 'ObjectExpression') {
|
|
for (const p of node.arguments[0].properties || []) {
|
|
if (p.type !== 'Property') continue;
|
|
const field = getPropName(p.key);
|
|
if (!field || p.value.type !== 'ObjectExpression') continue;
|
|
let required = false;
|
|
let type = null;
|
|
for (const conf of p.value.properties || []) {
|
|
if (conf.type !== 'Property') continue;
|
|
const confKey = getPropName(conf.key);
|
|
if (!confKey) continue;
|
|
if (confKey === 'required' && conf.value.type === 'Literal') {
|
|
required = Boolean(conf.value.value);
|
|
}
|
|
if (confKey === 'type' && conf.value.type === 'Literal') {
|
|
type = mapValidateType(conf.value.value);
|
|
}
|
|
}
|
|
addParam(field, { required, type: type || inferTypeByName(field), source: 'body' });
|
|
}
|
|
}
|
|
|
|
if (callee === 'Array.isArray' && node.arguments[0] && node.arguments[0].type === 'Identifier') {
|
|
const local = node.arguments[0].name;
|
|
const resolved = resolveName(local);
|
|
if (resolved !== local || entries.has(resolved)) {
|
|
addParam(resolved, { type: 'array' });
|
|
}
|
|
}
|
|
|
|
if ((callee === 'parseInt' || callee === 'Number') && node.arguments[0]) {
|
|
const arg = node.arguments[0];
|
|
if (arg.type === 'Identifier') {
|
|
const resolved = resolveName(arg.name);
|
|
if (resolved !== arg.name || entries.has(resolved)) {
|
|
addParam(resolved, { type: 'number' });
|
|
}
|
|
}
|
|
if (arg.type === 'MemberExpression' || arg.type === 'ChainExpression') {
|
|
const p = memberPath(arg);
|
|
if (p) {
|
|
const parts = p.split('.');
|
|
if (parts.length === 2 && signatureSet.has(parts[0])) addParam(parts[1], { type: 'number', source: 'body' });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (node.type === 'MemberExpression' || node.type === 'ChainExpression') {
|
|
const p = memberPath(node);
|
|
if (!p) return;
|
|
const parts = p.split('.');
|
|
if (parts.length === 2 && signatureSet.has(parts[0])) {
|
|
addParam(parts[1], { source: 'body' });
|
|
}
|
|
if (parts.length >= 3 && parts[0] === 'ctx' && parts[1] === 'query') {
|
|
addParam(parts[2], { source: 'query' });
|
|
}
|
|
if (parts.length >= 3 && parts[0] === 'ctx' && parts[1] === 'params') {
|
|
addParam(parts[2], { source: 'path' });
|
|
}
|
|
if (parts.length >= 4 && parts[0] === 'ctx' && parts[1] === 'request' && parts[2] === 'body') {
|
|
addParam(parts[3], { source: 'body' });
|
|
}
|
|
}
|
|
});
|
|
|
|
for (const f of listFields) {
|
|
addParam(f, { source: 'body' });
|
|
}
|
|
|
|
const result = Array.from(entries.values()).map(item => ({
|
|
name: item.name,
|
|
type: item.type,
|
|
required: item.required,
|
|
source: item.source.size ? Array.from(item.source).sort().join('|') : 'body',
|
|
description: `${item.name} 参数`,
|
|
}));
|
|
|
|
result.sort((a, b) => {
|
|
if (a.required !== b.required) return a.required ? -1 : 1;
|
|
return a.name.localeCompare(b.name);
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
function buildMethodParamMap() {
|
|
const map = new Map();
|
|
const files = walkFiles(methodsDir);
|
|
|
|
for (const file of files) {
|
|
const source = fs.readFileSync(file, 'utf8');
|
|
const ast = acorn.parse(source, { ecmaVersion: 'latest', sourceType: 'script' });
|
|
const relative = path.relative(methodsDir, file).replace(/\\/g, '/').replace(/\.js$/, '');
|
|
const moduleName = relative.replace(/\//g, '.');
|
|
|
|
for (const node of ast.body) {
|
|
if (node.type !== 'ClassDeclaration') continue;
|
|
const noNeed = extractNoNeedLists(node);
|
|
for (const method of node.body.body) {
|
|
if (method.type !== 'MethodDefinition') continue;
|
|
if (method.kind !== 'method') continue;
|
|
if (!method.value.async) continue;
|
|
const methodName = method.key && method.key.name;
|
|
if (!methodName) continue;
|
|
|
|
const fullMethod = `${moduleName}.${methodName}`;
|
|
const params = collectMethodParams(method);
|
|
|
|
for (const p of params) {
|
|
if (noNeed.noNeedLogin.includes('*') || noNeed.noNeedLogin.includes(methodName)) {
|
|
if (p.name.toLowerCase().includes('authorization') || p.source === 'header') p.required = false;
|
|
}
|
|
}
|
|
|
|
map.set(fullMethod, params);
|
|
}
|
|
}
|
|
}
|
|
|
|
return map;
|
|
}
|
|
|
|
const methodParamMap = buildMethodParamMap();
|
|
const doc = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
|
|
|
|
let updated = 0;
|
|
for (const group of doc.methods.groups || []) {
|
|
for (const item of group.list || []) {
|
|
const next = methodParamMap.get(item.method) || [];
|
|
item.params = next;
|
|
updated++;
|
|
}
|
|
}
|
|
|
|
doc.version = '0.3.0';
|
|
doc.updatedAt = '2026-03-22';
|
|
|
|
fs.writeFileSync(jsonPath, JSON.stringify(doc, null, 2) + '\n', 'utf8');
|
|
console.log(`updated methods: ${updated}`);
|
|
console.log(`mapped methods: ${methodParamMap.size}`);
|