Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | /** * Browser Hand Templates Registry * * Central registry for all browser automation task templates. */ import type { TaskTemplate, TemplateCategory, TemplateRegistry, TaskTemplateParam, ValidationError, ValidationResult, } from './types'; import { basicTemplates } from './basic'; import { scrapingTemplates } from './scraping'; import { automationTemplates } from './automation'; // ============================================================================ // Re-export Types // ============================================================================ export * from './types'; // ============================================================================ // All Built-in Templates // ============================================================================ export const BUILTIN_TEMPLATES: TaskTemplate[] = [ ...basicTemplates, ...scrapingTemplates, ...automationTemplates, ]; // ============================================================================ // Template Registry Implementation // ============================================================================ function createTemplateRegistry(): TemplateRegistry { const templates = new Map<string, TaskTemplate>(); const byCategory = new Map<TemplateCategory, TaskTemplate[]>(); // Initialize category maps byCategory.set('basic', []); byCategory.set('scraping', []); byCategory.set('automation', []); function register(template: TaskTemplate): void { if (templates.has(template.id)) { console.warn(`[BrowserHand] Template "${template.id}" already registered, overwriting`); } templates.set(template.id, template); const categoryList = byCategory.get(template.category); if (categoryList) { // Remove existing if updating const existingIndex = categoryList.findIndex((t) => t.id === template.id); if (existingIndex >= 0) { categoryList.splice(existingIndex, 1); } categoryList.push(template); } } function get(id: string): TaskTemplate | undefined { return templates.get(id); } function getByCategory(category: TemplateCategory): TaskTemplate[] { return byCategory.get(category) ?? []; } function getAll(): TaskTemplate[] { return Array.from(templates.values()); } // Register all built-in templates BUILTIN_TEMPLATES.forEach(register); return { templates, byCategory, register, get, getByCategory, getAll, }; } // ============================================================================ // Singleton Registry Instance // ============================================================================ export const templateRegistry = createTemplateRegistry(); // ============================================================================ // Validation Utilities // ============================================================================ /** * Validate template parameters against their definitions */ export function validateTemplateParams( templateParams: TaskTemplateParam[], providedParams: Record<string, unknown> ): ValidationResult { const errors: ValidationError[] = []; for (const param of templateParams) { const value = providedParams[param.key]; // Check required if (param.required && (value === undefined || value === null || value === '')) { errors.push({ param: param.key, message: `${param.label} 是必填项`, }); continue; } // Skip further validation if not provided and not required if (value === undefined || value === null || value === '') { continue; } // Type-specific validation switch (param.type) { case 'url': if (typeof value === 'string' && !isValidUrl(value)) { errors.push({ param: param.key, message: `${param.label} 必须是有效的 URL`, }); } break; case 'number': const numValue = Number(value); if (isNaN(numValue)) { errors.push({ param: param.key, message: `${param.label} 必须是数字`, }); } else { if (param.min !== undefined && numValue < param.min) { errors.push({ param: param.key, message: `${param.label} 不能小于 ${param.min}`, }); } if (param.max !== undefined && numValue > param.max) { errors.push({ param: param.key, message: `${param.label} 不能大于 ${param.max}`, }); } } break; case 'json': if (typeof value === 'string') { try { JSON.parse(value); } catch { errors.push({ param: param.key, message: `${param.label} 必须是有效的 JSON`, }); } } break; case 'text': case 'textarea': if (param.pattern && typeof value === 'string') { const regex = new RegExp(param.pattern); if (!regex.test(value)) { errors.push({ param: param.key, message: `${param.label} 格式不正确`, }); } } break; } } return { valid: errors.length === 0, errors, }; } /** * Check if string is a valid URL */ function isValidUrl(str: string): boolean { try { const url = new URL(str); return url.protocol === 'http:' || url.protocol === 'https:'; } catch { return false; } } /** * Get default values for template parameters */ export function getDefaultParams(templateParams: TaskTemplateParam[]): Record<string, unknown> { const defaults: Record<string, unknown> = {}; for (const param of templateParams) { if (param.default !== undefined) { defaults[param.key] = param.default; } } return defaults; } /** * Merge provided params with defaults */ export function mergeParamsWithDefaults( templateParams: TaskTemplateParam[], providedParams: Record<string, unknown> ): Record<string, unknown> { const defaults = getDefaultParams(templateParams); return { ...defaults, ...providedParams }; } // ============================================================================ // Convenience Exports // ============================================================================ export const getTemplate = (id: string) => templateRegistry.get(id); export const getTemplatesByCategory = (category: TemplateCategory) => templateRegistry.getByCategory(category); export const getAllTemplates = () => templateRegistry.getAll(); export const registerTemplate = (template: TaskTemplate) => templateRegistry.register(template); |