monaco-auto-typings
Version:
provides automatic dependency type completion for Monaco Editor
68 lines (67 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.REQUEST_TIMEOUT = void 0;
exports.fetchWithTimeout = fetchWithTimeout;
exports.isValidUrl = isValidUrl;
exports.escapeRegExp = escapeRegExp;
exports.createLogger = createLogger;
/**
* HTTP请求超时时间(毫秒)
*/
exports.REQUEST_TIMEOUT = 30000;
/**
* 创建带超时的fetch请求
*/
async function fetchWithTimeout(url, timeout = exports.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是否有效
*/
function isValidUrl(url) {
try {
new URL(url);
return true;
}
catch (_a) {
return false;
}
}
/**
* 转义正则表达式特殊字符
*/
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/**
* 日志输出工具
*/
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);
}
}
};
}