Skip to content

Commit

Permalink
Switches $lib/api/documents to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
allanlasser committed Mar 1, 2024
1 parent 7b4810a commit fa6989a
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 28 deletions.
30 changes: 10 additions & 20 deletions src/lib/api/documents.js → src/lib/api/documents.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
}

Expand All @@ -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<Document> {
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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/documents/DocumentListItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(embed)/documents/[id]/pages/[page]/+page.js
Original file line number Diff line number Diff line change
@@ -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} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/routes/app/+layout.js
Original file line number Diff line number Diff line change
@@ -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") || "";
Expand Down
2 changes: 1 addition & 1 deletion src/routes/documents/[id]-[slug]/+layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down

0 comments on commit fa6989a

Please sign in to comment.