Skip to content

Commit

Permalink
feat(federation): ✨ Add Collections
Browse files Browse the repository at this point in the history
  • Loading branch information
CPlusPatch committed Aug 26, 2024
1 parent 1cffb93 commit 1203269
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions federation/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import type { z } from "zod";
import type {
CollectionSchema,
DeleteSchema,
EntitySchema,
FollowAcceptSchema,
Expand All @@ -31,6 +32,7 @@ type AnyZod = z.ZodType<any, any, any>;
type InferType<T extends AnyZod> = z.infer<T>;

export type Note = InferType<typeof NoteSchema>;
export type Collection = InferType<typeof CollectionSchema>;
export type EntityExtensionProperty = InferType<typeof ExtensionPropertySchema>;
export type VanityExtension = InferType<typeof VanityExtensionSchema>;
export type User = InferType<typeof UserSchema>;
Expand Down
14 changes: 14 additions & 0 deletions federation/schemas/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ export const NoteSchema = EntitySchema.extend({
.nullable(),
});

export const CollectionSchema = z.object({
author: z.string().url().nullable(),
first: z.string().url(),
last: z.string().url(),
total: z
.number()
.int()
.nonnegative()
.max(2 ** 64 - 1),
next: z.string().url().nullable(),
previous: z.string().url().nullable(),
items: z.array(z.any()),
});

export const PublicKeyDataSchema = z
.object({
key: z.string().min(1),
Expand Down
11 changes: 11 additions & 0 deletions federation/validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { z } from "zod";
import { fromError } from "zod-validation-error";
import type {
Collection,
ContentFormat,
CustomEmojiExtension,
Delete,
Expand All @@ -22,6 +23,7 @@ import type {
VanityExtension,
} from "./schemas";
import {
CollectionSchema,
DeleteSchema,
EntitySchema,
FollowAcceptSchema,
Expand Down Expand Up @@ -99,6 +101,15 @@ export class EntityValidator {
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
Expand Down

0 comments on commit 1203269

Please sign in to comment.