import { Db } from "./db"; import * as requestHandler from "../utils/httpRequest"; /** * 数据库模块的通用请求方法 * * @author haroldhu * @internal */ export class Request { /** * db 的实例 * * @internal */ private db: Db; /** * 初始化 * * @internal * @param db */ constructor(db: Db) { this.db = db; } /** * 发送请求 * * @param api - 接口 * @param data - 参数 */ async send(api?: string, data?: Object): Promise { const params = Object.assign({}, data, { action: `database.${api}` }); // console.log(this.db.config.config); const slowQueryWarning = setTimeout(() => { console.warn('Database operation is longer than 3s. Please check query performance and your network environment.') }, 3000) try { return await requestHandler({ timeout: this.db.config.timeout, config: this.db.config.config, params, method: "post", headers: { "content-type": "application/json" } }); } finally { clearTimeout(slowQueryWarning) } } }