{"id":13675803,"url":"https://github.com/chronark/zod-bird","last_synced_at":"2025-04-04T15:07:50.876Z","repository":{"id":167827244,"uuid":"643454116","full_name":"chronark/zod-bird","owner":"chronark","description":"Fully typed Tinybird pipes using zod","archived":false,"fork":false,"pushed_at":"2024-10-02T16:23:42.000Z","size":522,"stargazers_count":167,"open_issues_count":5,"forks_count":15,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-03T11:00:05.812Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chronark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-21T07:59:44.000Z","updated_at":"2025-02-28T11:14:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"ec2d1dc6-14b3-45d0-8fb5-c2ce58a4c4e1","html_url":"https://github.com/chronark/zod-bird","commit_stats":{"total_commits":32,"total_committers":9,"mean_commits":"3.5555555555555554","dds":0.28125,"last_synced_commit":"06f4c7f197a19614e11b1eae67e40a591314f68d"},"previous_names":["chronark/zod-bird"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chronark%2Fzod-bird","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chronark%2Fzod-bird/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chronark%2Fzod-bird/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chronark%2Fzod-bird/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chronark","download_url":"https://codeload.github.com/chronark/zod-bird/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198450,"owners_count":20900080,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T12:01:03.672Z","updated_at":"2025-04-04T15:07:50.851Z","avatar_url":"https://github.com/chronark.png","language":"TypeScript","funding_links":[],"categories":["SDKs \u0026 Utilities 🧰"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n    \u003ch1 align=\"center\"\u003e@chronark/zod-bird\u003c/h1\u003e\n    \u003ch5\u003eFully typed Tinybird pipes using zod\u003c/h5\u003e\n\u003c/div\u003e\n\n\u003cbr/\u003e\n\n- typesafe\n- handles building the url params for you\n- easy transformation of resulting data\n- built in cache directives for nextjs\n- built in retry logic for ratelimited requests\n\n\n```ts\nimport { Tinybird } from \"@chronark/zod-bird\";\nimport { z } from \"zod\";\n\nconst tb = new Tinybird({ token: \"token\" });\n\nexport const getEvents = tb.buildPipe({\n  pipe: \"get_events__v1\",\n  parameters: z.object({\n    tenantId: z.string(),\n  }),\n  data: z.object({\n    event: z.string(),\n    time: z.number().transform((t) =\u003e new Date(t)),\n  }),\n});\n\n\nconst res = await getEvents({ tenantId: \"chronark\" })\n\n// res.data = {event: string, time: Date}[]\n// if no rows found, res.data will be an empty array\n```\n\n## Install\n\n```\nnpm i @chronark/zod-bird\n```\n\n\n## Ingesting Data\n\n```ts\n// lib/tinybird.ts\nimport { Tinybird } from \"./client\";\nimport { z } from \"zod\";\n\nconst tb = new Tinybird({ token: process.env.TINYBIRD_TOKEN! });\n\nexport const publishEvent = tb.buildIngestEndpoint({\n  datasource: \"events__v1\",\n  event: z.object({\n    id: z.string(),\n    tenantId: z.string(),\n    channelId: z.string(),\n    time: z.number().int(),\n    event: z.string(),\n    content: z.string().optional().default(\"\"),\n    metadata: z.string().optional().default(\"\"),\n  }),\n});\n```\n\n```ts\n// somewhere.ts\nimport { publishEvent } from \"./lib/tinybird\";\n\nawait publishEvent({\n  id: \"1\",\n  tenantId: \"1\",\n  channelId: \"1\",\n  time: Date.now(),\n  event: \"test\",\n  content: \"test\",\n  metadata: JSON.stringify({ test: \"test\" }),\n});\n```\n\nOr multiple events in bulk, using a single request to Tinybird.\n\n```ts\n// somewhere.ts\nimport { publishEvent } from \"./lib/tinybird\";\n\nawait publishEvent([\n  {\n    id: \"1\",\n    tenantId: \"1\",\n    channelId: \"1\",\n    time: Date.now(),\n    event: \"test\",\n    content: \"test\",\n    metadata: JSON.stringify({ test: \"test\" }),\n  },\n  {\n    id: \"2\",\n    tenantId: \"2\",\n    channelId: \"2\",\n    time: Date.now(),\n    event: \"test2\",\n    content: \"test2\",\n    metadata: JSON.stringify({ test2: \"test2\" }),\n  },\n]);\n```\n\n\n\n## Querying Endpoints\n\n```ts\n// lib/tinybird.ts\nimport { Tinybird } from \"./client\";\nimport { z } from \"zod\";\n\nconst tb = new Tinybird({ token: process.env.TINYBIRD_TOKEN! });\n\nexport const getChannelActivity = tb.buildPipe({\n  pipe: \"get_channel_activity__v1\",\n  parameters: z.object({\n    tenantId: z.string(),\n    channelId: z.string().optional(),\n    start: z.number(),\n    end: z.number().optional(),\n    granularity: z.enum([\"1m\", \"1h\", \"1d\", \"1w\", \"1M\"]),\n  }),\n  data: z.object({\n    time: z.string().transform((t) =\u003e new Date(t).getTime()),\n    count: z\n      .number()\n      .nullable()\n      .optional()\n      .transform((v) =\u003e (typeof v === \"number\" ? v : 0)),\n  }),\n});\n```\n\n```ts\n// somewhere.ts\nimport { getChannelActivity } from \"./lib/tinybird\";\n\n\nconst res = await getChannelActivity({\n   tenantId: \"1\",\n   channelId: \"1\",\n   start: 123,\n   end: 1234,\n   granularity: \"1h\"\n})\n\n```\n`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`.\n\n## Error handling\n\nFor `429` (rate limit exceeded) and `500` errors, `zod-bird` automatically retries them ([source code](https://github.com/chronark/zod-bird/blob/52944e5aebcfb547f9ccaf1e3fca03b158953e8f/src/client.ts#L45-L60)).\n\nAll other errors will throw an `Exception`, so you should catch it via a try catch:\n\n```typescript\ntry {\n    const res = await getEvents({ tenantId: \"chronark\" })\n    return res.data\n} catch (e) {\n    console.error(e)\n    return e.messsage\n}\n```\n\n## Advanced\n\n### Caching\n\nYou can add cache directives to each pipe.\n\n\n#### Disable caching (useful in Next.js where everything is cached by default)\n\n```ts\ntb.buildPipe({\n  pipe: \"some_pipe__v1\",\n  parameters: z.object({\n   hello: z.string()\n  }),\n  data: z.object({\n    response: z.string()\n  }),\n   opts: {\n      cache: \"no-store\" // \u003c-------- Add this\n   };\n});\n\n```\n\n#### Cache for 15 minutes\n\n```ts\n\ntb.buildPipe({\n  pipe: \"some_pipe__v1\",\n  parameters: z.object({\n   hello: z.string()\n  }),\n  data: z.object({\n    response: z.string()\n  }),\n   opts: {\n      next: {\n        revalidate: 900 ;// \u003c-------- Add this\n      }\n    };\n});\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchronark%2Fzod-bird","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchronark%2Fzod-bird","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchronark%2Fzod-bird/lists"}