UNPKG

bc_resource_mcp

Version:

MCP server for Baichuan resource

48 lines (47 loc) 1.65 kB
// 封装 axios 请求, 设置baseURL, 设置超时时间, 设置请求头, 设置响应拦截器, 设置请求拦截器 import axios from "axios"; export class APIServer { config; instance; constructor(config) { this.config = config; this.instance = axios.create({ baseURL: "https://gateway-resource-platform.bcbook.cn", timeout: 10000, headers: {}, }); } async safeCall(fn) { try { const response = await fn(); if (response.status >= 400) { const errorData = response.data; const code = errorData.errorCode || -1; const message = errorData.message || "<unknown>"; throw new Error(`API error: ${code} ${message}`); } return response.data; } catch (error) { if (axios.isAxiosError(error)) { throw new Error(`Request failed: ${error.message}`); } throw error; } } async listBooks(params) { return this.safeCall(() => this.instance.get("/platform-webapi/page/books", { params: { ...params, moduleId: this.config.moduleId }, })); } async listChapters(params) { return this.safeCall(() => this.instance.get("/platform-webapi/book/resource/getChapterAudios", { params: { ...params, moduleId: this.config.moduleId }, })); } async fetchResource(id) { return this.safeCall(() => this.instance.get("/platform-webapi/book/resource/getAudioDetail", { params: { id }, })); } }