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 326ca3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 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/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 326ca3d

Please sign in to comment.