parseCodebaseRecord

tcmts.records.parseCodebaseRecord · #re4iorna · term · runs anywhere

A codebase record as read, at its key: what it says, if it's one (at its own key, a name isCodebaseName takes, its URL an http(s) one), else undefined.

import { CODEBASE_COLLECTION, isCodebaseName } from "#codebase/tcmts/records";
import type { CodebaseRecord } from "#codebase/tcmts/records";

export function parseCodebaseRecord(rkey: string, v: unknown): CodebaseRecord | undefined {
  if (!v || typeof v !== "object") return undefined;
  const r = v as Record<string, unknown>;
  if (r.$type !== CODEBASE_COLLECTION) return undefined;
  const { name, url, description, createdAt } = r;
  if (typeof name !== "string" || name !== rkey || !isCodebaseName(name)) return undefined;
  if (typeof url !== "string" || url.length > 2048 || !/^https?:\/\/[^/]/i.test(url))
    return undefined;
  if (typeof createdAt !== "string") return undefined;
  return {
    $type: CODEBASE_COLLECTION,
    name,
    url,
    description: typeof description === "string" ? description.slice(0, 3000) : "",
    createdAt,
  };
}

uses

used by