refactor(api): ♻️ Move from @hono/zod-openapi to hono-openapi

hono-openapi is easier to work with and generates better OpenAPI definitions
This commit is contained in:
Jesse Wierzbinski 2025-03-29 03:30:06 +01:00
parent 0576aff972
commit 58342e86e1
No known key found for this signature in database
240 changed files with 9494 additions and 9575 deletions

View file

@ -1,113 +0,0 @@
import { apiRoute } from "@/api";
import { createRoute, z } from "@hono/zod-openapi";
import type { Entity } from "@versia/federation/types";
import { ApiError } from "~/classes/errors/api-error";
import { InboxJobType, inboxQueue } from "~/classes/queues/inbox";
const schemas = {
param: z.object({
uuid: z.string().uuid(),
}),
header: z.object({
"versia-signature": z.string().optional(),
"versia-signed-at": z.coerce.number().optional(),
"versia-signed-by": z
.string()
.url()
.or(z.string().startsWith("instance "))
.optional(),
authorization: z.string().optional(),
}),
body: z.any(),
};
const route = createRoute({
method: "post",
path: "/users/{uuid}/inbox",
summary: "Receive federation inbox",
tags: ["Federation"],
request: {
params: schemas.param,
headers: schemas.header,
body: {
content: {
"application/json": {
schema: schemas.body,
},
},
},
},
responses: {
200: {
description: "Request processed",
},
201: {
description: "Request accepted",
},
400: {
description: "Bad request",
content: {
"application/json": {
schema: ApiError.zodSchema,
},
},
},
401: {
description: "Signature could not be verified",
content: {
"application/json": {
schema: ApiError.zodSchema,
},
},
},
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: ApiError.zodSchema,
},
},
},
404: {
description: "Not found",
content: {
"application/json": {
schema: ApiError.zodSchema,
},
},
},
500: {
description: "Internal server error",
content: {
"application/json": {
schema: z.object({
error: z.string(),
message: z.string(),
}),
},
},
},
},
});
export default apiRoute((app) =>
app.openapi(route, async (context) => {
const body: Entity = await context.req.valid("json");
await inboxQueue.add(InboxJobType.ProcessEntity, {
data: body,
headers: context.req.valid("header"),
request: {
body: await context.req.text(),
method: context.req.method,
url: context.req.url,
},
ip: context.env.ip ?? null,
});
return context.body(
"Request processing initiated.\nImplement the Instance Messaging Extension to receive any eventual feedback (errors, etc.)",
200,
);
}),
);

View file

@ -1,75 +0,0 @@
import { apiRoute } from "@/api";
import { createRoute, z } from "@hono/zod-openapi";
import { User as UserSchema } from "@versia/federation/schemas";
import { User } from "@versia/kit/db";
import { ApiError } from "~/classes/errors/api-error";
const schemas = {
param: z.object({
uuid: z.string().uuid(),
}),
};
const route = createRoute({
method: "get",
path: "/users/{uuid}",
summary: "Get user data",
request: {
params: schemas.param,
},
tags: ["Federation"],
responses: {
200: {
description: "User data",
content: {
"application/json": {
schema: UserSchema,
},
},
},
301: {
description:
"Redirect to user profile (for web browsers). Uses user-agent for detection.",
},
404: ApiError.accountNotFound().schema,
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: ApiError.zodSchema,
},
},
},
},
});
export default apiRoute((app) =>
app.openapi(route, async (context) => {
const { uuid } = context.req.valid("param");
const user = await User.fromId(uuid);
if (!user) {
throw ApiError.accountNotFound();
}
if (user.isRemote()) {
throw new ApiError(403, "User is not on this instance");
}
// Try to detect a web browser and redirect to the user's profile page
if (context.req.header("user-agent")?.includes("Mozilla")) {
return context.redirect(user.toApi().url);
}
const userJson = user.toVersia();
const { headers } = await user.sign(
userJson,
new URL(context.req.url),
"GET",
);
return context.json(userJson, 200, headers.toJSON());
}),
);

View file

@ -1,135 +0,0 @@
import { apiRoute } from "@/api";
import { createRoute, z } from "@hono/zod-openapi";
import {
Collection as CollectionSchema,
Note as NoteSchema,
} from "@versia/federation/schemas";
import { Note, User, db } from "@versia/kit/db";
import { Notes } from "@versia/kit/tables";
import { and, eq, inArray } from "drizzle-orm";
import { ApiError } from "~/classes/errors/api-error";
import { config } from "~/config.ts";
const schemas = {
param: z.object({
uuid: z.string().uuid(),
}),
query: z.object({
page: z.string().optional(),
}),
};
const route = createRoute({
method: "get",
path: "/users/{uuid}/outbox",
summary: "Get user outbox",
request: {
params: schemas.param,
query: schemas.query,
},
tags: ["Federation"],
responses: {
200: {
description: "User outbox",
content: {
"application/json": {
schema: CollectionSchema.extend({
items: z.array(NoteSchema),
}),
},
},
},
404: {
description: "User not found",
content: {
"application/json": {
schema: ApiError.zodSchema,
},
},
},
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: ApiError.zodSchema,
},
},
},
},
});
const NOTES_PER_PAGE = 20;
export default apiRoute((app) =>
app.openapi(route, async (context) => {
const { uuid } = context.req.valid("param");
const author = await User.fromId(uuid);
if (!author) {
throw new ApiError(404, "User not found");
}
if (author.isRemote()) {
throw new ApiError(403, "User is not on this instance");
}
const pageNumber = Number(context.req.valid("query").page) || 1;
const notes = await Note.manyFromSql(
and(
eq(Notes.authorId, uuid),
inArray(Notes.visibility, ["public", "unlisted"]),
),
undefined,
NOTES_PER_PAGE,
NOTES_PER_PAGE * (pageNumber - 1),
);
const totalNotes = await db.$count(
Notes,
and(
eq(Notes.authorId, uuid),
inArray(Notes.visibility, ["public", "unlisted"]),
),
);
const json = {
first: new URL(
`/users/${uuid}/outbox?page=1`,
config.http.base_url,
).toString(),
last: new URL(
`/users/${uuid}/outbox?page=${Math.ceil(
totalNotes / NOTES_PER_PAGE,
)}`,
config.http.base_url,
).toString(),
total: totalNotes,
author: author.getUri().toString(),
next:
notes.length === NOTES_PER_PAGE
? new URL(
`/users/${uuid}/outbox?page=${pageNumber + 1}`,
config.http.base_url,
).toString()
: null,
previous:
pageNumber > 1
? new URL(
`/users/${uuid}/outbox?page=${pageNumber - 1}`,
config.http.base_url,
).toString()
: null,
items: notes.map((note) => note.toVersia()),
};
const { headers } = await author.sign(
json,
new URL(context.req.url),
"GET",
);
return context.json(json, 200, headers.toJSON());
}),
);

View file

@ -0,0 +1,111 @@
import { apiRoute, handleZodError } from "@/api";
import type { Entity } from "@versia/federation/types";
import { describeRoute } from "hono-openapi";
import { resolver, validator } from "hono-openapi/zod";
import { z } from "zod";
import { ApiError } from "~/classes/errors/api-error";
import { InboxJobType, inboxQueue } from "~/classes/queues/inbox";
export default apiRoute((app) =>
app.post(
"/users/:uuid/inbox",
describeRoute({
summary: "Receive federation inbox",
tags: ["Federation"],
responses: {
200: {
description: "Request processed",
},
201: {
description: "Request accepted",
},
400: {
description: "Bad request",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
401: {
description: "Signature could not be verified",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
404: {
description: "Not found",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
500: {
description: "Internal server error",
content: {
"application/json": {
schema: resolver(
z.object({
error: z.string(),
message: z.string(),
}),
),
},
},
},
},
}),
validator(
"param",
z.object({
uuid: z.string().uuid(),
}),
handleZodError,
),
validator(
"header",
z.object({
"versia-signature": z.string().optional(),
"versia-signed-at": z.coerce.number().optional(),
"versia-signed-by": z
.string()
.url()
.or(z.string().startsWith("instance "))
.optional(),
authorization: z.string().optional(),
}),
handleZodError,
),
validator("json", z.any(), handleZodError),
async (context) => {
const body: Entity = await context.req.valid("json");
await inboxQueue.add(InboxJobType.ProcessEntity, {
data: body,
headers: context.req.valid("header"),
request: {
body: await context.req.text(),
method: context.req.method,
url: context.req.url,
},
ip: context.env.ip ?? null,
});
return context.body(
"Request processing initiated.\nImplement the Instance Messaging Extension to receive any eventual feedback (errors, etc.)",
200,
);
},
),
);

75
api/users/[uuid]/index.ts Normal file
View file

@ -0,0 +1,75 @@
import { apiRoute, handleZodError } from "@/api";
import { User as UserSchema } from "@versia/federation/schemas";
import { User } from "@versia/kit/db";
import { describeRoute } from "hono-openapi";
import { resolver, validator } from "hono-openapi/zod";
import { z } from "zod";
import { ApiError } from "~/classes/errors/api-error";
export default apiRoute((app) =>
app.get(
"/users/{uuid}",
describeRoute({
summary: "Get user data",
tags: ["Federation"],
responses: {
200: {
description: "User data",
content: {
"application/json": {
schema: resolver(UserSchema),
},
},
},
301: {
description:
"Redirect to user profile (for web browsers). Uses user-agent for detection.",
},
404: ApiError.accountNotFound().schema,
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
},
}),
validator(
"param",
z.object({
uuid: z.string().uuid(),
}),
handleZodError,
),
async (context) => {
const { uuid } = context.req.valid("param");
const user = await User.fromId(uuid);
if (!user) {
throw ApiError.accountNotFound();
}
if (user.isRemote()) {
throw new ApiError(403, "User is not on this instance");
}
// Try to detect a web browser and redirect to the user's profile page
if (context.req.header("user-agent")?.includes("Mozilla")) {
return context.redirect(user.toApi().url);
}
const userJson = user.toVersia();
const { headers } = await user.sign(
userJson,
new URL(context.req.url),
"GET",
);
return context.json(userJson, 200, headers.toJSON());
},
),
);

View file

@ -0,0 +1,138 @@
import { apiRoute, handleZodError } from "@/api";
import {
Collection as CollectionSchema,
Note as NoteSchema,
} from "@versia/federation/schemas";
import { Note, User, db } from "@versia/kit/db";
import { Notes } from "@versia/kit/tables";
import { and, eq, inArray } from "drizzle-orm";
import { describeRoute } from "hono-openapi";
import { resolver, validator } from "hono-openapi/zod";
import { z } from "zod";
import { ApiError } from "~/classes/errors/api-error";
import { config } from "~/config.ts";
const NOTES_PER_PAGE = 20;
export default apiRoute((app) =>
app.get(
"/users/:uuid/outbox",
describeRoute({
summary: "Get user outbox",
tags: ["Federation"],
responses: {
200: {
description: "User outbox",
content: {
"application/json": {
schema: CollectionSchema.extend({
items: z.array(NoteSchema),
}),
},
},
},
404: {
description: "User not found",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
},
}),
validator(
"param",
z.object({
uuid: z.string().uuid(),
}),
handleZodError,
),
validator(
"query",
z.object({
page: z.string().optional(),
}),
handleZodError,
),
async (context) => {
const { uuid } = context.req.valid("param");
const author = await User.fromId(uuid);
if (!author) {
throw new ApiError(404, "User not found");
}
if (author.isRemote()) {
throw new ApiError(403, "User is not on this instance");
}
const pageNumber = Number(context.req.valid("query").page) || 1;
const notes = await Note.manyFromSql(
and(
eq(Notes.authorId, uuid),
inArray(Notes.visibility, ["public", "unlisted"]),
),
undefined,
NOTES_PER_PAGE,
NOTES_PER_PAGE * (pageNumber - 1),
);
const totalNotes = await db.$count(
Notes,
and(
eq(Notes.authorId, uuid),
inArray(Notes.visibility, ["public", "unlisted"]),
),
);
const json = {
first: new URL(
`/users/${uuid}/outbox?page=1`,
config.http.base_url,
).toString(),
last: new URL(
`/users/${uuid}/outbox?page=${Math.ceil(
totalNotes / NOTES_PER_PAGE,
)}`,
config.http.base_url,
).toString(),
total: totalNotes,
author: author.getUri().toString(),
next:
notes.length === NOTES_PER_PAGE
? new URL(
`/users/${uuid}/outbox?page=${pageNumber + 1}`,
config.http.base_url,
).toString()
: null,
previous:
pageNumber > 1
? new URL(
`/users/${uuid}/outbox?page=${pageNumber - 1}`,
config.http.base_url,
).toString()
: null,
items: notes.map((note) => note.toVersia()),
};
const { headers } = await author.sign(
json,
new URL(context.req.url),
"GET",
);
return context.json(json, 200, headers.toJSON());
},
),
);