@chronark/zod-bird (英语)
使用 Zod 完全打入小鸟管
- 类型安全
- 处理为您构建 url 参数
- 数据易于转换
- 在缓存指令中为下个js所建
- 以重试逻辑构建, 用于限制速率的请求
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
安装
npm i @chronark/zod-bird
输入数据
// 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" }),
});
或多事件成批地,使用单向天鸟求取.
// 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" }),
},
]);
查询终点
// 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 是来自小鸟端点的响应,但现在已完全输入,数据已经按照定义的图解解析 data。 。 。 .
处理错误
对于 429 (超过限额)和 500 错误, zod-bird 自动重试( e)源代码) (中文(简体) ).
其它错误都会丢出 Exception因此,你应该抓住它 通过尝试捕获:
try {
const res = await getEvents({ tenantId: "chronark" })
return res.data
} catch (e) {
console.error(e)
return e.messsage
}
高级
缓存
你可以在每个管子上添加缓存指令.
禁用缓存( 用于默认缓存万事的 Next.js )
tb.buildPipe({
pipe: "some_pipe__v1",
parameters: z.object({
hello: z.string()
}),
data: z.object({
response: z.string()
}),
opts: {
cache: "no-store" // <-------- Add this
};
});
缓存15分钟
tb.buildPipe({
pipe: "some_pipe__v1",
parameters: z.object({
hello: z.string()
}),
data: z.object({
response: z.string()
}),
opts: {
next: {
revalidate: 900 ;// <-------- Add this
}
};
});