Skip to content

Commit

Permalink
fix: noop types
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Jan 8, 2024
1 parent 6ee27f1 commit 393ab98
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 144 deletions.
36 changes: 36 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "https://biomejs.dev/schemas/1.0.0/schema.json",
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"a11y": {
"noSvgWithoutTitle": "off"
},
"correctness": {
"noUnusedVariables": "warn"
},
"security": {
"noDangerouslySetInnerHtml": "off"
},
"style": {
"useBlockStatements": "error",
"noNonNullAssertion": "off"
},
"performance": {
"noDelete": "off"
},
"suspicious": {
"noExplicitAny": "off"
}
},
"ignore": ["node_modules", "dist"]
},
"formatter": {
"indentStyle": "space",
"indentWidth": 2,
"enabled": true,
"lineWidth": 100,
"ignore": ["node_modules", "dist"]
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
"scripts": {
"test": "node --loader=tsx --test ./**/*.test.ts ",
"build": "tsup",
"fmt": "rome check --apply-unsafe ./src && rome format . --write"
"fmt": "pnpm biome format . --write && pnpm biome check . --apply-unsafe "
},
"keywords": [],
"author": "Andreas Thomas",
"license": "ISC",
"devDependencies": {
"@biomejs/biome": "^1.4.1",
"@types/node": "^20.2.1",
"rome": "^12.1.2",
"tsup": "^6.2.3",
"tsx": "^3.10.1",
"typescript": "^5.0.4"
},
"dependencies": {
"zod": "^3.21.4"
}
}
}
136 changes: 71 additions & 65 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 0 additions & 29 deletions rome.json

This file was deleted.

50 changes: 38 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import { type PipeErrorResponse, eventIngestReponseData, pipeResponseWithoutData } from "./util";
import { z } from "zod";
import { type PipeErrorResponse, eventIngestReponseData, pipeResponseWithoutData } from "./util";

export type Config = {
baseUrl?: string;
} & (
| {
token: string;
noop?: never;
}
| {
token?: never;
noop: true;
}
);

export class Tinybird {
private readonly baseUrl: string;
private readonly token: string;

constructor(opts: { token: string; baseUrl?: string }) {
this.baseUrl = opts.baseUrl ?? "https://api.tinybird.co";
this.token = opts.token;
private readonly noop: boolean;

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;
}
}

private async fetch(
Expand Down Expand Up @@ -41,13 +61,9 @@ export class Tinybird {
return body;
}

public buildPipe<
TParameters extends z.ZodSchema<any>,
TData extends z.ZodSchema<any>,
>(req: {
public buildPipe<TParameters extends z.ZodSchema<any>, TData extends z.ZodSchema<any>>(req: {
pipe: string;
parameters?: TParameters;
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
data: TData;
opts?: {
cache?: RequestCache;
Expand All @@ -69,7 +85,9 @@ export class Tinybird {
}
validatedParams = v.data;
}

if (this.noop) {
return { meta: [], data: [] };
}
const res = await this.fetch(req.pipe, validatedParams, req.opts);
const validatedResponse = outputSchema.safeParse(res);
if (!validatedResponse.success) {
Expand All @@ -83,7 +101,9 @@ export class Tinybird {
public buildIngestEndpoint<TSchema extends z.ZodSchema<any>>(req: {
datasource: string;
event: TSchema;
}): (events: z.input<TSchema> | z.input<TSchema>[]) => Promise<z.infer<typeof eventIngestReponseData>> {
}): (
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) {
Expand All @@ -96,6 +116,12 @@ export class Tinybird {
validatedEvents = v.data;
}

if (this.noop) {
return {
successful_rows: Array.isArray(validatedEvents) ? validatedEvents.length : 1,
quarantined_rows: 0,
};
}
const url = new URL("/v0/events", this.baseUrl);
url.searchParams.set("name", req.datasource);

Expand Down
4 changes: 1 addition & 3 deletions src/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ export const publishEvents = tb.buildIngestEndpoint({
export const publishTransformedEvents = tb.buildIngestEndpoint({
datasource: "events__v1",
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)),
}),
});

Expand Down
Loading

0 comments on commit 393ab98

Please sign in to comment.