refactor(api): ♻️ Properly reuse error messages and schemas

This commit is contained in:
Jesse Wierzbinski 2025-03-24 14:42:09 +01:00
parent 7112a66e4c
commit 65e2e19ff1
No known key found for this signature in database
84 changed files with 478 additions and 597 deletions

View file

@ -1,8 +1,8 @@
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";
import { ErrorSchema } from "~/types/api";
const schemas = {
param: z.object({
@ -47,7 +47,7 @@ const route = createRoute({
description: "Bad request",
content: {
"application/json": {
schema: ErrorSchema,
schema: ApiError.zodSchema,
},
},
},
@ -55,7 +55,7 @@ const route = createRoute({
description: "Signature could not be verified",
content: {
"application/json": {
schema: ErrorSchema,
schema: ApiError.zodSchema,
},
},
},
@ -63,7 +63,7 @@ const route = createRoute({
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: ErrorSchema,
schema: ApiError.zodSchema,
},
},
},
@ -71,7 +71,7 @@ const route = createRoute({
description: "Not found",
content: {
"application/json": {
schema: ErrorSchema,
schema: ApiError.zodSchema,
},
},
},

View file

@ -3,7 +3,6 @@ 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";
import { ErrorSchema } from "~/types/api";
const schemas = {
param: z.object({
@ -31,19 +30,12 @@ const route = createRoute({
description:
"Redirect to user profile (for web browsers). Uses user-agent for detection.",
},
404: {
description: "User not found",
content: {
"application/json": {
schema: ErrorSchema,
},
},
},
404: ApiError.accountNotFound().schema,
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: ErrorSchema,
schema: ApiError.zodSchema,
},
},
},
@ -57,7 +49,7 @@ export default apiRoute((app) =>
const user = await User.fromId(uuid);
if (!user) {
throw new ApiError(404, "User not found");
throw ApiError.accountNotFound();
}
if (user.isRemote()) {

View file

@ -9,7 +9,6 @@ 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";
import { ErrorSchema } from "~/types/api";
const schemas = {
param: z.object({
@ -43,7 +42,7 @@ const route = createRoute({
description: "User not found",
content: {
"application/json": {
schema: ErrorSchema,
schema: ApiError.zodSchema,
},
},
},
@ -51,7 +50,7 @@ const route = createRoute({
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: ErrorSchema,
schema: ApiError.zodSchema,
},
},
},