Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/great-beers-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@farbenmeer/bunny": patch
"@farbenmeer/tapi": patch
---

Add generateOpenAPISchema method to TAPI and serve it as /openapi.json from bunny
5 changes: 5 additions & 0 deletions .changeset/shy-cars-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farbenmeer/tapi": patch
---

fix GetRoute type
5 changes: 5 additions & 0 deletions .changeset/tangy-shrimps-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farbenmeer/react-tapi": patch
---

useQuery returns a lacy promise
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: CI

on:
pull_request:
pull_request_review:
types: [submitted]

jobs:
unit-tests:
Expand All @@ -14,4 +16,5 @@ jobs:
bun-version: latest

- run: bun install
- run: bun run build
- run: bun run test
23 changes: 18 additions & 5 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"packages/1-bun-auth": {
"name": "@farbenmeer/bun-auth",
"version": "0.1.3",
"version": "0.1.4",
"dependencies": {
"oauth4webapi": "^3.8.1",
},
Expand Down Expand Up @@ -51,7 +51,10 @@
},
"packages/1-tapi": {
"name": "@farbenmeer/tapi",
"version": "0.1.8",
"version": "0.1.9",
"dependencies": {
"zod-openapi": "^5.4.0",
},
"devDependencies": {
"@types/bun": "latest",
"zod": "^4.0.17",
Expand All @@ -66,8 +69,13 @@
"version": "0.1.5",
"devDependencies": {
"@farbenmeer/tapi": "workspace:^",
"@happy-dom/global-registrator": "^18.0.1",
"@testing-library/dom": "^10.4.1",
"@testing-library/jest-dom": "^6.8.0",
"@testing-library/react": "^16.3.0",
"@types/bun": "latest",
"react": "^19.1.1",
"zod": "^4.1.5",
},
"peerDependencies": {
"@farbenmeer/tapi": "workspace:^",
Expand All @@ -77,7 +85,7 @@
},
"packages/3-bunny": {
"name": "@farbenmeer/bunny",
"version": "0.1.3",
"version": "0.1.4",
"bin": {
"bunny": "./dist/cli.js",
"@farbenmeer/bunny": "./dist/cli.js",
Expand All @@ -101,9 +109,8 @@
},
"packages/4-bunny-boilerplate": {
"name": "@farbenmeer/bunny-boilerplate",
"version": "0.1.5",
"version": "0.1.6",
"dependencies": {
"@farbenmeer/bun-auth": "workspace:*",
"@farbenmeer/bunny": "workspace:*",
"drizzle-orm": "^0.44",
"react": "^19",
Expand Down Expand Up @@ -584,8 +591,14 @@

"zod": ["[email protected]", "", {}, "sha512-1PHjlYRevNxxdy2JZ8JcNAw7rX8V9P1AKkP+x/xZfxB0K5FYfuV+Ug6P/6NVSR2jHQ+FzDDoDHS04nYUsOIyLQ=="],

"zod-openapi": ["[email protected]", "", { "peerDependencies": { "zod": "^3.25.74 || ^4.0.0" } }, "sha512-5FdEnJmeNFmSEzfoH10GGF0OQFtSkJCnoXBZR618/Aozv2NF7dhS6y4dj+88rwAgNezbUH+9GJC3QsEiyu3rUQ=="],

"@esbuild-kit/core-utils/esbuild": ["[email protected]", "", { "optionalDependencies": { "@esbuild/android-arm": "0.18.20", "@esbuild/android-arm64": "0.18.20", "@esbuild/android-x64": "0.18.20", "@esbuild/darwin-arm64": "0.18.20", "@esbuild/darwin-x64": "0.18.20", "@esbuild/freebsd-arm64": "0.18.20", "@esbuild/freebsd-x64": "0.18.20", "@esbuild/linux-arm": "0.18.20", "@esbuild/linux-arm64": "0.18.20", "@esbuild/linux-ia32": "0.18.20", "@esbuild/linux-loong64": "0.18.20", "@esbuild/linux-mips64el": "0.18.20", "@esbuild/linux-ppc64": "0.18.20", "@esbuild/linux-riscv64": "0.18.20", "@esbuild/linux-s390x": "0.18.20", "@esbuild/linux-x64": "0.18.20", "@esbuild/netbsd-x64": "0.18.20", "@esbuild/openbsd-x64": "0.18.20", "@esbuild/sunos-x64": "0.18.20", "@esbuild/win32-arm64": "0.18.20", "@esbuild/win32-ia32": "0.18.20", "@esbuild/win32-x64": "0.18.20" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-ceqxoedUrcayh7Y7ZX6NdbbDzGROiyVBgC4PriJThBKSVPWnnFHZAkfI1lJT8QFkOwH4qOS2SJkS4wvpGl8BpA=="],

"@farbenmeer/react-tapi/zod": ["[email protected]", "", {}, "sha512-rcUUZqlLJgBC33IT3PNMgsCq6TzLQEG/Ei/KTCU0PedSWRMAXoOUN+4t/0H+Q8bdnLPdqUYnvboJT0bn/229qg=="],

"@farbenmeer/tapi/zod": ["[email protected]", "", {}, "sha512-rcUUZqlLJgBC33IT3PNMgsCq6TzLQEG/Ei/KTCU0PedSWRMAXoOUN+4t/0H+Q8bdnLPdqUYnvboJT0bn/229qg=="],

"@manypkg/find-root/@types/node": ["@types/[email protected]", "", {}, "sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ=="],

"@manypkg/find-root/fs-extra": ["[email protected]", "", { "dependencies": { "graceful-fs": "^4.2.0", "jsonfile": "^4.0.0", "universalify": "^0.1.0" } }, "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g=="],
Expand Down
10 changes: 8 additions & 2 deletions packages/1-lacy/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { lacy } from "./index";

describe("lacy", () => {
test("Makes a property on an object accessible", async () => {
const eagerPromise = lacy(Promise.resolve({ a: 1 }));
const lacyPromise = lacy(Promise.resolve({ a: 1 }));

expect(await eagerPromise.a).toBe(1);
expect(await lacyPromise.a).toBe(1);
});

test("Works for an array element", async () => {
Expand All @@ -32,4 +32,10 @@ describe("lacy", () => {

expect(await lacyPromise.test).toBeUndefined();
});

test("returns a raw promise when then is called", () => {
const lacyPromise = lacy(Promise.resolve(null));

expect(lacyPromise.then() instanceof Promise).toBeTrue();
});
});
2 changes: 1 addition & 1 deletion packages/1-lacy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type LacyPromise<T, P = never> = T extends (...args: infer A) => infer R
export type LacyPromise<T, P = never> = T extends (...args: infer A) => infer R
? (...args: A) => LacyPromise<R>
: {
[key in Exclude<keyof T, "then" | "catch" | "finally">]: LacyPromise<
Expand Down
2 changes: 1 addition & 1 deletion packages/1-router/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"noEmit": false
},
"exclude": ["**/*.test.tsx"]
"exclude": ["**/*.test.ts", "**/*.test.tsx"]
}
4 changes: 3 additions & 1 deletion packages/1-tapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"typescript": "^5",
"zod": "^4.0.17"
},
"dependencies": {},
"dependencies": {
"zod-openapi": "^5.4.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/farbenmeer/tapi.git"
Expand Down
15 changes: 2 additions & 13 deletions packages/1-tapi/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
import type { Observable } from "./client-types";

export { createFetchClient } from "./create-fetch-client";
export type { Observable } from "./client-types";

export type GetRoute<R, Q = never> = Q extends never
? (query?: {}, req?: RequestInit) => Promise<R> & Observable<R>
: (query: Q, req?: RequestInit) => Promise<R> & Observable<R>;

export type PostRoute<R, Q = unknown, B = unknown> = (
query: Q | FormData,
body: B | FormData,
req?: RequestInit
) => Promise<R>;
export { createFetchClient } from "./create-fetch-client";
export type { GetRoute, PostRoute } from "./route-types";
13 changes: 13 additions & 0 deletions packages/1-tapi/src/client/route-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Observable } from "./client-types";

export type GetRoute<R, Q = undefined> = Q extends undefined
? (query?: {}, req?: RequestInit) => Promise<R> & Observable<R>
: (query: Q, req?: RequestInit) => Promise<R> & Observable<R>;

type T = GetRoute<string>;

export type PostRoute<R, Q = unknown, B = unknown> = (
query: Q | FormData,
body: B | FormData,
req?: RequestInit
) => Promise<R>;
1 change: 1 addition & 0 deletions packages/1-tapi/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { defineHandler } from "./define-handler";
export { HttpError } from "../shared/http-error";
export { TResponse } from "./t-response";
export type { TRequest } from "./t-request";
export { generateOpenAPISchema } from "./openapi";
46 changes: 46 additions & 0 deletions packages/1-tapi/src/server/openapi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, test } from "bun:test";
import { api } from "./define-api.test";
import { generateOpenAPISchema } from "./openapi";

describe("OpenAPI", () => {
test("generate basic OpenAPI schema", async () => {
const schema = await generateOpenAPISchema(api, {
info: { title: "Test API", version: "0.1.0" },
});
expect(schema.info.title).toBe("Test API");
expect(schema.info.version).toBe("0.1.0");
expect(schema.paths?.["/books"].get?.parameters).toEqual([]);

expect(
(schema.paths?.["/books"].post?.requestBody as any).content[
"application/json"
].schema.properties.id.type
).toBe("string");
expect(
(schema.paths?.["/books"].post?.requestBody as any).content[
"application/json"
].schema.properties.title.type
).toBe("string");
expect(
(schema.paths?.["/books"].post?.requestBody as any).content[
"application/json"
].schema.required
).toEqual(["id", "title"]);

expect(schema.paths?.["/authorized"]).toEqual({
get: {
parameters: [],
responses: {
"200": {
description: "200 OK",
content: {
"application/json": {
schema: {},
},
},
},
},
},
});
});
});
65 changes: 65 additions & 0 deletions packages/1-tapi/src/server/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { createDocument, type ZodOpenApiObject } from "zod-openapi";
import { ApiDefinition } from "./define-api";
import type { Path } from "../shared/path";
import type { BaseRoute } from "../shared/route";
import z from "zod/v4";
import type { MaybePromise } from "bun";

interface Options {
info: {
title: string;
version: string;
};
}

export async function generateOpenAPISchema(
apiDefinition: ApiDefinition<Record<Path, MaybePromise<BaseRoute>>>,
options: Options
): Promise<ReturnType<typeof createDocument>> {
return createDocument({
openapi: "3.1.1",
info: options.info,
paths: Object.fromEntries(
await Promise.all(
Object.entries(apiDefinition.routes).map(async ([path, route]) => [
transformPath(path),
Object.fromEntries(
Object.entries(await route).map(([method, handler]) => [
method.toLowerCase(),
{
requestParams: {
path: z.object(handler.schema.params),
query: z.object(handler.schema.query),
},
requestBody: handler.schema.body && {
content: {
"application/json": {
schema: handler.schema.body,
},
},
},
responses: {
"200": {
description: "200 OK",
content: {
"application/json": {
schema: handler.schema.response || z.any(),
},
},
},
},
},
])
),
])
)
),
});
}

function transformPath(path: string) {
return path
.split("/")
.map((segment) => segment.replace(/^\[(.+)\]$/, "{$1}"))
.join("/");
}
2 changes: 2 additions & 0 deletions packages/2-react-tapi/bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
preload = ["./happy-dom.ts", "./testing-library.ts"]
3 changes: 3 additions & 0 deletions packages/2-react-tapi/happy-dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { GlobalRegistrator } from "@happy-dom/global-registrator";

GlobalRegistrator.register();
13 changes: 10 additions & 3 deletions packages/2-react-tapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@
"dist"
],
"scripts": {
"build": "tsc --noEmit false",
"release": "bun run build && bun publish"
"build": "tsc -p tsconfig.build.json",
"release": "bun run build && bun publish",
"test": "bun test"
},
"dependencies": {},
"devDependencies": {
"@farbenmeer/tapi": "workspace:^",
"@farbenmeer/lacy": "workspace:^",
"@happy-dom/global-registrator": "^18.0.1",
"@testing-library/dom": "^10.4.1",
"@testing-library/jest-dom": "^6.8.0",
"@testing-library/react": "^16.3.0",
"@types/bun": "latest",
"react": "^19.1.1",
"@farbenmeer/tapi": "workspace:^"
"zod": "^4.1.5"
},
"peerDependencies": {
"typescript": "^5",
Expand Down
Loading
Loading