monaco-auto-typings
Version:
provides automatic dependency type completion for Monaco Editor
61 lines (60 loc) • 1.44 kB
JavaScript
/**
* HTTP请求超时时间(毫秒)
*/
export const REQUEST_TIMEOUT = 30000;
/**
* 创建带超时的fetch请求
*/
export async function fetchWithTimeout(url, timeout = REQUEST_TIMEOUT) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, { signal: controller.signal });
clearTimeout(timeoutId);
return response;
}
catch (error) {
clearTimeout(timeoutId);
throw error;
}
}
/**
* 检查URL是否有效
*/
export function isValidUrl(url) {
try {
new URL(url);
return true;
}
catch (_a) {
return false;
}
}
/**
* 转义正则表达式特殊字符
*/
export function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/**
* 日志输出工具
*/
export function createLogger(verbose) {
return {
info: (message, ...args) => {
if (verbose) {
console.log(`[Monaco Auto Typings] ${message}`, ...args);
}
},
error: (message, ...args) => {
if (verbose) {
console.error(`[Monaco Auto Typings] ${message}`, ...args);
}
},
warn: (message, ...args) => {
if (verbose) {
console.warn(`[Monaco Auto Typings] ${message}`, ...args);
}
}
};
}