0

Fetch mesh links from OpenWifiMap

by
Published Sep 16, 2022

Fetches the list of mesh links and their quality for a specific node from OpenWifiMap.

Script helper
  • Submitted by jaller94 Deno
    Created 1377 days ago
    1
    export async function main(
    2
      id: string,
    3
    ) {
    4
      const resp = await fetch(
    5
        `https://api.openwifimap.net/db/${encodeURIComponent(id)}`,
    6
      );
    7
      if (!resp.ok) {
    8
        throw Error(`HTTP Status ${resp.status}`);
    9
      }
    10
      const data = await resp.json();
    11
      if (!Array.isArray(data.links)) {
    12
        throw Error('Response did not contain links.');
    13
      }
    14
      const { links } = data;
    15
      let summary = `Connected to ${pluralize("link", links.length)}.`;
    16
      if (links.length > 0) {
    17
        summary += ` ${links.map((link: { id: string; quality: number }) => `${link.id} (${link.quality})`).join(", ")}`;
    18
      }
    19
      return {
    20
        links,
    21
        summary,
    22
      };
    23
    }
    24
    
    
    25
    function pluralize(word: string, count: number) {
    26
      return `${count} ${word}${count === 1 ? "" : "s"}`;
    27
    }