refactor(api): ♻️ Convery more routes to use OpenAPI

This commit is contained in:
Jesse Wierzbinski 2024-08-27 17:40:58 +02:00
parent 1ab1c68d36
commit b0b750c05d
No known key found for this signature in database
6 changed files with 289 additions and 131 deletions

View file

@ -11,6 +11,7 @@ import {
eq,
inArray,
} from "drizzle-orm";
import { z } from "zod";
import { db } from "~/drizzle/db";
import { Emojis, Instances } from "~/drizzle/schema";
import { BaseInterface } from "./base";
@ -21,6 +22,14 @@ export type EmojiWithInstance = InferSelectModel<typeof Emojis> & {
};
export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
static schema = z.object({
shortcode: z.string(),
url: z.string(),
visible_in_picker: z.boolean(),
category: z.string().optional(),
static_url: z.string(),
});
async reload(): Promise<void> {
const reloaded = await Emoji.fromId(this.data.id);

View file

@ -1,5 +1,5 @@
import { proxyUrl } from "@/response";
import type { RolePermission } from "@versia/client/types";
import { RolePermission } from "@versia/client/types";
import {
type InferInsertModel,
type InferSelectModel,
@ -9,6 +9,7 @@ import {
eq,
inArray,
} from "drizzle-orm";
import { z } from "zod";
import { db } from "~/drizzle/db";
import { RoleToUsers, Roles } from "~/drizzle/schema";
import { config } from "~/packages/config-manager/index";
@ -17,6 +18,16 @@ import { BaseInterface } from "./base";
export type RoleType = InferSelectModel<typeof Roles>;
export class Role extends BaseInterface<typeof Roles> {
static schema = z.object({
id: z.string(),
name: z.string(),
permissions: z.array(z.nativeEnum(RolePermission)),
priority: z.number(),
description: z.string().nullable(),
visible: z.boolean(),
icon: z.string().nullable(),
});
async reload(): Promise<void> {
const reloaded = await Role.fromId(this.data.id);

View file

@ -35,6 +35,7 @@ import {
sql,
} from "drizzle-orm";
import { htmlToText } from "html-to-text";
import { z } from "zod";
import {
type UserWithRelations,
findManyUsers,
@ -64,6 +65,57 @@ import { Role } from "./role";
* Gives helpers to fetch users from database in a nice format
*/
export class User extends BaseInterface<typeof Users, UserWithRelations> {
static schema = z.object({
id: z.string(),
username: z.string(),
acct: z.string(),
display_name: z.string(),
locked: z.boolean(),
discoverable: z.boolean().optional(),
group: z.boolean().nullable(),
noindex: z.boolean().nullable(),
suspended: z.boolean().nullable(),
limited: z.boolean().nullable(),
created_at: z.string(),
followers_count: z.number(),
following_count: z.number(),
statuses_count: z.number(),
note: z.string(),
uri: z.string(),
url: z.string(),
avatar: z.string(),
avatar_static: z.string(),
header: z.string(),
header_static: z.string(),
emojis: z.array(Emoji.schema),
fields: z.array(
z.object({
name: z.string(),
value: z.string(),
verified: z.boolean().nullable().optional(),
verified_at: z.string().nullable().optional(),
}),
),
// FIXME: Use a proper type
moved: z.any().nullable(),
bot: z.boolean().nullable(),
source: z
.object({
privacy: z.string().nullable(),
sensitive: z.boolean().nullable(),
language: z.string().nullable(),
note: z.string(),
})
.optional(),
role: z
.object({
name: z.string(),
})
.optional(),
roles: z.array(Role.schema),
mute_expires_at: z.string().optional(),
});
async reload(): Promise<void> {
const reloaded = await User.fromId(this.data.id);