diff --git a/src/lib/api/documents.js b/src/lib/api/documents.ts similarity index 89% rename from src/lib/api/documents.js rename to src/lib/api/documents.ts index 1cedabed3..74b127e7c 100644 --- a/src/lib/api/documents.js +++ b/src/lib/api/documents.ts @@ -1,20 +1,13 @@ /** API helpers related to documents. * Lots of duplicated code here that should get consolidated at some point. */ -import { error } from "@sveltejs/kit"; +import { error, type NumericRange } from "@sveltejs/kit"; import { APP_URL, BASE_API_URL } from "@/config/config.js"; import { DEFAULT_EXPAND } from "@/api/common.js"; import { isOrg } from "@/api/types/orgAndUser"; +import { isErrorCode } from "../utils"; -/** - * Search documents - * - * @async - * @param {query} string - * @param {boolean} highlight - * @param {globalThis.fetch} fetch - * @returns {import('./types').DocumentResults} - */ +/** Search documents */ export async function search( query = "", highlight = false, @@ -24,11 +17,11 @@ export async function search( endpoint.searchParams.set("expand", DEFAULT_EXPAND); endpoint.searchParams.set("q", query); - endpoint.searchParams.set("hl", highlight); + endpoint.searchParams.set("hl", String(highlight)); const resp = await fetch(endpoint, { credentials: "include" }); - if (!resp.ok) { + if (isErrorCode(resp.status)) { error(resp.status, resp.statusText); } @@ -38,21 +31,18 @@ export async function search( /** * Load a single document from the API * Example: https://api.www.documentcloud.org/api/documents/1/ - * - * @async - * @export - * @param {number} id - * @param {globalThis.fetch} fetch - * @returns {import('./types').Document} */ -export async function get(id, fetch) { +export async function get( + id: number, + fetch: typeof globalThis.fetch, +): Promise { const endpoint = new URL(`documents/${id}.json`, BASE_API_URL); const expand = ["user", "organization", "projects", "revisions"]; endpoint.searchParams.set("expand", expand.join(",")); const resp = await fetch(endpoint, { credentials: "include" }); - if (!resp.ok) { + if (isErrorCode(resp.status)) { error(resp.status, resp.statusText); } diff --git a/src/routes/documents/[id]-[slug]/+layout.js b/src/routes/documents/[id]-[slug]/+layout.js index ee2f4707d..ee4225464 100644 --- a/src/routes/documents/[id]-[slug]/+layout.js +++ b/src/routes/documents/[id]-[slug]/+layout.js @@ -5,7 +5,7 @@ */ import { redirect } from "@sveltejs/kit"; -import * as documents from "$lib/api/documents.js"; +import * as documents from "@/lib/api/documents"; /** @type {import('./$types').PageLoad} */ export async function load({ fetch, params }) {