/** * 获取符合要求的请求 * @param entries 监测的请求对象 * @param type 设置满足条件的请求类型 * @returns {PerformanceEntry[]} */ const getRequests = (entries = performance.getEntriesByType('resource'), type = ['xmlhttprequest']) => entries?.filter(e => type.includes(e.initiatorType)) || [] /** * 观察者工具对象,用于前端接口监测 */ class Observer { constructor() { this.saveLogs(getRequests()) this.ins = new PerformanceObserver((list, ob) => { const watchLogs = getRequests(list.getEntriesByType("resource")) this.saveLogs(watchLogs) }) this.ins.observe({entryTypes: ["resource"]}) } saveLogs(list = []) { list.map(e => { if (!/sockjs/.test(e.name)) { const api = { status: e.responseStatus, path: e.name, url: location.href, nodeProcess: process.env.NODE_ENV, } console.log(api) // http.post("/node/monitorApi/addOrUpdate", api) } }) } } export default Observer