Programmatic API
Using as a Library
PS Guard can be used programmatically for custom integrations:
typescript
import { runPSGuard } from "@promise-inc/ps-guard";
async function checkPerformance() { const result = await runPSGuard("https://your-site.com", { device: "mobile", minScore: 90, preset: "nextjs", });
if (!result.passed) { console.error("Performance check failed!"); console.error("Failed metrics:", result.metrics.filter(m => !m.passed)); process.exit(1); }
console.log("All metrics passed! Score:", result.score);}
checkPerformance();Core Functions
typescript
// Main API — audit a single URLasync function runPSGuard( url: string, options?: Partial<PSGuardConfig>): Promise<PSGuardResult>;
// Load configuration from project directoryasync function loadConfig( cwd: string, overrides?: Partial<PSGuardConfig>): Promise<PSGuardConfig>;
// Resolve a preset by namefunction resolvePreset(name: string): Partial<PSGuardConfig>;
// Run Lighthouse audit directlyasync function runLighthouse( config: PSGuardConfig): Promise<LighthouseResult>;
// Parse metrics from Lighthouse resultsfunction parseMetrics( audits: LHRAudits, thresholds: Thresholds): MetricResult[];
// Validate results against thresholdsfunction validateResults( lhr: LighthouseResult, config: PSGuardConfig): ValidationResult;
// Fetch and parse sitemapasync function fetchSitemap(url: string): Promise<string[]>;
// Run multi-URL auditasync function runMultiAudit( options: MultiAuditOptions): Promise<MultiAuditResult>;
// Generate HTML reportfunction generateHtmlReport(results: AuditResults): string;Types
typescript
interface PSGuardResult { passed: boolean; score: number; url: string; device: "mobile" | "desktop"; metrics: MetricResult[]; hints: Record<string, string[]>;}
interface MetricResult { name: "lcp" | "cls" | "inp" | "ttfb" | "fcp"; value: number; threshold: number; passed: boolean; displayValue: string;}
interface MultiAuditResult { passed: boolean; results: PSGuardResult[]; summary: { total: number; passed: number; failed: number; averageScore: number; worstScore: number; };}Custom Integration Example
typescript
import { runMultiAudit, fetchSitemap, generateHtmlReport } from "@promise-inc/ps-guard";import fs from "fs/promises";
async function auditAndReport() { const urls = await fetchSitemap("https://your-site.com/sitemap.xml");
const result = await runMultiAudit({ urls: urls.slice(0, 20), device: "mobile", thresholds: { lcp: 2500, cls: 0.1, inp: 200, }, });
// Generate HTML report const html = generateHtmlReport(result); await fs.writeFile("performance-report.html", html);
// Generate JSON for dashboards await fs.writeFile( "performance-report.json", JSON.stringify(result, null, 2) );
console.log(`Audited ${result.summary.total} pages`); console.log(`Passed: ${result.summary.passed}, Failed: ${result.summary.failed}`); console.log(`Average score: ${result.summary.averageScore}`);
return result.passed;}
auditAndReport();The programmatic API launches Chrome headless internally. Make sure Chrome is available on the machine.