@chronark/zod-bird
Advanced tools
Comparing version 0.3.1 to 0.3.3-canary.0
@@ -65,14 +65,21 @@ import { z } from 'zod'; | ||
type Config = { | ||
baseUrl?: string; | ||
} & ({ | ||
token: string; | ||
noop?: never; | ||
} | { | ||
token?: never; | ||
noop: true; | ||
}); | ||
declare class Tinybird { | ||
private readonly baseUrl; | ||
private readonly token; | ||
constructor(opts: { | ||
token: string; | ||
baseUrl?: string; | ||
}); | ||
private readonly noop; | ||
constructor(config: Config); | ||
private fetch; | ||
buildPipe<TParameters extends Record<string, unknown>, TData extends Record<string, unknown>>(req: { | ||
buildPipe<TParameters extends z.ZodSchema<any>, TData extends z.ZodSchema<any>>(req: { | ||
pipe: string; | ||
parameters?: z.ZodSchema<TParameters>; | ||
data: z.ZodSchema<TData, any, any>; | ||
parameters?: TParameters; | ||
data: TData; | ||
opts?: { | ||
@@ -85,9 +92,9 @@ cache?: RequestCache; | ||
}; | ||
}): (params: TParameters) => Promise<z.infer<typeof pipeResponseWithoutData> & { | ||
data: TData[]; | ||
}): (params: z.input<TParameters>) => Promise<z.infer<typeof pipeResponseWithoutData> & { | ||
data: z.output<TData>[]; | ||
}>; | ||
buildIngestEndpoint<TOutput extends Record<string, unknown>, TInput = TOutput>(req: { | ||
buildIngestEndpoint<TSchema extends z.ZodSchema<any>>(req: { | ||
datasource: string; | ||
event: z.ZodSchema<TOutput, z.ZodTypeDef, TInput>; | ||
}): (events: TInput | TInput[]) => Promise<z.infer<typeof eventIngestReponseData>>; | ||
event: TSchema; | ||
}): (events: z.input<TSchema> | z.input<TSchema>[]) => Promise<z.infer<typeof eventIngestReponseData>>; | ||
} | ||
@@ -98,24 +105,6 @@ | ||
*/ | ||
declare class NoopTinybird { | ||
private fetch; | ||
buildPipe<TParameters extends Record<string, unknown>, TData extends Record<string, unknown>>(req: { | ||
pipe: string; | ||
parameters?: z.ZodSchema<TParameters>; | ||
data: z.ZodSchema<TData, any, any>; | ||
opts?: { | ||
cache?: RequestCache; | ||
/** | ||
* Number of seconds to revalidate the cache | ||
*/ | ||
revalidate?: number; | ||
}; | ||
}): (params: TParameters) => Promise<z.infer<typeof pipeResponseWithoutData> & { | ||
data: TData[]; | ||
}>; | ||
buildIngestEndpoint<TOutput extends Record<string, unknown>, TInput = TOutput>(req: { | ||
datasource: string; | ||
event: z.ZodSchema<TOutput, z.ZodTypeDef, TInput>; | ||
}): (events: TInput | TInput[]) => Promise<z.infer<typeof eventIngestReponseData>>; | ||
declare class NoopTinybird extends Tinybird { | ||
constructor(); | ||
} | ||
export { NoopTinybird, Tinybird }; | ||
export { Config, NoopTinybird, Tinybird }; |
@@ -28,2 +28,5 @@ "use strict"; | ||
// src/client.ts | ||
var import_zod2 = require("zod"); | ||
// src/util.ts | ||
@@ -51,9 +54,15 @@ var import_zod = require("zod"); | ||
// src/client.ts | ||
var import_zod2 = require("zod"); | ||
var Tinybird = class { | ||
baseUrl; | ||
token; | ||
constructor(opts) { | ||
this.baseUrl = opts.baseUrl ?? "https://api.tinybird.co"; | ||
this.token = opts.token; | ||
noop; | ||
constructor(config) { | ||
this.baseUrl = config.baseUrl ?? "https://api.tinybird.co"; | ||
if (config.noop) { | ||
this.token = ""; | ||
this.noop = true; | ||
} else { | ||
this.token = config.token; | ||
this.noop = false; | ||
} | ||
} | ||
@@ -95,2 +104,5 @@ async fetch(pipe, parameters = {}, opts) { | ||
} | ||
if (this.noop) { | ||
return { meta: [], data: [] }; | ||
} | ||
const res = await this.fetch(req.pipe, validatedParams, req.opts); | ||
@@ -114,2 +126,8 @@ const validatedResponse = outputSchema.safeParse(res); | ||
} | ||
if (this.noop) { | ||
return { | ||
successful_rows: Array.isArray(validatedEvents) ? validatedEvents.length : 1, | ||
quarantined_rows: 0 | ||
}; | ||
} | ||
const url = new URL("/v0/events", this.baseUrl); | ||
@@ -156,47 +174,6 @@ url.searchParams.set("name", req.datasource); | ||
// src/noop.ts | ||
var import_zod3 = require("zod"); | ||
var NoopTinybird = class { | ||
async fetch() { | ||
return { | ||
meta: [], | ||
data: [] | ||
}; | ||
var NoopTinybird = class extends Tinybird { | ||
constructor() { | ||
super({ noop: true }); | ||
} | ||
buildPipe(req) { | ||
const outputSchema = pipeResponseWithoutData.setKey("data", import_zod3.z.array(req.data)); | ||
return async (params) => { | ||
let validatedParams = void 0; | ||
if (req.parameters) { | ||
const v = req.parameters.safeParse(params); | ||
if (!v.success) { | ||
throw new Error(v.error.message); | ||
} | ||
validatedParams = v.data; | ||
} | ||
const res = await this.fetch(); | ||
const validatedResponse = outputSchema.safeParse(res); | ||
if (!validatedResponse.success) { | ||
throw new Error(validatedResponse.error.message); | ||
} | ||
return validatedResponse.data; | ||
}; | ||
} | ||
buildIngestEndpoint(req) { | ||
return async (events) => { | ||
let validatedEvents = void 0; | ||
if (req.event) { | ||
const v = req.event.safeParse(events); | ||
if (!v.success) { | ||
throw new Error(v.error.message); | ||
} | ||
validatedEvents = v.data; | ||
} | ||
const res = await this.fetch(); | ||
const validatedResponse = eventIngestReponseData.safeParse(res); | ||
if (!validatedResponse.success) { | ||
throw new Error(validatedResponse.error.message); | ||
} | ||
return validatedResponse.data; | ||
}; | ||
} | ||
}; | ||
@@ -203,0 +180,0 @@ // Annotate the CommonJS export names for ESM import in node: |
{ | ||
"name": "@chronark/zod-bird", | ||
"version": "0.3.1", | ||
"version": "0.3.3-canary.0", | ||
"description": "", | ||
@@ -12,4 +12,4 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@biomejs/biome": "^1.4.1", | ||
"@types/node": "^20.2.1", | ||
"rome": "^12.1.2", | ||
"tsup": "^6.2.3", | ||
@@ -25,4 +25,4 @@ "tsx": "^3.10.1", | ||
"build": "tsup", | ||
"fmt": "rome check --apply-unsafe ./src && rome format . --write" | ||
"fmt": "pnpm biome format . --write && pnpm biome check . --apply-unsafe " | ||
} | ||
} |
@@ -0,11 +1,31 @@ | ||
import { z } from "zod"; | ||
import { type PipeErrorResponse, eventIngestReponseData, pipeResponseWithoutData } from "./util"; | ||
import { z } from "zod"; | ||
export type Config = { | ||
baseUrl?: string; | ||
} & ( | ||
| { | ||
token: string; | ||
noop?: never; | ||
} | ||
| { | ||
token?: never; | ||
noop: true; | ||
} | ||
); | ||
export class Tinybird { | ||
private readonly baseUrl: string; | ||
private readonly token: string; | ||
private readonly noop: boolean; | ||
constructor(opts: { token: string; baseUrl?: string }) { | ||
this.baseUrl = opts.baseUrl ?? "https://api.tinybird.co"; | ||
this.token = opts.token; | ||
constructor(config: Config) { | ||
this.baseUrl = config.baseUrl ?? "https://api.tinybird.co"; | ||
if (config.noop) { | ||
this.token = ""; | ||
this.noop = true; | ||
} else { | ||
this.token = config.token; | ||
this.noop = false; | ||
} | ||
} | ||
@@ -44,10 +64,6 @@ | ||
public buildPipe< | ||
TParameters extends Record<string, unknown>, | ||
TData extends Record<string, unknown>, | ||
>(req: { | ||
public buildPipe<TParameters extends z.ZodSchema<any>, TData extends z.ZodSchema<any>>(req: { | ||
pipe: string; | ||
parameters?: z.ZodSchema<TParameters>; | ||
// rome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
data: z.ZodSchema<TData, any, any>; | ||
parameters?: TParameters; | ||
data: TData; | ||
opts?: { | ||
@@ -61,7 +77,7 @@ cache?: RequestCache; | ||
}): ( | ||
params: TParameters, | ||
) => Promise<z.infer<typeof pipeResponseWithoutData> & { data: TData[] }> { | ||
params: z.input<TParameters>, | ||
) => Promise<z.infer<typeof pipeResponseWithoutData> & { data: z.output<TData>[] }> { | ||
const outputSchema = pipeResponseWithoutData.setKey("data", z.array(req.data)); | ||
return async (params: TParameters) => { | ||
let validatedParams: TParameters | undefined = undefined; | ||
return async (params: z.input<TParameters>) => { | ||
let validatedParams: z.input<TParameters> | undefined = undefined; | ||
if (req.parameters) { | ||
@@ -74,3 +90,5 @@ const v = req.parameters.safeParse(params); | ||
} | ||
if (this.noop) { | ||
return { meta: [], data: [] }; | ||
} | ||
const res = await this.fetch(req.pipe, validatedParams, req.opts); | ||
@@ -86,8 +104,10 @@ const validatedResponse = outputSchema.safeParse(res); | ||
public buildIngestEndpoint<TOutput extends Record<string, unknown>, TInput = TOutput>(req: { | ||
public buildIngestEndpoint<TSchema extends z.ZodSchema<any>>(req: { | ||
datasource: string; | ||
event: z.ZodSchema<TOutput, z.ZodTypeDef, TInput>; | ||
}): (events: TInput | TInput[]) => Promise<z.infer<typeof eventIngestReponseData>> { | ||
return async (events: TInput | TInput[]) => { | ||
let validatedEvents: TOutput | TOutput[] | undefined = undefined; | ||
event: TSchema; | ||
}): ( | ||
events: z.input<TSchema> | z.input<TSchema>[], | ||
) => Promise<z.infer<typeof eventIngestReponseData>> { | ||
return async (events: z.input<TSchema> | z.input<TSchema>[]) => { | ||
let validatedEvents: z.output<TSchema> | z.output<TSchema>[] | undefined = undefined; | ||
if (req.event) { | ||
@@ -103,2 +123,8 @@ const v = Array.isArray(events) | ||
if (this.noop) { | ||
return { | ||
successful_rows: Array.isArray(validatedEvents) ? validatedEvents.length : 1, | ||
quarantined_rows: 0, | ||
}; | ||
} | ||
const url = new URL("/v0/events", this.baseUrl); | ||
@@ -105,0 +131,0 @@ url.searchParams.set("name", req.datasource); |
@@ -89,5 +89,3 @@ /** | ||
event: z.object({ | ||
actor: z | ||
.object({ id: z.string(), name: z.string() }) | ||
.transform((val) => JSON.stringify(val)), | ||
actor: z.object({ id: z.string(), name: z.string() }).transform((val) => JSON.stringify(val)), | ||
}), | ||
@@ -94,0 +92,0 @@ }); |
@@ -1,80 +0,9 @@ | ||
import { z } from "zod"; | ||
import { eventIngestReponseData, pipeResponseWithoutData } from "./util"; | ||
import { Tinybird } from "./client"; | ||
/** | ||
* NoopTinybird is a mock implementation of the Tinybird client that doesn't do anything and returns empty data. | ||
*/ | ||
export class NoopTinybird { | ||
private async fetch() { | ||
return { | ||
meta: [], | ||
data: [], | ||
}; | ||
export class NoopTinybird extends Tinybird { | ||
constructor() { | ||
super({ noop: true }); | ||
} | ||
public buildPipe< | ||
TParameters extends Record<string, unknown>, | ||
TData extends Record<string, unknown>, | ||
>(req: { | ||
pipe: string; | ||
parameters?: z.ZodSchema<TParameters>; | ||
// rome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
data: z.ZodSchema<TData, any, any>; | ||
opts?: { | ||
cache?: RequestCache; | ||
/** | ||
* Number of seconds to revalidate the cache | ||
*/ | ||
revalidate?: number; | ||
}; | ||
}): ( | ||
params: TParameters, | ||
) => Promise<z.infer<typeof pipeResponseWithoutData> & { data: TData[] }> { | ||
const outputSchema = pipeResponseWithoutData.setKey("data", z.array(req.data)); | ||
return async (params: TParameters) => { | ||
let validatedParams: TParameters | undefined = undefined; | ||
if (req.parameters) { | ||
const v = req.parameters.safeParse(params); | ||
if (!v.success) { | ||
throw new Error(v.error.message); | ||
} | ||
validatedParams = v.data; | ||
} | ||
const res = await this.fetch(); | ||
const validatedResponse = outputSchema.safeParse(res); | ||
if (!validatedResponse.success) { | ||
throw new Error(validatedResponse.error.message); | ||
} | ||
return validatedResponse.data; | ||
}; | ||
} | ||
public buildIngestEndpoint<TOutput extends Record<string, unknown>, TInput = TOutput>(req: { | ||
datasource: string; | ||
event: z.ZodSchema<TOutput, z.ZodTypeDef, TInput>; | ||
}): (events: TInput | TInput[]) => Promise<z.infer<typeof eventIngestReponseData>> { | ||
return async (events: TInput | TInput[]) => { | ||
let validatedEvents: TOutput | TOutput[] | undefined = undefined; | ||
if (req.event) { | ||
const v = req.event.safeParse(events); | ||
if (!v.success) { | ||
throw new Error(v.error.message); | ||
} | ||
validatedEvents = v.data; | ||
} | ||
const res = await this.fetch(); | ||
const validatedResponse = eventIngestReponseData.safeParse(res); | ||
if (!validatedResponse.success) { | ||
throw new Error(validatedResponse.error.message); | ||
} | ||
return validatedResponse.data; | ||
}; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60367
854