diff --git a/src/api.ts b/src/api.ts index 23c7447dd..5d1242011 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,25 +1,27 @@ import type { Config } from '@app/config'; -export async function get(path: string, config: Config): Promise { - if (! config.seed.api) return null; +export async function get(path: string, config: Config): Promise { + if (! config.seed.api) + throw new Error("Seed HTTP API unavailable"); const url = `${config.seed.api}/v1/${path}`; + let response = null; try { - const response = await fetch(url, { + response = await fetch(url, { method: 'GET', headers: { 'Accept': 'application/json', } }); - - if (! response.ok) { - return null; - } - return response.json(); - } catch { + } catch (err) { throw new ApiError(url, "API request failed"); } + + if (! response.ok) { + throw new ApiError(url, "Not found"); + } + return response.json(); } class ApiError extends Error { diff --git a/src/base/projects/Browser.svelte b/src/base/projects/Browser.svelte index 64a1b0a6e..1210ef063 100644 --- a/src/base/projects/Browser.svelte +++ b/src/base/projects/Browser.svelte @@ -15,17 +15,13 @@ export let onSelect: (event: { detail: string }) => void; export let org: string | null = null; - let blob: Promise | null = null; - const fetchTree = async (path: string) => { return proj.getTree(urn, commit, path, config); }; - $: if (path === "/") { - blob = proj.getReadme(urn, commit, config); - } else { - blob = proj.getBlob(urn, commit, path, config); - } + $: getBlob = path === "/" + ? proj.getReadme(urn, commit, config) + : proj.getBlob(urn, commit, path, config); $: getAnchor = org ? Org.getAnchor(org, urn, config) : null; @@ -138,14 +134,10 @@
- {#await blob} + {#await getBlob} {:then blob} - {#if blob} - - {:else} - - {/if} + {:catch}
@@ -167,6 +159,8 @@ {:catch err}
+ API request to {err.url} failed
diff --git a/src/base/projects/View.svelte b/src/base/projects/View.svelte index 4aeb30f18..f3db6adf3 100644 --- a/src/base/projects/View.svelte +++ b/src/base/projects/View.svelte @@ -13,8 +13,8 @@ }; export let urn: string; - export let org: string | undefined; - export let commit: string | undefined; + export let org: string = ""; + export let commit: string = ""; export let config: Config; export let path: string; diff --git a/src/project.ts b/src/project.ts index 2f797236d..b782b3e27 100644 --- a/src/project.ts +++ b/src/project.ts @@ -66,7 +66,7 @@ export interface Tree { path: string; } -export async function getInfo(urn: string, config: Config): Promise { +export async function getInfo(urn: string, config: Config): Promise { return api.get(`projects/${urn}`, config); } @@ -75,7 +75,7 @@ export async function getTree( commit: string, path: string, config: Config -): Promise { +): Promise { if (path === "/") { path = ""; } @@ -87,7 +87,7 @@ export async function getBlob( commit: string, path: string, config: Config -): Promise { +): Promise { return api.get(`projects/${urn}/blob/${commit}/${path}`, config); } @@ -95,7 +95,7 @@ export async function getReadme( urn: string, commit: string, config: Config -): Promise { +): Promise { return api.get(`projects/${urn}/readme/${commit}`, config); }