@chronark/zod-bird

中文文档
1.0.0发布于 2025/11/04MITTypeScriptESM + CJS零依赖近 30 天下载 18.9万

@chronark/zod-bird

Fully typed Tinybird pipes using zod
原始使用文档
文档目录6 节
  1. Install
  2. Ingesting Data
  3. Querying Endpoints
  4. Error handling
  5. Advanced
  6. Caching

@chronark/zod-bird

Fully typed Tinybird pipes using zod

  • typesafe
  • handles building the url params for you
  • easy transformation of resulting data
  • built in cache directives for nextjs
  • built in retry logic for ratelimited requests
import { Tinybird } from "@chronark/zod-bird";
import { z } from "zod";

const tb = new Tinybird({ token: "token" });

export const getEvents = tb.buildPipe({
  pipe: "get_events__v1",
  parameters: z.object({
    tenantId: z.string(),
  }),
  data: z.object({
    event: z.string(),
    time: z.number().transform((t) => new Date(t)),
  }),
});


const res = await getEvents({ tenantId: "chronark" })

// res.data = {event: string, time: Date}[]
// if no rows found, res.data will be an empty array

Install

npm i @chronark/zod-bird

Ingesting Data

// lib/tinybird.ts
import { Tinybird } from "./client";
import { z } from "zod";

const tb = new Tinybird({ token: process.env.TINYBIRD_TOKEN! });

export const publishEvent = tb.buildIngestEndpoint({
  datasource: "events__v1",
  event: z.object({
    id: z.string(),
    tenantId: z.string(),
    channelId: z.string(),
    time: z.number().int(),
    event: z.string(),
    content: z.string().optional().default(""),
    metadata: z.string().optional().default(""),
  }),
});
// somewhere.ts
import { publishEvent } from "./lib/tinybird";

await publishEvent({
  id: "1",
  tenantId: "1",
  channelId: "1",
  time: Date.now(),
  event: "test",
  content: "test",
  metadata: JSON.stringify({ test: "test" }),
});

Or multiple events in bulk, using a single request to Tinybird.

// somewhere.ts
import { publishEvent } from "./lib/tinybird";

await publishEvent([
  {
    id: "1",
    tenantId: "1",
    channelId: "1",
    time: Date.now(),
    event: "test",
    content: "test",
    metadata: JSON.stringify({ test: "test" }),
  },
  {
    id: "2",
    tenantId: "2",
    channelId: "2",
    time: Date.now(),
    event: "test2",
    content: "test2",
    metadata: JSON.stringify({ test2: "test2" }),
  },
]);

Querying Endpoints

// lib/tinybird.ts
import { Tinybird } from "./client";
import { z } from "zod";

const tb = new Tinybird({ token: process.env.TINYBIRD_TOKEN! });

export const getChannelActivity = tb.buildPipe({
  pipe: "get_channel_activity__v1",
  parameters: z.object({
    tenantId: z.string(),
    channelId: z.string().optional(),
    start: z.number(),
    end: z.number().optional(),
    granularity: z.enum(["1m", "1h", "1d", "1w", "1M"]),
  }),
  data: z.object({
    time: z.string().transform((t) => new Date(t).getTime()),
    count: z
      .number()
      .nullable()
      .optional()
      .transform((v) => (typeof v === "number" ? v : 0)),
  }),
});
// somewhere.ts
import { getChannelActivity } from "./lib/tinybird";


const res = await getChannelActivity({
   tenantId: "1",
   channelId: "1",
   start: 123,
   end: 1234,
   granularity: "1h"
})

res is the response from the tinybird endpoint, but now fully typed and the data has been parsed according to the schema defined in data.

Error handling

For 429 (rate limit exceeded) and 500 errors, zod-bird automatically retries them (source code).

All other errors will throw an Exception, so you should catch it via a try catch:

try {
    const res = await getEvents({ tenantId: "chronark" })
    return res.data
} catch (e) {
    console.error(e)
    return e.messsage
}

Advanced

Caching

You can add cache directives to each pipe.

Disable caching (useful in Next.js where everything is cached by default)

tb.buildPipe({
  pipe: "some_pipe__v1",
  parameters: z.object({
   hello: z.string()
  }),
  data: z.object({
    response: z.string()
  }),
   opts: {
      cache: "no-store" // <-------- Add this
   };
});

Cache for 15 minutes


tb.buildPipe({
  pipe: "some_pipe__v1",
  parameters: z.object({
   hello: z.string()
  }),
  data: z.object({
    response: z.string()
  }),
   opts: {
      next: {
        revalidate: 900 ;// <-------- Add this
      }
    };
});

版本与发布记录

@chronark/zod-bird 共发布 22 个版本,首次发布于 2023/05/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2025/11/041 年前
0.3.102024/10/032 年前
0.3.92024/05/092 年前
0.3.9-rc.02024/05/092 年前
0.3.72024/04/172 年前
0.3.62024/04/162 年前

关于 @chronark/zod-bird 的常见问题

@chronark/zod-bird 是什么?

@chronark/zod-bird Fully typed Tinybird pipes using zod

如何安装 @chronark/zod-bird?

在项目目录运行 npm install @chronark/zod-bird,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^4.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@chronark/zod-bird 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chronark/zod-bird 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chronark/zod-bird 可以商用吗?

@chronark/zod-bird 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chronark/zod-bird 的下载量有多少?

根据 npm 官方下载统计,@chronark/zod-bird 在 2026/10/10 – 2026/11/08 这 30 天内被下载 188,725 次,日均约 6,291 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chronark/zod-bird 还在维护吗?

最新版本 1.0.0 发布于 2025/11/04(1 年前);至今共发布 22 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/05/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。