Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Get rid of `null` return values in API
Alexis Sellier committed 4 years ago
commit 1c8c1b925e3316f0bea3008975aea232895b1643
parent 4e8081136fc4c85ef5978aa3dd6987bda9c2b6f3
4 files changed +24 -28
modified src/api.ts
@@ -1,25 +1,27 @@
import type { Config } from '@app/config';

-
export async function get(path: string, config: Config): Promise<any | null> {
-
  if (! config.seed.api) return null;
+
export async function get(path: string, config: Config): Promise<any> {
+
  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 {
modified 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<proj.Blob | null> | 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;
</script>

@@ -138,14 +134,10 @@
          </div>
        </div>
        <div class="column-right">
-
          {#await blob}
+
          {#await getBlob}
            <Loading small center />
          {:then blob}
-
            {#if blob}
-
              <Blob {blob} />
-
            {:else}
-
              <!-- Project has no README -->
-
            {/if}
+
            <Blob {blob} />
          {:catch}
            <div class="error error-message file-not-found">
              <header>
@@ -167,6 +159,8 @@
  {:catch err}
    <div class="container center-content">
      <div class="error error-message text-small">
+
        <!-- TODO: Differentiate between (1) commit doesn't exist and (2) failed
+
             to fetch - this needs a change to the backend. -->
        API request to <code class="text-small">{err.url}</code> failed
      </div>
    </div>
modified 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;

modified src/project.ts
@@ -66,7 +66,7 @@ export interface Tree {
  path: string;
}

-
export async function getInfo(urn: string, config: Config): Promise<Info | null> {
+
export async function getInfo(urn: string, config: Config): Promise<Info> {
  return api.get(`projects/${urn}`, config);
}

@@ -75,7 +75,7 @@ export async function getTree(
  commit: string,
  path: string,
  config: Config
-
): Promise<any | null> {
+
): Promise<any> {
  if (path === "/") {
    path = "";
  }
@@ -87,7 +87,7 @@ export async function getBlob(
  commit: string,
  path: string,
  config: Config
-
): Promise<Blob | null> {
+
): Promise<Blob> {
  return api.get(`projects/${urn}/blob/${commit}/${path}`, config);
}

@@ -95,7 +95,7 @@ export async function getReadme(
  urn: string,
  commit: string,
  config: Config
-
): Promise<Blob | null> {
+
): Promise<Blob> {
  return api.get(`projects/${urn}/readme/${commit}`, config);
}