File size: 2,279 Bytes
1794757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import type { DataSourceSpec, SourceFetchResult } from "./types";

export type DataSourceAdapter = {
  canFetch: boolean;
  fetch: () => Promise<SourceFetchResult>;
};

function ok(sourceId: string, payload: string | unknown): SourceFetchResult {
  return {
    status: "ok",
    sourceId,
    payload,
    fetchedAt: new Date().toISOString(),
  };
}

function placeholder(sourceId: string, reason: string): SourceFetchResult {
  return {
    status: "placeholder",
    sourceId,
    reason,
    fetchedAt: new Date().toISOString(),
  };
}

export function createDataSourceAdapter(source: DataSourceSpec): DataSourceAdapter {
  switch (source.endpoint.kind) {
    case "url": {
      const endpoint = source.endpoint;
      if (source.kind === "rss" || source.kind === "scrape") {
        return {
          canFetch: true,
          fetch: async () => {
            const response = await fetch(endpoint.url, { method: endpoint.method ?? "GET" });
            const payload = await response.text();
            return ok(source.id, payload);
          },
        };
      }

      return {
        canFetch: true,
        fetch: async () => {
          const response = await fetch(endpoint.url, { method: endpoint.method ?? "GET" });
          const payload = await response.json();
          return ok(source.id, payload);
        },
      };
    }

    case "worldmonitor": {
      const endpoint = source.endpoint;
      return {
        canFetch: false,
        fetch: async () =>
          placeholder(
            source.id,
            `World Monitor RPC adapter not wired yet: ${endpoint.rpc}. Route this through the Vercel proxy or Python sidecar.`,
          ),
      };
    }

    case "telegram": {
      const endpoint = source.endpoint;
      return {
        canFetch: false,
        fetch: async () =>
          placeholder(
            source.id,
            `Telegram relay not implemented yet for @${endpoint.handle}. Keep this behind a session/MTProto adapter.`,
          ),
      };
    }

    case "video": {
      const endpoint = source.endpoint;
      return {
        canFetch: false,
        fetch: async () =>
          placeholder(source.id, `Video channel ${endpoint.channel} is a monitoring source, not a fetchable feed.`),
      };
    }
  }
}