@tinybirdco/sdk 0.0.64 → 0.0.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,29 @@ import { definePipe, node } from "./pipe.js";
13
13
  import { t } from "./types.js";
14
14
 
15
15
  describe("Project Schema", () => {
16
+ const originalEnv = { ...process.env };
17
+
18
+ beforeEach(() => {
19
+ process.env = { ...originalEnv };
20
+ delete process.env.VERCEL_ENV;
21
+ delete process.env.GITHUB_HEAD_REF;
22
+ delete process.env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME;
23
+ delete process.env.CI;
24
+ delete process.env.TINYBIRD_PREVIEW_MODE;
25
+ delete process.env.VERCEL_GIT_COMMIT_REF;
26
+ delete process.env.GITHUB_REF_NAME;
27
+ delete process.env.CI_COMMIT_BRANCH;
28
+ delete process.env.CIRCLE_BRANCH;
29
+ delete process.env.BUILD_SOURCEBRANCHNAME;
30
+ delete process.env.BITBUCKET_BRANCH;
31
+ delete process.env.TINYBIRD_BRANCH_NAME;
32
+ delete process.env.TINYBIRD_BRANCH_TOKEN;
33
+ });
34
+
35
+ afterEach(() => {
36
+ process.env = { ...originalEnv };
37
+ });
38
+
16
39
  describe("defineProject", () => {
17
40
  it("creates a project with empty config", () => {
18
41
  const project = defineProject({});
@@ -390,6 +413,7 @@ describe("Project Schema", () => {
390
413
  const events = defineDatasource("events", {
391
414
  schema: { id: t.string() },
392
415
  });
416
+ const customFetch = vi.fn();
393
417
 
394
418
  // Should accept all options without throwing
395
419
  const client = createTinybirdClient({
@@ -397,6 +421,7 @@ describe("Project Schema", () => {
397
421
  pipes: {},
398
422
  baseUrl: "https://custom.tinybird.co",
399
423
  token: "test-token",
424
+ fetch: customFetch as typeof fetch,
400
425
  configDir: "/custom/config/dir",
401
426
  devMode: true,
402
427
  });
@@ -440,5 +465,59 @@ describe("Project Schema", () => {
440
465
  expect(client._options).toBeDefined();
441
466
  expect(() => client.client).toThrow("Client not initialized");
442
467
  });
468
+
469
+ it("uses custom fetch for typed endpoint queries", async () => {
470
+ const topEvents = definePipe("top_events", {
471
+ nodes: [node({ name: "endpoint", sql: "SELECT 1" })],
472
+ output: { count: t.int64() },
473
+ endpoint: true,
474
+ });
475
+ const customFetch = vi.fn().mockResolvedValueOnce({
476
+ ok: true,
477
+ json: () =>
478
+ Promise.resolve({
479
+ data: [],
480
+ meta: [],
481
+ rows: 0,
482
+ statistics: {
483
+ elapsed: 0,
484
+ rows_read: 0,
485
+ bytes_read: 0,
486
+ },
487
+ }),
488
+ });
489
+ const originalFetch = global.fetch;
490
+ const globalFetch = vi.fn().mockRejectedValue(
491
+ new Error("global fetch should not be called")
492
+ );
493
+ global.fetch = globalFetch as typeof fetch;
494
+
495
+ try {
496
+ const client = createTinybirdClient({
497
+ datasources: {},
498
+ pipes: { topEvents },
499
+ baseUrl: "https://api.tinybird.co",
500
+ token: "test-token",
501
+ fetch: customFetch as typeof fetch,
502
+ devMode: false,
503
+ });
504
+
505
+ const result = await client.topEvents.query({});
506
+
507
+ expect(result.rows).toBe(0);
508
+ expect(customFetch).toHaveBeenCalledTimes(1);
509
+ expect(globalFetch).not.toHaveBeenCalled();
510
+
511
+ const [url, init] = customFetch.mock.calls[0] as [string, RequestInit];
512
+ const parsed = new URL(url);
513
+ expect(parsed.pathname).toBe("/v0/pipes/top_events.json");
514
+ expect(parsed.searchParams.get("from")).toBe("ts-sdk");
515
+ expect(new Headers(init.headers).get("Authorization")).toBe(
516
+ "Bearer test-token"
517
+ );
518
+ } finally {
519
+ global.fetch = originalFetch;
520
+ }
521
+ });
443
522
  });
444
523
  });
@@ -130,6 +130,8 @@ export interface TinybirdClientConfig<
130
130
  baseUrl?: string;
131
131
  /** Tinybird API token (defaults to TINYBIRD_TOKEN env var) */
132
132
  token?: string;
133
+ /** Custom fetch implementation (optional, defaults to global fetch) */
134
+ fetch?: typeof fetch;
133
135
  /**
134
136
  * Directory to use as the starting point when searching for tinybird.json config.
135
137
  * In monorepo setups, this should be set to the directory containing tinybird.json
@@ -295,6 +297,7 @@ export const Tinybird: TinybirdConstructor = class Tinybird<
295
297
  readonly #options: {
296
298
  baseUrl?: string;
297
299
  token?: string;
300
+ fetch?: typeof fetch;
298
301
  configDir?: string;
299
302
  devMode?: boolean;
300
303
  };
@@ -303,6 +306,7 @@ export const Tinybird: TinybirdConstructor = class Tinybird<
303
306
  this.#options = {
304
307
  baseUrl: config.baseUrl,
305
308
  token: config.token,
309
+ fetch: config.fetch,
306
310
  configDir: config.configDir,
307
311
  devMode: config.devMode,
308
312
  };
@@ -392,11 +396,16 @@ export const Tinybird: TinybirdConstructor = class Tinybird<
392
396
 
393
397
  const baseUrl =
394
398
  this.#options.baseUrl ?? process.env.TINYBIRD_URL ?? "https://api.tinybird.co";
395
- const token = await resolveToken({ baseUrl, token: this.#options.token });
399
+ const token = await resolveToken({
400
+ baseUrl,
401
+ token: this.#options.token,
402
+ fetch: this.#options.fetch,
403
+ });
396
404
 
397
405
  this.#client = createClient({
398
406
  baseUrl,
399
407
  token,
408
+ fetch: this.#options.fetch,
400
409
  devMode: this.#options.devMode ?? process.env.NODE_ENV === "development",
401
410
  configDir: this.#options.configDir,
402
411
  });