Something went wrong. Try again.
Something went wrong. Try again.
1.2 kB · 29 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930// Validate + build a dev.at-intent.usage record. Written into a USER's repo by// an app when the person uses it — the discovery footprint a consumer scans.
import { USAGE_NSID } from "./nsid.mjs";
const RECORD_FIELDS = ["app", "capability", "config", "lastSeenAt", "createdAt"];
export function validateUsage(u) { const errors = []; const warnings = []; if (!u || typeof u !== "object") return { errors: ["usage is not an object"], warnings };
if (!u.app || typeof u.app !== "string") errors.push("app: required DID of the handler"); else if (!u.app.startsWith("did:")) errors.push("app: must be a DID (did:plc:... or did:web:...)");
if (u.capability && !u.capability.startsWith("at://")) errors.push("capability: must be an AT-URI"); if (u.lastSeenAt && Number.isNaN(Date.parse(u.lastSeenAt))) errors.push("lastSeenAt: not a valid datetime");
return { errors, warnings };}
export function buildUsageRecord(u, now) { const record = { $type: USAGE_NSID }; for (const f of RECORD_FIELDS) { if (u[f] !== undefined) record[f] = u[f]; } if (!record.createdAt) record.createdAt = now || new Date().toISOString(); return record;}