mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-24 09:13:50 +02:00
rewrite web request logging function
This commit is contained in:
@@ -26,4 +26,40 @@ window.$debug = AppDebug;
|
||||
window.utils = utils;
|
||||
window.dayjs = dayjs;
|
||||
|
||||
/**
|
||||
* @param {string} tag
|
||||
* @param {string|unknown[]} target
|
||||
* @param {...any} rest
|
||||
*/
|
||||
export function logWebRequest(tag, target, ...rest) {
|
||||
if (!AppDebug.debugWebRequests) return;
|
||||
console.log(tag, target, ...rest);
|
||||
}
|
||||
|
||||
let _queryLogDepth = 0;
|
||||
|
||||
/**
|
||||
* Wraps an async fn so that any API request made inside
|
||||
* will NOT emit the default [API …] debug log (the query
|
||||
* layer prints its own log instead).
|
||||
* @template T
|
||||
* @param {() => Promise<T>} fn
|
||||
* @returns {Promise<T>}
|
||||
*/
|
||||
export async function withQueryLog(fn) {
|
||||
_queryLogDepth++;
|
||||
try {
|
||||
return await fn();
|
||||
} finally {
|
||||
_queryLogDepth--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {boolean} true when inside a withQueryLog callback
|
||||
*/
|
||||
export function isApiLogSuppressed() {
|
||||
return _queryLogDepth > 0;
|
||||
}
|
||||
|
||||
export { AppDebug };
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
useUserStore
|
||||
} from '../stores';
|
||||
import { getCurrentUser } from '../coordinators/userCoordinator';
|
||||
import { AppDebug } from './appConfig.js';
|
||||
import { AppDebug, isApiLogSuppressed, logWebRequest } from './appConfig.js';
|
||||
import { escapeTag } from '../shared/utils';
|
||||
import { i18n } from '../plugins/i18n';
|
||||
import { statusCodes } from '../shared/constants/api.js';
|
||||
@@ -139,11 +139,17 @@ export function request(endpoint, options) {
|
||||
throw `API request blocked while logged out: ${endpoint}`;
|
||||
}
|
||||
const parsed = parseResponse(response);
|
||||
if (AppDebug.debugWebRequests) {
|
||||
if (!isApiLogSuppressed()) {
|
||||
const tag = `[API ${init.method}]`;
|
||||
if (!parsed.data) {
|
||||
console.log(init, 'no data', parsed);
|
||||
logWebRequest(tag, endpoint, `(${parsed.status}) no data`);
|
||||
} else {
|
||||
console.log(init, 'parsed data', parsed.data);
|
||||
logWebRequest(
|
||||
tag,
|
||||
endpoint,
|
||||
`(${parsed.status})`,
|
||||
parsed.data
|
||||
);
|
||||
}
|
||||
}
|
||||
if (parsed.hasApiError) {
|
||||
|
||||
Reference in New Issue
Block a user