tangle_indexer


git clone https://radroots.dev/git/tangle_indexer.git
Log | Files | Refs | Submodules | LICENSE

index.ts (1708B)


      1 import { _env } from "$lib/utils/_env";
      2 import { type FetchJsonResult, type HttpFetch, fetch_json } from "@radroots/apps-lib";
      3 import type { RadrootsListingEventMetadata } from "@radroots/events-bindings";
      4 import type { RadrootsEventsIndexedManifest } from "@radroots/events-indexed-bindings";
      5 
      6 export type ListingRoutesKind = "country" | "author" | "npub" | "nip05";
      7 
      8 const { RADROOTS_MARKET_INDEXES_URL: idx_url } = _env;
      9 
     10 export type ListingIndexedData = {
     11     manifest: RadrootsEventsIndexedManifest;
     12     events: RadrootsListingEventMetadata[];
     13 };
     14 
     15 export async function fetch_listing_indexes(
     16     fetch_fn: HttpFetch,
     17     kind: ListingRoutesKind
     18 ): Promise<FetchJsonResult<string[]>> {
     19     const url = `${idx_url}/events/30402/${kind}/indexes.json`;
     20     return fetch_json<string[]>(fetch_fn, url);
     21 }
     22 
     23 export async function load_listing_indexed(
     24     fetch_fn: HttpFetch,
     25     kind: ListingRoutesKind,
     26     key: string
     27 ): Promise<FetchJsonResult<ListingIndexedData>> {
     28     const manifest_url = `${idx_url}/events/30402/${kind}/${key}/manifest.json`;
     29     const manifest_res = await fetch_json<RadrootsEventsIndexedManifest>(fetch_fn, manifest_url);
     30     if (!manifest_res.ok) return manifest_res;
     31 
     32     let events: RadrootsListingEventMetadata[] = [];
     33     if (manifest_res.data.shards.length > 0) {
     34         const shard = manifest_res.data.shards[0];
     35         const shard_url = `${idx_url}/events/30402/${kind}/${key}/${shard.file}?v=${shard.sha256}`;
     36         const events_res = await fetch_json<RadrootsListingEventMetadata[]>(fetch_fn, shard_url);
     37         if (!events_res.ok) return events_res;
     38         events = events_res.data;
     39     }
     40 
     41     return { ok: true, data: { manifest: manifest_res.data, events } };
     42 }