UNPKG

apptise-core

Version:

Core library for Apptise unified notification system

173 lines 5.24 kB
import { ErrorType } from './types.js'; /** * 通知插件抽象基类 * 所有通知服务插件都必须继承此类并实现相应的抽象方法 */ export class NotificationPlugin { /** * 验证插件配置是否有效 * @param config - 插件配置 * @returns 是否有效 */ validateConfig(config) { return (typeof config.serviceId === 'string' && config.serviceId === this.registration.serviceId && typeof config.url === 'string' && config.url.length > 0); } /** * 验证通知消息是否有效 * @param message - 通知消息 * @returns 是否有效 */ validateMessage(message) { return typeof message.title === 'string' && message.title.length > 0; } /** * 检查 URL 是否被此插件支持 * @param url - 要检查的 URL * @returns 是否支持 */ supportsUrl(url) { try { const parsedUrl = this.parseUrlBase(url); return this.registration.protocols.includes(parsedUrl.protocol); } catch { return false; } } /** * 获取插件信息 * @returns 插件注册信息 */ getRegistration() { return { ...this.registration }; } /** * 创建成功的通知结果 * @param response - 响应数据 * @param duration - 发送耗时 * @returns 成功的通知结果 */ createSuccessResult(response, duration) { const result = { success: true, serviceId: this.registration.serviceId, }; if (response !== undefined) { result.response = response; } if (duration !== undefined) { result.duration = duration; } return result; } /** * 创建失败的通知结果 * @param error - 错误信息 * @param response - 响应数据 * @param duration - 发送耗时 * @returns 失败的通知结果 */ createErrorResult(error, response, duration) { const result = { success: false, serviceId: this.registration.serviceId, error, }; if (response !== undefined) { result.response = response; } if (duration !== undefined) { result.duration = duration; } return result; } /** * 创建 Apptise 错误 * @param type - 错误类型 * @param message - 错误消息 * @param originalError - 原始错误 * @param details - 错误详情 * @returns Apptise 错误实例 */ createError(type, message, originalError, details) { const error = new Error(message); error.type = type; error.serviceId = this.registration.serviceId; if (originalError !== undefined) { error.originalError = originalError; } if (details !== undefined) { error.details = details; } return error; } /** * 基础 URL 解析方法 * @param url - 要解析的 URL * @returns 解析后的 URL 对象 * @throws {ApptiseError} 当 URL 格式无效时抛出错误 */ parseUrlBase(url) { try { const parsedUrl = new URL(url); const result = { protocol: parsedUrl.protocol.replace(':', ''), searchParams: parsedUrl.searchParams, }; if (parsedUrl.hostname) { result.hostname = parsedUrl.hostname; } if (parsedUrl.port) { result.port = parsedUrl.port; } if (parsedUrl.pathname) { result.pathname = parsedUrl.pathname; } if (parsedUrl.username) { result.username = parsedUrl.username; } if (parsedUrl.password) { result.password = parsedUrl.password; } if (parsedUrl.hash) { result.hash = parsedUrl.hash.substring(1); } return result; } catch (originalError) { throw this.createError(ErrorType.INVALID_URL, `Invalid URL format: ${url}`, originalError, { url }); } } /** * 测量执行时间的辅助方法 * @param fn - 要执行的异步函数 * @returns 包含结果和执行时间的对象 */ async measureTime(fn) { const startTime = Date.now(); const result = await fn(); const duration = Date.now() - startTime; return { result, duration }; } /** * 安全地执行异步操作,捕获并转换错误 * @param fn - 要执行的异步函数 * @param errorType - 默认错误类型 * @returns 执行结果 */ async safeExecute(fn, errorType = ErrorType.UNKNOWN_ERROR) { try { return await fn(); } catch (originalError) { if (originalError.type) { throw originalError; } throw this.createError(errorType, originalError.message || 'Unknown error occurred', originalError); } } } //# sourceMappingURL=plugin.js.map