MCPcopy
hub / github.com/CapSoftware/Cap / Tinybird

Class Tinybird

packages/web-backend/src/Tinybird/index.ts:28–287  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26}
27
28export class Tinybird extends Effect.Service<Tinybird>()("Tinybird", {
29 effect: Effect.gen(function* () {
30 const env = serverEnv();
31 const token = env.TINYBIRD_TOKEN;
32 const host = env.TINYBIRD_HOST;
33
34 const enabled = Boolean(token && host);
35
36 yield* Effect.logDebug("Initializing Tinybird service", {
37 hasToken: Boolean(token),
38 hasHost: Boolean(host),
39 enabled,
40 });
41
42 if (!enabled) {
43 yield* Effect.logWarning(
44 "Tinybird is disabled: TINYBIRD_TOKEN and/or TINYBIRD_HOST not set",
45 );
46 }
47
48 const request = <T>(path: string, init?: RequestInit) => {
49 if (!enabled) return Effect.succeed<TinybirdResponse<T>>({ data: [] });
50
51 return Effect.tryPromise({
52 try: async () => {
53 const url = `${host}/v0${path}`;
54 const response = await fetch(url, {
55 ...init,
56 headers: {
57 Authorization: `Bearer ${token}`,
58 Accept: "application/json",
59 "Content-Type": "application/json",
60 ...(init?.headers ?? {}),
61 },
62 });
63
64 const textBody = await response.text();
65
66 if (!response.ok) {
67 const errorMessage =
68 textBody || `Tinybird request failed (${response.status})`;
69 console.error("Tinybird request failed", {
70 path,
71 status: response.status,
72 statusText: response.statusText,
73 body: textBody,
74 });
75 throw new Error(errorMessage);
76 }
77
78 if (!textBody) {
79 console.log("Tinybird empty response", { path });
80 return { data: [] } as TinybirdResponse<T>;
81 }
82
83 let parsed: unknown;
84 try {
85 parsed = JSON.parse(textBody);

Callers

nothing calls this directly

Calls 1

serverEnvFunction · 0.90

Tested by

no test coverage detected