mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
refactor(api): 🔥 Remove old ID lookup API
This commit is contained in:
parent
dd38a3900c
commit
232ce83e4d
|
|
@ -1,32 +0,0 @@
|
|||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import type { Account as ApiAccount } from "@versia/client/types";
|
||||
import { fakeRequest, getTestUsers } from "~/tests/utils";
|
||||
|
||||
const { users, deleteUsers } = await getTestUsers(5);
|
||||
|
||||
afterAll(async () => {
|
||||
await deleteUsers();
|
||||
});
|
||||
|
||||
// /api/v1/accounts/id
|
||||
describe("/api/v1/accounts/id", () => {
|
||||
test("should correctly get user from username", async () => {
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/id?username=${users[0].data.username}`,
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const data = (await response.json()) as ApiAccount;
|
||||
|
||||
expect(data.id).toBe(users[0].id);
|
||||
});
|
||||
|
||||
test("should return 404 for non-existent user", async () => {
|
||||
const response = await fakeRequest(
|
||||
"/api/v1/accounts/id?username=nonexistent",
|
||||
);
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
import { accountNotFound, apiRoute, auth, reusedResponses } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { Account as AccountSchema } from "@versia/client-ng/schemas";
|
||||
import { User } from "@versia/kit/db";
|
||||
import { RolePermissions, Users } from "@versia/kit/tables";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/api/v1/accounts/id",
|
||||
summary: "Get account by username",
|
||||
description: "Get an account by username",
|
||||
tags: ["Accounts"],
|
||||
middleware: [
|
||||
auth({
|
||||
auth: false,
|
||||
permissions: [RolePermissions.Search],
|
||||
}),
|
||||
] as const,
|
||||
request: {
|
||||
query: z.object({
|
||||
username: AccountSchema.shape.username.transform((v) =>
|
||||
v.toLowerCase(),
|
||||
),
|
||||
}),
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: "Account",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: AccountSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
404: accountNotFound,
|
||||
422: reusedResponses[422],
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
const { username } = context.req.valid("query");
|
||||
|
||||
const user = await User.fromSql(
|
||||
and(eq(Users.username, username), isNull(Users.instanceId)),
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
throw new ApiError(404, "User not found");
|
||||
}
|
||||
|
||||
return context.json(user.toApi(), 200);
|
||||
}),
|
||||
);
|
||||
Loading…
Reference in a new issue