This release is 4 versions behind 0.1.4 — the latest version of @versia/federation. Jump to latest
@versia/federation@0.1.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
Federation types, validators and cryptography for Versia server implementations.
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
JSR Score
52%
Published
4 months ago (0.1.0)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276import type { z } from "npm:zod@^3.23.8"; import { fromError } from "npm:zod-validation-error@^3.3.1"; import type { Collection, ContentFormat, CustomEmojiExtension, Delete, DislikeExtension, Entity, EntityExtensionProperty, Follow, FollowAccept, FollowReject, Group, InstanceMetadata, LikeExtension, Note, PollVoteExtension, ReactionExtension, ShareExtension, Unfollow, User, VanityExtension, } from "./schemas.ts"; import { CollectionSchema, DeleteSchema, EntitySchema, FollowAcceptSchema, FollowRejectSchema, FollowSchema, GroupSchema, InstanceMetadataSchema, NoteSchema, UnfollowSchema, UserSchema, } from "./schemas/base.ts"; import { ContentFormatSchema } from "./schemas/content_format.ts"; import { ExtensionPropertySchema } from "./schemas/extensions.ts"; import { CustomEmojiExtensionSchema } from "./schemas/extensions/custom_emojis.ts"; import { LikeSchema } from "./schemas/extensions/likes.ts"; import { VoteSchema } from "./schemas/extensions/polls.ts"; import { ReactionSchema } from "./schemas/extensions/reactions.ts"; import { ShareSchema } from "./schemas/extensions/share.ts"; import { VanityExtensionSchema } from "./schemas/extensions/vanity.ts"; // biome-ignore lint/suspicious/noExplicitAny: Used only as a base type type AnyZod = z.ZodType<any, any, any>; type InferType<T extends AnyZod> = z.infer<T>; /** * Validates entities against their respective schemas. * @module federation/validator * @see module:federation/schemas/base * @example * import { EntityValidator, type ValidationError } from "@versia/federation"; * const validator = new EntityValidator(); * * // Will throw a special ValidationError with a human-friendly error message * // and a machine-friendly error object if the data is invalid. * const note = await validator.Note({ * type: "Note", * content: "Hello, world!", * }); * * try { * await validator.Note({ * type: "Note", * content: 123, * }); * } catch (error) { * sendUser((error as ValidationError).toString()); * } * * // Types are also included for TypeScript users that don't use the extracted ones * import type { Note } from "@versia/federation/types"; * * const note: Note = { * ... * }; */ export class EntityValidator { private async validate<T extends AnyZod>( schema: T, data: unknown, ): Promise<InferType<T>> { try { return (await schema.parseAsync(data)) as InferType<T>; } catch (error) { throw fromError(error); } } /** * Validates a Note entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public Note(data: unknown): Promise<Note> { return this.validate(NoteSchema, data); } /** * Validates a Collection entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public Collection(data: unknown): Promise<Collection> { return this.validate(CollectionSchema, data); } /** * Validates a VanityExtension entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public VanityExtension(data: unknown): Promise<VanityExtension> { return this.validate(VanityExtensionSchema, data); } /** * Validates a User entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public User(data: unknown): Promise<User> { return this.validate(UserSchema, data); } /** * Validates a Follow entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public Follow(data: unknown): Promise<Follow> { return this.validate(FollowSchema, data); } /** * Validates a FollowAccept entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public FollowAccept(data: unknown): Promise<FollowAccept> { return this.validate(FollowAcceptSchema, data); } /** * Validates a FollowReject entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public FollowReject(data: unknown): Promise<FollowReject> { return this.validate(FollowRejectSchema, data); } /** * Validates a ContentFormat entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public ContentFormat(data: unknown): Promise<ContentFormat> { return this.validate(ContentFormatSchema, data); } /** * Validates a CustomEmojiExtension entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public CustomEmojiExtension(data: unknown): Promise<CustomEmojiExtension> { return this.validate(CustomEmojiExtensionSchema, data); } /** * Validates an Entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public Entity(data: unknown): Promise<Entity> { return this.validate(EntitySchema, data); } /** * Validates an ExtensionProperty. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public EntityExtensionProperty( data: unknown, ): Promise<EntityExtensionProperty> { return this.validate(ExtensionPropertySchema, data); } /** * Validates a Delete entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public Delete(data: unknown): Promise<Delete> { return this.validate(DeleteSchema, data); } /** * Validates a Group entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public Group(data: unknown): Promise<Group> { return this.validate(GroupSchema, data); } /** * Validates an InstanceMetadata entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public InstanceMetadata(data: unknown): Promise<InstanceMetadata> { return this.validate(InstanceMetadataSchema, data); } /** * Validates an Unfollow entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public Unfollow(data: unknown): Promise<Unfollow> { return this.validate(UnfollowSchema, data); } /** * Validates a Like entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public LikeExtension(data: unknown): Promise<LikeExtension> { return this.validate(LikeSchema, data); } /** * Validates a Dislike entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public DislikeExtension(data: unknown): Promise<DislikeExtension> { return this.validate(LikeSchema, data); } /** * Validates a Vote entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public PollVoteExtension(data: unknown): Promise<PollVoteExtension> { return this.validate(VoteSchema, data); } /** * Validates a Reaction entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public ReactionExtension(data: unknown): Promise<ReactionExtension> { return this.validate(ReactionSchema, data); } /** * Validates a Share entity. * @param data - The data to validate * @returns A promise that resolves to the validated data. */ public ShareExtension(data: unknown): Promise<ShareExtension> { return this.validate(ShareSchema, data); } }