refactor: Restructure ApifoxService with dependency injection and MCP integration
- Move APIFOX_TOKEN to index.js and pass via config object to ApifoxService - Convert ApifoxService to accept config object with projectId, token, and baseUrl parameters - Move axios instance creation from module level into ApifoxService constructor - Replace global http client references with instance-based this.http throughout service methods - Initialize ApifoxService in index.js with environment variables - Remove GitLab API client code and replace with Apifox-focused tool implementations - Simplify MCP server tool registration to use ApifoxService methods - Add init() call in ApifoxService constructor for future initialization logic - Improve code organization by centralizing API client configuration within service class - Enable better testability and configuration flexibility through dependency injection pattern
This commit is contained in:
1 parent
10467c194c
commit
ad543045c7
2 files changed
+150
-365
No files matched your search
@@ -2,33 +2,32 @@ import axios from "axios";
|
||||
import NodeCache from 'node-cache';
|
||||
import fs from 'fs'
|
||||
|
||||
|
||||
const APIFOX_API_BASE = "https://apifox.gzmiyuan.com/api/v1";
|
||||
|
||||
process.env.APIFOX_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MzQ1MTIwLCJ0cyI6IjUyOGZiZDMwNGM3MjU2NDMiLCJpYXQiOjE3NjQxNTQyODEzMzB9.IZvoCb3pN5AUl375c4Dhpur1GdYBqyHWVkJ94bDQ6BQ';
|
||||
|
||||
const http = axios.create({
|
||||
baseURL: APIFOX_API_BASE,
|
||||
headers: {
|
||||
"X-Client-Mode": "web",
|
||||
"X-Client-Version": "2.7.19",
|
||||
"X-Device-Id": "bd94aea0-7b65-48ba-9274-4b1177897954",
|
||||
"Authorization": `Bearer ${process.env.APIFOX_TOKEN || ''}`
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export class ApifoxService {
|
||||
constructor(projectId) {
|
||||
constructor(config = {}) {
|
||||
const { projectId, token, baseUrl } = config;
|
||||
this.projectId = projectId;
|
||||
this.branchId = null;
|
||||
this.apiTreeList = null;
|
||||
this.http = axios.create({
|
||||
baseURL: baseUrl || APIFOX_API_BASE,
|
||||
headers: {
|
||||
"X-Client-Mode": "web",
|
||||
"X-Client-Version": "2.7.19",
|
||||
"X-Device-Id": "bd94aea0-7b65-48ba-9274-4b1177897954",
|
||||
"Authorization": `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化node-cache,设置默认5分钟过期时间
|
||||
this.cache = new NodeCache({
|
||||
stdTTL: 5 * 60, // 5分钟,单位秒
|
||||
checkperiod: 60, // 每分钟检查一次过期缓存
|
||||
useClones: false // 创建缓存对象的克隆以避免引用问题
|
||||
});
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +65,7 @@ export class ApifoxService {
|
||||
*/
|
||||
async get(url, config = {}) {
|
||||
try {
|
||||
const response = await http.get(url, {
|
||||
const response = await this.http.get(url, {
|
||||
...config,
|
||||
headers: {
|
||||
...config.headers,
|
||||
@@ -89,7 +88,7 @@ export class ApifoxService {
|
||||
*/
|
||||
async post(url, data = {}, config = {}) {
|
||||
try {
|
||||
const response = await http.post(url, data, {
|
||||
const response = await this.http.post(url, data, {
|
||||
...config,
|
||||
headers: {
|
||||
...config.headers,
|
||||
|
||||
Reference in new issue
Block a user