JS/TS Examples for adaline.report
JavaScript and TypeScript code examples for the Adaline Report API using fetch with async/await.
All examples use the native fetch API with async/await. Works in Node.js 18+, Deno, Bun, and all modern browsers.
TypeScript Types
interface ComplianceReport {
report_id: string;
overall_score: number;
grade: "A" | "B" | "C" | "D" | "F";
compliant: boolean;
total_passed: number;
total_checks: number;
categories: Record<string, CategoryScore>;
checks: Check[];
}
interface CategoryScore {
score: number;
weight: number;
passed: number;
total: number;
failures: Check[];
}
interface Check {
check_id: string;
passed: boolean;
details: string;
severity: "critical" | "major" | "minor";
}
interface Tag {
id: string;
name: string;
color: string | null;
created_at: string;
}
Setup
const API_KEY = "your-api-key";
const BASE_URL = "https://api.adaline.report";
const headers = {
"X-API-Key": API_KEY,
};
async function apiRequest(path: string, options: RequestInit = {}) {
const res = await fetch(`${BASE_URL}${path}`, {
...options,
headers: { ...headers, ...options.headers },
});
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.detail || `API error: ${res.status}`);
}
if (res.status === 204) return null;
return res.json();
}
Scan a Document
async function scanDocument(filePath: string): Promise<ComplianceReport> {
// Node.js
const fs = await import("fs");
const file = fs.readFileSync(filePath);
const blob = new Blob([file]);
const formData = new FormData();
formData.append("file", blob, filePath.split("/").pop());
const res = await fetch(`${BASE_URL}/api/v1/scan`, {
method: "POST",
headers: { "X-API-Key": API_KEY },
body: formData,
});
if (!res.ok) throw new Error(`Scan failed: ${res.status}`);
return res.json();
}
const result = await scanDocument("./syllabus.pdf");
console.log(`Score: ${result.overall_score}% (Grade ${result.grade})`);
console.log(`Compliant: ${result.compliant}`);
Browser Upload
// From a file input element
async function scanFromInput(fileInput: HTMLInputElement) {
const file = fileInput.files?.[0];
if (!file) return;
const formData = new FormData();
formData.append("file", file);
const res = await fetch(`${BASE_URL}/api/v1/scan`, {
method: "POST",
headers: { "X-API-Key": API_KEY },
body: formData,
});
return res.json();
}
List Reports
async function listReports(page = 1, pageSize = 20) {
return apiRequest(`/api/v1/reports?page=${page}&page_size=${pageSize}`);
}
const { items, total } = await listReports();
for (const report of items) {
console.log(`${report.filename}: ${report.overall_score}% (${report.grade})`);
}
Get Report Details
async function getReport(reportId: string): Promise<ComplianceReport> {
return apiRequest(`/api/v1/reports/${reportId}`);
}
const report = await getReport("your-report-id");
// Print category breakdown
for (const [name, cat] of Object.entries(report.categories)) {
console.log(` ${name}: ${cat.score}% (${cat.passed}/${cat.total})`);
}
// Print failed checks
const failures = report.checks.filter((c) => !c.passed);
for (const check of failures) {
console.log(` [${check.severity}] ${check.details}`);
}
Tags
async function createTag(name: string, color?: string): Promise<Tag> {
return apiRequest("/api/v1/tags", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, color }),
});
}
async function tagReport(tagId: string, reportId: string) {
return apiRequest(`/api/v1/tags/${tagId}/reports/${reportId}`, {
method: "POST",
});
}
const tag = await createTag("needs-review", "red");
await tagReport(tag.id, "your-report-id");
Share a Report
async function shareReport(reportId: string) {
return apiRequest(`/api/v1/reports/${reportId}/share`, {
method: "POST",
});
}
const share = await shareReport("your-report-id");
console.log(`Share URL: ${share.share_url}`);
Batch Scan
import { readdir } from "fs/promises";
import { join } from "path";
async function batchScan(directory: string) {
const files = await readdir(directory);
const supported = [".pdf", ".docx", ".pptx", ".html"];
const results = [];
for (const file of files) {
if (!supported.some((ext) => file.endsWith(ext))) continue;
console.log(`Scanning ${file}...`);
try {
const result = await scanDocument(join(directory, file));
results.push({ file, score: result.overall_score, grade: result.grade });
} catch (err) {
results.push({ file, error: (err as Error).message });
}
}
return results;
}
const results = await batchScan("./documents");
console.table(results);
Error Handling
async function safeScan(filePath: string) {
try {
return await scanDocument(filePath);
} catch (err) {
if (err instanceof Error) {
if (err.message.includes("401")) {
console.error("Invalid API key");
} else if (err.message.includes("429")) {
console.error("Rate limited -- wait and retry");
} else if (err.message.includes("400")) {
console.error("Bad request -- check file format");
} else {
console.error("Scan failed:", err.message);
}
}
return null;
}
}
Full Workflow
async function complianceWorkflow(filePath: string) {
// 1. Scan
console.log(`Scanning ${filePath}...`);
const report = await scanDocument(filePath);
console.log(`Score: ${report.overall_score}% (${report.grade})`);
// 2. Check critical issues
const critical = report.checks.filter(
(c) => !c.passed && c.severity === "critical"
);
if (critical.length > 0) {
console.log(`\n${critical.length} critical issue(s):`);
critical.forEach((c) => console.log(` - ${c.details}`));
}
// 3. Tag based on result
const tagName = report.compliant ? "compliant" : "needs-fixes";
const tagColor = report.compliant ? "green" : "yellow";
const tag = await createTag(tagName, tagColor);
await tagReport(tag.id, report.report_id);
// 4. Share
const share = await shareReport(report.report_id);
console.log(`\nReport: ${share.share_url}`);
return report;
}
await complianceWorkflow("./quarterly-report.pdf");