UNPKG

@casoon/auditmysite

Version:

Professional website analysis suite with robust accessibility testing, Core Web Vitals performance monitoring, SEO analysis, and content optimization insights. Features isolated browser contexts, retry mechanisms, and comprehensive API endpoints for profe

144 lines (120 loc) • 4.52 kB
/** * šŸš€ Basic AuditMySite SDK Usage Example * * This example demonstrates the simplest way to use the AuditMySite SDK * for accessibility testing in a Node.js application. */ const { AuditSDK } = require('../dist/sdk/audit-sdk'); async function basicExample() { console.log('šŸŽÆ Basic AuditMySite SDK Example\n'); // Initialize the SDK const audit = new AuditSDK({ verbose: true, defaultOutputDir: './audit-reports' }); try { // Example 1: Quick audit with minimal configuration console.log('šŸ“‹ Running quick audit...'); const result = await audit.quickAudit('https://example.com/sitemap.xml', { maxPages: 5, formats: ['html', 'json'] }); console.log('āœ… Audit completed!'); console.log(` šŸ“„ Tested: ${result.summary.testedPages} pages`); console.log(` āœ… Passed: ${result.summary.passedPages}`); console.log(` āŒ Failed: ${result.summary.failedPages}`); console.log(` šŸ“Š Success Rate: ${Math.round((result.summary.passedPages / result.summary.testedPages) * 100)}%`); console.log(` ā±ļø Duration: ${Math.round(result.duration / 1000)}s`); // Show generated reports if (result.reports.length > 0) { console.log('\nšŸ“ Generated Reports:'); result.reports.forEach(report => { console.log(` ${report.format.toUpperCase()}: ${report.path} (${Math.round(report.size / 1024)}KB)`); }); } } catch (error) { console.error('āŒ Audit failed:', error.message); process.exit(1); } } // Example 2: Using fluent API with event listeners async function fluentAPIExample() { console.log('\nšŸ”— Fluent API Example with Event Listeners\n'); const audit = new AuditSDK(); try { const result = await audit.audit() .sitemap('https://example.com/sitemap.xml') .maxPages(3) .standard('WCAG2AA') .formats(['html', 'markdown']) .includePerformance() .viewport(1920, 1080) .on('audit:start', (event) => { console.log('šŸš€ Audit started!'); }) .on('audit:progress', (event) => { const { current, total, percentage, currentUrl } = event.data; console.log(`šŸ“Š Progress: ${current}/${total} (${percentage}%) - ${currentUrl || 'Processing...'}`); }) .on('audit:page:complete', (event) => { const { url, result } = event.data; const status = result.passed ? 'āœ…' : 'āŒ'; console.log(`${status} ${url} - ${result.errors.length} errors`); }) .on('report:complete', (event) => { const { format, size } = event.data; console.log(`šŸ“„ ${format.toUpperCase()} report generated (${Math.round(size / 1024)}KB)`); }) .run(); console.log('\nšŸŽ‰ Fluent API audit completed successfully!'); } catch (error) { console.error('āŒ Fluent API audit failed:', error.message); } } // Example 3: Error handling and connection testing async function errorHandlingExample() { console.log('\nšŸ›”ļø Error Handling & Connection Testing Example\n'); const audit = new AuditSDK(); // Test connection first const sitemapUrl = 'https://nonexistent-site.com/sitemap.xml'; console.log(`šŸ” Testing connection to: ${sitemapUrl}`); const connectionTest = await audit.testConnection(sitemapUrl); if (!connectionTest.success) { console.log(`āŒ Connection test failed: ${connectionTest.error}`); console.log('šŸ’” Tip: Make sure the sitemap URL is accessible and valid'); return; } console.log('āœ… Connection test passed!'); // Run audit with proper error handling try { await audit.quickAudit(sitemapUrl, { maxPages: 5, formats: ['json'] }); } catch (error) { if (error.code === 'INVALID_SITEMAP') { console.error('🚫 Invalid sitemap:', error.message); console.log('šŸ’” Please check the sitemap URL and ensure it\'s accessible'); } else if (error.code === 'TIMEOUT') { console.error('ā° Audit timed out:', error.message); console.log('šŸ’” Try reducing maxPages or increasing timeout'); } else { console.error('šŸ’„ Unexpected error:', error.message); } } } // Run all examples async function runAllExamples() { await basicExample(); await fluentAPIExample(); await errorHandlingExample(); } // Only run if this file is executed directly if (require.main === module) { runAllExamples().catch(console.error); } module.exports = { basicExample, fluentAPIExample, errorHandlingExample };