Skip to content

Commit

Permalink
feat(federation): ✨ Expose all Zod schemas in package
Browse files Browse the repository at this point in the history
  • Loading branch information
CPlusPatch committed Sep 16, 2024
1 parent a31695d commit 6c15ceb
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 63 deletions.
2 changes: 1 addition & 1 deletion build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import dts from "bun-plugin-dts";
import ora from "ora";

const entrypoints = {
federation: ["index.ts", "schemas.ts"],
federation: ["index.ts", "schemas.ts", "types.ts"],
client: ["index.ts", "types.ts"],
};

Expand Down
2 changes: 1 addition & 1 deletion federation/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
ShareExtension,
Unfollow,
User,
} from "./schemas";
} from "./types";
import type { EntityValidator } from "./validator";

type MaybePromise<T> = T | Promise<T>;
Expand Down
3 changes: 2 additions & 1 deletion federation/jsr.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "0.1.0",
"exports": {
".": "./index.ts",
"./types": "./schemas.ts"
"./types": "./types.ts",
"./schemas": "./schemas.ts"
}
}
5 changes: 5 additions & 0 deletions federation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
"types": "./dist/index.d.ts"
},
"./types": {
"import": "./dist/types.js",
"default": "./dist/types.js",
"types": "./dist/types.d.ts"
},
"./schemas": {
"import": "./dist/schemas.js",
"default": "./dist/schemas.js",
"types": "./dist/schemas.d.ts"
Expand Down
2 changes: 1 addition & 1 deletion federation/requester/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fromZodError } from "zod-validation-error";
import type { SignatureConstructor } from "../cryptography";
import type { User } from "../schemas";
import { WebFingerSchema } from "../schemas/webfinger";
import type { User } from "../types";
import { DEFAULT_UA } from "./constants";

type HttpVerb = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
Expand Down
70 changes: 33 additions & 37 deletions federation/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* @file TypeScript type definitions
* @module federation/types
* @file Zod schema definitions
* @module federation/schemas
*/

import type { z } from "zod";
import type {
import {
CollectionSchema,
DeleteSchema,
EntitySchema,
Expand All @@ -17,37 +16,34 @@ import type {
UnfollowSchema,
UserSchema,
} from "./schemas/base";
import type { ContentFormatSchema } from "./schemas/content_format";
import type { ExtensionPropertySchema } from "./schemas/extensions";
import type { CustomEmojiExtensionSchema } from "./schemas/extensions/custom_emojis";
import type { LikeSchema } from "./schemas/extensions/likes";
import type { VoteSchema } from "./schemas/extensions/polls";
import type { ReactionSchema } from "./schemas/extensions/reactions";
import type { ShareSchema } from "./schemas/extensions/share";
import type { VanityExtensionSchema } from "./schemas/extensions/vanity";
import { ContentFormatSchema } from "./schemas/content_format";
import { ExtensionPropertySchema } from "./schemas/extensions";
import { CustomEmojiExtensionSchema } from "./schemas/extensions/custom_emojis";
import { DislikeSchema, LikeSchema } from "./schemas/extensions/likes";
import { VoteSchema } from "./schemas/extensions/polls";
import { ReactionSchema } from "./schemas/extensions/reactions";
import { ShareSchema } from "./schemas/extensions/share";
import { VanityExtensionSchema } from "./schemas/extensions/vanity";

// 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>;

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>;
export type Follow = InferType<typeof FollowSchema>;
export type FollowAccept = InferType<typeof FollowAcceptSchema>;
export type FollowReject = InferType<typeof FollowRejectSchema>;
export type ContentFormat = InferType<typeof ContentFormatSchema>;
export type CustomEmojiExtension = InferType<typeof CustomEmojiExtensionSchema>;
export type Entity = InferType<typeof EntitySchema>;
export type Delete = InferType<typeof DeleteSchema>;
export type Group = InferType<typeof GroupSchema>;
export type InstanceMetadata = InferType<typeof InstanceMetadataSchema>;
export type Unfollow = InferType<typeof UnfollowSchema>;
export type LikeExtension = InferType<typeof LikeSchema>;
export type DislikeExtension = InferType<typeof LikeSchema>;
export type PollVoteExtension = InferType<typeof VoteSchema>;
export type ReactionExtension = InferType<typeof ReactionSchema>;
export type ShareExtension = InferType<typeof ShareSchema>;
export {
NoteSchema as Note,
CollectionSchema as Collection,
EntitySchema as Entity,
FollowSchema as Follow,
UnfollowSchema as Unfollow,
FollowAcceptSchema as FollowAccept,
FollowRejectSchema as FollowReject,
GroupSchema as Group,
InstanceMetadataSchema as InstanceMetadata,
UserSchema as User,
ContentFormatSchema as ContentFormat,
ExtensionPropertySchema as EntityExtensionProperty,
CustomEmojiExtensionSchema as CustomEmojiExtension,
DeleteSchema as Delete,
VanityExtensionSchema as VanityExtension,
LikeSchema as LikeExtension,
DislikeSchema as DislikeExtension,
VoteSchema as PollVoteExtension,
ReactionSchema as ReactionExtension,
ShareSchema as ShareExtension,
};
53 changes: 53 additions & 0 deletions federation/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @file TypeScript type definitions
* @module federation/types
*/

import type { z } from "zod";
import type {
CollectionSchema,
DeleteSchema,
EntitySchema,
FollowAcceptSchema,
FollowRejectSchema,
FollowSchema,
GroupSchema,
InstanceMetadataSchema,
NoteSchema,
UnfollowSchema,
UserSchema,
} from "./schemas/base";
import type { ContentFormatSchema } from "./schemas/content_format";
import type { ExtensionPropertySchema } from "./schemas/extensions";
import type { CustomEmojiExtensionSchema } from "./schemas/extensions/custom_emojis";
import type { DislikeSchema, LikeSchema } from "./schemas/extensions/likes";
import type { VoteSchema } from "./schemas/extensions/polls";
import type { ReactionSchema } from "./schemas/extensions/reactions";
import type { ShareSchema } from "./schemas/extensions/share";
import type { VanityExtensionSchema } from "./schemas/extensions/vanity";

// 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>;

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>;
export type Follow = InferType<typeof FollowSchema>;
export type FollowAccept = InferType<typeof FollowAcceptSchema>;
export type FollowReject = InferType<typeof FollowRejectSchema>;
export type ContentFormat = InferType<typeof ContentFormatSchema>;
export type CustomEmojiExtension = InferType<typeof CustomEmojiExtensionSchema>;
export type Entity = InferType<typeof EntitySchema>;
export type Delete = InferType<typeof DeleteSchema>;
export type Group = InferType<typeof GroupSchema>;
export type InstanceMetadata = InferType<typeof InstanceMetadataSchema>;
export type Unfollow = InferType<typeof UnfollowSchema>;
export type LikeExtension = InferType<typeof LikeSchema>;
export type DislikeExtension = InferType<typeof DislikeSchema>;
export type PollVoteExtension = InferType<typeof VoteSchema>;
export type ReactionExtension = InferType<typeof ReactionSchema>;
export type ShareExtension = InferType<typeof ShareSchema>;
44 changes: 22 additions & 22 deletions federation/validator.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
import type { z } from "zod";
import { fromError } from "zod-validation-error";
import type {
Collection,
ContentFormat,
CustomEmojiExtension,
Delete,
DislikeExtension,
Entity,
EntityExtensionProperty,
Follow,
FollowAccept,
FollowReject,
Group,
InstanceMetadata,
LikeExtension,
Note,
PollVoteExtension,
ReactionExtension,
ShareExtension,
Unfollow,
User,
VanityExtension,
} from "./schemas";
import {
CollectionSchema,
DeleteSchema,
Expand All @@ -43,6 +21,28 @@ import { VoteSchema } from "./schemas/extensions/polls";
import { ReactionSchema } from "./schemas/extensions/reactions";
import { ShareSchema } from "./schemas/extensions/share";
import { VanityExtensionSchema } from "./schemas/extensions/vanity";
import type {
Collection,
ContentFormat,
CustomEmojiExtension,
Delete,
DislikeExtension,
Entity,
EntityExtensionProperty,
Follow,
FollowAccept,
FollowReject,
Group,
InstanceMetadata,
LikeExtension,
Note,
PollVoteExtension,
ReactionExtension,
ShareExtension,
Unfollow,
User,
VanityExtension,
} from "./types";

// biome-ignore lint/suspicious/noExplicitAny: Used only as a base type
type AnyZod = z.ZodType<any, any, any>;
Expand Down

0 comments on commit 6c15ceb

Please sign in to comment.