From fa6989a9c3ef9e27d564007038f257333327e02f Mon Sep 17 00:00:00 2001 From: Allan Lasser Date: Fri, 1 Mar 2024 15:36:17 -0500 Subject: [PATCH] Switches $lib/api/documents to TypeScript --- src/lib/api/{documents.js => documents.ts} | 30 +++++++------------ .../documents/DocumentListItem.svelte | 2 +- .../[id]/annotations/[note_id]/+page.js | 2 +- .../[id]/annotations/[note_id]/+page.svelte | 2 +- .../documents/[id]/pages/[page]/+page.js | 2 +- .../documents/[id]/pages/[page]/+page.svelte | 2 +- src/routes/app/+layout.js | 4 +-- src/routes/documents/[id]-[slug]/+layout.js | 2 +- 8 files changed, 18 insertions(+), 28 deletions(-) rename src/lib/api/{documents.js => documents.ts} (89%) 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/lib/components/documents/DocumentListItem.svelte b/src/lib/components/documents/DocumentListItem.svelte index 0e9e2c993..9e0cbb237 100644 --- a/src/lib/components/documents/DocumentListItem.svelte +++ b/src/lib/components/documents/DocumentListItem.svelte @@ -10,7 +10,7 @@ pageImageUrl, canonicalUrl, userOrgString, - } from "$lib/api/documents.js"; + } from "@/lib/api/documents"; import { pageSizesFromSpec } from "@/api/pageSize.js"; export let document: Document; diff --git a/src/routes/(embed)/documents/[id]/annotations/[note_id]/+page.js b/src/routes/(embed)/documents/[id]/annotations/[note_id]/+page.js index b8e3c4d81..a9de0b125 100644 --- a/src/routes/(embed)/documents/[id]/annotations/[note_id]/+page.js +++ b/src/routes/(embed)/documents/[id]/annotations/[note_id]/+page.js @@ -1,5 +1,5 @@ // load a note for embedding -import * as documents from "$lib/api/documents.js"; +import * as documents from "@/lib/api/documents"; import * as notesApi from "$lib/api/notes.js"; /** @type {import('./$types').PageLoad} */ diff --git a/src/routes/(embed)/documents/[id]/annotations/[note_id]/+page.svelte b/src/routes/(embed)/documents/[id]/annotations/[note_id]/+page.svelte index c1470d38a..0dd4a98ca 100644 --- a/src/routes/(embed)/documents/[id]/annotations/[note_id]/+page.svelte +++ b/src/routes/(embed)/documents/[id]/annotations/[note_id]/+page.svelte @@ -4,7 +4,7 @@ import { _ } from "svelte-i18n"; import { informSize } from "@/embed/iframeSizer.js"; - import { pageImageUrl } from "$lib/api/documents.js"; + import { pageImageUrl } from "@/lib/api/documents"; import * as notes from "$lib/api/notes.js"; import { embedUrl } from "$lib/api/embed.js"; import { canonicalNoteUrl, noteUrl } from "$lib/api/notes.js"; diff --git a/src/routes/(embed)/documents/[id]/pages/[page]/+page.js b/src/routes/(embed)/documents/[id]/pages/[page]/+page.js index b5abb45ea..2fb53c484 100644 --- a/src/routes/(embed)/documents/[id]/pages/[page]/+page.js +++ b/src/routes/(embed)/documents/[id]/pages/[page]/+page.js @@ -1,5 +1,5 @@ // load data for a single page embed -import * as documents from "$lib/api/documents.js"; +import * as documents from "@/lib/api/documents"; import * as notesApi from "$lib/api/notes.js"; /** @type {import('./$types').PageLoad} */ diff --git a/src/routes/(embed)/documents/[id]/pages/[page]/+page.svelte b/src/routes/(embed)/documents/[id]/pages/[page]/+page.svelte index 87aef1346..71940346e 100644 --- a/src/routes/(embed)/documents/[id]/pages/[page]/+page.svelte +++ b/src/routes/(embed)/documents/[id]/pages/[page]/+page.svelte @@ -13,7 +13,7 @@ pageUrl, textUrl, userOrgString, - } from "$lib/api/documents.js"; + } from "@/lib/api/documents"; import { embedUrl } from "$lib/api/embed.js"; export let data; diff --git a/src/routes/app/+layout.js b/src/routes/app/+layout.js index 75c1c01d4..145807b61 100644 --- a/src/routes/app/+layout.js +++ b/src/routes/app/+layout.js @@ -1,5 +1,5 @@ -import { search } from "$lib/api/documents.js"; -import { getPinnedAddons } from "@/lib/api/addons.js"; +import { search } from "$lib/api/documents"; +import { getPinnedAddons } from "$lib/api/addons.js"; export async function load({ url, fetch }) { const query = url.searchParams.get("q") || ""; 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 }) {