refactor(api): 🏷️ Use more new schemas

This commit is contained in:
Jesse Wierzbinski 2025-02-12 23:33:07 +01:00
parent bff1c5f734
commit a0ce18337a
No known key found for this signature in database
6 changed files with 27 additions and 45 deletions

View file

@ -829,6 +829,7 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
pinned: data.pinned,
// TODO: Add polls
poll: null,
// @ts-expect-error broken recursive types
reblog: data.reblog
? await new Note(data.reblog as NoteTypeWithRelations).toApi(
userFetching,
@ -844,6 +845,7 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
visibility: data.visibility,
url: data.uri || this.getMastoUri().toString(),
bookmarked: false,
// @ts-expect-error broken recursive types
quote: data.quotingId
? ((await Note.fromId(data.quotingId, userFetching?.id).then(
(n) => n?.toApi(userFetching),

View file

@ -1,7 +1,4 @@
import type {
Alerts,
PushSubscription as ApiPushSubscription,
} from "@versia/client/types";
import type { z } from "@hono/zod-openapi";
import { type Token, type User, db } from "@versia/kit/db";
import { PushSubscriptions, Tokens } from "@versia/kit/tables";
import {
@ -12,6 +9,7 @@ import {
eq,
inArray,
} from "drizzle-orm";
import type { WebPushSubscription as WebPushSubscriptionSchema } from "../schemas/pushsubscription.ts";
import { BaseInterface } from "./base.ts";
type PushSubscriptionType = InferSelectModel<typeof PushSubscriptions>;
@ -165,7 +163,7 @@ export class PushSubscription extends BaseInterface<
return this.data.id;
}
public getAlerts(): Alerts {
public getAlerts(): z.infer<typeof WebPushSubscriptionSchema.shape.alerts> {
return {
mention: this.data.alerts.mention ?? false,
favourite: this.data.alerts.favourite ?? false,
@ -180,7 +178,7 @@ export class PushSubscription extends BaseInterface<
};
}
public toApi(): ApiPushSubscription {
public toApi(): z.infer<typeof WebPushSubscriptionSchema> {
return {
id: this.data.id,
alerts: this.getAlerts(),

View file

@ -1,5 +1,4 @@
import { z } from "@hono/zod-openapi";
import type { Token as ApiToken } from "@versia/client/types";
import type { z } from "@hono/zod-openapi";
import { type Application, User, db } from "@versia/kit/db";
import { Tokens } from "@versia/kit/tables";
import {
@ -10,6 +9,7 @@ import {
eq,
inArray,
} from "drizzle-orm";
import type { Token as TokenSchema } from "../schemas/token.ts";
import { BaseInterface } from "./base.ts";
type TokenType = InferSelectModel<typeof Tokens> & {
@ -17,13 +17,6 @@ type TokenType = InferSelectModel<typeof Tokens> & {
};
export class Token extends BaseInterface<typeof Tokens, TokenType> {
public static schema: z.ZodType<ApiToken> = z.object({
access_token: z.string(),
token_type: z.enum(["bearer"]),
scope: z.string(),
created_at: z.number(),
});
public static $type: TokenType;
public async reload(): Promise<void> {
@ -160,7 +153,7 @@ export class Token extends BaseInterface<typeof Tokens, TokenType> {
return await User.fromId(this.data.userId);
}
public toApi(): ApiToken {
public toApi(): z.infer<typeof TokenSchema> {
return {
access_token: this.data.accessToken,
token_type: "Bearer",

View file

@ -5,7 +5,6 @@ import { proxyUrl } from "@/response";
import { sentry } from "@/sentry";
import type { z } from "@hono/zod-openapi";
import { getLogger } from "@logtape/logtape";
import type { Mention as ApiMention } from "@versia/client/types";
import {
EntityValidator,
FederationRequester,
@ -53,6 +52,7 @@ import type { KnownEntity } from "~/types/api.ts";
import { DeliveryJobType, deliveryQueue } from "../queues/delivery.ts";
import { PushJobType, pushQueue } from "../queues/push.ts";
import type { Account, Source } from "../schemas/account.ts";
import type { Mention as MentionSchema } from "../schemas/status.ts";
import { BaseInterface } from "./base.ts";
import { Emoji } from "./emoji.ts";
import { Instance } from "./instance.ts";
@ -1260,7 +1260,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
};
}
public toMention(): ApiMention {
public toMention(): z.infer<typeof MentionSchema> {
return {
url: this.getUri().toString(),
username: this.data.username,