Edits history of script submission #157 for ' Fetch mesh links from OpenWifiMap (helper)'

  • deno
    export async function main(
      id: string,
    ) {
      const resp = await fetch(
        `https://api.openwifimap.net/db/${encodeURIComponent(id)}`,
      );
      if (!resp.ok) {
        throw Error(`HTTP Status ${resp.status}`);
      }
      const data = await resp.json();
      if (!Array.isArray(data.links)) {
        throw Error('Response did not contain links.');
      }
      const { links } = data;
      let summary = `Connected to ${pluralize("link", links.length)}.`;
      if (links.length > 0) {
        summary += ` ${links.map((link: { id: string; quality: number }) => `${link.id} (${link.quality})`).join(", ")}`;
      }
      return {
        links,
        summary,
      };
    }
    
    function pluralize(word: string, count: number) {
      return `${count} ${word}${count === 1 ? "" : "s"}`;
    }

    Submitted by jaller94 1377 days ago