All files / src/services/api HistoricalApiRouter.tsx

100% Statements 12/12
88.88% Branches 8/9
100% Functions 5/5
100% Lines 12/12

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                                                                                33177x 4818x   4818x   131x     131x               47x           47x                                 30936x   30936x 107x   30936x         47x            
import {
  DiffResponse,
  ResourceHistorySource,
  VersionList,
} from "@/objects/History";
import { APIRouter } from "./ApiRouter";
 
/**
 * An {@link APIRouter} for a resource that keeps an edit history.
 *
 * svc-core mounts the same two endpoints under every such resource
 * (`GET /{id}/history` and `GET /{id}/history/diff`, from one route factory),
 * so this mirrors that factory on the client instead of each router carrying
 * its own copy of the pair.
 *
 * Extend this rather than `APIRouter` when — and only when — the backend
 * router calls `register_history_routes(...)`. Routers without history keep
 * extending `APIRouter`, so the two reads are not offered where they would
 * 404.
 */
/**
 * A router whose backend mounts history under *sub-paths* rather than at the
 * router's own prefix.
 *
 * Evaluations are the case: svc-core registers history at
 * `/evaluations/experiments/{id}/history` and
 * `/evaluations/test-suites/{id}/history`, and nothing at
 * `/evaluations/{id}/history`. A router like that must expose its scopes and
 * *not* a bare `history()`, because the bare one would build a path the
 * backend does not serve — a 404 that only shows up at runtime. Keeping it
 * off the type means a call site cannot reach for it by mistake.
 */
export class ScopedHistoryRouter extends APIRouter {
  /**
   * A history source for one resource type under this router.
   *
   * Routers carrying several historied resources expose one scope each
   * (see `EvaluationRouter.experiments` / `.testSuites`) so a call site can
   * hand a single object to `ResourceHistorySection` either way.
   */
  protected historyScope = (basePath = ""): ResourceHistorySource => {
    const segment = basePath ? `${basePath.replace(/^\/|\/$/g, "")}/` : "";
 
    return {
      history: async (id: string): Promise<VersionList | null> => {
        const res = await this.api.get<VersionList>(
          this.endpoint(`${segment}${id}/history`),
        );
        return this.validRes(res) ? res.data : null;
      },
 
      historyDiff: async (
        id: string,
        toVersion: number,
        fromVersion?: number | null,
      ): Promise<DiffResponse | null> => {
        const res = await this.api.get<DiffResponse>(
          this.endpoint(`${segment}${id}/history/diff`, {
            to_version: toVersion,
            ...(fromVersion != null ? { from_version: fromVersion } : {}),
          }),
        );
        return this.validRes(res) ? res.data : null;
      },
    };
  };
}
 
export class HistoricalAPIRouter
  extends ScopedHistoryRouter
  implements ResourceHistorySource
{
  /**
   * Path segment between the router prefix and the resource id, for backends
   * whose resource does not sit directly under the prefix — organisations
   * mount history at `/organisations/id/{id}/history`. Empty for the common
   * case. A router with *several* historied resources extends
   * {@link ScopedHistoryRouter} instead and exposes one scope each.
   */
  protected historyBasePath = "";
 
  history = async (id: string): Promise<VersionList | null> =>
    this.historyScope(this.historyBasePath).history(id);
 
  historyDiff = async (
    id: string,
    toVersion: number,
    fromVersion?: number | null,
  ): Promise<DiffResponse | null> =>
    this.historyScope(this.historyBasePath).historyDiff(
      id,
      toVersion,
      fromVersion,
    );
}