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

Function createTinybirdClient

scripts/analytics/shared.js:139–201  ·  view source on GitHub ↗
(authOverride)

Source from the content-addressed store, hash-verified

137}
138
139function createTinybirdClient(authOverride) {
140 const auth = authOverride ?? resolveTinybirdAuth();
141
142 const request = async (path, init = {}) => {
143 const url = new URL(path, formatHost(auth.host));
144 const headers = {
145 Authorization: `Bearer ${auth.token}`,
146 ...(init.headers ?? {}),
147 };
148 let response;
149 try {
150 response = await fetch(url, { ...init, headers });
151 } catch (error) {
152 const err = new Error(
153 `Tinybird request failed: ${error instanceof Error ? error.message : String(error)}`,
154 );
155 err.cause = error;
156 throw err;
157 }
158 const text = await response.text();
159 let payload = text;
160 try {
161 payload = text ? JSON.parse(text) : {};
162 } catch {
163 // keep raw text when JSON parsing fails
164 }
165
166 if (!response.ok) {
167 const message = payload?.error || payload?.message || response.statusText;
168 const err = new Error(
169 `Tinybird request failed (${response.status}): ${message}`,
170 );
171 err.status = response.status;
172 err.payload = payload;
173 throw err;
174 }
175
176 return payload;
177 };
178
179 const getResource = async (resourcePath, { allowNotFound = false } = {}) => {
180 try {
181 return await request(resourcePath);
182 } catch (error) {
183 if (allowNotFound && error.status === 404) return null;
184 throw error;
185 }
186 };
187
188 return {
189 host: formatHost(auth.host),
190 token: auth.token,
191 userToken: auth.userToken,
192 workspaceId: auth.workspaceId,
193 workspaceName: auth.workspaceName,
194 tinybPath: auth.tinybPath,
195 request,
196 getDatasource: (name, options) =>

Callers 3

mainFunction · 0.90
deleteAllDataFunction · 0.90
mainFunction · 0.90

Calls 3

resolveTinybirdAuthFunction · 0.85
formatHostFunction · 0.85
getResourceFunction · 0.85

Tested by

no test coverage detected