feat(ApifoxService): 添加日志功能,支持将日志写入文件并创建日志目录
This commit is contained in:
1 parent
be63d49f40
commit
30d4c8a9da
1 file changed
+69
-4
@@ -1,15 +1,17 @@
|
||||
import axios from "axios";
|
||||
import NodeCache from 'node-cache';
|
||||
// import fs from 'fs'
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const APIFOX_API_BASE = "https://apifox.gzmiyuan.com/api/v1";
|
||||
|
||||
export class ApifoxService {
|
||||
constructor(config = {}) {
|
||||
const { projectId, token, baseUrl } = config;
|
||||
const { projectId, token, baseUrl, logFilePath } = config;
|
||||
this.projectId = projectId;
|
||||
this.branchId = null;
|
||||
this.apiTreeList = null;
|
||||
this.logFilePath = logFilePath || path.join(process.cwd(), 'apifox-service.log');
|
||||
this.http = axios.create({
|
||||
baseURL: baseUrl || APIFOX_API_BASE,
|
||||
headers: {
|
||||
@@ -43,6 +45,50 @@ export class ApifoxService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入日志到文件
|
||||
* @param {string} level - 日志级别 (INFO, ERROR, WARN, DEBUG)
|
||||
* @param {string} message - 日志消息
|
||||
* @param {object} data - 可选的额外数据
|
||||
*/
|
||||
logToFile(level, message, data = null) {
|
||||
try {
|
||||
const timestamp = new Date().toLocaleString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit'
|
||||
});
|
||||
|
||||
let logLine = `[${timestamp}] ${level.toUpperCase()}: ${message}`;
|
||||
|
||||
if (data) {
|
||||
if (typeof data === 'object') {
|
||||
logLine += `\n 数据: ${JSON.stringify(data, null, 2)}`;
|
||||
} else {
|
||||
logLine += `\n 数据: ${data}`;
|
||||
}
|
||||
}
|
||||
|
||||
logLine += '\n';
|
||||
|
||||
// 确保日志目录存在
|
||||
const logDir = path.dirname(this.logFilePath);
|
||||
if (!fs.existsSync(logDir)) {
|
||||
fs.mkdirSync(logDir, { recursive: true });
|
||||
}
|
||||
|
||||
// 追加写入日志文件
|
||||
fs.appendFileSync(this.logFilePath, logLine);
|
||||
|
||||
// 同时输出到控制台
|
||||
} catch (error) {
|
||||
console.error('写入日志失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化服务,获取branchId
|
||||
* @returns {Promise<void>}
|
||||
@@ -264,14 +310,33 @@ export class ApifoxService {
|
||||
const requestSchemaId = this.getSchemaRefId(apiDetail.requestBody.jsonSchema);
|
||||
const responseSchema = Array.isArray(apiDetail.responses) ? apiDetail.responses[0].jsonSchema : apiDetail.responses.jsonSchema;
|
||||
const responseSchemaId = this.getSchemaRefId(responseSchema);
|
||||
const respDataSchemaId = this.getSchemaRefId(dataSchema.find((item) => item.id.toString() === responseSchemaId).jsonSchema.properties.data)
|
||||
const responseDataSchema = dataSchema.find((item) => item.id.toString() === responseSchemaId).jsonSchema.properties.data;
|
||||
|
||||
let respDataSchemaId;
|
||||
const isArray = responseDataSchema.type === 'array';
|
||||
// 处理数组和对象两种情况
|
||||
if (isArray && responseDataSchema.items && responseDataSchema.items.$ref) {
|
||||
// 数组情况:从 items.$ref 中提取 schema ID
|
||||
respDataSchemaId = this.getSchemaRefId(responseDataSchema.items);
|
||||
} else if (responseDataSchema.$ref) {
|
||||
// 对象情况:直接从 $ref 中提取 schema ID
|
||||
respDataSchemaId = this.getSchemaRefId(responseDataSchema);
|
||||
} else {
|
||||
// 如果都没有 $ref,记录警告并返回 null
|
||||
this.logToFile('WARN', `无法从 responseDataSchema 中提取 schema ID`, responseDataSchema);
|
||||
respDataSchemaId = null;
|
||||
}
|
||||
|
||||
return {
|
||||
path: apiDetail.path,
|
||||
name: apiDetail.name,
|
||||
method: apiDetail.method,
|
||||
url: apiDetail.url,
|
||||
requestSchema: await this.getSchemaById(requestSchemaId),
|
||||
responseSchema: await this.getSchemaById(respDataSchemaId),
|
||||
responseSchema: isArray ? {
|
||||
type: 'array',
|
||||
items: respDataSchemaId ? await this.getSchemaById(respDataSchemaId) : null,
|
||||
} : await this.getSchemaById(respDataSchemaId),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user