Skip to content

Commit

Permalink
Forgot I hadn't done projects.get yet
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Apr 16, 2024
1 parent 615249c commit f0474ef
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
3 changes: 2 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
61 changes: 40 additions & 21 deletions src/lib/api/projects.js → src/lib/api/projects.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -8,34 +10,51 @@ import { BASE_API_URL } from "@/config/config.js";
* @export
* @param {number} id
* @param {globalThis.fetch} fetch
* @returns {import('./types').Project}
* @returns {Promise<import('./types').Project>}
*/
export async function get(id, fetch) {}
export async function get(
id: number,
fetch = globalThis.fetch,
): Promise<Project> {
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
*
* @export
* @param {any} params filter params
* @param {globalThis.fetch} fetch
* @returns {Promise<import('./types').ProjectResults>}
*/
export async function list(params = {}, fetch) {
export async function list(
params: any = {},
fetch = globalThis.fetch,
): Promise<ProjectResults> {
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,
});
}

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

Expand Down
1 change: 0 additions & 1 deletion src/lib/components/common/Logo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
viewBox="0 0 182 32"
fill="none"
xmlns="http://www.w3.org/2000/svg"
title="DocumentCloud"
class="icon"
>
<path
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(embed)/projects/[project_id]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// load data for project embeds

import * as projects from "$lib/api/projects.js";
import * as projects from "$lib/api/projects";

/** @type {import('./$types').PageLoad} */
export async function load({ params, fetch }) {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/app/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getPinnedAddons } from "@/lib/api/addons";
import * as projects from "$lib/api/projects.js";
import * as projects from "$lib/api/projects";

export async function load({ url, fetch }) {
const pinnedAddons = getPinnedAddons(fetch);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/app/upload/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Data loading for upload
*/

import * as projectsApi from "$lib/api/projects.js";
import * as projectsApi from "$lib/api/projects";

export async function load({ fetch, parent }) {
const { me } = await parent();
Expand Down

0 comments on commit f0474ef

Please sign in to comment.