From f0474efb9505f130788cc6f30f26651ec31bd223 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Tue, 16 Apr 2024 18:14:32 -0400 Subject: [PATCH] Forgot I hadn't done projects.get yet --- jsconfig.json | 3 +- src/lib/api/{projects.js => projects.ts} | 61 ++++++++++++------- src/lib/components/common/Logo.svelte | 1 - .../(embed)/projects/[project_id]/+page.ts | 2 +- src/routes/app/+layout.ts | 2 +- src/routes/app/upload/+layout.ts | 2 +- 6 files changed, 45 insertions(+), 26 deletions(-) rename src/lib/api/{projects.js => projects.ts} (50%) diff --git a/jsconfig.json b/jsconfig.json index 97939e152..63c97a632 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -9,7 +9,8 @@ "skipLibCheck": true, "sourceMap": true, "strict": false, - "moduleResolution": "bundler" + "moduleResolution": "bundler", + "types": ["@testing-library/jest-dom"] }, // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias and https://kit.svelte.dev/docs/configuration#files // diff --git a/src/lib/api/projects.js b/src/lib/api/projects.ts similarity index 50% rename from src/lib/api/projects.js rename to src/lib/api/projects.ts index c894d910f..4fe942d11 100644 --- a/src/lib/api/projects.js +++ b/src/lib/api/projects.ts @@ -1,5 +1,7 @@ // api methods for projects -import { error } from "@sveltejs/kit"; +import type { Project, ProjectResults, ProjectMembershipList } from "./types"; + +import { error, type NumericRange } from "@sveltejs/kit"; import { BASE_API_URL } from "@/config/config.js"; /** @@ -8,9 +10,26 @@ import { BASE_API_URL } from "@/config/config.js"; * @export * @param {number} id * @param {globalThis.fetch} fetch - * @returns {import('./types').Project} + * @returns {Promise} */ -export async function get(id, fetch) {} +export async function get( + id: number, + fetch = globalThis.fetch, +): Promise { + const endpoint = new URL(`projects/${id}/`, BASE_API_URL); + + const res = await fetch(endpoint, { credentials: "include" }).catch((e) => { + error(500, { message: e }); + }); + + if (!res.ok) { + error(res.status as NumericRange<400, 599>, { + message: res.statusText, + }); + } + + return res.json(); +} /** * Get a list of projects @@ -18,24 +37,24 @@ export async function get(id, fetch) {} * @export * @param {any} params filter params * @param {globalThis.fetch} fetch - * @returns {Promise} */ -export async function list(params = {}, fetch) { +export async function list( + params: any = {}, + fetch = globalThis.fetch, +): Promise { const endpoint = new URL("projects/", BASE_API_URL); for (const [k, v] of Object.entries(params)) { - endpoint.searchParams.set(k, v); + endpoint.searchParams.set(k, String(v)); } - const res = await fetch(endpoint, { credentials: "include" }).catch((e) => ({ - ok: false, - error: e, - })); + const res = await fetch(endpoint, { credentials: "include" }).catch((e) => { + error(500, { message: e }); + }); if (!res.ok) { - error(res.status, { + error(res.status as NumericRange<400, 599>, { message: res.statusText, - error: res.error, }); } @@ -48,26 +67,26 @@ export async function list(params = {}, fetch) { * @export * @param {number} id * @param {globalThis.fetch} fetch - * @returns {import('./types').ProjectMembershipList} */ -export async function documents(id, fetch) { +export async function documents( + id: number | string, + fetch = globalThis.fetch, +): Promise { const endpoint = new URL(`projects/${id}/documents/`, BASE_API_URL); const expand = ["user", "organization", "document"]; // might make these configurable later endpoint.searchParams.set("expand", expand.join(",")); endpoint.searchParams.set("ordering", "-created_at"); - endpoint.searchParams.set("per_page", 12); + endpoint.searchParams.set("per_page", "12"); - const res = await fetch(endpoint, { credentials: "include" }).catch((e) => ({ - ok: false, - error: e, - })); + const res = await fetch(endpoint, { credentials: "include" }).catch((e) => { + error(500, { message: e }); + }); if (!res.ok) { - error(res.status, { + error(res.status as NumericRange<400, 599>, { message: res.statusText, - error: res.error, }); } diff --git a/src/lib/components/common/Logo.svelte b/src/lib/components/common/Logo.svelte index 1ccabdb0c..fecaab696 100644 --- a/src/lib/components/common/Logo.svelte +++ b/src/lib/components/common/Logo.svelte @@ -2,7 +2,6 @@ viewBox="0 0 182 32" fill="none" xmlns="http://www.w3.org/2000/svg" - title="DocumentCloud" class="icon" >