Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/lib/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { resolveCanonical } from "./api";

const originalFetch = globalThis.fetch;

afterEach(() => {
globalThis.fetch = originalFetch;
vi.restoreAllMocks();
});

describe("resolveCanonical", () => {
it("returns the canonical id when the catalog has a mirror", async () => {
globalThis.fetch = vi.fn(async () =>
new Response(JSON.stringify({ found: true, dataset_id: "on002718" }), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
) as unknown as typeof fetch;

expect(await resolveCanonical("ds002718")).toBe("on002718");
});

it("returns null when found is false", async () => {
globalThis.fetch = vi.fn(async () =>
new Response(JSON.stringify({ found: false }), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
) as unknown as typeof fetch;

expect(await resolveCanonical("ds007222")).toBeNull();
});

it("returns null when the endpoint responds non-2xx", async () => {
globalThis.fetch = vi.fn(async () => new Response("nope", { status: 503 })) as unknown as typeof fetch;
expect(await resolveCanonical("ds002718")).toBeNull();
});

it("url-encodes the source id", async () => {
const captured: string[] = [];
globalThis.fetch = vi.fn(async (input: RequestInfo | URL) => {
captured.push(typeof input === "string" ? input : input.toString());
return new Response(JSON.stringify({ found: false }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}) as unknown as typeof fetch;

await resolveCanonical("ds 00 27 18");
expect(captured[0]).toContain("/datasets/resolve/ds%2000%2027%2018");
});
});
22 changes: 22 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,25 @@ export async function getDataset(
const raw = (await res.json()) as Dataset | { dataset: Dataset };
return "dataset" in raw ? raw.dataset : raw;
}

/**
* Resolve an OpenNeuro source id (ds*) to its canonical NEMAR dataset id (on*),
* when a mirror exists in the catalog. Returns null when the id has no mirror
* yet or the endpoint is unreachable -- callers should treat null as
* "no canonical, render as-is".
*/
export async function resolveCanonical(
sourceId: string,
init: { signal?: AbortSignal; apiBase?: string } = {},
): Promise<string | null> {
const url = `${apiBase(init.apiBase)}/datasets/resolve/${encodeURIComponent(sourceId)}`;
const res = await fetch(url, {
signal: init.signal,
headers: { Accept: "application/json" },
});
if (!res.ok) {
return null;
}
const json = (await res.json()) as { found?: boolean; dataset_id?: string };
return json.found && json.dataset_id ? json.dataset_id : null;
}
12 changes: 11 additions & 1 deletion src/pages/dataset/[id].astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Readme from "../../components/Readme.astro";
import BidsTree from "../../components/BidsTree.astro";
import DetailRail from "../../components/DetailRail.astro";
import { archiveZipUrl, getLanding, getMetadata } from "../../lib/data-api";
import { getDataset } from "../../lib/api";
import { getDataset, resolveCanonical } from "../../lib/api";
import { detectProvenance } from "../../lib/provenance";
import { splitModalities } from "../../lib/format";
import { ORG_DATASETS } from "../../lib/constants";
Expand All @@ -20,6 +20,16 @@ if (!id) {
}

const url = new URL(Astro.request.url);

// OpenNeuro source ids (ds*) redirect to their NEMAR canonical (on*) when a
// mirror exists. Preserves the query string so deep links survive the hop.
if (/^ds\d{6}$/.test(id)) {
const canonical = await resolveCanonical(id).catch(() => null);
if (canonical) {
return Astro.redirect(`/dataset/${canonical}${url.search}`, 301);
}
}

const versionParam = url.searchParams.get("v");

// Fan out the landing, metadata, and catalog-row fetches in parallel.
Expand Down