All files / src/lib demo-catalog.ts

100% Statements 10/10
62.96% Branches 17/27
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                                    6x 6x   7x     6x   3x 3x 3x 3x                                   6x                
import { EDC_CONTEXT } from "@/lib/edc/client";
 
/** One participant's entry in public/mock/assets.json. */
export interface DemoAssetEntry {
  participantId?: string;
  identity?: string;
  assets?: Record<string, unknown>[];
}
 
/**
 * The DSP catalog a provider offers in the demonstrator, built from the
 * bundled demo assets (public/mock/assets.json).
 *
 * Two callers share it. `/api/negotiations?catalog=true` falls back to it on
 * the hub when the connector has no participant context for the id, and the
 * negotiate page builds it itself in the GitHub Pages build, where there is no
 * API route and the catalog request used to resolve to negotiations.json, an
 * array with no datasets, so every provider showed "No datasets with offers
 * found" (#876).
 *
 * Returns null when the provider has no entry or no assets.
 */
export function buildDemoCatalog(
  entries: DemoAssetEntry[],
  providerDid: string,
): Record<string, unknown> | null {
  const wanted = decodeURIComponent(providerDid).toLowerCase();
  const entry = entries.find(
    (e) =>
      decodeURIComponent(e.identity ?? "").toLowerCase() === wanted ||
      e.participantId === providerDid,
  );
  if (!entry || !(entry.assets ?? []).length) return null;
 
  const dataset = (entry.assets ?? []).map((asset) => {
    const props = (asset.properties ?? {}) as Record<string, unknown>;
    const assetId = (asset["@id"] ?? props.id ?? "") as string;
    return {
      "@id": assetId,
      "@type": "dcat:Dataset",
      name: props.name ?? assetId,
      description: props.description ?? "",
      contenttype: props.contenttype ?? "",
      hasPolicy: [
        {
          "@id": `demo-offer:${assetId}`,
          "@type": "odrl:Offer",
          assigner: entry.identity ?? providerDid,
          target: assetId,
          permission: [{ action: "use" }],
        },
      ],
    };
  });
 
  return {
    "@context": [EDC_CONTEXT],
    "@type": "dcat:Catalog",
    participantId: entry.identity ?? providerDid,
    dataset,
    demo: true,
  };
}