All files / src/lib api.ts

84.84% Statements 28/33
88.46% Branches 23/26
80% Functions 4/5
88.46% Lines 23/26

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177  17x                                                                                       17x                                                                                                                                                               4x 4x 4x 4x               79x 79x 78x   77x 77x   13x 335x   1x             113x 113x   113x     79x 79x               79x   79x 79x     34x    
/** Static-export mock path mapping — keyed by exact endpoint or prefix */
const STATIC_MOCK_MAP: Record<string, string> = {
  "/api/catalog": "/mock/catalog.json",
  "/api/catalog/jsonld": "/mock/catalog_jsonld.json",
  "/api/graph": "/mock/graph.json",
  "/api/compliance": "/mock/compliance.json",
  "/api/compliance/tck": "/mock/compliance_tck.json",
  "/api/permits": "/mock/permits.json",
  "/api/activity-report": "/mock/activity_report.json",
  "/api/compliance/requests": "/mock/requests.json",
  "/api/compliance/applications": "/mock/applications.json",
  "/api/compliance/findings": "/mock/findings.json",
  "/api/compliance/results": "/mock/results.json",
  "/api/information": "/mock/information.json",
  "/api/jurisdictions": "/mock/jurisdictions.json",
  "/api/compliance/information-requests": "/mock/information_requests.json",
  "/api/patient": "/mock/patient.json",
  "/api/patient/profile": "/mock/patient_profile_list.json",
  "/api/patient/insights": "/mock/patient_insights.json",
  "/api/patient/research": "/mock/patient_research.json",
  "/api/patient/programmes": "/mock/patient_programmes.json",
  "/api/patient/opt-out": "/mock/patient_opt_out.json",
  "/api/patient/observations": "/mock/patient_observations.json",
  "/api/patient/app-devices": "/mock/patient_app_devices.json",
  "/api/patient/wallet-link": "/mock/patient_wallet_link.json",
  "/api/analytics": "/mock/analytics.json",
  "/api/eehrxf": "/mock/eehrxf.json",
  "/api/nlq": "/mock/nlq_templates.json",
  "/api/federated": "/mock/federated_stats.json",
  "/api/credentials": "/mock/credentials.json",
  "/api/credentials/definitions": "/mock/credential_definitions.json",
  "/api/trust-center": "/mock/trust_center.json",
  "/api/participants": "/mock/participants.json",
  "/api/participants/me": "/mock/participants_me.json",
  "/api/assets": "/mock/assets.json",
  "/api/admin/tenants": "/mock/admin_tenants.json",
  "/api/admin/participants": "/mock/admin_participants.json",
  "/api/admin/policies": "/mock/admin_policies.json",
  "/api/admin/components": "/mock/admin_components.json",
  "/api/admin/components/topology": "/mock/admin_components_topology.json",
  "/api/admin/spe": "/mock/admin_spe.json",
  "/api/odrl/scope": "/mock/odrl_scope.json",
};
 
/** Prefix-based mock paths — matched via startsWith (checked after exact, first match wins) */
const STATIC_MOCK_PREFIX: [string, string][] = [
  // HealthDCAT-AP Release 8 JSON-LD of one dataset (#700)
  ["/api/catalog/jsonld?", "/mock/catalog_jsonld.json"],
  // Persona-specific graph views (checked before default /api/graph)
  ["/api/graph?persona=patient", "/mock/graph_patient.json"],
  ["/api/graph?persona=hospital", "/mock/graph_hospital.json"],
  ["/api/graph?persona=researcher", "/mock/graph_researcher.json"],
  ["/api/graph?persona=edc-admin", "/mock/graph_edc_admin.json"],
  ["/api/graph?persona=hdab", "/mock/graph_hdab.json"],
  ["/api/graph?persona=trust-center", "/mock/graph_trust_center.json"],
  ["/api/graph?", "/mock/graph.json"],
  // Graph node properties (return empty in static mode)
  ["/api/graph/node?", "/mock/graph.json"],
  ["/api/graph/expand?", "/mock/graph.json"],
  // Patient profile — specific patient IDs first
  ["/api/patient/profile?patientId=P1", "/mock/patient_profile_patient1.json"],
  ["/api/patient/profile?patientId=P2", "/mock/patient_profile_patient2.json"],
  ["/api/patient/profile?", "/mock/patient_profile_patient1.json"],
  // Patient sub-routes with query params. The pages ask for the persona's
  // own record (useOwnRecord), so patient2 gets P2's file; any other id, P1
  // included, gets the P1 file.
  [
    "/api/patient/insights?patientId=P2",
    "/mock/patient_insights_patient2.json",
  ],
  [
    "/api/patient/research?patientId=P2",
    "/mock/patient_research_patient2.json",
  ],
  ["/api/patient/insights?", "/mock/patient_insights.json"],
  ["/api/patient/research?", "/mock/patient_research.json"],
  ["/api/patient/opt-out?", "/mock/patient_opt_out.json"],
  ["/api/patient/observations?", "/mock/patient_observations.json"],
  // FHIR documents of a record (#1171): P1's index, Patient Summary and
  // newest lab report, whichever record or report is named.
  ["/api/patient/documents?", "/mock/patient_documents.json"],
  [
    "/api/patient/documents/patient-summary?",
    "/mock/patient_document_patient_summary.json",
  ],
  [
    "/api/patient/documents/lab-report?",
    "/mock/patient_document_lab_report.json",
  ],
  // Bonus programs (#934): the list for a named record, and the member's own
  // result for a program year, which the static demo shows as goal met.
  ["/api/patient/programmes?", "/mock/patient_programmes.json"],
  [
    "/api/patient/programmes/evaluation?",
    "/mock/patient_programmes_evaluation.json",
  ],
  // Persona overview (issue #271): the patient view is served, the rest say so
  ["/api/overview?persona=patient", "/mock/overview_patient.json"],
  ["/api/overview?persona=hdab", "/mock/overview_hdab.json"],
  ["/api/overview?persona=hospital", "/mock/overview_hospital.json"],
  ["/api/overview?persona=researcher", "/mock/overview_researcher.json"],
  ["/api/overview?persona=", "/mock/overview_not_implemented.json"],
  ["/api/overview", "/mock/overview_patient.json"],
  // One patient's clinical timeline, the same synthetic events for any id
  // (a PATIENT persona reads its own timeline from /api/patient instead)
  ["/api/patient?patientId=", "/mock/patient_timeline.json"],
  // Legacy patient endpoint
  ["/api/patient?", "/mock/patient_default.json"],
  // Other prefixes
  ["/api/compliance?", "/mock/compliance_check.json"],
  ["/api/negotiations", "/mock/negotiations.json"],
  // Which transfers the SPEs hold already (#876)
  ["/api/spe-imports?", "/mock/spe_imports.json"],
  // An imported transfer in the graph viewer (#876)
  ["/api/spe-imports/", "/mock/spe_import_graph.json"],
  ["/api/transfers", "/mock/transfers.json"],
  // One contract agreement, for the transfer details (#876)
  ["/api/agreements/", "/mock/agreement.json"],
  ["/api/tasks", "/mock/tasks.json"],
  ["/api/admin/audit", "/mock/admin_audit.json"],
  ["/api/participants/", "/mock/credentials.json"],
];
 
/** The demo patient persona in use, if any (static mode only). */
function patientPersona(): "patient1" | "patient2" | null {
  Iif (typeof window === "undefined") return null;
  try {
    const raw = localStorage.getItem("demo-persona");
    return raw === "patient1" || raw === "patient2" ? raw : null;
  } catch {
    return null;
  }
}
 
function resolveMockPath(endpoint: string): string {
  // PATIENT role restriction: return restricted patient data (own record only)
  const persona = endpoint === "/api/patient" ? patientPersona() : null;
  if (persona === "patient2") return "/mock/patient_restricted_patient2.json";
  if (persona === "patient1") return "/mock/patient_restricted.json";
 
  const exact = STATIC_MOCK_MAP[endpoint];
  if (exact) return exact;
 
  for (const [prefix, path] of STATIC_MOCK_PREFIX) {
    if (endpoint.startsWith(prefix)) return path;
  }
  return endpoint; // fallback: pass through unchanged
}
 
export async function fetchApi(
  endpoint: string,
  init?: RequestInit,
): Promise<Response> {
  const isStatic = process.env.NEXT_PUBLIC_STATIC_EXPORT === "true";
  const basePath = isStatic ? "/MinimumViableHealthDataspacev2" : "";
 
  if (isStatic) {
    // Return synthetic success for non-GET mutations (POST/PUT/DELETE/PATCH)
    // so donate/revoke/create buttons work visually in the static demo.
    const method = (init?.method ?? "GET").toUpperCase();
    Iif (method !== "GET" && method !== "HEAD") {
      await new Promise((resolve) => setTimeout(resolve, 400));
      return new Response(
        JSON.stringify({ ok: true, message: "Registered in demo mode." }),
        { status: 200, headers: { "Content-Type": "application/json" } },
      );
    }
 
    const mockPath = resolveMockPath(endpoint);
    // Simulate small latency to make it feel natural
    await new Promise((resolve) => setTimeout(resolve, 300));
    return fetch(basePath + mockPath, init);
  }
 
  return fetch(endpoint, init);
}